From 54c382306416513a7cecc21ee1ef3dbe4d7deb13 Mon Sep 17 00:00:00 2001 From: Andreas Fahrecker Date: Tue, 13 Oct 2020 15:08:59 +0200 Subject: [PATCH] Fixed error in app state reducer and implemented new actions in timer reducer Signed-off-by: Andreas Fahrecker --- lib/reducers/app_state_reducer.dart | 5 ++--- lib/reducers/timer_reducer.dart | 31 +++++++++++++++++++++++++---- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/lib/reducers/app_state_reducer.dart b/lib/reducers/app_state_reducer.dart index 1f4673e..7beccd0 100644 --- a/lib/reducers/app_state_reducer.dart +++ b/lib/reducers/app_state_reducer.dart @@ -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)); } diff --git a/lib/reducers/timer_reducer.dart b/lib/reducers/timer_reducer.dart index 4a741a6..3500ca6 100644 --- a/lib/reducers/timer_reducer.dart +++ b/lib/reducers/timer_reducer.dart @@ -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( - [TypedReducer(_updateTimer)]); +final timersReducer = combineReducers>([ + TypedReducer, TimersLoadedAction>(_setLoadedTimers), + TypedReducer, TimersNotLoadedAction>(_setEmptyTimers), + TypedReducer, AddTimerAction>(_addTimer), + TypedReducer, UpdateTimerAction>(_updateTimer), + TypedReducer, DeleteTimerAction>(_deleteTimer), +]); -Timer _updateTimer(Timer timer, UpdateTimerAction action) { - return action.updatedTimer; +List _setLoadedTimers(List timers, TimersLoadedAction action) { + return action.timers; +} + +List _setEmptyTimers(List timers, TimersNotLoadedAction action) { + return []; +} + +List _addTimer(List timers, AddTimerAction action) { + return List.from(timers, growable: false)..add(action.timer); +} + +List _updateTimer(List timers, UpdateTimerAction action) { + return timers + .map((timer) => timer.id == action.id ? action.updatedTimer : timer) + .toList(growable: false); +} + +List _deleteTimer(List timers, DeleteTimerAction action) { + return timers.where((timer) => timer.id != action.id).toList(growable: false); }