UserRedux.dart 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import 'package:jpush_flutter/jpush_flutter.dart';
  2. import '../model/UserInfo.dart';
  3. import 'package:redux/redux.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. JPush jpush = JPush();
  18. if (action.userInfo != null) {
  19. if (userInfo.id != action.userInfo.id) {
  20. jpush.setAlias(action.userInfo.id.toString()).then((map) {});
  21. }
  22. } else {
  23. jpush.deleteAlias();
  24. }
  25. userInfo = action.userInfo;
  26. return userInfo;
  27. }
  28. ///定一个 UpdateUserAction ,用于发起 userInfo 的的改变
  29. ///类名随你喜欢定义,只要通过上面TypedReducer绑定就好
  30. class UpdateUserAction {
  31. final UserInfo userInfo;
  32. UpdateUserAction(this.userInfo);
  33. }
  34. class LogoutAction {
  35. LogoutAction();
  36. }