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

[WIP] fix: fix guard with auto tab scaffold #1841

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
64 changes: 59 additions & 5 deletions auto_route/lib/src/router/controller/routing_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -657,17 +657,68 @@ class TabsRouter extends RoutingController {
int? get previousIndex => _previousIndex;

/// Updates [_activeIndex] and triggers a rebuild
void setActiveIndex(int index, {bool notify = true}) {
void setActiveIndex(int index, {bool notify = true}) async {
assert(index >= 0 && index < _pages.length);

if (_activeIndex != index) {
_previousIndex = _activeIndex;
_activeIndex = index;
if (notify) {
notifyAll();
final match = _routeMatch[index];
final result = await _canNavigate(match, onFailure: (_) {});
if (result.continueNavigation) {
_previousIndex = _activeIndex;
_activeIndex = index;
if (notify) {
notifyAll();
}
}
}
}

late final AutoRouteGuard? _rootGuard =
(root is AutoRouteGuard) ? (root as AutoRouteGuard) : null;
Future<ResolverResult> _canNavigate(
RouteMatch route, {
OnNavigationFailure? onFailure,
}) async {
final guards = <AutoRouteGuard>[
if (_rootGuard != null) _rootGuard!,
...route.guards,
];
if (guards.isEmpty) {
return SynchronousFuture(
const ResolverResult(
continueNavigation: true,
reevaluateNext: true,
),
);
}
for (var guard in guards) {
final completer = Completer<ResolverResult>();

guard.onNavigation(
NavigationResolver(
root,
completer,
route,
),
root);
final result = await completer.future;
if (!result.continueNavigation) {
if (onFailure != null) {
onFailure(RejectedByGuardFailure(route, guard));
}

return const ResolverResult(
continueNavigation: false,
reevaluateNext: false,
);
}
}
return const ResolverResult(
continueNavigation: true,
reevaluateNext: true,
);
}

@override
List<AutoRoutePage> get stack => List.unmodifiable(_pages);

Expand Down Expand Up @@ -700,10 +751,13 @@ class TabsRouter extends RoutingController {
}
}

List<RouteMatch> _routeMatch = [];

/// Pushes given [routes] to [_pages] stack
/// after match validation and deciding initial index
void setupRoutes(List<PageRouteInfo> routes) {
final routesToPush = _matchAllOrReportFailure(routes)!;
_routeMatch = routesToPush;
if (_routeData.hasPendingChildren) {
final preMatchedRoute = _routeData.pendingChildren.last;
final correspondingRouteIndex = routesToPush.indexWhere(
Expand Down
Loading