From 842d57e1ba96ed4563196f68143b84e7ca71e3f7 Mon Sep 17 00:00:00 2001 From: busslina Date: Tue, 11 Jun 2024 16:08:59 +0200 Subject: [PATCH] Providing BuildContext on BeamGuard.beamToNamed --- package/lib/src/beam_guard.dart | 3 ++- package/test/beam_guard_test.dart | 20 ++++++++++---------- package/test/beamer_delegate_test.dart | 9 +++++---- package/test/nested_navigation.dart | 2 +- 4 files changed, 18 insertions(+), 16 deletions(-) diff --git a/package/lib/src/beam_guard.dart b/package/lib/src/beam_guard.dart index 8771201..e5798e3 100644 --- a/package/lib/src/beam_guard.dart +++ b/package/lib/src/beam_guard.dart @@ -80,6 +80,7 @@ class BeamGuard { /// `deepLink` holds the potential deep-link that was set manually via /// [BeamerDelegate.setDeepLink] or came from the platforms. final String Function( + BuildContext context, BeamStack? origin, BeamStack target, String? deepLink, @@ -163,7 +164,7 @@ class BeamGuard { } if (beamToNamed != null) { - final redirectNamed = beamToNamed!(origin, target, deepLink); + final redirectNamed = beamToNamed!(context, origin, target, deepLink); if (redirectNamed == target.state.routeInformation.uri.toString()) { // just block if this will produce an immediate infinite loop return true; diff --git a/package/test/beam_guard_test.dart b/package/test/beam_guard_test.dart index 79ebe83..844ed02 100644 --- a/package/test/beam_guard_test.dart +++ b/package/test/beam_guard_test.dart @@ -378,7 +378,7 @@ void main() { BeamGuard( pathPatterns: ['/l2'], check: (context, loc) => false, - beamToNamed: (_, __, ___) => '/l1', + beamToNamed: (context, _, __, ___) => '/l1', ), ], ); @@ -413,12 +413,12 @@ void main() { BeamGuard( pathPatterns: ['/2'], check: (_, __) => false, - beamToNamed: (_, __, ___) => '/3', + beamToNamed: (context, _, __, ___) => '/3', ), BeamGuard( pathPatterns: ['/3'], check: (_, __) => false, - beamToNamed: (_, __, ___) => '/1', + beamToNamed: (context, _, __, ___) => '/1', ), ], ); @@ -489,7 +489,7 @@ void main() { BeamGuard( pathPatterns: ['/2'], check: (context, stack) => false, - beamToNamed: (origin, target, _) { + beamToNamed: (context, origin, target, _) { final targetState = target.state as BeamState; final destinationUri = Uri(path: '/1', queryParameters: targetState.queryParameters) @@ -610,7 +610,7 @@ void main() { final guard = BeamGuard( pathPatterns: ['/guarded'], check: (_, __) => false, - beamToNamed: (_, __, ___) => '/allowed', + beamToNamed: (context, _, __, ___) => '/allowed', replaceCurrentStack: false, ); @@ -711,12 +711,12 @@ void main() { pathPatterns: ['/x'], guardNonMatching: true, check: (_, __) => true, - beamToNamed: (_, __, ___) => '/s1', + beamToNamed: (context, _, __, ___) => '/s1', ), BeamGuard( pathPatterns: ['/s1/s2'], check: (_, __) => false, - beamToNamed: (_, to, __) { + beamToNamed: (context, _, to, __) { return to.state.routeInformation.uri.path + '/s3'; }, ), @@ -759,7 +759,7 @@ void main() { BeamGuard( pathPatterns: ['/guarded'], check: (_, __) => false, - beamToNamed: (_, __, ___) => '/ok', + beamToNamed: (context, _, __, ___) => '/ok', ), ], ); @@ -796,7 +796,7 @@ void main() { BeamGuard( pathPatterns: ['/guarded'], check: (_, __) => false, - beamToNamed: (_, __, ___) => '/ok', + beamToNamed: (context, _, __, ___) => '/ok', ), ], ); @@ -830,7 +830,7 @@ void main() { checkTriggered = true; return false; }, - beamToNamed: (_, __, ___) => '/', + beamToNamed: (context, _, __, ___) => '/', ), ], ); diff --git a/package/test/beamer_delegate_test.dart b/package/test/beamer_delegate_test.dart index 207d9ac..9b74360 100644 --- a/package/test/beamer_delegate_test.dart +++ b/package/test/beamer_delegate_test.dart @@ -696,7 +696,7 @@ void main() { BeamGuard( pathPatterns: ['/r1'], check: (_, __) => guardCheck.value, - beamToNamed: (_, __, ___) => '/r2', + beamToNamed: (context, _, __, ___) => '/r2', ), ], updateListenable: guardCheck, @@ -779,20 +779,21 @@ void main() { BeamGuard( pathPatterns: ['/splash'], check: (_, __) => isLoading, - beamToNamed: (_, __, deepLink) => + beamToNamed: (context, _, __, deepLink) => isAuthenticated ? (deepLink ?? '/home') : '/login', ), BeamGuard( pathPatterns: ['/login'], check: (_, __) => !isAuthenticated && !isLoading, - beamToNamed: (_, __, deepLink) => + beamToNamed: (context, _, __, deepLink) => isAuthenticated ? (deepLink ?? '/home') : '/splash', ), BeamGuard( pathPatterns: ['/splash', '/login'], guardNonMatching: true, check: (_, __) => isAuthenticated, - beamToNamed: (_, __, ___) => isLoading ? '/splash' : '/login', + beamToNamed: (context, _, __, ___) => + isLoading ? '/splash' : '/login', ), ], ); diff --git a/package/test/nested_navigation.dart b/package/test/nested_navigation.dart index b006685..16d3040 100644 --- a/package/test/nested_navigation.dart +++ b/package/test/nested_navigation.dart @@ -87,7 +87,7 @@ class _MainStackState extends State { guardNonMatching: true, check: (context, state) => AuthenticationNotifier.of(context).isUserAuthenticated, - beamToNamed: (origin, target, _) => '/login') + beamToNamed: (context, origin, target, _) => '/login') ], clearBeamingHistoryOn: { '/login'