Fixed error in app state reducer and implemented new actions in timer
reducer Signed-off-by: Andreas Fahrecker <AndreasFahrecker@gmail.com>
This commit is contained in:
parent
90b19e304d
commit
54c3823064
@ -1,7 +1,6 @@
|
||||
import 'package:time_progress_calculator/actions/timer_actions.dart';
|
||||
import 'package:time_progress_calculator/models/app_state.dart';
|
||||
import 'package:time_progress_calculator/reducers/timer_reducer.dart';
|
||||
|
||||
AppState appReducer(AppState state, Action action) {
|
||||
return AppState(timer: timersReducer(state.timer, action));
|
||||
AppState appStateReducer(AppState state, dynamic action) {
|
||||
return AppState(timers: timersReducer(state.timers, action));
|
||||
}
|
||||
|
@ -2,9 +2,32 @@ import 'package:time_progress_calculator/actions/timer_actions.dart';
|
||||
import 'package:time_progress_calculator/models/timer.dart';
|
||||
import 'package:redux/redux.dart';
|
||||
|
||||
final timersReducer = combineReducers<Timer>(
|
||||
[TypedReducer<Timer, UpdateTimerAction>(_updateTimer)]);
|
||||
final timersReducer = combineReducers<List<Timer>>([
|
||||
TypedReducer<List<Timer>, TimersLoadedAction>(_setLoadedTimers),
|
||||
TypedReducer<List<Timer>, TimersNotLoadedAction>(_setEmptyTimers),
|
||||
TypedReducer<List<Timer>, AddTimerAction>(_addTimer),
|
||||
TypedReducer<List<Timer>, UpdateTimerAction>(_updateTimer),
|
||||
TypedReducer<List<Timer>, DeleteTimerAction>(_deleteTimer),
|
||||
]);
|
||||
|
||||
Timer _updateTimer(Timer timer, UpdateTimerAction action) {
|
||||
return action.updatedTimer;
|
||||
List<Timer> _setLoadedTimers(List<Timer> timers, TimersLoadedAction action) {
|
||||
return action.timers;
|
||||
}
|
||||
|
||||
List<Timer> _setEmptyTimers(List<Timer> timers, TimersNotLoadedAction action) {
|
||||
return [];
|
||||
}
|
||||
|
||||
List<Timer> _addTimer(List<Timer> timers, AddTimerAction action) {
|
||||
return List.from(timers, growable: false)..add(action.timer);
|
||||
}
|
||||
|
||||
List<Timer> _updateTimer(List<Timer> timers, UpdateTimerAction action) {
|
||||
return timers
|
||||
.map((timer) => timer.id == action.id ? action.updatedTimer : timer)
|
||||
.toList(growable: false);
|
||||
}
|
||||
|
||||
List<Timer> _deleteTimer(List<Timer> timers, DeleteTimerAction action) {
|
||||
return timers.where((timer) => timer.id != action.id).toList(growable: false);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user