Skip to content

Commit 9bfe412

Browse files
committed
add login logout action
1 parent 7449936 commit 9bfe412

File tree

9 files changed

+113
-31
lines changed

9 files changed

+113
-31
lines changed

lib/common/redux/gsy_state.dart

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'package:flutter/material.dart';
22
import 'package:gsy_github_app_flutter/common/model/User.dart';
3+
import 'package:gsy_github_app_flutter/common/redux/login_redux.dart';
34
import 'package:gsy_github_app_flutter/common/redux/middleware/epic_middleware.dart';
45
import 'package:gsy_github_app_flutter/common/redux/user_redux.dart';
56
import 'package:gsy_github_app_flutter/common/redux/theme_redux.dart';
@@ -26,8 +27,11 @@ class GSYState {
2627
///当前手机平台默认语言
2728
Locale platformLocale;
2829

30+
///是否登录
31+
bool login;
32+
2933
///构造方法
30-
GSYState({this.userInfo, this.themeData, this.locale});
34+
GSYState({this.userInfo, this.themeData, this.locale, this.login});
3135
}
3236

3337
///创建 Reducer
@@ -43,12 +47,13 @@ GSYState appReducer(GSYState state, action) {
4347

4448
///通过 LocaleReducer 将 GSYState 内的 locale 和 action 关联在一起
4549
locale: LocaleReducer(state.locale, action),
50+
login: LoginReducer(state.login, action),
4651
);
4752
}
4853

49-
50-
5154
final List<Middleware<GSYState>> middleware = [
5255
EpicMiddleware<GSYState>(UserInfoEpic()),
56+
EpicMiddleware<GSYState>(LoginEpic()),
5357
UserInfoMiddleware(),
54-
];
58+
LoginMiddleware(),
59+
];

lib/common/redux/login_redux.dart

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:gsy_github_app_flutter/common/ab/sql_manager.dart';
3+
import 'package:gsy_github_app_flutter/common/dao/user_dao.dart';
4+
import 'package:gsy_github_app_flutter/common/redux/gsy_state.dart';
5+
import 'package:gsy_github_app_flutter/common/redux/middleware/epic.dart';
6+
import 'package:gsy_github_app_flutter/common/redux/middleware/epic_store.dart';
7+
import 'package:gsy_github_app_flutter/common/utils/common_utils.dart';
8+
import 'package:gsy_github_app_flutter/common/utils/navigator_utils.dart';
9+
import 'package:redux/redux.dart';
10+
import 'package:rxdart/rxdart.dart';
11+
12+
/**
13+
* 登录相关Redux
14+
* Created by guoshuyu
15+
* Date: 2018-07-16
16+
*/
17+
final LoginReducer = combineReducers<bool>([
18+
TypedReducer<bool, LoginSuccessAction>(_loginResult),
19+
TypedReducer<bool, LogoutAction>(_logoutResult),
20+
]);
21+
22+
bool _loginResult(bool result, LoginSuccessAction action) {
23+
if (action.success == true) {
24+
NavigatorUtils.goHome(action.context);
25+
}
26+
return action.success;
27+
}
28+
29+
30+
class LoginAction {
31+
final BuildContext context;
32+
final String username;
33+
final String password;
34+
35+
LoginAction(this.context, this.username, this.password);
36+
}
37+
38+
class LoginSuccessAction {
39+
final BuildContext context;
40+
final bool success;
41+
42+
LoginSuccessAction(this.context, this.success);
43+
}
44+
45+
class LogoutAction {
46+
final BuildContext context;
47+
LogoutAction(this.context);
48+
}
49+
50+
bool _logoutResult(bool result, LogoutAction action) {
51+
return true;
52+
}
53+
54+
55+
class LoginMiddleware implements MiddlewareClass<GSYState> {
56+
57+
@override
58+
void call(Store<GSYState> store, dynamic action, NextDispatcher next) {
59+
if (action is LogoutAction) {
60+
UserDao.clearAll(store);
61+
SqlManager.close();
62+
NavigatorUtils.goLogin(action.context);
63+
}
64+
// Make sure to forward actions to the next middleware in the chain!
65+
next(action);
66+
}
67+
}
68+
69+
class LoginEpic implements EpicClass<GSYState> {
70+
@override
71+
Stream<dynamic> call(Stream<dynamic> actions, EpicStore<GSYState> store) {
72+
return Observable(actions)
73+
.whereType<LoginAction>()
74+
.switchMap((action) => _loginIn(action, store))
75+
.debounce(
76+
((result) => TimerStream(result, const Duration(seconds: 1))));
77+
}
78+
79+
80+
Stream<dynamic> _loginIn(
81+
LoginAction action, EpicStore<GSYState> store) async* {
82+
CommonUtils.showLoadingDialog(action.context);
83+
var res = await UserDao.login(
84+
action.username.trim(), action.password.trim(), store);
85+
Navigator.pop(action.context);
86+
yield LoginSuccessAction(action.context, (res != null && res.result));
87+
}
88+
}

