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

fix(lib/get_navigation): Improve preventDuplicates and preventDuplicateHandlingMode in navigator2 #3262

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/get_navigation/src/extension_navigation.dart
Original file line number Diff line number Diff line change
@@ -632,6 +632,7 @@ extension GetNavigationExt on GetInterface {
dynamic arguments,
String? id,
Map<String, String>? parameters,
bool preventDuplicates = false,
}) {
// if (preventDuplicates && page == currentRoute) {
// return null;
@@ -645,7 +646,7 @@ extension GetNavigationExt on GetInterface {
page,
arguments: arguments,
id: id,
// preventDuplicates: preventDuplicates,
preventDuplicates: preventDuplicates,
parameters: parameters,
);
}
@@ -693,6 +694,7 @@ extension GetNavigationExt on GetInterface {
String? id,
dynamic arguments,
Map<String, String>? parameters,
bool preventDuplicates = false,
}) {
if (parameters != null) {
final uri = Uri(path: page, queryParameters: parameters);
@@ -705,6 +707,7 @@ extension GetNavigationExt on GetInterface {
id: id,
arguments: arguments,
parameters: parameters,
preventDuplicates: preventDuplicates,
Copy link
Owner

Choose a reason for hiding this comment

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

IDEM

);
}

@@ -725,6 +728,7 @@ extension GetNavigationExt on GetInterface {
String? id,
dynamic result,
Map<String, String>? parameters,
bool preventDuplicates = false,
}) {
if (parameters != null) {
final uri = Uri(path: page, queryParameters: parameters);
@@ -734,6 +738,7 @@ extension GetNavigationExt on GetInterface {
page,
arguments: arguments,
result: result,
preventDuplicates: preventDuplicates,
);
}

@@ -1025,11 +1030,13 @@ extension GetNavigationExt on GetInterface {
bool Function(GetPage) predicate, [
Object? arguments,
String? id,
bool preventDuplicates = false,
]) {
return searchDelegate(id).offUntil(
page,
predicate,
arguments,
preventDuplicates,
);
}

11 changes: 10 additions & 1 deletion lib/get_navigation/src/routes/get_navigation_interface.dart
Original file line number Diff line number Diff line change
@@ -117,6 +117,7 @@ mixin IGetNavigation {
dynamic arguments,
String? id,
Map<String, String>? parameters,
bool preventDuplicates = false,
});

Future<T?>? offAllNamed<T>(
@@ -133,25 +134,33 @@ mixin IGetNavigation {
dynamic arguments,
String? id,
Map<String, String>? parameters,
bool preventDuplicates = false,
});

Future<T?> toNamedAndOffUntil<T>(
String page,
bool Function(GetPage) predicate, [
Object? data,
bool preventDuplicates = false,
]);

Future<T?> offUntil<T>(
Widget Function() page,
bool Function(GetPage) predicate, [
Object? arguments,
bool preventDuplicates = false,
]);

void removeRoute<T>(String name);

void back<T>([T? result]);

Future<R?> backAndtoNamed<T, R>(String page, {T? result, Object? arguments});
Future<R?> backAndtoNamed<T, R>(
String page, {
T? result,
Object? arguments,
bool preventDuplicates = true,
Copy link
Owner

Choose a reason for hiding this comment

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

IDEM

});

void backUntil(bool Function(GetPage) predicate);

