From c7ce34407e884c4f6fabd5983c2f6550a596d94f Mon Sep 17 00:00:00 2001 From: suvaissov Date: Thu, 13 May 2021 21:04:56 +0600 Subject: [PATCH] ui render --- lib/core/models/flow/product_dao.dart | 3 + lib/core/redux/actions/sell_actions.dart | 52 ++++++- lib/core/services/db_service.dart | 5 + .../tabs/component/product_list_item.dart | 16 ++- lib/views/work/tabs/sell_view.dart | 132 +++++++++++------- lib/views/work/work_view.dart | 9 ++ 6 files changed, 162 insertions(+), 55 deletions(-) diff --git a/lib/core/models/flow/product_dao.dart b/lib/core/models/flow/product_dao.dart index 122f7a6..78816ea 100644 --- a/lib/core/models/flow/product_dao.dart +++ b/lib/core/models/flow/product_dao.dart @@ -8,6 +8,7 @@ class ProductDao { String eanCode; int article; String excise; + int transactionId; Map toMap() { @@ -21,6 +22,7 @@ class ProductDao { 'eanCode': eanCode, 'article': article, 'excise': excise, + 'transactionId' : transactionId, }; return map; } @@ -37,6 +39,7 @@ class ProductDao { eanCode = map['eanCode']; article = map['article']; excise = map['excise']; + transactionId = map['transactionId']; } } \ No newline at end of file diff --git a/lib/core/redux/actions/sell_actions.dart b/lib/core/redux/actions/sell_actions.dart index 84f8adb..1d47d5b 100644 --- a/lib/core/redux/actions/sell_actions.dart +++ b/lib/core/redux/actions/sell_actions.dart @@ -76,6 +76,7 @@ ThunkAction addSellItem({Good good, String excise}) { if (set.isNotEmpty) { Category category = Category.fromMap(set.first); item.categoryId = category.id; + item.categoryName = category.name; } } @@ -99,8 +100,55 @@ ThunkAction addSellItem({Good good, String excise}) { }; } +ThunkAction removeSellItem({int transactionId}) { + return (Store store) async { + + int appCompanyId = store.state.userState.auth.companyId; + String uuid = store.state.sellState.transactionState.uuid; + + int count = await _dbService.delete(TransactionTableName, transactionId); + log.i('removeSellItem ${count} by transactionId:${transactionId}'); + // List> set = await _dbService.queryRowsWithWhere( + // TransactionTableName, + // '$TransactionColumnId = ? ', + // [transactionId]); + // if (set.isNotEmpty) { + // for (Map map in set) { + // Transaction _transaction = Transaction.fromMap(map); + // ProductDao _product = ProductDao.fromMap(jsonDecode(_transaction.data)); + // if (_product.id == good.id && _product.excise == excise) { + // transaction = _transaction; + // break; + // } + // } + // } + + // refresh from db ? after save data + await loadSellData(store); + }; +} + +Future removeAllSellData(Store store) async { + try { + log.i('removeAllSellData'); + int appCompanyId = store.state.userState.auth.companyId; + String uuid = store.state.sellState.transactionState.uuid; + await _dbService.deleteByWhere( + TransactionTableName, + '$TransactionColumnAppCompanyId = ? ' + ' and $TransactionColumnStatus = ? ' + ' and ${TransactionColumnType} = ?' + ' and ${TransactionColumnUuid} = ?', + [appCompanyId, TransactionStatusPrepare, TransactionTypeSell, uuid]); + await loadSellData(store); + } catch (e, stack) { + log.e('removeAllSellData', e, stack); + } +} + Future loadSellData(Store store) async { try { + log.i('loadSellData'); int appCompanyId = store.state.userState.auth.companyId; List> set = await _dbService.queryRowsWithWhere( TransactionTableName, @@ -112,7 +160,9 @@ Future loadSellData(Store store) async { for (Map map in set) { Transaction transaction = Transaction.fromMap(map); uuid = transaction.uuid; - list.add(ProductDao.fromMap(jsonDecode(transaction.data))); + ProductDao productDao = ProductDao.fromMap(jsonDecode(transaction.data)); + productDao.transactionId = transaction.id; + list.add(productDao); } store.dispatch(SetSellStateAction(SellState(items: list, transactionState: TransactionState()..uuid = uuid))); } catch (e, stack) { diff --git a/lib/core/services/db_service.dart b/lib/core/services/db_service.dart index 37ae2ec..ad079d5 100644 --- a/lib/core/services/db_service.dart +++ b/lib/core/services/db_service.dart @@ -144,5 +144,10 @@ class DbService extends BaseService { return await db.delete(table); } + Future deleteByWhere(String table, String where, List args) async { + Database db = await instance.database; + return await db.delete(table, where: where, whereArgs: args); + } + Future close() async => instance.close(); } diff --git a/lib/views/work/tabs/component/product_list_item.dart b/lib/views/work/tabs/component/product_list_item.dart index c958bc8..53e00c3 100644 --- a/lib/views/work/tabs/component/product_list_item.dart +++ b/lib/views/work/tabs/component/product_list_item.dart @@ -1,5 +1,7 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; +import 'package:satu/core/redux/actions/sell_actions.dart'; +import 'package:satu/core/redux/store.dart'; import 'package:satu/shared/app_colors.dart'; import 'package:satu/shared/shared_styles.dart'; import 'package:satu/shared/ui_helpers.dart'; @@ -11,8 +13,9 @@ class ProductListItem extends StatelessWidget { final num price; final num count; final bool isOdd; + final int transactionId; - const ProductListItem({Key key, this.name, this.ean, this.categoryName, this.price, this.count, this.isOdd}) : super(key: key); + const ProductListItem({Key key, this.name, this.ean, this.categoryName, this.price, this.count, this.isOdd, this.transactionId}) : super(key: key); @override Widget build(BuildContext context) { @@ -27,16 +30,16 @@ class ProductListItem extends StatelessWidget { context: context, builder: (BuildContext context) { return AlertDialog( - title: const Text("Confirm"), - content: const Text("Are you sure you wish to delete this item?"), + title: const Text("Внимание"), + content: Text("Удалить товар \"${this.name}\" - ${count} ед. ?"), actions: [ FlatButton( onPressed: () => Navigator.of(context).pop(true), - child: const Text("DELETE") + child: const Text("Удалить") ), FlatButton( onPressed: () => Navigator.of(context).pop(false), - child: const Text("CANCEL"), + child: const Text("Отмена"), ), ], ); @@ -45,6 +48,7 @@ class ProductListItem extends StatelessWidget { }, onDismissed: (direction) { print(direction); + Redux.store.dispatch(removeSellItem( transactionId: this.transactionId )); }, key: Key(name), child: ListTile( @@ -63,7 +67,7 @@ class ProductListItem extends StatelessWidget { Text(name , style: const TextStyle( fontWeight: FontWeight.w500 ), overflow: TextOverflow.ellipsis, maxLines: 2,), verticalSpaceTiny, Text('Штрих-код: $ean' , style: productSubTextStyle,), - Text(categoryName, style: productSubTextStyle,) + Text(categoryName ?? '', style: productSubTextStyle,) ], ), ), diff --git a/lib/views/work/tabs/sell_view.dart b/lib/views/work/tabs/sell_view.dart index 04c7837..0c7d0ad 100644 --- a/lib/views/work/tabs/sell_view.dart +++ b/lib/views/work/tabs/sell_view.dart @@ -1,12 +1,16 @@ +import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_redux/flutter_redux.dart'; +import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:satu/core/models/flow/product_dao.dart'; +import 'package:satu/core/redux/actions/sell_actions.dart'; import 'package:satu/core/redux/state/sell_state.dart'; import 'package:satu/core/redux/store.dart'; import 'package:satu/core/services/navigator_service.dart'; import 'package:satu/core/utils/locator.dart'; import 'package:satu/routes/route_names.dart'; import 'package:satu/shared/app_colors.dart'; +import 'package:satu/shared/ui_helpers.dart'; import 'package:satu/views/work/tabs/component/product_list_item.dart'; import 'package:satu/views/work/tabs/component/products_app_bar.dart'; import 'package:satu/views/work/tabs/component/products_header_bar.dart'; @@ -16,45 +20,85 @@ class SellView extends StatelessWidget { @override Widget build(BuildContext context) { return StoreConnector( - converter: (store) => store.state.sellState, - builder: (_, state) { - return Scaffold( - appBar: ProductsAppBar( - title: 'Продажа', - actions: actions(), - elevation: 2.0, - child: ProductHeaderBar( - count: state.items.length, - sum: sumProducts(state.items), - ), - backgroundColor: backgroundColor, - childHeight: 80, - ), - body: Column( - children: [ - //ProductHeaderBar(count: 14, sum: 25000,), - Expanded( - child: ListView.builder( - physics: BouncingScrollPhysics(), - itemCount: state.items.length, - itemBuilder: (BuildContext context, int index) { - ProductDao product = state.items.elementAt(index); - return ProductListItem( - ean: product.eanCode, - isOdd: index % 2 == 0, - name: product.productName, - price: product.price, - count: product.count, - categoryName: product.productName, - ); - }, - ), + converter: (store) => store.state.sellState, + builder: (_, state) { + return Scaffold( + appBar: ProductsAppBar( + title: 'Продажа', + actions: actions(), + elevation: 2.0, + child: ProductHeaderBar( + count: state.items.length, + sum: sumProducts(state.items), ), - ], - ), - ); - } - ); + backgroundColor: backgroundColor, + childHeight: 80, + ), + body: ListView.builder( + physics: BouncingScrollPhysics(), + itemCount: state.items.length, + itemBuilder: (BuildContext context, int index) { + ProductDao product = state.items.elementAt(index); + return ProductListItem( + key: UniqueKey(), + ean: product.eanCode, + isOdd: index % 2 == 0, + name: product.productName, + price: product.price, + count: product.count, + categoryName: product.categoryName, + transactionId: product.transactionId, + ); + }, + ), + floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked, + floatingActionButton: floatingActionButtonRender(), + ); + }); + } + + Widget floatingActionButtonRender() { + return StoreConnector( + converter: (store) => store.state.sellState, + builder: (_, snapshot) { + return Padding( + padding: const EdgeInsets.symmetric(horizontal: 12.0, vertical: 16.0), + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + crossAxisAlignment: CrossAxisAlignment.end, + children: [ + Visibility( + visible: snapshot.items.isNotEmpty, + child: FloatingActionButton( + elevation: 2, + backgroundColor: greenColor, + onPressed: () => print('check'), + child: Icon( + Icons.check, + color: whiteColor, + size: 30.sp + ), + )), + Column( + mainAxisAlignment: MainAxisAlignment.end, + children: [ + FloatingActionButton( + elevation: 2, + onPressed: () => locator().push(AddProductViewRoute), + child: Icon(Icons.add, size: 30.sp,), + ), + verticalSpaceMedium, + FloatingActionButton( + elevation: 2, + onPressed: () => locator().push(AddByBarcodeViewRoute), + child: Icon(Icons.qr_code_rounded, size: 30.sp), + ), + ], + ) + ], + ), + ); + }); } List actions() { @@ -62,17 +106,9 @@ class SellView extends StatelessWidget { Padding( padding: const EdgeInsets.all(8.0), child: IconButton( - icon: Icon(Icons.add_box, size: 30.0, color: yellowColor), + icon: Icon(Icons.delete, size: 30.0, color: yellowColor), onPressed: () { - locator().push(AddProductViewRoute); - }), - ), - Padding( - padding: const EdgeInsets.all(8.0), - child: IconButton( - icon: Icon(Icons.camera_enhance_rounded, size: 30.0, color: yellowColor), - onPressed: () { - locator().push(AddByBarcodeViewRoute); + Redux.store.dispatch(removeAllSellData); }), ) ]; diff --git a/lib/views/work/work_view.dart b/lib/views/work/work_view.dart index 5ed6c3a..33a249e 100644 --- a/lib/views/work/work_view.dart +++ b/lib/views/work/work_view.dart @@ -1,5 +1,7 @@ import 'package:flutter/material.dart'; import 'package:material_design_icons_flutter/material_design_icons_flutter.dart'; +import 'package:satu/core/redux/actions/sell_actions.dart'; +import 'package:satu/core/redux/store.dart'; import 'package:satu/shared/app_colors.dart'; import 'package:satu/views/work/tabs/buy_view.dart'; import 'package:satu/views/work/tabs/journal_view.dart'; @@ -24,6 +26,13 @@ class _WorkViewState extends State { ]; + @override + void initState() { + super.initState(); + // state sell view + Redux.store.dispatch(loadSellData); + } + void _onItemTapped(int index) { setState(() { _selectedIndex = index;