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
Show file tree
Hide file tree
Changes from 2 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
Expand Up @@ -629,6 +629,7 @@ extension GetNavigationExt on GetInterface {
dynamic arguments,
String? id,
Map<String, String>? parameters,
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

}) {
// if (preventDuplicates && page == currentRoute) {
// return null;
Expand All @@ -642,7 +643,7 @@ extension GetNavigationExt on GetInterface {
page,
arguments: arguments,
id: id,
// preventDuplicates: preventDuplicates,
preventDuplicates: preventDuplicates,
parameters: parameters,
);
}
Expand Down Expand Up @@ -690,6 +691,7 @@ extension GetNavigationExt on GetInterface {
String? id,
dynamic arguments,
Map<String, String>? parameters,
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.

Maybe this value should be false by default

Copy link
Author

Choose a reason for hiding this comment

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

@jonataslaw Okay, commited

}) {
if (parameters != null) {
final uri = Uri(path: page, queryParameters: parameters);
Expand All @@ -702,6 +704,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

);
}

Expand All @@ -722,6 +725,7 @@ extension GetNavigationExt on GetInterface {
String? id,
dynamic result,
Map<String, String>? parameters,
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

}) {
if (parameters != null) {
final uri = Uri(path: page, queryParameters: parameters);
Expand All @@ -731,6 +735,7 @@ extension GetNavigationExt on GetInterface {
page,
arguments: arguments,
result: result,
preventDuplicates: preventDuplicates,
);
}

Expand Down Expand Up @@ -1003,11 +1008,13 @@ extension GetNavigationExt on GetInterface {
bool Function(GetPage) predicate, [
Object? arguments,
String? id,
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

]) {
return searchDelegate(id).offUntil(
page,
predicate,
arguments,
preventDuplicates,
);
}

Expand Down
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
Expand Up @@ -117,6 +117,7 @@ mixin IGetNavigation {
dynamic arguments,
String? id,
Map<String, String>? parameters,
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

});

Future<T?>? offAllNamed<T>(
Expand All @@ -133,25 +134,33 @@ mixin IGetNavigation {
dynamic arguments,
String? id,
Map<String, String>? parameters,
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

});

Future<T?> toNamedAndOffUntil<T>(
String page,
bool Function(GetPage) predicate, [
Object? data,
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

]);

Future<T?> offUntil<T>(
Widget Function() page,
bool Function(GetPage) predicate, [
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 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);

Expand Down
5 changes: 4 additions & 1 deletion lib/get_navigation/src/routes/get_route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
);
}

Expand Down
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
Expand Up @@ -355,7 +355,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 {
Expand Down Expand Up @@ -407,7 +410,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,
Expand Down Expand Up @@ -521,9 +527,13 @@ class GetDelegate extends RouterDelegate<RouteDecoder>
dynamic arguments,
String? id,
Map<String, String>? parameters,
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

}) 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;
Expand All @@ -541,9 +551,13 @@ class GetDelegate extends RouterDelegate<RouteDecoder>
dynamic arguments,
String? id,
Map<String, String>? parameters,
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

}) 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);
Expand All @@ -554,10 +568,14 @@ class GetDelegate extends RouterDelegate<RouteDecoder>
String page,
bool Function(GetPage) predicate, [
Object? data,
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

]) async {
final arguments = _buildPageSettings(page, data);

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

if (route == null) return null;

Expand All @@ -573,12 +591,17 @@ class GetDelegate extends RouterDelegate<RouteDecoder>
Widget Function() page,
bool Function(GetPage) predicate, [
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

]) async {
while (_activePages.isNotEmpty && !predicate(_activePages.last.route!)) {
_popWithResult();
}

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

@override
Expand All @@ -602,10 +625,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);
Expand Down Expand Up @@ -692,7 +722,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) {
Expand All @@ -704,12 +737,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);
Expand All @@ -722,6 +762,7 @@ class GetDelegate extends RouterDelegate<RouteDecoder>
arguments: arguments,
parameters: parameters,
key: ValueKey(arguments.name),
preventDuplicates: preventDuplicates,
);

return decoder;
Expand All @@ -737,11 +778,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
Expand Down