Skip to content

Commit

Permalink
Add pushToCatalog and popFromCatalog methods to Routes class
Browse files Browse the repository at this point in the history
  • Loading branch information
PlugFox committed Dec 18, 2023
1 parent 20083cc commit a1dc601
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 9 deletions.
46 changes: 46 additions & 0 deletions example/lib/src/common/router/routes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,50 @@ enum Routes with OctopusRoute {
? CustomUserPage()
: super.pageBuilder(context, node);
*/

/// Pushes the [route] to the catalog tab.
/// [id] is the product or category id for the [route].
static void pushToCatalog(BuildContext context, Routes route, String id) =>
Octopus.of(context).setState((state) {
final node = state.find((n) => n.name == 'catalog-tab');
if (node == null) {
return state
..removeByName(Routes.shop.name)
..add(Routes.shop.node(
children: <OctopusNode>[
OctopusNode.mutable(
'catalog-tab',
children: <OctopusNode>[
Routes.catalog.node(),
route.node(arguments: {'id': id}),
],
),
],
))
..arguments['shop'] = 'catalog';
}
node.children.add(route.node(arguments: {'id': id}));
return state..arguments['shop'] = 'catalog';
});

/// Pops the last [route] from the catalog tab.
static void popFromCatalog(BuildContext context) =>
Octopus.of(context).setState((state) {
final node = state.find((n) => n.name == 'catalog-tab');
if (node == null || node.children.length < 2) {
return state
..removeByName(Routes.shop.name)
..add(Routes.shop.node(
children: <OctopusNode>[
OctopusNode.mutable(
'catalog-tab',
children: <OctopusNode>[Routes.catalog.node()],
),
],
))
..arguments['shop'] = 'catalog';
}
node.children.removeLast();
return state;
});
}
4 changes: 2 additions & 2 deletions example/lib/src/feature/shop/widget/catalog_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,10 @@ class _CatalogTile extends StatelessWidget {
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.bodySmall,
),
onTap: () => Octopus.push(
onTap: () => Routes.pushToCatalog(
context,
Routes.category,
arguments: <String, String>{'id': category.id},
category.id,
),
/* onTap: () => Octopus.of(context).setState(
(state) => state
Expand Down
15 changes: 9 additions & 6 deletions lib/src/controller/information_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,15 @@ class OctopusInformationProvider extends RouteInformationProvider
replace = routeInformation.replace;
} */

SystemNavigator.selectMultiEntryHistory();
SystemNavigator.routeInformationUpdated(
uri: routeInformation.uri,
state: routeInformation.state,
replace: replace,
);
// If the route is different from the current route, then update the engine.
if (routeInformation.uri != _value.uri) {
SystemNavigator.selectMultiEntryHistory(); // selectSingleEntryHistory
SystemNavigator.routeInformationUpdated(
uri: routeInformation.uri,
state: routeInformation.state,
replace: replace,
);
}
_value = _valueInEngine = routeInformation;
}

Expand Down
2 changes: 2 additions & 0 deletions lib/src/state/state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ final class OctopusState$Immutable extends OctopusState
String toString() => _$representation;
}

// TODO(plugfox): add helper methods for state & nodes

/// Node of the router state tree
sealed class OctopusNode extends _OctopusTree {
/// Node of the router state tree
Expand Down
2 changes: 1 addition & 1 deletion lib/src/widget/navigator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ class _OctopusNestedNavigatorBuilderState
void _handleStateChange() {
if (!mounted) return;
final newNodes = _getNodesFromContext();
if (newNodes.isEmpty && _router.isIdle) {
if (newNodes.isEmpty) {
// If nodes are empty we should check the bucket and add it if necessary.
_checkBucket();
}
Expand Down

0 comments on commit a1dc601

Please sign in to comment.