-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathapp.dart
147 lines (133 loc) · 4.76 KB
/
app.dart
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
138
139
140
141
142
143
144
145
146
147
import 'package:flutter/material.dart';
import 'package:flutter_bloc_pattern/flutter_bloc_pattern.dart';
import 'package:flutter_disposebag/flutter_disposebag.dart';
import 'package:flutter_provider/flutter_provider.dart';
import 'package:node_auth/domain/models/app_error.dart';
import 'package:node_auth/domain/models/auth_state.dart';
import 'package:node_auth/domain/repositories/user_repository.dart';
import 'package:node_auth/domain/usecases/get_auth_state_stream_use_case.dart';
import 'package:node_auth/domain/usecases/get_auth_state_use_case.dart';
import 'package:node_auth/domain/usecases/#_use_case.dart';
import 'package:node_auth/domain/usecases/logout_use_case.dart';
import 'package:node_auth/domain/usecases/register_use_case.dart';
import 'package:node_auth/domain/usecases/upload_image_use_case.dart';
import 'package:node_auth/pages/home/home.dart';
import 'package:node_auth/pages/#/#.dart';
import 'package:node_auth/pages/register/register.dart';
import 'package:node_auth/pages/reset_password/reset_password_page.dart';
import 'package:node_auth/utils/streams.dart';
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
final routes = <String, WidgetBuilder>{
Navigator.defaultRouteName: (context) {
return Provider<GetAuthStateUseCase>.factory(
(context) => GetAuthStateUseCase(context.get()),
child: const Home(),
);
},
RegisterPage.routeName: (context) {
return BlocProvider<RegisterBloc>(
initBloc: (context) => RegisterBloc(
RegisterUseCase(context.get()),
),
child: const RegisterPage(),
);
},
HomePage.routeName: (context) {
return BlocProvider<HomeBloc>(
initBloc: (context) {
final userRepository = context.get<UserRepository>();
return HomeBloc(
LogoutUseCase(userRepository),
GetAuthStateStreamUseCase(userRepository),
UploadImageUseCase(userRepository),
);
},
child: const HomePage(),
);
},
LoginPage.routeName: (context) {
return BlocProvider<LoginBloc>(
initBloc: (context) => LoginBloc(
LoginUseCase(context.get()),
),
child: const LoginPage(),
);
},
ResetPasswordPage.routeName: (context) {
return const ResetPasswordPage();
},
};
final themeData = ThemeData(brightness: Brightness.dark);
return Provider<Map<String, WidgetBuilder>>.value(
routes,
child: MaterialApp(
title: 'Flutter Demo',
theme: themeData.copyWith(
colorScheme: themeData.colorScheme.copyWith(
secondary: const Color(0xFF00e676),
),
),
routes: routes,
debugShowCheckedModeBanner: false,
),
);
}
}
class Home extends StatefulWidget {
const Home({super.key});
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> with DisposeBagMixin {
late final StateStream<Result<AuthenticationState>?> authState$;
@override
void initState() {
super.initState();
final getAuthState = Provider.of<GetAuthStateUseCase>(context);
authState$ = getAuthState().castAsNullable().publishState(null)
..connect().disposedBy(bag);
}
@override
Widget build(BuildContext context) {
final routes = Provider.of<Map<String, WidgetBuilder>>(context);
return RxStreamBuilder<Result<AuthenticationState>?>(
stream: authState$,
builder: (context, result) {
if (result == null) {
debugPrint('[HOME] home [1] >> [waiting...]');
return Container(
width: double.infinity,
height: double.infinity,
color: Theme.of(context).cardColor,
child: const Center(
child: CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation(Colors.white),
),
),
);
}
return result.fold(
ifLeft: (appError) {
debugPrint(
'[HOME] home [2] >> [error -> NotAuthenticated] error=$appError');
return routes[LoginPage.routeName]!(context);
},
ifRight: (authState) {
if (authState is UnauthenticatedState) {
debugPrint('[HOME] home [3] >> [Unauthenticated]');
return routes[LoginPage.routeName]!(context);
}
if (authState is AuthenticatedState) {
debugPrint('[HOME] home [4] >> [Authenticated]');
return routes[HomePage.routeName]!(context);
}
throw StateError('Unknown auth state: $authState');
},
);
},
);
}
}