Refactor
This commit is contained in:
parent
7fff37fd3b
commit
2257c32422
|
|
@ -37,6 +37,8 @@ class PersistenceRepository {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
AppState _deserialize(String data) {
|
AppState _deserialize(String data) {
|
||||||
return serializers.deserializeWith(AppState.serializer, json.decode(data));
|
return serializers.deserializeWith(AppState.serializer, json.decode(data));
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
@ -6,6 +6,7 @@ import 'package:invoiceninja/redux/client/client_actions.dart';
|
||||||
import 'package:invoiceninja/redux/client/client_middleware.dart';
|
import 'package:invoiceninja/redux/client/client_middleware.dart';
|
||||||
import 'package:invoiceninja/redux/invoice/invoice_actions.dart';
|
import 'package:invoiceninja/redux/invoice/invoice_actions.dart';
|
||||||
import 'package:invoiceninja/ui/client/client_screen.dart';
|
import 'package:invoiceninja/ui/client/client_screen.dart';
|
||||||
|
import 'package:invoiceninja/ui/product/edit/product_edit_vm.dart';
|
||||||
import 'package:redux/redux.dart';
|
import 'package:redux/redux.dart';
|
||||||
import 'package:invoiceninja/utils/localization.dart';
|
import 'package:invoiceninja/utils/localization.dart';
|
||||||
import 'package:invoiceninja/ui/auth/login_vm.dart';
|
import 'package:invoiceninja/ui/auth/login_vm.dart';
|
||||||
|
|
@ -95,6 +96,7 @@ class _InvoiceNinjaAppState extends State<InvoiceNinjaApp> {
|
||||||
widget.store.dispatch(LoadProductsAction());
|
widget.store.dispatch(LoadProductsAction());
|
||||||
return ProductScreen();
|
return ProductScreen();
|
||||||
},
|
},
|
||||||
|
ProductEditScreen.route: (context) => ProductEditScreen(),
|
||||||
ClientScreen.route: (context) {
|
ClientScreen.route: (context) {
|
||||||
widget.store.dispatch(LoadClientsAction());
|
widget.store.dispatch(LoadClientsAction());
|
||||||
return ClientScreen();
|
return ClientScreen();
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
import 'package:flutter/widgets.dart';
|
||||||
import 'package:invoiceninja/data/models/models.dart';
|
import 'package:invoiceninja/data/models/models.dart';
|
||||||
import 'package:built_collection/built_collection.dart';
|
import 'package:built_collection/built_collection.dart';
|
||||||
|
|
||||||
|
|
@ -32,9 +32,10 @@ class LoadProductsSuccess {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class SelectProductAction {
|
class EditProductAction {
|
||||||
final ProductEntity product;
|
final ProductEntity product;
|
||||||
SelectProductAction(this.product);
|
final BuildContext context;
|
||||||
|
EditProductAction({this.product, this.context});
|
||||||
}
|
}
|
||||||
|
|
||||||
class SaveProductRequest {
|
class SaveProductRequest {
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,8 @@
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
import 'package:invoiceninja/data/models/models.dart';
|
import 'package:invoiceninja/data/models/models.dart';
|
||||||
import 'package:invoiceninja/redux/client/client_actions.dart';
|
import 'package:invoiceninja/redux/client/client_actions.dart';
|
||||||
|
import 'package:invoiceninja/redux/ui/ui_actions.dart';
|
||||||
|
import 'package:invoiceninja/ui/product/edit/product_edit_vm.dart';
|
||||||
import 'package:redux/redux.dart';
|
import 'package:redux/redux.dart';
|
||||||
import 'package:invoiceninja/redux/product/product_actions.dart';
|
import 'package:invoiceninja/redux/product/product_actions.dart';
|
||||||
import 'package:invoiceninja/redux/app/app_state.dart';
|
import 'package:invoiceninja/redux/app/app_state.dart';
|
||||||
|
|
@ -13,6 +16,7 @@ List<Middleware<AppState>> createStoreProductsMiddleware([
|
||||||
final archiveProduct = _archiveProduct(repository);
|
final archiveProduct = _archiveProduct(repository);
|
||||||
final deleteProduct = _deleteProduct(repository);
|
final deleteProduct = _deleteProduct(repository);
|
||||||
final restoreProduct = _restoreProduct(repository);
|
final restoreProduct = _restoreProduct(repository);
|
||||||
|
final editProduct = _editProduct();
|
||||||
|
|
||||||
return [
|
return [
|
||||||
TypedMiddleware<AppState, LoadProductsAction>(loadProducts),
|
TypedMiddleware<AppState, LoadProductsAction>(loadProducts),
|
||||||
|
|
@ -20,9 +24,19 @@ List<Middleware<AppState>> createStoreProductsMiddleware([
|
||||||
TypedMiddleware<AppState, ArchiveProductRequest>(archiveProduct),
|
TypedMiddleware<AppState, ArchiveProductRequest>(archiveProduct),
|
||||||
TypedMiddleware<AppState, DeleteProductRequest>(deleteProduct),
|
TypedMiddleware<AppState, DeleteProductRequest>(deleteProduct),
|
||||||
TypedMiddleware<AppState, RestoreProductRequest>(restoreProduct),
|
TypedMiddleware<AppState, RestoreProductRequest>(restoreProduct),
|
||||||
|
TypedMiddleware<AppState, EditProductAction>(editProduct),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Middleware<AppState> _editProduct() {
|
||||||
|
return (Store<AppState> store, action, NextDispatcher next) {
|
||||||
|
next(action);
|
||||||
|
|
||||||
|
store.dispatch(UpdateCurrentRoute(ProductEditScreen.route));
|
||||||
|
Navigator.of(action.context).pushReplacementNamed(ProductEditScreen.route);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
Middleware<AppState> _archiveProduct(ProductRepository repository) {
|
Middleware<AppState> _archiveProduct(ProductRepository repository) {
|
||||||
return (Store<AppState> store, action, NextDispatcher next) {
|
return (Store<AppState> store, action, NextDispatcher next) {
|
||||||
var origProduct = store.state.productState.map[action.productId];
|
var origProduct = store.state.productState.map[action.productId];
|
||||||
|
|
@ -106,7 +120,6 @@ Middleware<AppState> _saveProduct(ProductRepository repository) {
|
||||||
|
|
||||||
Middleware<AppState> _loadProducts(ProductRepository repository) {
|
Middleware<AppState> _loadProducts(ProductRepository repository) {
|
||||||
return (Store<AppState> store, action, NextDispatcher next) {
|
return (Store<AppState> store, action, NextDispatcher next) {
|
||||||
|
|
||||||
AppState state = store.state;
|
AppState state = store.state;
|
||||||
|
|
||||||
if (!state.productState.isStale && !action.force) {
|
if (!state.productState.isStale && !action.force) {
|
||||||
|
|
@ -120,9 +133,7 @@ Middleware<AppState> _loadProducts(ProductRepository repository) {
|
||||||
}
|
}
|
||||||
|
|
||||||
store.dispatch(LoadProductsRequest());
|
store.dispatch(LoadProductsRequest());
|
||||||
repository
|
repository.loadList(state.selectedCompany, state.authState).then((data) {
|
||||||
.loadList(state.selectedCompany, state.authState)
|
|
||||||
.then((data) {
|
|
||||||
store.dispatch(LoadProductsSuccess(data));
|
store.dispatch(LoadProductsSuccess(data));
|
||||||
if (action.completer != null) {
|
if (action.completer != null) {
|
||||||
action.completer.complete(null);
|
action.completer.complete(null);
|
||||||
|
|
|
||||||
|
|
@ -47,7 +47,7 @@ final productsReducer = combineReducers<ProductState>([
|
||||||
TypedReducer<ProductState, AddProductSuccess>(_addProduct),
|
TypedReducer<ProductState, AddProductSuccess>(_addProduct),
|
||||||
TypedReducer<ProductState, LoadProductsSuccess>(_setLoadedProducts),
|
TypedReducer<ProductState, LoadProductsSuccess>(_setLoadedProducts),
|
||||||
TypedReducer<ProductState, LoadProductsFailure>(_setNoProducts),
|
TypedReducer<ProductState, LoadProductsFailure>(_setNoProducts),
|
||||||
TypedReducer<ProductState, SelectProductAction>(_selectProduct),
|
TypedReducer<ProductState, EditProductAction>(_selectProduct),
|
||||||
|
|
||||||
TypedReducer<ProductState, ArchiveProductRequest>(_archiveProductRequest),
|
TypedReducer<ProductState, ArchiveProductRequest>(_archiveProductRequest),
|
||||||
TypedReducer<ProductState, ArchiveProductSuccess>(_archiveProductSuccess),
|
TypedReducer<ProductState, ArchiveProductSuccess>(_archiveProductSuccess),
|
||||||
|
|
@ -163,7 +163,7 @@ ProductState _setNoProducts(
|
||||||
}
|
}
|
||||||
|
|
||||||
ProductState _selectProduct(
|
ProductState _selectProduct(
|
||||||
ProductState productState, SelectProductAction action) {
|
ProductState productState, EditProductAction action) {
|
||||||
return productState.rebuild((b) => b
|
return productState.rebuild((b) => b
|
||||||
..editing.replace(action.product));
|
..editing.replace(action.product));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,4 @@
|
||||||
|
class UpdateCurrentRoute {
|
||||||
|
final String route;
|
||||||
|
UpdateCurrentRoute(this.route);
|
||||||
|
}
|
||||||
|
|
@ -11,6 +11,7 @@ import 'package:invoiceninja/redux/app/app_state.dart';
|
||||||
import 'package:invoiceninja/ui/app/snackbar_row.dart';
|
import 'package:invoiceninja/ui/app/snackbar_row.dart';
|
||||||
|
|
||||||
class ProductEditScreen extends StatelessWidget {
|
class ProductEditScreen extends StatelessWidget {
|
||||||
|
static final String route = '/products/edit';
|
||||||
ProductEditScreen({Key key}) : super(key: key);
|
ProductEditScreen({Key key}) : super(key: key);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
|
|
||||||
|
|
@ -69,10 +69,7 @@ class ProductListVM {
|
||||||
isLoading: store.state.isLoading,
|
isLoading: store.state.isLoading,
|
||||||
isLoaded: store.state.productState.isLoaded,
|
isLoaded: store.state.productState.isLoaded,
|
||||||
onProductTap: (context, product) {
|
onProductTap: (context, product) {
|
||||||
store.dispatch(SelectProductAction(product));
|
store.dispatch(EditProductAction(product: product, context: context));
|
||||||
Navigator
|
|
||||||
.of(context)
|
|
||||||
.push(MaterialPageRoute(builder: (_) => ProductEditScreen()));
|
|
||||||
},
|
},
|
||||||
onRefreshed: (context) => _handleRefresh(context),
|
onRefreshed: (context) => _handleRefresh(context),
|
||||||
onDismissed: (BuildContext context, ProductEntity product,
|
onDismissed: (BuildContext context, ProductEntity product,
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
import 'package:invoiceninja/redux/ui/ui_actions.dart';
|
||||||
import 'package:invoiceninja/ui/app/app_search.dart';
|
import 'package:invoiceninja/ui/app/app_search.dart';
|
||||||
import 'package:invoiceninja/ui/app/app_search_button.dart';
|
import 'package:invoiceninja/ui/app/app_search_button.dart';
|
||||||
import 'package:invoiceninja/ui/product/edit/product_edit_vm.dart';
|
import 'package:invoiceninja/ui/product/edit/product_edit_vm.dart';
|
||||||
|
|
@ -55,12 +56,13 @@ class ProductScreen extends StatelessWidget {
|
||||||
floatingActionButton: FloatingActionButton(
|
floatingActionButton: FloatingActionButton(
|
||||||
backgroundColor: Theme.of(context).primaryColorDark,
|
backgroundColor: Theme.of(context).primaryColorDark,
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
store.dispatch(SelectProductAction(ProductEntity()));
|
store.dispatch(
|
||||||
Navigator
|
EditProductAction(product: ProductEntity(), context: context));
|
||||||
.of(context)
|
|
||||||
.push(MaterialPageRoute(builder: (_) => ProductEditScreen()));
|
|
||||||
},
|
},
|
||||||
child: Icon(Icons.add,color: Colors.white,),
|
child: Icon(
|
||||||
|
Icons.add,
|
||||||
|
color: Colors.white,
|
||||||
|
),
|
||||||
tooltip: localization.newProduct,
|
tooltip: localization.newProduct,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue