Skip to content

Commit

Permalink
Merge pull request #665 from busslina/master
Browse files Browse the repository at this point in the history
Providing BuildContext on BeamGuard.beamToNamed
  • Loading branch information
slovnicki authored Jun 14, 2024
2 parents 1b624b5 + 842d57e commit fc74733
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 16 deletions.
3 changes: 2 additions & 1 deletion package/lib/src/beam_guard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
20 changes: 10 additions & 10 deletions package/test/beam_guard_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ void main() {
BeamGuard(
pathPatterns: ['/l2'],
check: (context, loc) => false,
beamToNamed: (_, __, ___) => '/l1',
beamToNamed: (context, _, __, ___) => '/l1',
),
],
);
Expand Down Expand Up @@ -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',
),
],
);
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -610,7 +610,7 @@ void main() {
final guard = BeamGuard(
pathPatterns: ['/guarded'],
check: (_, __) => false,
beamToNamed: (_, __, ___) => '/allowed',
beamToNamed: (context, _, __, ___) => '/allowed',
replaceCurrentStack: false,
);

Expand Down Expand Up @@ -711,12 +711,12 @@ void main() {
pathPatterns: <Pattern>['/x'],
guardNonMatching: true,
check: (_, __) => true,
beamToNamed: (_, __, ___) => '/s1',
beamToNamed: (context, _, __, ___) => '/s1',
),
BeamGuard(
pathPatterns: <Pattern>['/s1/s2'],
check: (_, __) => false,
beamToNamed: (_, to, __) {
beamToNamed: (context, _, to, __) {
return to.state.routeInformation.uri.path + '/s3';
},
),
Expand Down Expand Up @@ -759,7 +759,7 @@ void main() {
BeamGuard(
pathPatterns: <Pattern>['/guarded'],
check: (_, __) => false,
beamToNamed: (_, __, ___) => '/ok',
beamToNamed: (context, _, __, ___) => '/ok',
),
],
);
Expand Down Expand Up @@ -796,7 +796,7 @@ void main() {
BeamGuard(
pathPatterns: <Pattern>['/guarded'],
check: (_, __) => false,
beamToNamed: (_, __, ___) => '/ok',
beamToNamed: (context, _, __, ___) => '/ok',
),
],
);
Expand Down Expand Up @@ -830,7 +830,7 @@ void main() {
checkTriggered = true;
return false;
},
beamToNamed: (_, __, ___) => '/',
beamToNamed: (context, _, __, ___) => '/',
),
],
);
Expand Down
9 changes: 5 additions & 4 deletions package/test/beamer_delegate_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ void main() {
BeamGuard(
pathPatterns: ['/r1'],
check: (_, __) => guardCheck.value,
beamToNamed: (_, __, ___) => '/r2',
beamToNamed: (context, _, __, ___) => '/r2',
),
],
updateListenable: guardCheck,
Expand Down Expand Up @@ -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',
),
],
);
Expand Down
2 changes: 1 addition & 1 deletion package/test/nested_navigation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class _MainStackState extends State<MainStack> {
guardNonMatching: true,
check: (context, state) =>
AuthenticationNotifier.of(context).isUserAuthenticated,
beamToNamed: (origin, target, _) => '/login')
beamToNamed: (context, origin, target, _) => '/login')
],
clearBeamingHistoryOn: {
'/login'
Expand Down

0 comments on commit fc74733

Please sign in to comment.