UserRedux.dart 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import 'package:redux/redux.dart';
  2. import 'package:jpush_flutter/jpush_flutter.dart';
  3. import '../model/UserInfo.dart';
  4. /**
  5. * 用户相关Redux
  6. * Created by guoshuyu
  7. * Date: 2018-07-16
  8. */
  9. /// redux 的 combineReducers, 通过 TypedReducer 将 UpdateUserAction 与 reducers 关联起来
  10. final userReducer = combineReducers<UserInfo>([
  11. TypedReducer<UserInfo, UpdateUserAction>(_updateLoaded),
  12. ]);
  13. /// 如果有 UpdateUserAction 发起一个请求时
  14. /// 就会调用到 _updateLoaded
  15. /// _updateLoaded 这里接受一个新的userInfo,并返回
  16. UserInfo _updateLoaded(UserInfo userInfo, action) {
  17. final JPush jpush = JPush();
  18. if (action.userInfo != null) {
  19. if (userInfo == null || (userInfo.id != action.userInfo.id)) {
  20. try {
  21. jpush.setAlias(action.userInfo.id.toString()).then((map) {}).catchError((error) => {});
  22. } catch (e) {
  23. print(e.toString());
  24. }
  25. }
  26. } else {
  27. jpush.deleteAlias();
  28. }
  29. userInfo = action.userInfo;
  30. return userInfo;
  31. }
  32. ///定一个 UpdateUserAction ,用于发起 userInfo 的的改变
  33. ///类名随你喜欢定义,只要通过上面TypedReducer绑定就好
  34. class UpdateUserAction {
  35. final UserInfo userInfo;
  36. UpdateUserAction(this.userInfo);
  37. }
  38. class LogoutAction {
  39. LogoutAction();
  40. }