69 lines
1.9 KiB
Dart
69 lines
1.9 KiB
Dart
import 'package:aman_kassa_flutter/redux/actions/main_actions.dart';
|
|
import 'package:aman_kassa_flutter/redux/actions/user_actions.dart';
|
|
import 'package:aman_kassa_flutter/redux/reducers/main_reducer.dart';
|
|
import 'package:aman_kassa_flutter/redux/reducers/user_reducer.dart';
|
|
import 'package:aman_kassa_flutter/redux/state/main_state.dart';
|
|
import 'package:aman_kassa_flutter/redux/state/user_state.dart';
|
|
import 'package:meta/meta.dart';
|
|
import 'package:redux/redux.dart';
|
|
import 'package:redux_thunk/redux_thunk.dart';
|
|
|
|
|
|
//reducer context
|
|
AppState appReducer(AppState state, dynamic action) {
|
|
|
|
if (action is SetUserStateAction) { /** UserAction **/
|
|
final nextUserState = userReducer(state.userState, action);
|
|
return state.copyWith(userState: nextUserState);
|
|
} else if(action is SetMainStateAction) { /** MainAction **/
|
|
final nextMainState = mainReducer(state.mainState, action);
|
|
return state.copyWith(mainState: nextMainState);
|
|
}
|
|
return state;
|
|
}
|
|
|
|
//Main State
|
|
@immutable
|
|
class AppState {
|
|
final UserState userState;
|
|
final MainState mainState;
|
|
AppState({
|
|
@required this.userState,
|
|
@required this.mainState,
|
|
});
|
|
|
|
//stable work
|
|
AppState copyWith({
|
|
UserState userState,
|
|
MainState mainState,
|
|
}) {
|
|
return AppState(
|
|
userState: userState ?? this.userState,
|
|
mainState: mainState ?? this.mainState,
|
|
);
|
|
}
|
|
}
|
|
|
|
class Redux {
|
|
static Store<AppState> _store;
|
|
|
|
static Store<AppState> get store {
|
|
if (_store == null) {
|
|
throw Exception("store is not initialized");
|
|
} else {
|
|
return _store;
|
|
}
|
|
}
|
|
|
|
//initial context
|
|
static Future<void> init() async {
|
|
final userStateInitial = UserState.initial();
|
|
final mainStateInitial = MainState.initial();
|
|
|
|
_store = Store<AppState>(
|
|
appReducer,
|
|
middleware: [thunkMiddleware],
|
|
initialState: AppState(userState: userStateInitial, mainState: mainStateInitial),
|
|
);
|
|
}
|
|
} |