Skip to content

Commit 00e1169

Browse files
authored
Fixed: loading status (#5)
1 parent cc3262b commit 00e1169

12 files changed

+103
-62
lines changed

lib/redux/actions.dart

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
export 'package:flutter_todo_redux/redux/app/loader_actions.dart';
21
export 'package:flutter_todo_redux/redux/app/navigate_actions.dart';
32

43
export 'package:flutter_todo_redux/redux/home/home_actions.dart';

lib/redux/app/loader_actions.dart

-10
This file was deleted.

lib/redux/app/loader_reducer.dart

-11
This file was deleted.

lib/redux/app/loading_reducer.dart

-13
This file was deleted.

lib/redux/auth/auth_actions.dart

+12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:flutter_todo_redux/models/loading_status.dart';
12
import 'package:flutter_todo_redux/models/user.dart';
23

34
class AuthenticateAction {
@@ -31,3 +32,14 @@ class AuthenticationUserListAction {
3132
return "AuthenticationUserListAction {userList: $userList}";
3233
}
3334
}
35+
36+
class AuthenticationLoadingStatusAction {
37+
AuthenticationLoadingStatusAction({this.loadingStatus});
38+
39+
final LoadingStatus loadingStatus;
40+
41+
@override
42+
String toString() {
43+
return "AuthenticationLoadingStatusAction {loadingStatus: $loadingStatus}";
44+
}
45+
}

lib/redux/auth/auth_middleware.dart

+44-17
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
import 'package:redux/redux.dart';
22
import 'package:flutter/widgets.dart';
33

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;
515
import 'package:flutter_todo_redux/redux/app/app_state.dart';
616

17+
import 'package:flutter_todo_redux/models/loading_status.dart';
18+
719
import 'package:flutter_todo_redux/repository/auth_repository.dart';
820
import 'package:flutter_todo_redux/repository/user_repository.dart';
921

@@ -35,17 +47,21 @@ Middleware<AppState> _createHasAuthedMiddleware({
3547
@required AuthRepository repository,
3648
}) {
3749
return (Store store, action, NextDispatcher next) async {
38-
store.dispatch(LoaderAction(isLoading: true));
50+
store.dispatch(AuthenticationLoadingStatusAction(
51+
loadingStatus: LoadingStatus.loading));
3952

4053
final bool hasAuthed = await repository.hasAuthenticated();
4154

4255
if (hasAuthed) {
56+
store.dispatch(AuthenticationLoadingStatusAction(
57+
loadingStatus: LoadingStatus.success));
4358
return store.dispatch(AuthenticateSuccessAction());
4459
}
4560

4661
store.dispatch(NavigateAction(routeName: LoginPage.routeName));
62+
store.dispatch(
63+
AuthenticationLoadingStatusAction(loadingStatus: LoadingStatus.error));
4764

48-
store.dispatch(LoaderAction(isLoading: false));
4965
next(action);
5066
};
5167
}
@@ -54,20 +70,26 @@ Middleware<AppState> _createAuthMiddleware({
5470
@required AuthRepository repository,
5571
}) {
5672
return (Store store, action, NextDispatcher next) async {
57-
store.dispatch(LoaderAction(isLoading: true));
73+
store.dispatch(AuthenticationLoadingStatusAction(
74+
loadingStatus: LoadingStatus.loading));
5875

5976
await repository
6077
.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) {
6687
print('Error: $error');
88+
store.dispatch(AuthenticationLoadingStatusAction(
89+
loadingStatus: LoadingStatus.error));
6790
return store.dispatch(AuthenticateFailedAction());
6891
});
6992

70-
store.dispatch(LoaderAction(isLoading: false));
7193
next(action);
7294
};
7395
}
@@ -76,14 +98,16 @@ Middleware<AppState> _createUnAuthMiddleware({
7698
@required AuthRepository repository,
7799
}) {
78100
return (Store store, action, NextDispatcher next) async {
79-
store.dispatch(LoaderAction(isLoading: true));
101+
store.dispatch(AuthenticationLoadingStatusAction(
102+
loadingStatus: LoadingStatus.loading));
80103

81104
final bool auth = await repository.unAuthenticateUser();
82105
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));
84109
}
85110

