Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add navigate to view group action #860

Merged
merged 9 commits into from
Nov 6, 2023
22 changes: 22 additions & 0 deletions assets/schema/ensemble_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2950,6 +2950,28 @@
}
}
},
{
"title": "Navigate View Group",
"type": "object",
"required": [
"navigateViewGroup"
],
"properties": {
"navigateViewGroup": {
"type": "object",
"description": "Navigate between the screens in the view group",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add " (applicable only when inside a ViewGroup)"

"required": [
"viewIndex"
],
"properties": {
"viewIndex": {
"type": "integer",
"description": "Enter the index of the screen to navigate"
}
}
}
}
},
{
"title": "Navigate Modal Screen",
"type": "object",
Expand Down
20 changes: 20 additions & 0 deletions lib/framework/action.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:ensemble/framework/event.dart';
import 'package:ensemble/framework/extensions.dart';
import 'package:ensemble/framework/permissions_manager.dart';
import 'package:ensemble/framework/scope.dart';
import 'package:ensemble/framework/view/page_group.dart';
import 'package:ensemble/framework/widget/view_util.dart';
import 'package:ensemble/screen_controller.dart';
import 'package:ensemble/util/utils.dart';
Expand Down Expand Up @@ -145,6 +146,22 @@ class NavigateScreenAction extends BaseNavigateScreenAction {
}
}

class NavigateViewGroupAction extends EnsembleAction {
NavigateViewGroupAction({dynamic viewIndex}) : _viewIndex = viewIndex;

final dynamic _viewIndex;

factory NavigateViewGroupAction.from({Map? payload}) {
return NavigateViewGroupAction(viewIndex: payload?['viewIndex']);
}

@override
Future<void> execute(BuildContext context, ScopeManager scopeManager) {
pageNotifier.updatePage(_viewIndex);
return Future.value(null);
}
}

class NavigateModalScreenAction extends BaseNavigateScreenAction {
NavigateModalScreenAction({
super.initiator,
Expand Down Expand Up @@ -830,6 +847,7 @@ class CheckPermission extends EnsembleAction {
enum ActionType {
invokeAPI,
navigateScreen,
navigateViewGroup,
navigateModalScreen,
showBottomModal,
dismissBottomModal,
Expand Down Expand Up @@ -910,6 +928,8 @@ abstract class EnsembleAction {
if (actionType == ActionType.navigateScreen) {
return NavigateScreenAction.fromYaml(
initiator: initiator, payload: payload);
} else if (actionType == ActionType.navigateViewGroup) {
return NavigateViewGroupAction.from(payload: payload);
} else if (actionType == ActionType.navigateModalScreen) {
return NavigateModalScreenAction.fromYaml(
initiator: initiator, payload: payload);
Expand Down
29 changes: 27 additions & 2 deletions lib/framework/view/page_group.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ import 'package:ensemble/framework/extensions.dart';
import '../widget/custom_view.dart';
import 'bottom_nav_page_group.dart';

class PageGroupNotifier extends ChangeNotifier {
int _viewIndex = 0;

int get viewIndex => _viewIndex;

void updatePage(int index) {
_viewIndex = index;
notifyListeners();
}
}

final pageNotifier = PageGroupNotifier();

/// a collection of pages grouped under a navigation menu
class PageGroup extends StatefulWidget {
const PageGroup(
Expand Down Expand Up @@ -113,6 +126,14 @@ class PageGroupState extends State<PageGroup> with MediaQueryCapability {

@override
Widget build(BuildContext context) {
return ListenableBuilder(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will recreate the entire ViewGroup from scratch - we don't want that. We simply want to switch the Tab to another screen. Please make sure we don't rebuild items unnecessary. Optimization is important for our framework as the app could be gigantic.

listenable: pageNotifier,
builder: (context, _) => buildPageGroupChild(),
);
}

Widget buildPageGroupChild() {
final selectedPage = pageNotifier.viewIndex;
// skip rendering the menu if only 1 menu item, just the content itself
if (widget.menu.menuItems.length == 1) {
return pageWidgets[0];
Expand All @@ -125,16 +146,19 @@ class PageGroupState extends State<PageGroup> with MediaQueryCapability {
Drawer? drawer = _buildDrawer(context, widget.menu);
bool atStart = (widget.menu as DrawerMenu).atStart;
return PageGroupWidget(
key: UniqueKey(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't do this. Almost never use UniqueKey().

scopeManager: _scopeManager,
navigationDrawer: atStart ? drawer : null,
navigationEndDrawer: !atStart ? drawer : null,
child: pageWidgets[selectedPage]);
} else if (widget.menu is SidebarMenu) {
return PageGroupWidget(
key: UniqueKey(),
scopeManager: _scopeManager,
child: buildSidebarNavigation(context, widget.menu as SidebarMenu));
} else if (widget.menu is BottomNavBarMenu) {
return BottomNavPageGroup(
key: UniqueKey(),
scopeManager: _scopeManager,
selectedPage: selectedPage,
menu: widget.menu,
Expand All @@ -149,8 +173,9 @@ class PageGroupState extends State<PageGroup> with MediaQueryCapability {
Widget buildSidebarNavigation(BuildContext context, SidebarMenu menu) {
Widget sidebar = _buildSidebar(context, menu);
Widget? separator = _buildSidebarSeparator(menu);
Widget content = Expanded(child: pageWidgets[selectedPage]);

Widget content = Expanded(
child: IndexedStack(index: selectedPage, children: pageWidgets),
);
// figuring out the direction to lay things out
bool rtlLocale = Directionality.of(context) == TextDirection.rtl;
// standard layout is the sidebar menu then content
Expand Down