import 'dart:convert'; import 'package:aman_kassa_flutter/core/entity/Category.dart'; import 'package:aman_kassa_flutter/core/entity/Goods.dart'; import 'package:aman_kassa_flutter/core/locator.dart'; import 'package:aman_kassa_flutter/core/models/DictDao.dart'; import 'package:aman_kassa_flutter/core/models/ProductDao.dart'; import 'package:aman_kassa_flutter/core/services/ApiService.dart'; import 'package:aman_kassa_flutter/core/services/DataService.dart'; import 'package:aman_kassa_flutter/core/services/navigator_service.dart'; import 'package:aman_kassa_flutter/redux/state/main_state.dart'; import 'package:meta/meta.dart'; import 'package:redux/redux.dart'; import 'package:redux_thunk/redux_thunk.dart'; import '../store.dart'; @immutable class SetMainStateAction { final MainState mainState; SetMainStateAction(this.mainState); } final ApiService _api = locator(); final DataService _dataService = locator(); final NavigatorService _navigation = locator(); Future backBottomElement(Store store) async { List prevCategories = store.state.mainState.prevCategories; DictDao last = prevCategories.removeLast(); if (last != null) { store.dispatch( SetMainStateAction(MainState(prevCategories: prevCategories))); store.dispatch(selectBottomElement(last.id)); } } ThunkAction addCustomProductToKassaItems(String name, int count, double price) { return (Store store) async { List items = store.state.mainState.kassaItems; items.add(new ProductDao(name: name, count: count, price: price)); store.dispatch(SetMainStateAction(MainState(kassaItems: items))); }; } ThunkAction addProductToKassaItems(Good good) { return (Store store) async { List items = store.state.mainState.kassaItems; int index = items.indexWhere((element) => element.good?.id == good.id); if (index > -1) { store.dispatch(counterProductFromKassaItems(index, 1)); } else { items.add(new ProductDao( name: good.name, good: good, count: 1, price: good.price)); store.dispatch(SetMainStateAction(MainState(kassaItems: items))); } }; } ThunkAction removeProductFromKassaItems(int index) { return (Store store) async { List items = List.from(store.state.mainState.kassaItems); items.removeAt(index); store.dispatch(SetMainStateAction(MainState(kassaItems: items))); }; } ThunkAction counterProductFromKassaItems(int index, int counter) { return (Store store) async { List items = store.state.mainState.kassaItems; if (items.elementAt(index).count == 1 && counter < 0) { //if count to zero need delete element store.dispatch(removeProductFromKassaItems(index)); } else { items.elementAt(index).count += counter; store.dispatch(SetMainStateAction(MainState(kassaItems: items))); } }; } ThunkAction selectBottomElement(int parentId) { return (Store store) async { store.dispatch(SetMainStateAction(MainState(bottomSheetLoading: true))); try { List prevCategories = store.state.mainState.prevCategories; if (parentId == 0) { prevCategories = []; } store.state.mainState.bottomSheetElements.forEach((element) { if (element is Category && element.id == parentId) { prevCategories.add(DictDao(id: element.parentIn, name: element.name)); } }); List categories = await _dataService.getCategoriesByParentId(parentId: parentId); List goods = await _dataService.getGoodsByCategoryId(categoryId: parentId); List _bottomSheetElements = []; _bottomSheetElements.addAll(categories); _bottomSheetElements.addAll(goods); store.dispatch(SetMainStateAction(MainState( bottomSheetLoading: false, bottomSheetElements: _bottomSheetElements, prevCategories: prevCategories))); } catch (e) { print(e); store.dispatch(SetMainStateAction(MainState(bottomSheetLoading: false))); } }; }