|
| 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_bloc/flutter_bloc.dart'; |
5 | 10 | import 'package:go_router/go_router.dart'; |
6 | 11 |
|
| 12 | +enum Routes { |
| 13 | + auth, |
| 14 | + login, |
| 15 | + signUp, |
| 16 | + app, |
| 17 | + home; |
| 18 | + |
| 19 | + String get path => '/$name'; |
| 20 | + String get subPath => name; |
| 21 | + |
| 22 | + void nav({Object? extra}) { |
| 23 | + Routers.authRouter.goNamed( |
| 24 | + name, |
| 25 | + extra: extra, |
| 26 | + ); |
| 27 | + } |
| 28 | + |
| 29 | + static GoRouter get router => Routers.authRouter; |
| 30 | +} |
| 31 | + |
7 | 32 | class Routers { |
8 | 33 | static GoRouter authRouter = GoRouter( |
9 | | - initialLocation: "/splash", |
| 34 | + initialLocation: '/', |
10 | 35 | routes: [ |
11 | 36 | 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(), |
| 37 | + path: '/', |
| 38 | + builder: (context, state) { |
| 39 | + return BlocListener<AuthCubit, Resource>( |
| 40 | + listener: (_, state) { |
| 41 | + if (state is RSuccess) { |
| 42 | + switch (state.data) { |
| 43 | + case AuthStateAuthenticated _: |
| 44 | + Routes.app.nav(); |
| 45 | + break; |
| 46 | + case AuthStateUnauthenticated _: |
| 47 | + Routes.auth.nav(); |
| 48 | + break; |
| 49 | + case _: |
| 50 | + } |
| 51 | + } |
| 52 | + }, |
| 53 | + child: const SplashPage(instant: true), |
| 54 | + ); |
| 55 | + }, |
| 56 | + routes: [ |
| 57 | + GoRoute( |
| 58 | + name: Routes.auth.name, |
| 59 | + path: Routes.auth.path, |
| 60 | + redirect: (context, state) { |
| 61 | + if (getIt<AuthCubit>().isLoggedIn()) { |
| 62 | + return '${Routes.app.path}${Routes.home.path}'; |
| 63 | + } |
| 64 | + |
| 65 | + return '${Routes.auth.path}${Routes.login.path}'; |
| 66 | + }, |
| 67 | + routes: [ |
| 68 | + GoRoute( |
| 69 | + name: Routes.login.name, |
| 70 | + path: Routes.login.subPath, |
| 71 | + builder: (context, state) => const LoginPage(), |
| 72 | + ), |
| 73 | + GoRoute( |
| 74 | + name: Routes.signUp.name, |
| 75 | + path: Routes.signUp.subPath, |
| 76 | + builder: (context, state) => const SignUpPage(), |
| 77 | + ), |
| 78 | + ], |
| 79 | + ), |
| 80 | + GoRoute( |
| 81 | + name: Routes.app.name, |
| 82 | + path: Routes.app.path, |
| 83 | + builder: (context, state) => const SplashPage(), |
| 84 | + redirect: (context, state) { |
| 85 | + if (!getIt<AuthCubit>().isLoggedIn()) { |
| 86 | + return '${Routes.auth.path}${Routes.login.path}'; |
| 87 | + } |
| 88 | + |
| 89 | + return '${Routes.app.path}${Routes.home.path}'; |
| 90 | + }, |
| 91 | + routes: [ |
| 92 | + GoRoute( |
| 93 | + name: Routes.home.name, |
| 94 | + path: Routes.home.subPath, |
| 95 | + builder: (context, state) => const HomePage(), |
| 96 | + ), |
| 97 | + ], |
| 98 | + ), |
| 99 | + ], |
30 | 100 | ), |
31 | 101 | ], |
32 | 102 | ); |
|
0 commit comments