86-
store.dispatch(LoaderAction(isLoading: false));
87111
next(action);
88112
};
89113
}
@@ -110,16 +134,19 @@ Middleware<AppState> _createLoadAuthUserListMiddleware({
110134
@required UsersRepository repository,
111135
}) {
112136
return (Store store, action, NextDispatcher next) async {
113-
store.dispatch(LoaderAction(isLoading: true));
137+
store.dispatch(AuthenticationLoadingStatusAction(
138+
loadingStatus: LoadingStatus.loading));
114139

115140
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));
117144
}).catchError((error) {
145+
store.dispatch(AuthenticationLoadingStatusAction(
146+
loadingStatus: LoadingStatus.error));
118147
print('Error: $error');
119148
});
120149

121-
store.dispatch(LoaderAction(isLoading: false));
122-
123150
next(action);
124151
};
125152
}

lib/redux/auth/auth_reducer.dart

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import 'package:redux/redux.dart';
22
import 'package:flutter_todo_redux/redux/actions.dart'
3-
show AuthenticationUserListAction;
3+
show AuthenticationUserListAction, AuthenticationLoadingStatusAction;
44
import 'package:flutter_todo_redux/redux/auth/auth_state.dart';
55

66
final authReducer = combineReducers<AuthState>([
77
TypedReducer<AuthState, AuthenticationUserListAction>(_setUserList),
8+
TypedReducer<AuthState, AuthenticationLoadingStatusAction>(
9+
_setAuthenticationLoadingStatus)
810
]);
911

1012
AuthState _setUserList(
@@ -13,3 +15,10 @@ AuthState _setUserList(
1315
) {
1416
return state.copyWith(userList: action.userList);
1517
}
18+
19+
AuthState _setAuthenticationLoadingStatus(
20+
AuthState state,
21+
AuthenticationLoadingStatusAction action,
22+
) {
23+
return state.copyWith(loadingStatus: action.loadingStatus);
24+
}

lib/redux/home/home_actions.dart

+12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:flutter_todo_redux/models/loading_status.dart';
12
import 'package:flutter_todo_redux/models/todo.dart';
23

34
class IncrementCountAction {}
@@ -56,3 +57,14 @@ class AddTodoAction {
5657
return 'AddTodoAction{todo: $todo}';
5758
}
5859
}
60+
61+
class HomeLoadingStatusAction {
62+
HomeLoadingStatusAction({this.loadingStatus});
63+
64+
final LoadingStatus loadingStatus;
65+
66+
@override
67+
String toString() {
68+
return "HomeLoadingStatusAction {loadingStatus: $loadingStatus}";
69+
}
70+
}

lib/redux/home/home_middleware.dart

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import 'package:redux/redux.dart';
22

33
import 'package:flutter_todo_redux/redux/actions.dart'
4-
show LoaderAction, LoadTodosAction, TodosLoadedAction, TodosNotLoadedAction;
4+
show
5+
LoadTodosAction,
6+
TodosLoadedAction,
7+
TodosNotLoadedAction,
8+
HomeLoadingStatusAction;
59
import 'package:flutter_todo_redux/redux/app/app_state.dart';
610

11+
import 'package:flutter_todo_redux/models/loading_status.dart';
12+
713
import 'package:flutter_todo_redux/repository/todos_repository.dart';
814

