1
1
import 'package:redux/redux.dart' ;
2
2
import 'package:flutter/widgets.dart' ;
3
3
4
- import 'package:flutter_todo_redux/redux/actions.dart' ;
4
+ import 'package:flutter_todo_redux/redux/actions.dart'
5
+ show
6
+ HasAuthenticatedAction,
7
+ AuthenticateAction,
8
+ UnAuthenticateAction,
9
+ AuthenticateSuccessAction,
10
+ AuthenticateFailedAction,
11
+ LoadAuthenticationUserListAction,
12
+ AuthenticationUserListAction,
13
+ AuthenticationLoadingStatusAction,
14
+ NavigateAction;
5
15
import 'package:flutter_todo_redux/redux/app/app_state.dart' ;
6
16
17
+ import 'package:flutter_todo_redux/models/loading_status.dart' ;
18
+
7
19
import 'package:flutter_todo_redux/repository/auth_repository.dart' ;
8
20
import 'package:flutter_todo_redux/repository/user_repository.dart' ;
9
21
@@ -35,17 +47,21 @@ Middleware<AppState> _createHasAuthedMiddleware({
35
47
@required AuthRepository repository,
36
48
}) {
37
49
return (Store store, action, NextDispatcher next) async {
38
- store.dispatch (LoaderAction (isLoading: true ));
50
+ store.dispatch (AuthenticationLoadingStatusAction (
51
+ loadingStatus: LoadingStatus .loading));
39
52
40
53
final bool hasAuthed = await repository.hasAuthenticated ();
41
54
42
55
if (hasAuthed) {
56
+ store.dispatch (AuthenticationLoadingStatusAction (
57
+ loadingStatus: LoadingStatus .success));
43
58
return store.dispatch (AuthenticateSuccessAction ());
44
59
}
45
60
46
61
store.dispatch (NavigateAction (routeName: LoginPage .routeName));
62
+ store.dispatch (
63
+ AuthenticationLoadingStatusAction (loadingStatus: LoadingStatus .error));
47
64
48
- store.dispatch (LoaderAction (isLoading: false ));
49
65
next (action);
50
66
};
51
67
}
@@ -54,20 +70,26 @@ Middleware<AppState> _createAuthMiddleware({
54
70
@required AuthRepository repository,
55
71
}) {
56
72
return (Store store, action, NextDispatcher next) async {
57
- store.dispatch (LoaderAction (isLoading: true ));
73
+ store.dispatch (AuthenticationLoadingStatusAction (
74
+ loadingStatus: LoadingStatus .loading));
58
75
59
76
await repository
60
77
.authenticateUser (
61
- username: action.username,
62
- password: action.password,
63
- )
64
- .then ((user) => store.dispatch (AuthenticateSuccessAction ()))
65
- .catchError ((error) {
78
+ username: action.username,
79
+ password: action.password,
80
+ )
81
+ .then ((user) {
82
+ store.dispatch (AuthenticationLoadingStatusAction (
83
+ loadingStatus: LoadingStatus .success));
84
+
85
+ store.dispatch (AuthenticateSuccessAction ());
86
+ }).catchError ((error) {
66
87
print ('Error: $error ' );
88
+ store.dispatch (AuthenticationLoadingStatusAction (
89
+ loadingStatus: LoadingStatus .error));
67
90
return store.dispatch (AuthenticateFailedAction ());
68
91
});
69
92
70
- store.dispatch (LoaderAction (isLoading: false ));
71
93
next (action);
72
94
};
73
95
}
@@ -76,14 +98,16 @@ Middleware<AppState> _createUnAuthMiddleware({
76
98
@required AuthRepository repository,
77
99
}) {
78
100
return (Store store, action, NextDispatcher next) async {
79
- store.dispatch (LoaderAction (isLoading: true ));
101
+ store.dispatch (AuthenticationLoadingStatusAction (
102
+ loadingStatus: LoadingStatus .loading));
80
103
81
104
final bool auth = await repository.unAuthenticateUser ();
82
105
if (! auth) {
83
- return store.dispatch (NavigateAction (routeName: LoginPage .routeName));
106
+ store.dispatch (NavigateAction (routeName: LoginPage .routeName));
107
+ store.dispatch (AuthenticationLoadingStatusAction (
108
+ loadingStatus: LoadingStatus .success));
84
109
}
85
110
86
- store.dispatch (LoaderAction (isLoading: false ));
87
111
next (action);
88
112
};
89
113
}
@@ -110,16 +134,19 @@ Middleware<AppState> _createLoadAuthUserListMiddleware({
110
134
@required UsersRepository repository,
111
135
}) {
112
136
return (Store store, action, NextDispatcher next) async {
113
- store.dispatch (LoaderAction (isLoading: true ));
137
+ store.dispatch (AuthenticationLoadingStatusAction (
138
+ loadingStatus: LoadingStatus .loading));
114
139
115
140
await repository.getUsersList ().then ((userList) {
116
- return store.dispatch (AuthenticationUserListAction (userList: userList));
141
+ store.dispatch (AuthenticationLoadingStatusAction (
142
+ loadingStatus: LoadingStatus .success));
143
+ store.dispatch (AuthenticationUserListAction (userList: userList));
117
144
}).catchError ((error) {
145
+ store.dispatch (AuthenticationLoadingStatusAction (
146
+ loadingStatus: LoadingStatus .error));
118
147
print ('Error: $error ' );
119
148
});
120
149
121
- store.dispatch (LoaderAction (isLoading: false ));
122
-
123
150
next (action);
124
151
};
125
152
}
0 commit comments