lib/common/redux/middleware/epic_store.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,8 @@ class EpicStore<State> {
1515
State get state => _store.state;
1616

1717
Stream<State> get onChange => _store.onChange;
18+
19+
void dispatch(dynamic action) {
20+
_store.dispatch(action);
21+
}
1822
}

lib/common/redux/user_redux.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class UserInfoEpic implements EpicClass<GSYState> {
5454
Stream<dynamic> call(Stream<dynamic> actions, EpicStore<GSYState> store) {
5555
return Observable(actions)
5656
// to UpdateUserAction actions
57-
.ofType(TypeToken<FetchUserAction>())
57+
.whereType<FetchUserAction>()
5858
// Don't start until the 10ms
5959
.debounce(((_) => TimerStream(true, const Duration(milliseconds: 10))))
6060
.switchMap((action) => _loadUserInfo());

lib/common/utils/navigator_utils.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import 'package:gsy_github_app_flutter/common/router/anima_route.dart';
66
import 'package:gsy_github_app_flutter/page/code_detail_page_web.dart';
77
import 'package:gsy_github_app_flutter/page/common_list_page.dart';
88
import 'package:gsy_github_app_flutter/page/gsy_webview.dart';
9-
import 'package:gsy_github_app_flutter/page/home_page.dart';
9+
import 'package:gsy_github_app_flutter/page/home/home_page.dart';
1010
import 'package:gsy_github_app_flutter/page/honor_list_page.dart';
1111
import 'package:gsy_github_app_flutter/page/issue_detail_page.dart';
12-
import 'package:gsy_github_app_flutter/page/login_page.dart';
12+
import 'package:gsy_github_app_flutter/page/login/login_page.dart';
1313
import 'package:gsy_github_app_flutter/page/notify_page.dart';
1414
import 'package:gsy_github_app_flutter/page/person_page.dart';
1515
import 'package:gsy_github_app_flutter/page/photoview_page.dart';

lib/main.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import 'package:gsy_github_app_flutter/common/redux/gsy_state.dart';
1111
import 'package:gsy_github_app_flutter/common/model/User.dart';
1212
import 'package:gsy_github_app_flutter/common/style/gsy_style.dart';
1313
import 'package:gsy_github_app_flutter/common/utils/common_utils.dart';
14-
import 'package:gsy_github_app_flutter/page/home_page.dart';
15-
import 'package:gsy_github_app_flutter/page/login_page.dart';
14+
import 'package:gsy_github_app_flutter/page/home/home_page.dart';
15+
import 'package:gsy_github_app_flutter/page/login/login_page.dart';
1616
import 'package:gsy_github_app_flutter/page/welcome_page.dart';
1717
import 'package:flutter_redux/flutter_redux.dart';
1818
import 'package:redux/redux.dart';

lib/page/home_page.dart renamed to lib/page/home/home_page.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import 'package:gsy_github_app_flutter/page/my_page.dart';
1212
import 'package:gsy_github_app_flutter/page/trend_page.dart';
1313
import 'package:gsy_github_app_flutter/widget/gsy_tabbar_widget.dart';
1414
import 'package:gsy_github_app_flutter/widget/gsy_title_bar.dart';
15-
import 'package:gsy_github_app_flutter/widget/home_drawer.dart';
15+
import 'package:gsy_github_app_flutter/page/home/widget/home_drawer.dart';
1616

1717
/**
1818
* 主页

lib/widget/home_drawer.dart renamed to lib/page/home/widget/home_drawer.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:gsy_github_app_flutter/common/local/local_storage.dart';
99
import 'package:gsy_github_app_flutter/common/localization/default_localizations.dart';
1010
import 'package:gsy_github_app_flutter/common/model/User.dart';
1111
import 'package:gsy_github_app_flutter/common/redux/gsy_state.dart';
12+
import 'package:gsy_github_app_flutter/common/redux/login_redux.dart';
1213
import 'package:gsy_github_app_flutter/common/style/gsy_style.dart';
1314
import 'package:gsy_github_app_flutter/common/utils/common_utils.dart';
1415
import 'package:gsy_github_app_flutter/common/utils/navigator_utils.dart';
@@ -204,9 +205,7 @@ class HomeDrawer extends StatelessWidget {
204205
color: Colors.redAccent,
205206
textColor: GSYColors.textWhite,
206207
onPress: () {
207-
UserDao.clearAll(store);
208-
SqlManager.close();
209-
NavigatorUtils.goLogin(context);
208+
store.dispatch(LogoutAction(context));
210209
},
211210
),
212211
onTap: () {}),

lib/page/login_page.dart renamed to lib/page/login/login_page.dart

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@ import 'dart:async';
33
import 'package:flutter/material.dart';
44
import 'package:flutter_redux/flutter_redux.dart';
55
import 'package:gsy_github_app_flutter/common/config/config.dart';
6-
import 'package:gsy_github_app_flutter/common/dao/user_dao.dart';
76
import 'package:gsy_github_app_flutter/common/local/local_storage.dart';
8-
import 'package:gsy_github_app_flutter/common/localization/default_localizations.dart';
97
import 'package:gsy_github_app_flutter/common/redux/gsy_state.dart';
8+
import 'package:gsy_github_app_flutter/common/redux/login_redux.dart';
109
import 'package:gsy_github_app_flutter/common/style/gsy_style.dart';
1110
import 'package:gsy_github_app_flutter/common/utils/common_utils.dart';
12-
import 'package:gsy_github_app_flutter/common/utils/navigator_utils.dart';
1311
import 'package:gsy_github_app_flutter/widget/gsy_flex_button.dart';
1412
import 'package:gsy_github_app_flutter/widget/gsy_input_widget.dart';
1513

@@ -118,19 +116,8 @@ class _LoginPageState extends State<LoginPage> {
118116
if (_password == null || _password.length == 0) {
119117
return;
120118
}
121-
CommonUtils.showLoadingDialog(context);
122-
UserDao.login(
123-
_userName.trim(), _password.trim(), store)
124-
.then((res) {
125-
Navigator.pop(context);
126-
if (res != null && res.result) {
127-
new Future.delayed(const Duration(seconds: 1),
128-
() {
129-
NavigatorUtils.goHome(context);
130-
return true;
131-
});
132-
}
133-
});
119+
store.dispatch(
120+
LoginAction(context, _userName, _password));
134121
},
135122
),
136123
new Padding(padding: new EdgeInsets.all(15.0)),
@@ -140,8 +127,7 @@ class _LoginPageState extends State<LoginPage> {
140127
},
141128
child: Text(
142129
CommonUtils.getLocale(context).switch_language,
143-
style: TextStyle(
144-
color: GSYColors.subTextColor),
130+
style: TextStyle(color: GSYColors.subTextColor),
145131
),
146132
),
147133
new Padding(padding: new EdgeInsets.all(15.0)),

0 commit comments

Comments
 (0)