category_view.dart search start

null-safety-migration
suvaissov 2021-08-06 13:59:06 +06:00
parent f4c74f8b2b
commit 6b9aa04ccc
2 changed files with 100 additions and 46 deletions

View File

@ -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<List<Category>> getCategoryByParentId(int parentId ) async {
Future<List<Category>> getCategoryByParentId(int parentId) async {
final List<Category> list = [];
try {
final int? appCompanyId = Redux.store?.state.userState?.auth?.companyId;
final List<Map<String, dynamic>> elements = await _db.queryRowsWithWhere(categoryTableName, '$categoryColumnAppCompanyId = ? and $categoryColumnParentIn = ?', [appCompanyId, parentId]);
for (final Map<String, dynamic> element in elements){
final List<Map<String, dynamic>> elements = await _db.queryRowsWithWhere(
categoryTableName,
'$categoryColumnAppCompanyId = ? and $categoryColumnParentIn = ?',
[appCompanyId, parentId]);
for (final Map<String, dynamic> element in elements) {
list.add(Category.fromMap(element));
}
} catch (e, stack) {
log.e("getCategoryByParentId", e, stack);
log.e('getCategoryByParentId', e, stack);
}
return list;
}
Future<List<Good>> getGoodsByCategoryId(int categoryId ) async {
Future<List<Category>> getCategoriesAll() async {
final List<Category> list = [];
try {
final int? appCompanyId = Redux.store?.state.userState?.auth?.companyId;
final List<Map<String, dynamic>> elements = await _db.queryRowsWithWhere(
categoryTableName,
'$categoryColumnAppCompanyId = ? '
' order by $categoryColumnParentIn asc, $categoryColumnId asc ',
[appCompanyId]);
for (final Map<String, dynamic> element in elements) {
list.add(Category.fromMap(element));
}
} catch (e, stack) {
log.e('getCategoriesAll', e, stack);
}
return list;
}
Future<List<Good>> getGoodsByCategoryId(int categoryId) async {
final List<Good> list = [];
try {
final int? appCompanyId = Redux.store?.state.userState?.auth?.companyId;
final List<Map<String, dynamic>> elements = await _db.queryRowsWithWhere(goodTableName, '$GoodColumnAppCompanyId = ? and $GoodColumnCategoryId = ?', [appCompanyId, categoryId]);
for (final Map<String, dynamic> element in elements){
final List<Map<String, dynamic>> elements = await _db.queryRowsWithWhere(
goodTableName,
'$GoodColumnAppCompanyId = ? and $GoodColumnCategoryId = ?',
[appCompanyId, categoryId]);
for (final Map<String, dynamic> element in elements) {
list.add(Good.fromMap(element));
}
} catch (e, stack) {
log.e("getGoodsByCategoryId", e, stack);
log.e('getGoodsByCategoryId', e, stack);
}
return list;
}
Future<List<Good>> getGoodsByNameOrEan( String query ) async {
Future<List<Good>> getGoodsByNameOrEan(String query) async {
final List<Good> 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<Map<String, dynamic>> elements = await _db.queryRowsWithWhere(goodTableName, where, args);
for (final Map<String, dynamic> element in elements){
final List<Map<String, dynamic>> elements =
await _db.queryRowsWithWhere(goodTableName, where, args);
for (final Map<String, dynamic> element in elements) {
list.add(Good.fromMap(element));
}
} catch (e, stack) {
log.e("getGoodsByCategoryId", e, stack);
log.e('getGoodsByCategoryId', e, stack);
}
return list;
}
Future<List<Good>> getGoodsByEan( String code ) async {
Future<List<Good>> getGoodsByEan(String code) async {
final List<Good> 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<Map<String, dynamic>>
elements = await _db.queryRowsWithWhere(goodTableName, where, args);
for (final Map<String, dynamic> element in elements){
final List<Map<String, dynamic>> elements =
await _db.queryRowsWithWhere(goodTableName, where, args);
for (final Map<String, dynamic> 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);
}
}
}

View File

@ -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<CategoryDictionaryView> {
late TextEditingController _searchTextController;
final FocusNode _searchFocusNode = new FocusNode();
final List<Category> _contragents = [];
late List<Category> _categories = [];
late List<CategoryRowDao> items = [];
@override
void initState() {
@ -38,9 +38,15 @@ class _CategoryDictionaryViewState extends State<CategoryDictionaryView> {
reset();
}
});
initQuery();
super.initState();
}
Future<void> initQuery() async {
_categories = await _dictionaryService.getCategoriesAll();
searchByField('');
}
@override
void dispose() {
_searchTextController.dispose();
@ -70,15 +76,14 @@ class _CategoryDictionaryViewState extends State<CategoryDictionaryView> {
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<CategoryDictionaryView> {
void reset() {
_searchTextController.clear();
searchByField('');
}
void searchByField(String query) async {
List<Good> goods = await _dictionaryService.getGoodsByNameOrEan(query);
log.i(query);
final List<CategoryRowDao> list = [];
final Iterable<Category> 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;
}