|
| 1 | +import 'package:app/main/init.dart'; |
1 | 2 | import 'package:app/presentation/ui/pages/home/home_page.dart'; |
2 | 3 | import 'package:app/presentation/ui/pages/login/login_page.dart'; |
3 | 4 | import 'package:app/presentation/ui/pages/sign_up/sign_up_page.dart'; |
4 | 5 | import 'package:app/presentation/ui/pages/splash/splash_page.dart'; |
| 6 | +import 'package:common/core/resource.dart'; |
| 7 | +import 'package:domain/bloc/auth/auth_cubit.dart'; |
| 8 | +import 'package:domain/bloc/auth/auth_state.dart'; |
| 9 | +import 'package:flutter/widgets.dart'; |
| 10 | +import 'package:flutter_bloc/flutter_bloc.dart'; |
5 | 11 | import 'package:go_router/go_router.dart'; |
6 | 12 |
|
| 13 | +enum Routes { |
| 14 | + auth, |
| 15 | + login, |
| 16 | + signup, |
| 17 | + app, |
| 18 | + home, |
| 19 | + placeholder; |
| 20 | + |
| 21 | + String get path => '/$name'; |
| 22 | + String get subPath => name; |
| 23 | + |
| 24 | + void nav(BuildContext context, {Object? extra}) { |
| 25 | + context.router.goNamed( |
| 26 | + name, |
| 27 | + extra: extra, |
| 28 | + ); |
| 29 | + } |
| 30 | + |
| 31 | + static GoRouter init(BuildContext context, {String? initialLocation}) => |
| 32 | + Routers.appRouter(context, initialLocation: initialLocation); |
| 33 | +} |
| 34 | + |
| 35 | +extension ContextOnRouter on BuildContext { |
| 36 | + GoRouter get router => GoRouter.of(this); |
| 37 | +} |
| 38 | + |
7 | 39 | class Routers { |
8 | | - static GoRouter authRouter = GoRouter( |
9 | | - initialLocation: "/splash", |
10 | | - routes: [ |
11 | | - GoRoute( |
12 | | - name: "login", |
13 | | - path: "/login", |
14 | | - builder: (context, state) => const LoginPage(), |
15 | | - ), |
16 | | - GoRoute( |
17 | | - name: "splash", |
18 | | - path: "/splash", |
19 | | - builder: (context, state) => const SplashPage(), |
20 | | - ), |
21 | | - GoRoute( |
22 | | - name: "signUp", |
23 | | - path: "/signUp", |
24 | | - builder: (context, state) => const SignUpPage(), |
25 | | - ), |
26 | | - GoRoute( |
27 | | - name: "home", |
28 | | - path: "/home", |
29 | | - builder: (context, state) => const HomePage(), |
30 | | - ), |
31 | | - ], |
32 | | - ); |
| 40 | + static GoRouter appRouter( |
| 41 | + BuildContext context, { |
| 42 | + String? initialLocation, |
| 43 | + }) => |
| 44 | + GoRouter( |
| 45 | + initialLocation: initialLocation ?? |
| 46 | + (getIt<AuthCubit>().isLoggedIn() |
| 47 | + ? Routes.app.path |
| 48 | + : Routes.auth.path), |
| 49 | + routes: [ |
| 50 | + GoRoute( |
| 51 | + path: '/', |
| 52 | + builder: (context, state) { |
| 53 | + return BlocListener<AuthCubit, Resource>( |
| 54 | + listenWhen: (previous, current) => current is RSuccess, |
| 55 | + listener: (_, appState) { |
| 56 | + if (appState is RSuccess) { |
| 57 | + switch (appState.data) { |
| 58 | + case AuthStateAuthenticated _: |
| 59 | + debugPrint('User is authenticated: ${state.fullPath}'); |
| 60 | + if (state.fullPath?.startsWith(Routes.app.path) ?? |
| 61 | + false) { |
| 62 | + // Already navigating to app, do nothing |
| 63 | + return; |
| 64 | + } |
| 65 | + debugPrint('Navigating to app route'); |
| 66 | + Routes.app.nav(context); |
| 67 | + break; |
| 68 | + case AuthStateUnauthenticated _: |
| 69 | + debugPrint( |
| 70 | + 'User is unauthenticated: ${state.fullPath}'); |
| 71 | + if (state.fullPath?.startsWith(Routes.auth.path) ?? |
| 72 | + false) { |
| 73 | + // Already navigating to auth, do nothing |
| 74 | + return; |
| 75 | + } |
| 76 | + debugPrint('Navigating to auth route'); |
| 77 | + Routes.auth.nav(context); |
| 78 | + break; |
| 79 | + case _: |
| 80 | + } |
| 81 | + } |
| 82 | + }, |
| 83 | + child: const SplashPage(), |
| 84 | + ); |
| 85 | + }, |
| 86 | + routes: [ |
| 87 | + ShellRoute( |
| 88 | + builder: (context, state, child) => child, |
| 89 | + routes: [ |
| 90 | + GoRoute( |
| 91 | + name: Routes.auth.name, |
| 92 | + path: Routes.auth.path, |
| 93 | + redirect: (context, state) { |
| 94 | + if (getIt<AuthCubit>().isLoggedIn()) { |
| 95 | + return Routes.app.path; |
| 96 | + } |
| 97 | + return null; |
| 98 | + }, |
| 99 | + builder: (context, state) => const LoginPage(), |
| 100 | + routes: [ |
| 101 | + GoRoute( |
| 102 | + name: Routes.signup.name, |
| 103 | + path: Routes.signup.subPath, |
| 104 | + builder: (context, state) => const SignUpPage(), |
| 105 | + ), |
| 106 | + ], |
| 107 | + ), |
| 108 | + ], |
| 109 | + ), |
| 110 | + ShellRoute( |
| 111 | + builder: (context, state, child) => child, |
| 112 | + routes: [ |
| 113 | + GoRoute( |
| 114 | + name: Routes.app.name, |
| 115 | + path: Routes.app.path, |
| 116 | + redirect: (context, state) { |
| 117 | + if (!getIt<AuthCubit>().isLoggedIn()) { |
| 118 | + return Routes.auth.path; |
| 119 | + } |
| 120 | + return null; |
| 121 | + }, |
| 122 | + builder: (context, state) => const HomePage(), |
| 123 | + routes: [ |
| 124 | + GoRoute( |
| 125 | + name: Routes.placeholder.name, |
| 126 | + path: Routes.placeholder.subPath, |
| 127 | + builder: (context, state) => const Placeholder(), |
| 128 | + ), |
| 129 | + ], |
| 130 | + ), |
| 131 | + ], |
| 132 | + ), |
| 133 | + ], |
| 134 | + ), |
| 135 | + ], |
| 136 | + ); |
33 | 137 | } |
0 commit comments