From 5cf79dd953d4a35f95f1d902bac93aac6103ec22 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Sun, 16 Aug 2020 20:50:16 +0300 Subject: [PATCH] Code refactor --- lib/data/models/serializers.g.dart | 4 +--- lib/redux/app/app_reducer.dart | 2 +- lib/redux/app/app_state.dart | 5 ++--- lib/redux/ui/pref_reducer.dart | 8 +++++--- lib/redux/ui/pref_state.dart | 9 ++------- lib/redux/ui/pref_state.g.dart | 23 +++++++++++++---------- 6 files changed, 24 insertions(+), 27 deletions(-) diff --git a/lib/data/models/serializers.g.dart b/lib/data/models/serializers.g.dart index 15508fbe6..4bf5a509c 100644 --- a/lib/data/models/serializers.g.dart +++ b/lib/data/models/serializers.g.dart @@ -186,9 +186,6 @@ Serializers _$serializers = (new Serializers().toBuilder() const FullType( BuiltList, const [const FullType(CompanyGatewayEntity)]), () => new ListBuilder()) - ..addBuilderFactory( - const FullType(BuiltList, const [const FullType(CompanyPrefState)]), - () => new ListBuilder()) ..addBuilderFactory( const FullType(BuiltList, const [const FullType(ContactEntity)]), () => new ListBuilder()) @@ -435,6 +432,7 @@ Serializers _$serializers = (new Serializers().toBuilder() ..addBuilderFactory(const FullType(BuiltList, const [const FullType(String)]), () => new ListBuilder()) ..addBuilderFactory(const FullType(BuiltMap, const [const FullType(String), const FullType(CompanyGatewayEntity)]), () => new MapBuilder()) ..addBuilderFactory(const FullType(BuiltList, const [const FullType(String)]), () => new ListBuilder()) + ..addBuilderFactory(const FullType(BuiltMap, const [const FullType(String), const FullType(CompanyPrefState)]), () => new MapBuilder()) ..addBuilderFactory(const FullType(BuiltMap, const [const FullType(String), const FullType(CurrencyEntity)]), () => new MapBuilder()) ..addBuilderFactory(const FullType(BuiltMap, const [const FullType(String), const FullType(SizeEntity)]), () => new MapBuilder()) ..addBuilderFactory(const FullType(BuiltMap, const [const FullType(String), const FullType(GatewayEntity)]), () => new MapBuilder()) diff --git a/lib/redux/app/app_reducer.dart b/lib/redux/app/app_reducer.dart index e72f87ed5..87df6aac9 100644 --- a/lib/redux/app/app_reducer.dart +++ b/lib/redux/app/app_reducer.dart @@ -58,7 +58,7 @@ AppState appReducer(AppState state, dynamic action) { state.userCompanyStates[state.uiState.selectedCompanyIndex], action) ..uiState.replace(uiReducer(state.uiState, action)) ..prefState.replace(prefReducer( - state.prefState, action, state.uiState.selectedCompanyIndex))); + state.prefState, action, state.company.id))); } final lastErrorReducer = combineReducers([ diff --git a/lib/redux/app/app_state.dart b/lib/redux/app/app_state.dart index a9deb464b..59e375f2c 100644 --- a/lib/redux/app/app_state.dart +++ b/lib/redux/app/app_state.dart @@ -166,8 +166,7 @@ abstract class AppState implements Built { '${account?.currentVersion ?? '0.0.0'}-${kClientVersion.split('.').last}'; List get historyList => - prefState.companyPrefs[uiState.selectedCompanyIndex].historyList - .where((history) { + prefState.companyPrefs[company.id].historyList.where((history) { final entityMap = getEntityMap(history.entityType); if (entityMap != null) { final entity = entityMap[history.id] as BaseEntity; @@ -179,7 +178,7 @@ abstract class AppState implements Built { }).toList(); List get unfilteredHistoryList => - prefState.companyPrefs[uiState.selectedCompanyIndex].historyList.toList(); + prefState.companyPrefs[company.id].historyList.toList(); bool shouldSelectEntity({EntityType entityType, List entityList}) { final entityUIState = getUIState(entityType); diff --git a/lib/redux/ui/pref_reducer.dart b/lib/redux/ui/pref_reducer.dart index b26048e46..30cf30927 100644 --- a/lib/redux/ui/pref_reducer.dart +++ b/lib/redux/ui/pref_reducer.dart @@ -37,11 +37,11 @@ import 'package:invoiceninja_flutter/redux/credit/credit_actions.dart'; import 'package:redux/redux.dart'; PrefState prefReducer( - PrefState state, dynamic action, int selectedCompanyIndex) { + PrefState state, dynamic action, String selectedCompanyId) { return state.rebuild( (b) => b - ..companyPrefs[selectedCompanyIndex] = - companyPrefReducer(state.companyPrefs[selectedCompanyIndex], action) + ..companyPrefs[selectedCompanyId] = + companyPrefReducer(state.companyPrefs[selectedCompanyId], action) ..appLayout = layoutReducer(state.appLayout, action) ..rowsPerPage = rowsPerPageReducer(state.rowsPerPage, action) ..moduleLayout = moduleLayoutReducer(state.moduleLayout, action) @@ -299,6 +299,8 @@ Reducer settingsUIReducer = combineReducers([ ]); CompanyPrefState companyPrefReducer(CompanyPrefState state, dynamic action) { + state ??= CompanyPrefState(); + return state.rebuild( (b) => b..historyList.replace(historyReducer(state.historyList, action))); } diff --git a/lib/redux/ui/pref_state.dart b/lib/redux/ui/pref_state.dart index c15aa19c8..c47ef0150 100644 --- a/lib/redux/ui/pref_state.dart +++ b/lib/redux/ui/pref_state.dart @@ -2,7 +2,6 @@ import 'package:built_collection/built_collection.dart'; import 'package:built_value/built_value.dart'; import 'package:built_value/serializer.dart'; import 'package:flutter/foundation.dart'; -import 'package:invoiceninja_flutter/constants.dart'; import 'package:invoiceninja_flutter/data/models/entities.dart'; part 'pref_state.g.dart'; @@ -24,11 +23,7 @@ abstract class PrefState implements Built { showFilterSidebar: false, longPressSelectionIsDefault: false, addDocumentsToInvoice: false, - companyPrefs: BuiltList( - List.generate(kMaxNumberOfCompanies, (i) => i + 1) - .map((index) => CompanyPrefState()) - .toList(), - ), + companyPrefs: BuiltMap(), ); } @@ -66,7 +61,7 @@ abstract class PrefState implements Built { int get rowsPerPage; - BuiltList get companyPrefs; + BuiltMap get companyPrefs; bool get isDesktop => appLayout == AppLayout.desktop; diff --git a/lib/redux/ui/pref_state.g.dart b/lib/redux/ui/pref_state.g.dart index 3156d69a7..245b051ce 100644 --- a/lib/redux/ui/pref_state.g.dart +++ b/lib/redux/ui/pref_state.g.dart @@ -155,8 +155,10 @@ class _$PrefStateSerializer implements StructuredSerializer { specifiedType: const FullType(int)), 'companyPrefs', serializers.serialize(object.companyPrefs, - specifiedType: const FullType( - BuiltList, const [const FullType(CompanyPrefState)])), + specifiedType: const FullType(BuiltMap, const [ + const FullType(String), + const FullType(CompanyPrefState) + ])), ]; return result; @@ -231,9 +233,10 @@ class _$PrefStateSerializer implements StructuredSerializer { break; case 'companyPrefs': result.companyPrefs.replace(serializers.deserialize(value, - specifiedType: const FullType( - BuiltList, const [const FullType(CompanyPrefState)])) - as BuiltList); + specifiedType: const FullType(BuiltMap, const [ + const FullType(String), + const FullType(CompanyPrefState) + ]))); break; } } @@ -443,7 +446,7 @@ class _$PrefState extends PrefState { @override final int rowsPerPage; @override - final BuiltList companyPrefs; + final BuiltMap companyPrefs; factory _$PrefState([void Function(PrefStateBuilder) updates]) => (new PrefStateBuilder()..update(updates)).build(); @@ -674,10 +677,10 @@ class PrefStateBuilder implements Builder { int get rowsPerPage => _$this._rowsPerPage; set rowsPerPage(int rowsPerPage) => _$this._rowsPerPage = rowsPerPage; - ListBuilder _companyPrefs; - ListBuilder get companyPrefs => - _$this._companyPrefs ??= new ListBuilder(); - set companyPrefs(ListBuilder companyPrefs) => + MapBuilder _companyPrefs; + MapBuilder get companyPrefs => + _$this._companyPrefs ??= new MapBuilder(); + set companyPrefs(MapBuilder companyPrefs) => _$this._companyPrefs = companyPrefs; PrefStateBuilder();