time_progress_tracker/lib/reducers/timer_reducer.dart
Andreas Fahrecker 63777c6b5c Fixed Error with nongrowable list in reducer
Signed-off-by: Andreas Fahrecker <AndreasFahrecker@gmail.com>
2020-10-15 15:53:28 +02:00

36 lines
1.2 KiB
Dart

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<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),
]);
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)
..add(action.timer)
..toList(growable: false);
}
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);
}