Skip to content

Commit 6bfe46b

Browse files
committed
change redux test
1 parent 0d60e9c commit 6bfe46b

5 files changed

Lines changed: 32 additions & 16 deletions

File tree

‎lib/common/dao/UserDao.dart‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import 'package:gsy_github_app_flutter/common/local/LocalStorage.dart';
88
import 'package:gsy_github_app_flutter/common/model/User.dart';
99
import 'package:gsy_github_app_flutter/common/net/Address.dart';
1010
import 'package:gsy_github_app_flutter/common/net/Api.dart';
11+
import 'package:gsy_github_app_flutter/common/redux/UserRedux.dart';
1112
import 'package:redux/redux.dart';
12-
import 'package:gsy_github_app_flutter/common/redux/GSYState.dart';
1313

1414
class UserDao {
1515
static login(userName, password, callback) async {
@@ -50,7 +50,7 @@ class UserDao {
5050
var token = await LocalStorage.get(Config.TOKEN_KEY);
5151
var res = await getUserInfoLocal();
5252
if (res != null && res.result && token != null) {
53-
store.dispatch(UserActions(res.data));
53+
store.dispatch(UpdateUserAction(res.data));
5454
}
5555
return new DataResult(res.data, (res.result && (token != null)));
5656
}

‎lib/common/redux/GSYState.dart‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
import 'package:gsy_github_app_flutter/common/model/User.dart';
2+
import 'package:gsy_github_app_flutter/common/redux/UserRedux.dart';
23

34
class GSYState {
5+
46
User userInfo;
7+
58
GSYState({this.userInfo});
6-
}
79

8-
// One simple action: Increment
9-
class UserActions {
10-
final User userInfo;
11-
UserActions(this.userInfo);
1210
}
1311

14-
GSYState counterReducer(GSYState state, dynamic action) {
15-
if (action is UserActions) {
16-
state.userInfo = action.userInfo;
17-
}
18-
return state;
12+
13+
GSYState appReducer(GSYState state, action) {
14+
return GSYState(
15+
userInfo: UserReducer(state.userInfo, action),
16+
);
1917
}
2018

‎lib/common/redux/UserRedux.dart‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
import 'package:gsy_github_app_flutter/common/model/User.dart';
3+
import 'package:redux/redux.dart';
4+
5+
final UserReducer = combineReducers<User>([
6+
TypedReducer<User, UpdateUserAction>(_updateLoaded),
7+
]);
8+
9+
User _updateLoaded(User user, action) {
10+
user = action.userInfo;
11+
return user;
12+
}
13+
14+
class UpdateUserAction {
15+
final User userInfo;
16+
UpdateUserAction(this.userInfo);
17+
}

‎lib/main.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ void main() {
1515

1616
class FlutterReduxApp extends StatelessWidget {
1717

18-
final store = new Store<GSYState>(counterReducer, initialState: new GSYState(userInfo: User.empty()));
18+
final store = new Store<GSYState>(appReducer, initialState: new GSYState(userInfo: User.empty()));
1919

2020
FlutterReduxApp({Key key}) : super(key: key);
2121

‎lib/page/HomePage.dart‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
44
import 'package:flutter_redux/flutter_redux.dart';
55
import 'package:gsy_github_app_flutter/common/model/User.dart';
66
import 'package:gsy_github_app_flutter/common/redux/GSYState.dart';
7+
import 'package:gsy_github_app_flutter/common/redux/UserRedux.dart';
78
import 'package:gsy_github_app_flutter/common/style/GSYStyle.dart';
89
import 'package:gsy_github_app_flutter/widget/GSYTabBarWidget.dart';
910

@@ -23,9 +24,9 @@ class HomePage extends StatelessWidget {
2324
builder: (context, store) {
2425
new Future.delayed(const Duration(seconds: 2), () {
2526
User user = store.state.userInfo;
26-
user.login = "ffff";
27-
user.name = "ttttt";
28-
store.dispatch(new UserActions(user));
27+
user.login = "new login";
28+
user.name = "new name";
29+
store.dispatch(new UpdateUserAction(user));
2930
});
3031
return new Text(
3132
store.state.userInfo.login,

0 commit comments

Comments
 (0)