915
// import 'package:path_provider/path_provider.dart';
@@ -40,22 +46,26 @@ List<Middleware<AppState>> createStoreTodosMiddleware([
4046

4147
Middleware<AppState> _createLoadTodos(TodosRepository repository) {
4248
return (Store<AppState> store, action, NextDispatcher next) async {
43-
store.dispatch(LoaderAction(isLoading: true));
49+
store.dispatch(
50+
HomeLoadingStatusAction(loadingStatus: LoadingStatus.loading));
4451

4552
await repository.loadTodos().then(
4653
(todos) {
47-
return store.dispatch(
54+
store.dispatch(
4855
TodosLoadedAction(
4956
todos,
5057
),
5158
);
59+
store.dispatch(
60+
HomeLoadingStatusAction(loadingStatus: LoadingStatus.success));
5261
},
5362
).catchError((error) {
5463
print('Error: $error');
64+
store.dispatch(
65+
HomeLoadingStatusAction(loadingStatus: LoadingStatus.error));
5566
return store.dispatch(TodosNotLoadedAction());
5667
});
5768

58-
store.dispatch(LoaderAction(isLoading: false));
5969
next(action);
6070
};
6171
}

lib/redux/home/home_reducer.dart

+10-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import 'package:flutter_todo_redux/redux/actions.dart'
55
UpdateTodoAction,
66
DeleteTodoAction,
77
IncrementCountAction,
8-
DecrememtCountAction;
8+
DecrememtCountAction,
9+
HomeLoadingStatusAction;
910
import 'package:flutter_todo_redux/redux/home/home_state.dart';
1011

1112
import 'package:flutter_todo_redux/models/todo.dart';
@@ -16,6 +17,7 @@ final homeReducer = combineReducers<HomeState>([
1617
TypedReducer<HomeState, DeleteTodoAction>(_deleteTodo),
1718
TypedReducer<HomeState, IncrementCountAction>(_incrementCount),
1819
TypedReducer<HomeState, DecrememtCountAction>(_decrememtCount),
20+
TypedReducer<HomeState, HomeLoadingStatusAction>(_setHomeLoadingStatus),
1921
]);
2022

2123
HomeState _setLoadedTodos(HomeState state, TodosLoadedAction action) {
@@ -45,3 +47,10 @@ HomeState _incrementCount(HomeState state, IncrementCountAction action) {
4547
HomeState _decrememtCount(HomeState state, DecrememtCountAction action) {
4648
return state.copyWith(count: state.count - 1);
4749
}
50+
51+
HomeState _setHomeLoadingStatus(
52+
HomeState state,
53+
HomeLoadingStatusAction action,
54+
) {
55+
return state.copyWith(loadingStatus: action.loadingStatus);
56+
}

lib/redux/reducers.dart

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
export 'package:flutter_todo_redux/redux/app/app_reducer.dart';
2-
export 'package:flutter_todo_redux/redux/app/loading_reducer.dart';
3-
export 'package:flutter_todo_redux/redux/app/loader_reducer.dart';
42

53
export 'package:flutter_todo_redux/redux/home/home_reducer.dart';
64
export 'package:flutter_todo_redux/redux/auth/auth_reducer.dart';

lib/ui/home/home.dart

+1-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ class _HomePageState extends State<HomePage> {
4545
Widget build(BuildContext context) {
4646
Store<AppState> store = StoreProvider.of<AppState>(context);
4747
listenStore = store.onChange.listen((_store) async {
48-
await Future.delayed(Duration(seconds: 3));
4948
if (_store.homeState.loadingStatus != loadingStatus) {
5049
setState(() {
5150
loadingStatus = _store.homeState.loadingStatus;
@@ -57,7 +56,7 @@ class _HomePageState extends State<HomePage> {
5756
appBar: AppBar(
5857
title: Text(widget.title),
5958
actions: <Widget>[
60-
if (loadingStatus == LoadingStatus.loading)
59+
if (store.state.homeState.loadingStatus == LoadingStatus.loading)
6160
Icon(
6261
Icons.network_check,
6362
color: Colors.white,

0 commit comments

Comments
 (0)