import 'package:redux/redux.dart'; import 'package:built_collection/built_collection.dart'; import 'package:invoiceninja_flutter/redux/app/app_actions.dart'; import 'package:invoiceninja_flutter/data/models/models.dart'; import 'package:invoiceninja_flutter/redux/company/company_actions.dart'; import 'package:invoiceninja_flutter/redux/ui/entity_ui_state.dart'; import 'package:invoiceninja_flutter/redux/schedule/schedule_actions.dart'; import 'package:invoiceninja_flutter/redux/ui/list_ui_state.dart'; import 'package:invoiceninja_flutter/redux/schedule/schedule_state.dart'; EntityUIState scheduleUIReducer(ScheduleUIState state, dynamic action) { return state.rebuild((b) => b ..listUIState.replace(scheduleListReducer(state.listUIState, action)) ..editing.replace(editingReducer(state.editing, action)!) ..selectedId = selectedIdReducer(state.selectedId, action) ..forceSelected = forceSelectedReducer(state.forceSelected, action) ..tabIndex = tabIndexReducer(state.tabIndex, action)); } final forceSelectedReducer = combineReducers([ TypedReducer((completer, action) => true), TypedReducer((completer, action) => false), TypedReducer((completer, action) => false), TypedReducer((completer, action) => false), TypedReducer((completer, action) => false), TypedReducer((completer, action) => false), TypedReducer((completer, action) => false), TypedReducer((completer, action) => false), ]); final int? Function(int, dynamic) tabIndexReducer = combineReducers([ TypedReducer((completer, action) { return action.tabIndex; }), TypedReducer((completer, action) { return 0; }), ]); Reducer selectedIdReducer = combineReducers([ TypedReducer((completer, action) => ''), TypedReducer((completer, action) => ''), TypedReducer((selectedId, action) => action.entityType == EntityType.schedule ? action.entityId : selectedId), TypedReducer( (String? selectedId, dynamic action) => action.scheduleId), TypedReducer( (String? selectedId, dynamic action) => action.schedule.id), TypedReducer( (selectedId, action) => action.clearSelection ? '' : selectedId), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), TypedReducer( (selectedId, action) => action.clearSelection ? '' : action.entityType == EntityType.schedule ? action.entityId : selectedId), ]); final editingReducer = combineReducers([ TypedReducer(_updateEditing), TypedReducer(_updateEditing), TypedReducer((schedules, action) { return action.schedules[0]; }), TypedReducer((schedules, action) { return action.schedules[0]; }), TypedReducer((schedules, action) { return action.schedules[0]; }), TypedReducer(_updateEditing), TypedReducer((schedule, action) { return action.schedule.rebuild((b) => b..isChanged = true); }), TypedReducer(_clearEditing), ]); ScheduleEntity _clearEditing(ScheduleEntity? schedule, dynamic action) { return ScheduleEntity(ScheduleEntity.TEMPLATE_EMAIL_STATEMENT); } ScheduleEntity? _updateEditing(ScheduleEntity? schedule, dynamic action) { return action.schedule; } final scheduleListReducer = combineReducers([ TypedReducer(_sortSchedules), TypedReducer(_filterSchedulesByState), TypedReducer(_filterSchedules), TypedReducer( _filterSchedulesByCustom1), TypedReducer( _filterSchedulesByCustom2), TypedReducer(_startListMultiselect), TypedReducer(_addToListMultiselect), TypedReducer( _removeFromListMultiselect), TypedReducer(_clearListMultiselect), TypedReducer(_viewScheduleList), TypedReducer( (state, action) => state.rebuild((b) => b ..filter = null ..filterClearedAt = DateTime.now().millisecondsSinceEpoch)), ]); ListUIState _viewScheduleList( ListUIState scheduleListState, ViewScheduleList action) { return scheduleListState.rebuild((b) => b ..selectedIds = null ..filter = null ..filterClearedAt = DateTime.now().millisecondsSinceEpoch); } ListUIState _filterSchedulesByCustom1( ListUIState scheduleListState, FilterSchedulesByCustom1 action) { if (scheduleListState.custom1Filters.contains(action.value)) { return scheduleListState .rebuild((b) => b..custom1Filters.remove(action.value)); } else { return scheduleListState .rebuild((b) => b..custom1Filters.add(action.value)); } } ListUIState _filterSchedulesByCustom2( ListUIState scheduleListState, FilterSchedulesByCustom2 action) { if (scheduleListState.custom2Filters.contains(action.value)) { return scheduleListState .rebuild((b) => b..custom2Filters.remove(action.value)); } else { return scheduleListState .rebuild((b) => b..custom2Filters.add(action.value)); } } ListUIState _filterSchedulesByState( ListUIState scheduleListState, FilterSchedulesByState action) { if (scheduleListState.stateFilters.contains(action.state)) { return scheduleListState .rebuild((b) => b..stateFilters.remove(action.state)); } else { return scheduleListState.rebuild((b) => b..stateFilters.add(action.state)); } } ListUIState _filterSchedules( ListUIState scheduleListState, FilterSchedules action) { return scheduleListState.rebuild((b) => b ..filter = action.filter ..filterClearedAt = action.filter == null ? DateTime.now().millisecondsSinceEpoch : scheduleListState.filterClearedAt); } ListUIState _sortSchedules( ListUIState scheduleListState, SortSchedules action) { return scheduleListState.rebuild((b) => b ..sortAscending = b.sortField != action.field || !b.sortAscending! ..sortField = action.field); } ListUIState _startListMultiselect( ListUIState productListState, StartScheduleMultiselect action) { return productListState.rebuild((b) => b..selectedIds = ListBuilder()); } ListUIState _addToListMultiselect( ListUIState productListState, AddToScheduleMultiselect action) { return productListState.rebuild((b) => b..selectedIds.add(action.entity!.id)); } ListUIState _removeFromListMultiselect( ListUIState productListState, RemoveFromScheduleMultiselect action) { return productListState .rebuild((b) => b..selectedIds.remove(action.entity!.id)); } ListUIState _clearListMultiselect( ListUIState productListState, ClearScheduleMultiselect action) { return productListState.rebuild((b) => b..selectedIds = null); } final schedulesReducer = combineReducers([ TypedReducer(_updateSchedule), TypedReducer(_addSchedule), TypedReducer(_setLoadedSchedules), TypedReducer(_setLoadedSchedule), TypedReducer(_setLoadedCompany), TypedReducer(_archiveScheduleSuccess), TypedReducer(_deleteScheduleSuccess), TypedReducer(_restoreScheduleSuccess), ]); ScheduleState _archiveScheduleSuccess( ScheduleState scheduleState, ArchiveSchedulesSuccess action) { return scheduleState.rebuild((b) { for (final schedule in action.schedules) { b.map[schedule.id] = schedule; } }); } ScheduleState _deleteScheduleSuccess( ScheduleState scheduleState, DeleteSchedulesSuccess action) { return scheduleState.rebuild((b) { for (final schedule in action.schedules) { b.map[schedule.id] = schedule; } }); } ScheduleState _restoreScheduleSuccess( ScheduleState scheduleState, RestoreSchedulesSuccess action) { return scheduleState.rebuild((b) { for (final schedule in action.schedules) { b.map[schedule.id] = schedule; } }); } ScheduleState _addSchedule( ScheduleState scheduleState, AddScheduleSuccess action) { return scheduleState.rebuild((b) => b ..map[action.schedule.id] = action.schedule ..list.add(action.schedule.id)); } ScheduleState _updateSchedule( ScheduleState scheduleState, SaveScheduleSuccess action) { return scheduleState .rebuild((b) => b..map[action.schedule.id] = action.schedule); } ScheduleState _setLoadedSchedule( ScheduleState scheduleState, LoadScheduleSuccess action) { return scheduleState .rebuild((b) => b..map[action.schedule.id] = action.schedule); } ScheduleState _setLoadedSchedules( ScheduleState scheduleState, LoadSchedulesSuccess action) => scheduleState.loadSchedules(action.schedules); ScheduleState _setLoadedCompany( ScheduleState scheduleState, LoadCompanySuccess action) { final company = action.userCompany.company; return scheduleState.loadSchedules(company.schedules); }