100 lines
3.1 KiB
Dart
100 lines
3.1 KiB
Dart
import 'package:aman_kassa_flutter/redux/actions/kassa_actions.dart';
|
|
import 'package:aman_kassa_flutter/redux/actions/setting_actions.dart';
|
|
import 'package:aman_kassa_flutter/redux/actions/user_actions.dart';
|
|
import 'package:aman_kassa_flutter/redux/reducers/calc_reducer.dart';
|
|
import 'package:aman_kassa_flutter/redux/reducers/main_reducer.dart';
|
|
import 'package:aman_kassa_flutter/redux/reducers/setting_reducer.dart';
|
|
import 'package:aman_kassa_flutter/redux/reducers/user_reducer.dart';
|
|
import 'package:aman_kassa_flutter/redux/state/calc_state.dart';
|
|
import 'package:aman_kassa_flutter/redux/state/kassa_state.dart';
|
|
import 'package:aman_kassa_flutter/redux/state/setting_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';
|
|
|
|
import 'actions/calc_actions.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 SetKassaStateAction) {
|
|
/** KassaAction **/
|
|
final nextMainState = mainReducer(state.kassaState, action);
|
|
return state.copyWith(kassaState: nextMainState);
|
|
} else if (action is SetSettingStateAction) {
|
|
/** SettingAction **/
|
|
final nextSettingState = settingReducer(state.settingState, action);
|
|
return state.copyWith(settingState: nextSettingState);
|
|
} else if (action is SetCalcStateAction) {
|
|
/** CalcAction **/
|
|
final nextCalcState = calcReducer(state.calcState, action);
|
|
return state.copyWith(calcState: nextCalcState);
|
|
}
|
|
return state;
|
|
}
|
|
|
|
//Main State
|
|
@immutable
|
|
class AppState {
|
|
final UserState userState;
|
|
final KassaState kassaState;
|
|
final SettingState settingState;
|
|
final CalcState calcState;
|
|
|
|
AppState({
|
|
@required this.userState,
|
|
@required this.kassaState,
|
|
@required this.settingState,
|
|
@required this.calcState,
|
|
});
|
|
|
|
//stable work
|
|
AppState copyWith({
|
|
UserState userState,
|
|
KassaState kassaState,
|
|
SettingState settingState,
|
|
CalcState calcState,
|
|
}) {
|
|
return AppState(
|
|
userState: userState ?? this.userState,
|
|
kassaState: kassaState ?? this.kassaState,
|
|
settingState: settingState ?? this.settingState,
|
|
calcState: calcState ?? this.calcState,
|
|
);
|
|
}
|
|
}
|
|
|
|
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 kassaStateInitial = KassaState.initial();
|
|
final settingStateInitial = SettingState.initial();
|
|
final calcStateInitial = CalcState.initial();
|
|
|
|
_store = Store<AppState>(
|
|
appReducer,
|
|
middleware: [thunkMiddleware],
|
|
initialState: AppState(
|
|
userState: userStateInitial,
|
|
kassaState: kassaStateInitial,
|
|
settingState: settingStateInitial,
|
|
calcState: calcStateInitial),
|
|
);
|
|
}
|
|
}
|