Skip to content

Commit e6c49ee

Browse files
committed
refactor(router): improve routing logic
- Updated redirect logic - Improved navigation flow
1 parent 1452b59 commit e6c49ee

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

lib/authentication/bloc/authentication_bloc.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ class AuthenticationBloc
2020
super(AuthenticationInitial()) {
2121
on<AuthenticationUserChanged>(_onAuthenticationUserChanged);
2222
on<AuthenticationEmailSignInRequested>(
23-
_onAuthenticationEmailSignInRequested,);
23+
_onAuthenticationEmailSignInRequested,
24+
);
2425
on<AuthenticationGoogleSignInRequested>(
2526
_onAuthenticationGoogleSignInRequested,
2627
);

lib/authentication/view/authentication_page.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class _AuthenticationView extends StatelessWidget {
2222

2323
@override
2424
Widget build(BuildContext context) {
25-
return const Placeholder(child: Text('AUTHENTICATION PAGE'),);
25+
return const Placeholder(
26+
child: Text('AUTHENTICATION PAGE'),
27+
);
2628
}
2729
}

lib/headlines-feed/view/headlines_feed_page.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class _HeadlinesFeedViewState extends State<_HeadlinesFeedView> {
8888
IconButton(
8989
icon: const Icon(Icons.search),
9090
onPressed: () {
91-
context.pushNamed(Routes.searchName);
91+
context.goNamed(Routes.searchName);
9292
},
9393
),
9494
BlocBuilder<HeadlinesFeedBloc, HeadlinesFeedState>(

lib/router/router.dart

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,25 @@ final appRouter = GoRouter(
1212
initialLocation: Routes.headlinesFeed,
1313
redirect: (BuildContext context, GoRouterState state) {
1414
final appStatus = context.read<AppBloc>().state.status;
15+
const authenticationPath = Routes.authentication;
16+
const headlinesFeedPath = Routes.headlinesFeed;
17+
18+
// If the user is not authenticated, redirect to the headlines feed
19+
// unless they are already on a route within the headlines feed.
1520
if (appStatus != AppStatus.authenticated) {
16-
return Routes.headlinesFeed;
17-
} else {
18-
return Routes.authentication;
21+
if (!state.matchedLocation.startsWith(headlinesFeedPath)) {
22+
return headlinesFeedPath;
23+
}
24+
}
25+
// If the user is authenticated, redirect to the authentication page
26+
// unless they are already on a route within the authentication section.
27+
else {
28+
if (!state.matchedLocation.startsWith(authenticationPath)) {
29+
return authenticationPath;
30+
}
1931
}
32+
// Otherwise, allow the navigation to proceed.
33+
return null;
2034
},
2135
routes: [
2236
GoRoute(

0 commit comments

Comments
 (0)