5 changes: 4 additions & 1 deletion lib/get_navigation/src/routes/get_route.dart
Original file line number Diff line number Diff line change
@@ -133,6 +133,7 @@ class GetPage<T> extends Page<T> {
bool? canPop,
PopInvokedWithResultCallback<T>? onPopInvoked,
String? restorationId,
PreventDuplicateHandlingMode? preventDuplicateHandlingMode,
}) {
return GetPage(
key: key ?? this.key,
@@ -168,7 +169,9 @@ class GetPage<T> extends Page<T> {
inheritParentPath: inheritParentPath ?? this.inheritParentPath,
canPop: canPop ?? this.canPop,
onPopInvoked: onPopInvoked ?? this.onPopInvoked,
restorationId: restorationId ?? restorationId,
restorationId: restorationId ?? this.restorationId,
preventDuplicateHandlingMode:
preventDuplicateHandlingMode ?? this.preventDuplicateHandlingMode,
);
}

71 changes: 57 additions & 14 deletions lib/get_navigation/src/routes/get_router_delegate.dart
Original file line number Diff line number Diff line change
@@ -353,7 +353,10 @@ class GetDelegate extends RouterDelegate<RouteDecoder>
Map<String, String>? parameters,
}) async {
final args = _buildPageSettings(page, arguments);
final route = _getRouteDecoder<T>(args);
final route = _getRouteDecoder<T>(
args,
preventDuplicates: preventDuplicates,
);
if (route != null) {
return _push<T>(route);
} else {
@@ -405,7 +408,10 @@ class GetDelegate extends RouterDelegate<RouteDecoder>

_routeTree.addRoute(getPage);
final args = _buildPageSettings(routeName, arguments);
final route = _getRouteDecoder<T>(args);
final route = _getRouteDecoder<T>(
args,
preventDuplicates: preventDuplicates,
);
final result = await _push<T>(
route!,
rebuildStack: rebuildStack,
@@ -519,9 +525,13 @@ class GetDelegate extends RouterDelegate<RouteDecoder>
dynamic arguments,
String? id,
Map<String, String>? parameters,
bool preventDuplicates = false,
}) async {
final args = _buildPageSettings(page, arguments);
final route = _getRouteDecoder<T>(args);
final route = _getRouteDecoder<T>(
args,
preventDuplicates: preventDuplicates,
);
if (route == null) return null;

final newPredicate = predicate ?? (route) => false;
@@ -539,9 +549,13 @@ class GetDelegate extends RouterDelegate<RouteDecoder>
dynamic arguments,
String? id,
Map<String, String>? parameters,
bool preventDuplicates = false,
}) async {
final args = _buildPageSettings(page, arguments);
final route = _getRouteDecoder<T>(args);
final route = _getRouteDecoder<T>(
args,
preventDuplicates: preventDuplicates,
);
if (route == null) return null;
_popWithResult();
return _push<T>(route);
@@ -552,10 +566,14 @@ class GetDelegate extends RouterDelegate<RouteDecoder>
String page,
bool Function(GetPage) predicate, [
Object? data,
bool preventDuplicates = false,
]) async {
final arguments = _buildPageSettings(page, data);

final route = _getRouteDecoder<T>(arguments);
final route = _getRouteDecoder<T>(
arguments,
preventDuplicates: preventDuplicates,
);

if (route == null) return null;

@@ -571,12 +589,17 @@ class GetDelegate extends RouterDelegate<RouteDecoder>
Widget Function() page,
bool Function(GetPage) predicate, [
Object? arguments,
bool preventDuplicates = false,
]) async {
while (_activePages.isNotEmpty && !predicate(_activePages.last.route!)) {
_popWithResult();
}

return to<T>(page, arguments: arguments);
return to<T>(
page,
arguments: arguments,
preventDuplicates: preventDuplicates,
);
}

@override
@@ -600,10 +623,17 @@ class GetDelegate extends RouterDelegate<RouteDecoder>
}

@override
Future<R?> backAndtoNamed<T, R>(String page,
{T? result, Object? arguments}) async {
Future<R?> backAndtoNamed<T, R>(
String page, {
T? result,
Object? arguments,
bool preventDuplicates = true,
}) async {
final args = _buildPageSettings(page, arguments);
final route = _getRouteDecoder<R>(args);
final route = _getRouteDecoder<R>(
args,
preventDuplicates: preventDuplicates,
);
if (route == null) return null;
_popWithResult<T>(result);
return _push<R>(route);
@@ -690,7 +720,10 @@ class GetDelegate extends RouterDelegate<RouteDecoder>
}

@protected
RouteDecoder? _getRouteDecoder<T>(PageSettings arguments) {
RouteDecoder? _getRouteDecoder<T>(
PageSettings arguments, {
bool? preventDuplicates,
}) {
var page = arguments.uri.path;
final parameters = arguments.params;
if (parameters.isNotEmpty) {
@@ -702,12 +735,19 @@ class GetDelegate extends RouterDelegate<RouteDecoder>
final route = decoder.route;
if (route == null) return null;

return _configureRouterDecoder<T>(decoder, arguments);
return _configureRouterDecoder<T>(
decoder,
arguments,
preventDuplicates: preventDuplicates,
);
}

@protected
RouteDecoder _configureRouterDecoder<T>(
RouteDecoder decoder, PageSettings arguments) {
RouteDecoder decoder,
PageSettings arguments, {
bool? preventDuplicates,
}) {
final parameters =
arguments.params.isEmpty ? arguments.query : arguments.params;
arguments.params.addAll(arguments.query);
@@ -720,6 +760,7 @@ class GetDelegate extends RouterDelegate<RouteDecoder>
arguments: arguments,
parameters: parameters,
key: ValueKey(arguments.name),
preventDuplicates: preventDuplicates,
);

return decoder;
@@ -735,11 +776,13 @@ class GetDelegate extends RouterDelegate<RouteDecoder>
res.route?.preventDuplicateHandlingMode ??
PreventDuplicateHandlingMode.reorderRoutes;

final preventDuplicates = res.route?.preventDuplicates ?? true;

final onStackPage = _activePages
.firstWhereOrNull((element) => element.route?.key == res.route?.key);

/// There are no duplicate routes in the stack
if (onStackPage == null) {
/// There are no duplicate routes in the stack or route.preventDuplicates == false
if (onStackPage == null || preventDuplicates == false) {
_activePages.add(res);
} else {
/// There are duplicate routes, reorder