|
| 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 | +} |
0 commit comments