From f125db74ceea53ec0f7608f3c37ea98d4ab8e526 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 19 Jun 2018 04:59:40 -0700 Subject: [PATCH] Clean up startup --- lib/redux/app/app_middleware.dart | 33 +++++++++++++++++---------- lib/redux/app/app_state.dart | 13 +++++++---- lib/redux/client/client_state.dart | 2 ++ lib/redux/invoice/invoice_state.dart | 2 ++ lib/redux/product/product_state.dart | 2 ++ lib/redux/ui/entity_ui_state.dart | 1 + lib/ui/invoice/edit/invoice_edit.dart | 2 +- 7 files changed, 38 insertions(+), 17 deletions(-) diff --git a/lib/redux/app/app_middleware.dart b/lib/redux/app/app_middleware.dart index 482c3702a..c053ab8c8 100644 --- a/lib/redux/app/app_middleware.dart +++ b/lib/redux/app/app_middleware.dart @@ -1,5 +1,6 @@ import 'package:flutter/widgets.dart'; import 'package:invoiceninja/data/file_storage.dart'; +import 'package:invoiceninja/data/models/models.dart'; import 'package:invoiceninja/data/repositories/persistence_repository.dart'; import 'package:invoiceninja/redux/app/app_actions.dart'; import 'package:invoiceninja/redux/app/app_state.dart'; @@ -143,7 +144,7 @@ Middleware _createLoadState( authState.url.isNotEmpty) { NavigatorState navigator = Navigator.of(action.context); bool isFirst = true; - _getRoutes(appState).forEach((route){ + _getRoutes(appState).forEach((route) { if (isFirst) { navigator.pushReplacementNamed(route); } else { @@ -172,21 +173,29 @@ Middleware _createLoadState( List _getRoutes(AppState state) { List routes = []; var route = ''; + EntityType entityType = null; - state.uiState.currentRoute.split('/').forEach((part) { - if (part.isNotEmpty) { - // TODO automatically lookup state using the route/entity type - if (part == 'edit' && route != '/product' && route != '/invoice') { - switch (route) { - case '/client': - if (! state.clientUIState.selected.isNew()) { - routes.add(route + '/view'); - } - } + state.uiState.currentRoute + .split('/') + .where((part) => part.isNotEmpty) + .forEach((part) { + if (part == 'edit') { + // Only restore new unsaved entities to prevent conflicts + bool isNew = state.getUIState(entityType).isSelectedNew; + if (isNew) { + route += '/edit'; + } else if (entityType == EntityType.client) { + route += '/view'; } + } else { + if (entityType == null) { + entityType = EntityType.valueOf(part); + } + route += '/' + part; - routes.add(route); } + + routes.add(route); }); return routes; diff --git a/lib/redux/app/app_state.dart b/lib/redux/app/app_state.dart index 11911d1e2..fd21014d7 100644 --- a/lib/redux/app/app_state.dart +++ b/lib/redux/app/app_state.dart @@ -1,6 +1,7 @@ import 'package:invoiceninja/data/models/models.dart'; import 'package:invoiceninja/redux/client/client_state.dart'; import 'package:invoiceninja/redux/invoice/invoice_state.dart'; +import 'package:invoiceninja/redux/ui/entity_ui_state.dart'; import 'package:invoiceninja/redux/ui/ui_state.dart'; import 'package:invoiceninja/redux/ui/list_ui_state.dart'; import 'package:invoiceninja/redux/auth/auth_state.dart'; @@ -65,19 +66,23 @@ abstract class AppState implements Built { CompanyEntity get selectedCompany => this.selectedCompanyState.company; DashboardState get dashboardState => this.selectedCompanyState.dashboardState; - ListUIState getListState(EntityType type) { + EntityUIState getUIState(EntityType type) { switch (type) { case EntityType.product: - return productListState; + return productUIState; case EntityType.client: - return clientListState; + return clientUIState; case EntityType.invoice: - return invoiceListState; + return invoiceUIState; default: return null; } } + ListUIState getListState(EntityType type) { + return getUIState(type).listUIState; + } + ProductState get productState => this.selectedCompanyState.productState; ProductUIState get productUIState => this.uiState.productUIState; ListUIState get productListState => this.uiState.productUIState.listUIState; diff --git a/lib/redux/client/client_state.dart b/lib/redux/client/client_state.dart index cb8918f9e..c6164cb04 100644 --- a/lib/redux/client/client_state.dart +++ b/lib/redux/client/client_state.dart @@ -44,6 +44,8 @@ abstract class ClientUIState extends Object with EntityUIState implements Built< @nullable ClientEntity get selected; + bool get isSelectedNew => selected.isNew(); + factory ClientUIState() { return _$ClientUIState._( listUIState: ListUIState(ClientFields.name), diff --git a/lib/redux/invoice/invoice_state.dart b/lib/redux/invoice/invoice_state.dart index 01be146af..1911a6c26 100644 --- a/lib/redux/invoice/invoice_state.dart +++ b/lib/redux/invoice/invoice_state.dart @@ -44,6 +44,8 @@ abstract class InvoiceUIState extends Object with EntityUIState implements Built @nullable InvoiceEntity get selected; + bool get isSelectedNew => selected.isNew(); + factory InvoiceUIState() { return _$InvoiceUIState._( listUIState: ListUIState(InvoiceFields.invoiceNumber), diff --git a/lib/redux/product/product_state.dart b/lib/redux/product/product_state.dart index 9bf2f6540..73ce3da4b 100644 --- a/lib/redux/product/product_state.dart +++ b/lib/redux/product/product_state.dart @@ -44,6 +44,8 @@ abstract class ProductUIState extends Object with EntityUIState implements Built @nullable ProductEntity get selected; + bool get isSelectedNew => selected.isNew(); + factory ProductUIState() { return _$ProductUIState._( listUIState: ListUIState(ProductFields.productKey), diff --git a/lib/redux/ui/entity_ui_state.dart b/lib/redux/ui/entity_ui_state.dart index 4f792debf..71dfb2fb2 100644 --- a/lib/redux/ui/entity_ui_state.dart +++ b/lib/redux/ui/entity_ui_state.dart @@ -2,6 +2,7 @@ import 'package:invoiceninja/redux/ui/list_ui_state.dart'; abstract class EntityUIState { + bool get isSelectedNew; ListUIState get listUIState; } \ No newline at end of file diff --git a/lib/ui/invoice/edit/invoice_edit.dart b/lib/ui/invoice/edit/invoice_edit.dart index d159897fc..cdf98ce4b 100644 --- a/lib/ui/invoice/edit/invoice_edit.dart +++ b/lib/ui/invoice/edit/invoice_edit.dart @@ -59,7 +59,7 @@ class _InvoiceEditState extends State appBar: AppBar( title: Text(invoice.isNew() ? localization.newInvoice - : invoice.invoiceNumber), + : '${localization.invoice} ${invoice.invoiceNumber}'), actions: [ SaveIconButton( isLoading: widget.viewModel.isLoading,