-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
base: master
Are you sure you want to change the base?
fix(lib/get_navigation): Improve preventDuplicates and preventDuplicateHandlingMode in navigator2 #3262
Changes from 2 commits
a6df1ff
208f016
15459a6
d3ab1cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -629,6 +629,7 @@ extension GetNavigationExt on GetInterface { | |
dynamic arguments, | ||
String? id, | ||
Map<String, String>? parameters, | ||
bool preventDuplicates = true, | ||
}) { | ||
// if (preventDuplicates && page == currentRoute) { | ||
// return null; | ||
|
@@ -642,7 +643,7 @@ extension GetNavigationExt on GetInterface { | |
page, | ||
arguments: arguments, | ||
id: id, | ||
// preventDuplicates: preventDuplicates, | ||
preventDuplicates: preventDuplicates, | ||
parameters: parameters, | ||
); | ||
} | ||
|
@@ -690,6 +691,7 @@ extension GetNavigationExt on GetInterface { | |
String? id, | ||
dynamic arguments, | ||
Map<String, String>? parameters, | ||
bool preventDuplicates = true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe this value should be false by default There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
@@ -702,6 +704,7 @@ extension GetNavigationExt on GetInterface { | |
id: id, | ||
arguments: arguments, | ||
parameters: parameters, | ||
preventDuplicates: preventDuplicates, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IDEM |
||
); | ||
} | ||
|
||
|
@@ -722,6 +725,7 @@ extension GetNavigationExt on GetInterface { | |
String? id, | ||
dynamic result, | ||
Map<String, String>? parameters, | ||
bool preventDuplicates = true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IDEM |
||
}) { | ||
if (parameters != null) { | ||
final uri = Uri(path: page, queryParameters: parameters); | ||
|
@@ -731,6 +735,7 @@ extension GetNavigationExt on GetInterface { | |
page, | ||
arguments: arguments, | ||
result: result, | ||
preventDuplicates: preventDuplicates, | ||
); | ||
} | ||
|
||
|
@@ -1003,11 +1008,13 @@ extension GetNavigationExt on GetInterface { | |
bool Function(GetPage) predicate, [ | ||
Object? arguments, | ||
String? id, | ||
bool preventDuplicates = true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IDEM |
||
]) { | ||
return searchDelegate(id).offUntil( | ||
page, | ||
predicate, | ||
arguments, | ||
preventDuplicates, | ||
); | ||
} | ||
|
||
|
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 = true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IDEM |
||
}); | ||
|
||
Future<T?>? offAllNamed<T>( | ||
|
@@ -133,25 +134,33 @@ mixin IGetNavigation { | |
dynamic arguments, | ||
String? id, | ||
Map<String, String>? parameters, | ||
bool preventDuplicates = true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IDEM |
||
}); | ||
|
||
void backUntil(bool Function(GetPage) predicate); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -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, | ||
|
@@ -521,9 +527,13 @@ class GetDelegate extends RouterDelegate<RouteDecoder> | |
dynamic arguments, | ||
String? id, | ||
Map<String, String>? parameters, | ||
bool preventDuplicates = true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
@@ -541,9 +551,13 @@ class GetDelegate extends RouterDelegate<RouteDecoder> | |
dynamic arguments, | ||
String? id, | ||
Map<String, String>? parameters, | ||
bool preventDuplicates = true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
@@ -554,10 +568,14 @@ class GetDelegate extends RouterDelegate<RouteDecoder> | |
String page, | ||
bool Function(GetPage) predicate, [ | ||
Object? data, | ||
bool preventDuplicates = true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
||
|
@@ -573,12 +591,17 @@ class GetDelegate extends RouterDelegate<RouteDecoder> | |
Widget Function() page, | ||
bool Function(GetPage) predicate, [ | ||
Object? arguments, | ||
bool preventDuplicates = true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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); | ||
|
@@ -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) { | ||
|
@@ -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); | ||
|
@@ -722,6 +762,7 @@ class GetDelegate extends RouterDelegate<RouteDecoder> | |
arguments: arguments, | ||
parameters: parameters, | ||
key: ValueKey(arguments.name), | ||
preventDuplicates: preventDuplicates, | ||
); | ||
|
||
return decoder; | ||
|
@@ -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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IDEM