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:
Andreas Fahrecker 2020-10-13 15:08:59 +02:00
parent 90b19e304d
commit 54c3823064
2 changed files with 29 additions and 7 deletions

View File

@ -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/models/app_state.dart';
import 'package:time_progress_calculator/reducers/timer_reducer.dart'; import 'package:time_progress_calculator/reducers/timer_reducer.dart';
AppState appReducer(AppState state, Action action) { AppState appStateReducer(AppState state, dynamic action) {
return AppState(timer: timersReducer(state.timer, action)); return AppState(timers: timersReducer(state.timers, action));
} }

View File

@ -2,9 +2,32 @@ import 'package:time_progress_calculator/actions/timer_actions.dart';
import 'package:time_progress_calculator/models/timer.dart'; import 'package:time_progress_calculator/models/timer.dart';
import 'package:redux/redux.dart'; import 'package:redux/redux.dart';
final timersReducer = combineReducers<Timer>( final timersReducer = combineReducers<List<Timer>>([
[TypedReducer<Timer, UpdateTimerAction>(_updateTimer)]); 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) { List<Timer> _setLoadedTimers(List<Timer> timers, TimersLoadedAction action) {
return action.updatedTimer; 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);
} }