import 'dart:math'; import 'package:built_collection/built_collection.dart'; import 'package:invoiceninja_flutter/constants.dart'; import 'package:invoiceninja_flutter/data/models/entities.dart'; import 'package:invoiceninja_flutter/redux/app/app_actions.dart'; import 'package:invoiceninja_flutter/redux/client/client_actions.dart'; import 'package:invoiceninja_flutter/redux/company/company_actions.dart'; import 'package:invoiceninja_flutter/redux/company/company_state.dart'; import 'package:invoiceninja_flutter/redux/company_gateway/company_gateway_actions.dart'; import 'package:invoiceninja_flutter/redux/expense/expense_actions.dart'; import 'package:invoiceninja_flutter/redux/group/group_actions.dart'; import 'package:invoiceninja_flutter/redux/invoice/invoice_actions.dart'; import 'package:invoiceninja_flutter/redux/payment/payment_actions.dart'; import 'package:invoiceninja_flutter/redux/product/product_actions.dart'; import 'package:invoiceninja_flutter/redux/project/project_actions.dart'; import 'package:invoiceninja_flutter/redux/quote/quote_actions.dart'; import 'package:invoiceninja_flutter/redux/settings/settings_actions.dart'; import 'package:invoiceninja_flutter/redux/task/task_actions.dart'; import 'package:invoiceninja_flutter/redux/ui/pref_state.dart'; import 'package:invoiceninja_flutter/redux/ui/ui_actions.dart'; import 'package:invoiceninja_flutter/redux/user/user_actions.dart'; import 'package:invoiceninja_flutter/redux/vendor/vendor_actions.dart'; import 'package:redux/redux.dart'; PrefState prefReducer( PrefState state, dynamic action, int selectedCompanyIndex) { return state.rebuild((b) => b ..companyPrefs[selectedCompanyIndex] = companyPrefReducer(state.companyPrefs[selectedCompanyIndex], action) ..appLayout = layoutReducer(state.appLayout, action) ..moduleLayout = moduleLayoutReducer(state.moduleLayout, action) ..menuSidebarMode = manuSidebarReducer(state.menuSidebarMode, action) ..historySidebarMode = historySidebarReducer(state.historySidebarMode, action) ..isMenuVisible = menuVisibleReducer(state.isMenuVisible, action) ..isHistoryVisible = historyVisibleReducer(state.isHistoryVisible, action) ..enableDarkMode = darkModeReducer(state.enableDarkMode, action) ..longPressSelectionIsDefault = longPressReducer(state.longPressSelectionIsDefault, action) ..autoStartTasks = autoStartTasksReducer(state.autoStartTasks, action) ..requireAuthentication = requireAuthenticationReducer(state.requireAuthentication, action) ..emailPayment = emailPaymentReducer(state.emailPayment, action) ..addDocumentsToInvoice = addDocumentsToInvoiceReducer(state.addDocumentsToInvoice, action)); } Reducer menuVisibleReducer = combineReducers([ TypedReducer((value, action) { return action.sidebar == AppSidebar.menu ? !value : value; }), TypedReducer((value, action) { switch (action.menuMode) { case AppSidebarMode.visible: return true; case AppSidebarMode.collapse: case AppSidebarMode.float: return false; default: return value; } }), ]); Reducer historyVisibleReducer = combineReducers([ TypedReducer((value, action) { return action.sidebar == AppSidebar.history ? !value : value; }), TypedReducer((value, action) { return action.historyMode == AppSidebarMode.visible ? true : action.historyMode == AppSidebarMode.float ? false : value; }), ]); Reducer filterReducer = combineReducers([ TypedReducer((filter, action) { return action.filter; }), ]); Reducer filterClearedAtReducer = combineReducers([ TypedReducer((filterClearedAt, action) { return action.filter == null ? DateTime.now().millisecondsSinceEpoch : filterClearedAt; }), ]); Reducer layoutReducer = combineReducers([ TypedReducer((layout, action) { return action.layout ?? layout; }), ]); Reducer moduleLayoutReducer = combineReducers([ TypedReducer((moduleLayout, action) { if (moduleLayout == ModuleLayout.list) { return ModuleLayout.table; } else { return ModuleLayout.list; } }), ]); Reducer manuSidebarReducer = combineReducers([ TypedReducer((mode, action) { return action.menuMode ?? mode; }), ]); Reducer historySidebarReducer = combineReducers([ TypedReducer((mode, action) { return action.historyMode ?? mode; }), ]); Reducer emailPaymentReducer = combineReducers([ TypedReducer((emailPayment, action) { return action.emailPayment ?? emailPayment; }), ]); Reducer darkModeReducer = combineReducers([ TypedReducer((enableDarkMode, action) { return action.enableDarkMode ?? enableDarkMode; }), ]); Reducer longPressReducer = combineReducers([ TypedReducer( (longPressSelectionIsDefault, action) { return action.longPressSelectionIsDefault ?? longPressSelectionIsDefault; }), ]); Reducer autoStartTasksReducer = combineReducers([ TypedReducer((autoStartTasks, action) { return action.autoStartTasks ?? autoStartTasks; }), ]); Reducer addDocumentsToInvoiceReducer = combineReducers([ TypedReducer((addDocumentsToInvoice, action) { return action.addDocumentsToInvoice ?? addDocumentsToInvoice; }), ]); Reducer requireAuthenticationReducer = combineReducers([ TypedReducer((requireAuthentication, action) { return action.requireAuthentication ?? requireAuthentication; }), ]); Reducer currentRouteReducer = combineReducers([ TypedReducer((currentRoute, action) { return action.route; }), ]); Reducer previousRouteReducer = combineReducers([ TypedReducer((currentRoute, action) { return currentRoute; }), ]); Reducer selectedCompanyIndexReducer = combineReducers([ TypedReducer((selectedCompanyIndex, action) { return action.companyIndex; }), ]); Reducer settingsUIReducer = combineReducers([ TypedReducer((state, action) { return state.rebuild((b) => b ..updatedAt = DateTime.now().millisecondsSinceEpoch ..userCompany.replace(state.origUserCompany) ..entityType = EntityType.company ..isChanged = false); }), TypedReducer((state, action) { return state.rebuild((b) => b ..userCompany.replace(action.userCompany ?? state.userCompany) ..origUserCompany.replace(action.userCompany ?? state.origUserCompany) ..group.replace(action.group ?? state.group) ..origGroup.replace(action.group ?? state.origGroup) ..client.replace(action.client ?? state.client) ..origClient.replace(action.client ?? state.origClient) ..updatedAt = DateTime.now().millisecondsSinceEpoch ..section = action.section ?? state.section ..isChanged = false ..entityType = action.client != null ? EntityType.client : action.group != null ? EntityType.group : state.entityType); }), TypedReducer((state, action) { return state.rebuild((b) => b ..userCompany.company.replace(action.company) ..isChanged = true); }), TypedReducer((state, action) { switch (state.entityType) { case EntityType.client: return state.rebuild((b) => b //..client.settings.replace(action.settings) ..isChanged = true); case EntityType.group: return state.rebuild((b) => b ..group.settings.replace(action.settings) ..isChanged = true); default: return state.rebuild((b) => b ..userCompany.company.settings.replace(action.settings) ..isChanged = true); } }), TypedReducer((state, action) { return state.rebuild((b) => b ..userCompany.user.replace(action.user) ..isChanged = true); }), TypedReducer((state, action) { return state.rebuild((b) => b ..userCompany.replace(state.origUserCompany) ..group.replace(state.origGroup) ..client.replace(state.origClient) ..isChanged = false ..updatedAt = DateTime.now().millisecondsSinceEpoch); }), TypedReducer((state, action) { return state.rebuild((b) => b ..userCompany.company.replace(action.company) ..isChanged = false); }), TypedReducer((state, action) { return state.rebuild((b) => b ..group.replace(action.group) ..isChanged = false); }), TypedReducer((state, action) { return state.rebuild((b) => b ..client.replace(action.client) ..isChanged = false); }), TypedReducer((state, action) { return state.rebuild((b) => b ..filter = action.filter ..filterClearedAt = action.filter == null ? DateTime.now().millisecondsSinceEpoch : state.filterClearedAt); }), ]); CompanyPrefState companyPrefReducer(CompanyPrefState state, dynamic action) { return state.rebuild((b) => b ..accentColor = accentColorReducer(state.accentColor, action) ..historyList.replace(historyReducer(state.historyList, action))); } Reducer accentColorReducer = combineReducers([ TypedReducer((accentColor, action) { return action.accentColor ?? accentColor; }), ]); Reducer> historyReducer = combineReducers([ TypedReducer, ViewClient>((historyList, action) => _addToHistory(historyList, HistoryRecord(id: action.clientId, entityType: EntityType.client))), TypedReducer, EditClient>((historyList, action) => _addToHistory(historyList, HistoryRecord(id: action.client.id, entityType: EntityType.client))), TypedReducer, ViewProduct>((historyList, action) => _addToHistory(historyList, HistoryRecord(id: action.productId, entityType: EntityType.product))), TypedReducer, EditProduct>((historyList, action) => _addToHistory( historyList, HistoryRecord( id: action.product.id, entityType: EntityType.product))), TypedReducer, ViewInvoice>((historyList, action) => _addToHistory(historyList, HistoryRecord(id: action.invoiceId, entityType: EntityType.invoice))), TypedReducer, EditInvoice>((historyList, action) => _addToHistory( historyList, HistoryRecord( id: action.invoice.id, entityType: EntityType.invoice))), TypedReducer, ViewPayment>((historyList, action) => _addToHistory(historyList, HistoryRecord(id: action.paymentId, entityType: EntityType.payment))), TypedReducer, EditPayment>((historyList, action) => _addToHistory( historyList, HistoryRecord( id: action.payment.id, entityType: EntityType.payment))), TypedReducer, ViewQuote>((historyList, action) => _addToHistory(historyList, HistoryRecord(id: action.quoteId, entityType: EntityType.quote))), TypedReducer, EditQuote>((historyList, action) => _addToHistory(historyList, HistoryRecord(id: action.quote.id, entityType: EntityType.quote))), TypedReducer, ViewTask>((historyList, action) => _addToHistory(historyList, HistoryRecord(id: action.taskId, entityType: EntityType.task))), TypedReducer, EditTask>((historyList, action) => _addToHistory(historyList, HistoryRecord(id: action.task.id, entityType: EntityType.task))), TypedReducer, ViewProject>((historyList, action) => _addToHistory(historyList, HistoryRecord(id: action.projectId, entityType: EntityType.project))), TypedReducer, EditProject>((historyList, action) => _addToHistory( historyList, HistoryRecord( id: action.project.id, entityType: EntityType.project))), TypedReducer, ViewVendor>((historyList, action) => _addToHistory(historyList, HistoryRecord(id: action.vendorId, entityType: EntityType.vendor))), TypedReducer, EditVendor>((historyList, action) => _addToHistory(historyList, HistoryRecord(id: action.vendor.id, entityType: EntityType.vendor))), TypedReducer, ViewExpense>((historyList, action) => _addToHistory(historyList, HistoryRecord(id: action.expenseId, entityType: EntityType.expense))), TypedReducer, EditExpense>((historyList, action) => _addToHistory( historyList, HistoryRecord( id: action.expense.id, entityType: EntityType.expense))), TypedReducer, ViewCompanyGateway>( (historyList, action) => _addToHistory( historyList, HistoryRecord( id: action.companyGatewayId, entityType: EntityType.companyGateway))), TypedReducer, EditCompanyGateway>( (historyList, action) => _addToHistory( historyList, HistoryRecord( id: action.companyGateway.id, entityType: EntityType.companyGateway))), TypedReducer, ViewUser>((historyList, action) => _addToHistory(historyList, HistoryRecord(id: action.userId, entityType: EntityType.user))), TypedReducer, EditUser>((historyList, action) => _addToHistory(historyList, HistoryRecord(id: action.user.id, entityType: EntityType.user))), TypedReducer, ViewGroup>((historyList, action) => _addToHistory(historyList, HistoryRecord(id: action.groupId, entityType: EntityType.group))), TypedReducer, EditGroup>((historyList, action) => _addToHistory(historyList, HistoryRecord(id: action.group.id, entityType: EntityType.group))), // TODO add to starter.sh ]); BuiltList _addToHistory( BuiltList list, HistoryRecord record) { // don't track new records if (record.id.startsWith('-')) { return list; } final old = list.firstWhere((item) => item.matchesRecord(record), orElse: () => null); if (old != null) { return list.rebuild((b) => b ..remove(old) ..insert(0, record)); } else { return list.rebuild((b) => b ..insert(0, record) ..sublist(0, min(kMaxNumberOfHistory, list.length + 1))); } }