-
Notifications
You must be signed in to change notification settings - Fork 0
Improve app navigation. #111
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
Changes from all commits
e383801
b10b72b
394ee16
15f9331
24a3a95
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 |
|---|---|---|
| @@ -1,33 +1,137 @@ | ||
| import 'package:app/main/init.dart'; | ||
| import 'package:app/presentation/ui/pages/home/home_page.dart'; | ||
| import 'package:app/presentation/ui/pages/login/login_page.dart'; | ||
| import 'package:app/presentation/ui/pages/sign_up/sign_up_page.dart'; | ||
| import 'package:app/presentation/ui/pages/splash/splash_page.dart'; | ||
| import 'package:common/core/resource.dart'; | ||
| import 'package:domain/bloc/auth/auth_cubit.dart'; | ||
| import 'package:domain/bloc/auth/auth_state.dart'; | ||
| import 'package:flutter/widgets.dart'; | ||
| import 'package:flutter_bloc/flutter_bloc.dart'; | ||
| import 'package:go_router/go_router.dart'; | ||
|
|
||
| enum Routes { | ||
| auth, | ||
| login, | ||
| signup, | ||
| app, | ||
| home, | ||
| placeholder; | ||
|
|
||
| String get path => '/$name'; | ||
|
||
| String get subPath => name; | ||
|
|
||
| void nav(BuildContext context, {Object? extra}) { | ||
| context.router.goNamed( | ||
| name, | ||
| extra: extra, | ||
| ); | ||
| } | ||
|
|
||
| static GoRouter init(BuildContext context, {String? initialLocation}) => | ||
| Routers.appRouter(context, initialLocation: initialLocation); | ||
| } | ||
|
|
||
| extension ContextOnRouter on BuildContext { | ||
| GoRouter get router => GoRouter.of(this); | ||
| } | ||
|
|
||
| class Routers { | ||
| static GoRouter authRouter = GoRouter( | ||
| initialLocation: "/splash", | ||
| routes: [ | ||
| GoRoute( | ||
| name: "login", | ||
| path: "/login", | ||
| builder: (context, state) => const LoginPage(), | ||
| ), | ||
| GoRoute( | ||
| name: "splash", | ||
| path: "/splash", | ||
| builder: (context, state) => const SplashPage(), | ||
| ), | ||
| GoRoute( | ||
| name: "signUp", | ||
| path: "/signUp", | ||
| builder: (context, state) => const SignUpPage(), | ||
| ), | ||
| GoRoute( | ||
| name: "home", | ||
| path: "/home", | ||
| builder: (context, state) => const HomePage(), | ||
| ), | ||
| ], | ||
| ); | ||
| static GoRouter appRouter( | ||
| BuildContext context, { | ||
| String? initialLocation, | ||
| }) => | ||
| GoRouter( | ||
| initialLocation: initialLocation ?? | ||
| (getIt<AuthCubit>().isLoggedIn() | ||
| ? Routes.app.path | ||
| : Routes.auth.path), | ||
| routes: [ | ||
| GoRoute( | ||
| path: '/', | ||
| builder: (context, state) { | ||
| return BlocListener<AuthCubit, Resource>( | ||
| listenWhen: (previous, current) => current is RSuccess, | ||
| listener: (_, appState) { | ||
| if (appState is RSuccess) { | ||
| switch (appState.data) { | ||
| case AuthStateAuthenticated _: | ||
| debugPrint('User is authenticated: ${state.fullPath}'); | ||
| if (state.fullPath?.startsWith(Routes.app.path) ?? | ||
| false) { | ||
| // Already navigating to app, do nothing | ||
| return; | ||
| } | ||
| debugPrint('Navigating to app route'); | ||
| Routes.app.nav(context); | ||
| break; | ||
| case AuthStateUnauthenticated _: | ||
| debugPrint( | ||
| 'User is unauthenticated: ${state.fullPath}'); | ||
| if (state.fullPath?.startsWith(Routes.auth.path) ?? | ||
| false) { | ||
| // Already navigating to auth, do nothing | ||
| return; | ||
| } | ||
| debugPrint('Navigating to auth route'); | ||
| Routes.auth.nav(context); | ||
| break; | ||
| case _: | ||
| } | ||
| } | ||
| }, | ||
| child: const SplashPage(), | ||
| ); | ||
| }, | ||
| routes: [ | ||
| ShellRoute( | ||
| builder: (context, state, child) => child, | ||
| routes: [ | ||
| GoRoute( | ||
| name: Routes.auth.name, | ||
| path: Routes.auth.path, | ||
| redirect: (context, state) { | ||
| if (getIt<AuthCubit>().isLoggedIn()) { | ||
| return Routes.app.path; | ||
| } | ||
| return null; | ||
| }, | ||
| builder: (context, state) => const LoginPage(), | ||
| routes: [ | ||
| GoRoute( | ||
| name: Routes.signup.name, | ||
| path: Routes.signup.subPath, | ||
| builder: (context, state) => const SignUpPage(), | ||
| ), | ||
| ], | ||
| ), | ||
| ], | ||
| ), | ||
| ShellRoute( | ||
| builder: (context, state, child) => child, | ||
| routes: [ | ||
| GoRoute( | ||
| name: Routes.app.name, | ||
| path: Routes.app.path, | ||
| redirect: (context, state) { | ||
| if (!getIt<AuthCubit>().isLoggedIn()) { | ||
| return Routes.auth.path; | ||
| } | ||
| return null; | ||
| }, | ||
| builder: (context, state) => const HomePage(), | ||
| routes: [ | ||
| GoRoute( | ||
| name: Routes.placeholder.name, | ||
| path: Routes.placeholder.subPath, | ||
| builder: (context, state) => const Placeholder(), | ||
| ), | ||
| ], | ||
| ), | ||
| ], | ||
| ), | ||
| ], | ||
| ), | ||
| ], | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| # | ||
|
|
||
| list(APPEND FLUTTER_PLUGIN_LIST | ||
| gtk | ||
| ) | ||
|
|
||
| list(APPEND FLUTTER_FFI_PLUGIN_LIST | ||
|
|
||
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.
The
usePathUrlStrategy()call should be placed afterWidgetsFlutterBinding.ensureInitialized()but before any other initialization. However, it's currently placed after the binding initialization but on a separate line without a comment. While functionally correct, consider adding a comment explaining why path-based URLs are being used instead of hash-based URLs (e.g., for better SEO, cleaner URLs).