From f2fdfc644a6492f1821ce74978bf70b5eafe9381 Mon Sep 17 00:00:00 2001 From: Patrick Alvin Alcala Date: Tue, 22 Apr 2025 11:33:46 +0800 Subject: [PATCH] added cache for distributors --- .../distributorlist_cache_bloc.dart | 14 ++++++++++++++ .../distributorlist_cache_event.dart | 8 ++++++++ .../distributorlist_cache_state.dart | 5 +++++ .../functions/cache_getdistributorlist.dart | 14 ++++++++++++++ .../functions/cache_setdistributorlist.dart | 14 ++++++++++++++ lib/pages/add_medicine_page.dart | 19 +++++++++++++++---- 6 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 lib/blocs/caches/distributorlist/distributorlist_cache_bloc.dart create mode 100644 lib/blocs/caches/distributorlist/distributorlist_cache_event.dart create mode 100644 lib/blocs/caches/distributorlist/distributorlist_cache_state.dart create mode 100644 lib/blocs/caches/distributorlist/functions/cache_getdistributorlist.dart create mode 100644 lib/blocs/caches/distributorlist/functions/cache_setdistributorlist.dart diff --git a/lib/blocs/caches/distributorlist/distributorlist_cache_bloc.dart b/lib/blocs/caches/distributorlist/distributorlist_cache_bloc.dart new file mode 100644 index 0000000..53706af --- /dev/null +++ b/lib/blocs/caches/distributorlist/distributorlist_cache_bloc.dart @@ -0,0 +1,14 @@ +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:pharmacy_mobile/blocs/caches/distributorlist/distributorlist_cache_event.dart'; +import 'package:pharmacy_mobile/blocs/caches/distributorlist/distributorlist_cache_state.dart'; + +class DistributorListBloc extends Bloc { + DistributorListBloc() : super(DistributorListCacheState([])) { + on((event, emit) { + emit(DistributorListCacheState(event.value)); + }); + on((event, emit) { + emit(state); + }); + } +} diff --git a/lib/blocs/caches/distributorlist/distributorlist_cache_event.dart b/lib/blocs/caches/distributorlist/distributorlist_cache_event.dart new file mode 100644 index 0000000..142fe75 --- /dev/null +++ b/lib/blocs/caches/distributorlist/distributorlist_cache_event.dart @@ -0,0 +1,8 @@ +abstract class DistributorListCacheEvent {} + +class DistributorListCacheSet extends DistributorListCacheEvent { + final List value; + DistributorListCacheSet(this.value); +} + +class DistributorListCacheGet extends DistributorListCacheEvent {} diff --git a/lib/blocs/caches/distributorlist/distributorlist_cache_state.dart b/lib/blocs/caches/distributorlist/distributorlist_cache_state.dart new file mode 100644 index 0000000..8cc63ba --- /dev/null +++ b/lib/blocs/caches/distributorlist/distributorlist_cache_state.dart @@ -0,0 +1,5 @@ +class DistributorListCacheState { + final List value; + + DistributorListCacheState(this.value); +} diff --git a/lib/blocs/caches/distributorlist/functions/cache_getdistributorlist.dart b/lib/blocs/caches/distributorlist/functions/cache_getdistributorlist.dart new file mode 100644 index 0000000..62dd664 --- /dev/null +++ b/lib/blocs/caches/distributorlist/functions/cache_getdistributorlist.dart @@ -0,0 +1,14 @@ +import 'package:flutter/widgets.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:pharmacy_mobile/blocs/caches/distributorlist/distributorlist_cache_bloc.dart'; +import 'package:pharmacy_mobile/blocs/caches/distributorlist/distributorlist_cache_event.dart'; + +Future cacheGetDistributorList(BuildContext context) async { + try { + final distributorListCache = context.read(); + distributorListCache.add(DistributorListCacheGet()); + return distributorListCache.state.value; + } catch (e) { + return []; + } +} diff --git a/lib/blocs/caches/distributorlist/functions/cache_setdistributorlist.dart b/lib/blocs/caches/distributorlist/functions/cache_setdistributorlist.dart new file mode 100644 index 0000000..89241c8 --- /dev/null +++ b/lib/blocs/caches/distributorlist/functions/cache_setdistributorlist.dart @@ -0,0 +1,14 @@ +import 'package:flutter/widgets.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:pharmacy_mobile/blocs/caches/distributorlist/distributorlist_cache_bloc.dart'; +import 'package:pharmacy_mobile/blocs/caches/distributorlist/distributorlist_cache_event.dart'; + +Future cacheSetDistributorList(BuildContext context, List value) async { + try { + final distributorListCache = context.read(); + distributorListCache.add(DistributorListCacheSet(value)); + return true; + } catch (e) { + return false; + } +} diff --git a/lib/pages/add_medicine_page.dart b/lib/pages/add_medicine_page.dart index c6f8c5d..681d158 100644 --- a/lib/pages/add_medicine_page.dart +++ b/lib/pages/add_medicine_page.dart @@ -63,7 +63,7 @@ class _AddMedicinePageState extends State { late List _supplierList = []; late String _selectedManufacturer = ''; late String _selectedDistributor = ''; - late final String _selectedSupplier = ''; + late String _selectedSupplier = ''; late String uuid = ''; late bool imageUploaded = false; late String imageUrl = ''; @@ -206,6 +206,10 @@ class _AddMedicinePageState extends State { _selectedDistributor = distributor; } + void _updateSupplier(dynamic supplier) { + _selectedSupplier = supplier; + } + Future _scanBarcode() async { // String? barcode = await SimpleBarcodeScanner.scanBarcode( // context, @@ -238,8 +242,8 @@ class _AddMedicinePageState extends State { final medDistributorUUID = await _refDistributors.getUUID(_selectedDistributor); final medSupplierUUID = await _refSuppliers.getUUID(_selectedSupplier); - final posted = await _refMedicines.postMedicine( - uuid, medName, medManufacturerUUID, medGenericUUID, medTypeUUID, encrpytedBarcode, medDistributorUUID, medSupplierUUID); + final posted = await _refMedicines.postMedicine(uuid, medName, medManufacturerUUID, medGenericUUID, medTypeUUID, + encrpytedBarcode, medDistributorUUID, medSupplierUUID); if (posted) { if (mounted) { @@ -357,7 +361,7 @@ class _AddMedicinePageState extends State { color: 'green', child: Form( key: _formKey, - child: DropdownWrapperMultiWidget(list: _genericNameList, text: 'Data', children: [ + child: DropdownWrapperMultiWidget(list: _supplierList, text: 'Data', children: [ Column(crossAxisAlignment: CrossAxisAlignment.start, children: [ InputFormWidget(label: 'Name', controller: _nameController), const Gap(16), @@ -394,6 +398,13 @@ class _AddMedicinePageState extends State { onChanged: _updateDistributor, ), const Gap(16), + DropDownWidget( + label: 'Supplier', + list: _supplierList, + listTitle: 'supplier_name', + onChanged: _updateSupplier, + ), + const Gap(16), InputFormWidget(label: 'Barcode', controller: _barcodeController), ScanbarcodeWidget(onTap: _scanBarcode), const Gap(16),