-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouters.dart
More file actions
137 lines (131 loc) · 4.69 KB
/
routers.dart
File metadata and controls
137 lines (131 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import 'package:app/main/init.dart';
import 'package:app/presentation/ui/pages/main/home/home_page.dart';
import 'package:app/presentation/ui/pages/auth/login/login_page.dart';
import 'package:app/presentation/ui/pages/auth/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 go(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 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.go(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.go(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(),
),
],
),
],
),
],
),
],
);
}