From 6b9aa04ccc3ff7bd474e789c460bf8d8341221c7 Mon Sep 17 00:00:00 2001 From: suvaissov Date: Fri, 6 Aug 2021 13:59:06 +0600 Subject: [PATCH] category_view.dart search start --- lib/core/services/dictionary_service.dart | 90 ++++++++++++------- .../dictionaries/category/category_view.dart | 56 +++++++++--- 2 files changed, 100 insertions(+), 46 deletions(-) diff --git a/lib/core/services/dictionary_service.dart b/lib/core/services/dictionary_service.dart index 8295277..2bb6f0e 100644 --- a/lib/core/services/dictionary_service.dart +++ b/lib/core/services/dictionary_service.dart @@ -1,4 +1,3 @@ - import 'package:satu/core/base/base_service.dart'; import 'package:satu/core/entity/category_entity.dart'; import 'package:satu/core/entity/goods_entity.dart'; @@ -39,77 +38,104 @@ class DictionaryService extends BaseService { } } } catch (e, stack) { - log.e("categories", e, stack); + log.e('categories', e, stack); } } - Future> getCategoryByParentId(int parentId ) async { + Future> getCategoryByParentId(int parentId) async { final List list = []; try { final int? appCompanyId = Redux.store?.state.userState?.auth?.companyId; - final List> elements = await _db.queryRowsWithWhere(categoryTableName, '$categoryColumnAppCompanyId = ? and $categoryColumnParentIn = ?', [appCompanyId, parentId]); - for (final Map element in elements){ + final List> elements = await _db.queryRowsWithWhere( + categoryTableName, + '$categoryColumnAppCompanyId = ? and $categoryColumnParentIn = ?', + [appCompanyId, parentId]); + for (final Map element in elements) { list.add(Category.fromMap(element)); } } catch (e, stack) { - log.e("getCategoryByParentId", e, stack); + log.e('getCategoryByParentId', e, stack); } return list; } - Future> getGoodsByCategoryId(int categoryId ) async { + Future> getCategoriesAll() async { + final List list = []; + try { + final int? appCompanyId = Redux.store?.state.userState?.auth?.companyId; + final List> elements = await _db.queryRowsWithWhere( + categoryTableName, + '$categoryColumnAppCompanyId = ? ' + ' order by $categoryColumnParentIn asc, $categoryColumnId asc ', + [appCompanyId]); + for (final Map element in elements) { + list.add(Category.fromMap(element)); + } + } catch (e, stack) { + log.e('getCategoriesAll', e, stack); + } + return list; + } + + Future> getGoodsByCategoryId(int categoryId) async { final List list = []; try { final int? appCompanyId = Redux.store?.state.userState?.auth?.companyId; - final List> elements = await _db.queryRowsWithWhere(goodTableName, '$GoodColumnAppCompanyId = ? and $GoodColumnCategoryId = ?', [appCompanyId, categoryId]); - for (final Map element in elements){ + final List> elements = await _db.queryRowsWithWhere( + goodTableName, + '$GoodColumnAppCompanyId = ? and $GoodColumnCategoryId = ?', + [appCompanyId, categoryId]); + for (final Map element in elements) { list.add(Good.fromMap(element)); } } catch (e, stack) { - log.e("getGoodsByCategoryId", e, stack); + log.e('getGoodsByCategoryId', e, stack); } return list; } - Future> getGoodsByNameOrEan( String query ) async { + Future> getGoodsByNameOrEan(String query) async { final List list = []; try { final int? appCompanyId = Redux.store?.state.userState?.auth?.companyId; - String where = '( $GoodColumnAppCompanyId = ? and $GoodColumnName like ? ) '; + String where = + '( $GoodColumnAppCompanyId = ? and $GoodColumnName like ? ) '; final List args = [appCompanyId, '%$query%']; - if(_isNumericInt(query)){ + if (_isNumericInt(query)) { where += ' or $GoodColumnEan like ?'; args.add('${int.parse(query).toString()}%'); } - final List> elements = await _db.queryRowsWithWhere(goodTableName, where, args); - for (final Map element in elements){ + final List> elements = + await _db.queryRowsWithWhere(goodTableName, where, args); + for (final Map element in elements) { list.add(Good.fromMap(element)); } } catch (e, stack) { - log.e("getGoodsByCategoryId", e, stack); + log.e('getGoodsByCategoryId', e, stack); } return list; } - Future> getGoodsByEan( String code ) async { + Future> getGoodsByEan(String code) async { final List list = []; try { final int? appCompanyId = Redux.store?.state.userState?.auth?.companyId; - final String where = '( $GoodColumnAppCompanyId = ? and $GoodColumnEan like ? ) '; + final String where = + '( $GoodColumnAppCompanyId = ? and $GoodColumnEan like ? ) '; final List args = [appCompanyId, '%$code%']; - final List> - elements = await _db.queryRowsWithWhere(goodTableName, where, args); - for (final Map element in elements){ + final List> elements = + await _db.queryRowsWithWhere(goodTableName, where, args); + for (final Map element in elements) { list.add(Good.fromMap(element)); } } catch (e, stack) { - log.e("getGoodsByEan", e, stack); + log.e('getGoodsByEan', e, stack); } return list; } bool _isNumericInt(String str) { - if(str == null) { + if (str == null) { return false; } return int.tryParse(str) != null; @@ -126,19 +152,19 @@ class DictionaryService extends BaseService { ..id = good.id ..name = good.name ..categoryId = good.categoryId - ..ean = good.ean - ..articul = good.articul - ..price = good.price - ..optPrice = good.optPrice - ..basePrice = good.basePrice - ..divisible = good.divisible - ..updatedAt = good.updatedAt - ..appCompanyId = appCompanyId; + ..ean = good.ean + ..articul = good.articul + ..price = good.price + ..optPrice = good.optPrice + ..basePrice = good.basePrice + ..divisible = good.divisible + ..updatedAt = good.updatedAt + ..appCompanyId = appCompanyId; _db.insert(goodTableName, entity.toMap()); } } } catch (e, stack) { - log.e("goods", e, stack); + log.e('goods', e, stack); } } } diff --git a/lib/views/dictionaries/category/category_view.dart b/lib/views/dictionaries/category/category_view.dart index 5d8308f..16a6a6e 100644 --- a/lib/views/dictionaries/category/category_view.dart +++ b/lib/views/dictionaries/category/category_view.dart @@ -13,8 +13,6 @@ import 'package:satu/widgets/bar/products_app_bar.dart'; import 'package:satu/widgets/bar/products_title_bar.dart'; import 'package:satu/widgets/fields/input_field.dart'; - - class CategoryDictionaryView extends StatefulWidget { @override _CategoryDictionaryViewState createState() => _CategoryDictionaryViewState(); @@ -26,7 +24,9 @@ class _CategoryDictionaryViewState extends State { late TextEditingController _searchTextController; final FocusNode _searchFocusNode = new FocusNode(); - final List _contragents = []; + late List _categories = []; + + late List items = []; @override void initState() { @@ -38,9 +38,15 @@ class _CategoryDictionaryViewState extends State { reset(); } }); + initQuery(); super.initState(); } + Future initQuery() async { + _categories = await _dictionaryService.getCategoriesAll(); + searchByField(''); + } + @override void dispose() { _searchTextController.dispose(); @@ -70,15 +76,14 @@ class _CategoryDictionaryViewState extends State { Expanded( child: ListView.separated( physics: const BouncingScrollPhysics(), - itemCount: _contragents.length, + itemCount: items.length, itemBuilder: (BuildContext context, int index) { - final Category category = _contragents[index]; - return const DictionaryTile( - title: 'category.name', - subTitle: 'sub' - // key: Key('category_${category.id}'), - //onPress: () => () {}, - ); + final CategoryRowDao category = items[index]; + return DictionaryTile( + title: category.name, subTitle: category.parentName + // key: Key('category_${category.id}'), + //onPress: () => () {}, + ); }, separatorBuilder: (BuildContext context, int index) { return const Divider( @@ -95,13 +100,36 @@ class _CategoryDictionaryViewState extends State { void reset() { _searchTextController.clear(); + searchByField(''); } void searchByField(String query) async { - - List goods = await _dictionaryService.getGoodsByNameOrEan(query); + log.i(query); + final List list = []; + final Iterable filtered = query == '' + ? _categories + : _categories.where((element) => + element.name.toLowerCase().contains(query.toLowerCase())); + filtered.forEach((element) { + final Category category = _categories + .firstWhere((parent) => parent.id == element.parentId, orElse: () { + return Category()..name = ''; + }); + String parentName = category.name; + final CategoryRowDao rowDao = + CategoryRowDao(element.name, parentName, element.id); + list.add(rowDao); + }); setState(() { - goods; + items = list; }); } } + +class CategoryRowDao { + CategoryRowDao(this.name, this.parentName, this.id); + + final String name; + final String parentName; + final int? id; +}