From 64516a7df26ff309dd75e670cc8583e29f9dd000 Mon Sep 17 00:00:00 2001 From: Patrick Alvin Alcala Date: Wed, 9 Jul 2025 17:09:55 +0800 Subject: [PATCH] fixed cache --- lib/pages/add_category_page.dart | 13 +++++++++++++ lib/pages/add_distributor_page.dart | 18 +++++++++++++++--- lib/pages/add_generics_page.dart | 12 ++++++++++++ lib/pages/add_manufacturer_page.dart | 12 ++++++++++++ lib/pages/add_medicine_page.dart | 28 ++++++++++++++-------------- lib/pages/add_supplier_page.dart | 19 +++++++++++++++---- lib/pages/add_type_page.dart | 12 ++++++++++++ 7 files changed, 93 insertions(+), 21 deletions(-) diff --git a/lib/pages/add_category_page.dart b/lib/pages/add_category_page.dart index 6412409..a256609 100644 --- a/lib/pages/add_category_page.dart +++ b/lib/pages/add_category_page.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; import 'package:gap/gap.dart'; import 'package:internet_connection_checker/internet_connection_checker.dart'; +import 'package:pharmacy_mobile/blocs/caches/categorylist/functions/cache_setcategorylist.dart'; import 'package:pharmacy_mobile/functions/checkexisting_function.dart'; import 'package:pharmacy_mobile/tables/ref_categories.dart'; import 'package:pharmacy_mobile/widgets/buttonwithprogress_widget.dart'; @@ -23,8 +24,19 @@ class _AddCategoryPageState extends State { final _formKey = GlobalKey(); final _categoryController = TextEditingController(); final _refCategories = RefCategories(); + bool _isLoading = false; + Future _getCategoryListCache() async { + final categoryList = await _refCategories.getList(); + + if (categoryList.isNotEmpty) { + // ignore: use_build_context_synchronously + final setCache = await cacheSetCategoryList(context, categoryList); + if (!setCache) {} + } + } + void _saveCategory() async { setState(() => _isLoading = true); @@ -40,6 +52,7 @@ class _AddCategoryPageState extends State { final post = await _refCategories.postCategory(_categoryController.text.toUpperCase()); if (post && mounted) { + _getCategoryListCache(); showNotification(context, 'Category saved', true); WidgetsBinding.instance.addPostFrameCallback((_) { diff --git a/lib/pages/add_distributor_page.dart b/lib/pages/add_distributor_page.dart index f86b21b..1335093 100644 --- a/lib/pages/add_distributor_page.dart +++ b/lib/pages/add_distributor_page.dart @@ -2,6 +2,7 @@ import 'package:flutter/material.dart'; import 'package:gap/gap.dart'; import 'package:go_router/go_router.dart'; import 'package:internet_connection_checker/internet_connection_checker.dart'; +import 'package:pharmacy_mobile/blocs/caches/distributorlist/functions/cache_setdistributorlist.dart'; import 'package:pharmacy_mobile/functions/checkexisting_function.dart'; import 'package:pharmacy_mobile/tables/ref_distributors.dart'; import 'package:pharmacy_mobile/widgets/buttonwithprogress_widget.dart'; @@ -23,25 +24,36 @@ class _AddDistributorPageState extends State { final _formKey = GlobalKey(); final _nameController = TextEditingController(); final _addressController = TextEditingController(); - final _refdistributors = RefDistributors(); + final _refDistributors = RefDistributors(); late bool _isLoading = false; + Future _getDistributorListCache() async { + final distributorList = await _refDistributors.getList(); + + if (distributorList.isNotEmpty) { + // ignore: use_build_context_synchronously + final setCache = await cacheSetDistributorList(context, distributorList); + if (!setCache) {} + } + } + void _saveDistributor() async { setState(() => _isLoading = true); try { if (await InternetConnectionChecker.instance.hasConnection) { - final existing = await checkExisting(_refdistributors, _nameController); + final existing = await checkExisting(_refDistributors, _nameController); if (existing && mounted) { showNotification(context, 'Distributor already listed', false); return; } - final post = await _refdistributors.postDistributor(_nameController.text, _addressController.text); + final post = await _refDistributors.postDistributor(_nameController.text, _addressController.text); if (post && mounted) { + _getDistributorListCache(); showNotification(context, 'Distributor added to list', true); WidgetsBinding.instance.addPostFrameCallback((_) { diff --git a/lib/pages/add_generics_page.dart b/lib/pages/add_generics_page.dart index 7930187..97a3b7e 100644 --- a/lib/pages/add_generics_page.dart +++ b/lib/pages/add_generics_page.dart @@ -2,6 +2,7 @@ import 'package:gap/gap.dart'; import 'package:flutter/material.dart'; import 'package:internet_connection_checker/internet_connection_checker.dart'; import 'package:pharmacy_mobile/blocs/caches/categorylist/functions/cache_getcategorylist.dart'; +import 'package:pharmacy_mobile/blocs/caches/genericlist/functions/cache_setgenericlist.dart'; import 'package:pharmacy_mobile/functions/checkexisting_function.dart'; import 'package:pharmacy_mobile/tables/ref_categories.dart'; import 'package:pharmacy_mobile/tables/ref_generic_names.dart'; @@ -36,6 +37,16 @@ class _AddGenericsPageState extends State { late String _selectedCategory = ''; late String _categoryUUID = ''; + Future _getGenericListCache() async { + final genericNameList = await _refGenericNames.getList(); + + if (genericNameList.isNotEmpty) { + // ignore: use_build_context_synchronously + final setCache = await cacheSetGenericList(context, genericNameList); + if (!setCache) {} + } + } + void _getList() async { _categoryList = await _refCategories.getList(); @@ -94,6 +105,7 @@ class _AddGenericsPageState extends State { final existing = await checkExisting(_refGenericNames, _nameController); if (existing && mounted) { + _getGenericListCache(); showNotification(context, 'Generic Name already existing', false); return; } diff --git a/lib/pages/add_manufacturer_page.dart b/lib/pages/add_manufacturer_page.dart index a9e6ac2..117ce70 100644 --- a/lib/pages/add_manufacturer_page.dart +++ b/lib/pages/add_manufacturer_page.dart @@ -2,6 +2,7 @@ import 'package:flutter/material.dart'; import 'package:gap/gap.dart'; import 'package:go_router/go_router.dart'; import 'package:internet_connection_checker/internet_connection_checker.dart'; +import 'package:pharmacy_mobile/blocs/caches/manufacturerlist/functions/cache_setmanufacturerlist.dart'; import 'package:pharmacy_mobile/functions/checkexisting_function.dart'; import 'package:pharmacy_mobile/tables/ref_manufacturers.dart'; import 'package:pharmacy_mobile/widgets/buttonwithprogress_widget.dart'; @@ -27,6 +28,16 @@ class _AddManufacturerPageState extends State { late bool _isLoading = false; + Future _getManufacturerListCache() async { + final manufacturerList = await _refManufacturers.getList(); + + if (manufacturerList.isNotEmpty) { + // ignore: use_build_context_synchronously + final setCache = await cacheSetManufacturerList(context, manufacturerList); + if (!setCache) {} + } + } + void _saveManufacturer() async { setState(() => _isLoading = true); @@ -42,6 +53,7 @@ class _AddManufacturerPageState extends State { final post = await _refManufacturers.postManufacturer(_nameController.text, _addressController.text); if (post && mounted) { + _getManufacturerListCache(); showNotification(context, 'Manufacturer added to list', true); WidgetsBinding.instance.addPostFrameCallback((_) { diff --git a/lib/pages/add_medicine_page.dart b/lib/pages/add_medicine_page.dart index d58ffd6..feb3af1 100644 --- a/lib/pages/add_medicine_page.dart +++ b/lib/pages/add_medicine_page.dart @@ -70,6 +70,7 @@ class _AddMedicinePageState extends State { late bool imageUploaded = false; late String imageUrl = ''; late bool uploaded = false; + late String imageUUID = ''; Future _getGenerics() async { _genericNameList = await _refGenericNames.getList(); @@ -174,12 +175,6 @@ class _AddMedicinePageState extends State { final distributors = await _getDistributorsCache(); final suppliers = await _getSuppliersCache(); - print('generics: $generics'); - print('types: $types'); - print('manufacturers: $manufacturers'); - print('distributors: $distributors'); - print('suppliers: $suppliers'); - if (!generics || !types || !manufacturers || !distributors || !suppliers) { if (await InternetConnectionChecker.instance.hasConnection) { await _getGenerics(); @@ -268,6 +263,7 @@ class _AddMedicinePageState extends State { try { // final String encrpytedBarcode = await encrypt(_barcodeController.text); final String encrpytedBarcode = _barcodeController.text; + uuid = Uuid().v4(); if (await InternetConnectionChecker.instance.hasConnection) { final medName = _nameController.text; @@ -277,8 +273,12 @@ class _AddMedicinePageState extends State { final medDistributorUUID = await _refDistributors.getUUID(_selectedDistributor); final medSupplierUUID = await _refSuppliers.getUUID(_selectedSupplier); + if (imageUUID.isEmpty) { + imageUUID = '73105ee3-74bd-40ea-9434-f8176a980b43'; + } + final posted = await _refMedicines.postMedicine(uuid, medName, medManufacturerUUID, medGenericUUID, medTypeUUID, - encrpytedBarcode, medDistributorUUID, medSupplierUUID); + encrpytedBarcode, medDistributorUUID, medSupplierUUID, imageUUID); if (posted) { if (mounted) { @@ -315,7 +315,7 @@ class _AddMedicinePageState extends State { final imageBytes = await image!.readAsBytes(); final webpImage = await _webpConvert(imageBytes); - uuid = imageName; + imageUUID = imageName; if (mounted) { uploaded = await _storage.uploadImage(context, storageName, webpImage, '$imageName.webp'); @@ -466,12 +466,12 @@ class _AddMedicinePageState extends State { // const Center(child: CircularProgressIndicator(color: Colors.white)) // else // ButtonWidget(text: 'Save Medicine', onPressed: _saveMedicine) - if (uploaded) - ButtonWithProgressWidget( - trigger: _isLoading, - progressText: 'Adding Medicine', - buttonText: 'Save', - onPressed: _saveMedicine) + // if (uploaded) + ButtonWithProgressWidget( + trigger: _isLoading, + progressText: 'Adding Medicine', + buttonText: 'Save', + onPressed: _saveMedicine) ]) ]))) ])))); diff --git a/lib/pages/add_supplier_page.dart b/lib/pages/add_supplier_page.dart index 37cf543..3d79329 100644 --- a/lib/pages/add_supplier_page.dart +++ b/lib/pages/add_supplier_page.dart @@ -2,6 +2,7 @@ import 'package:flutter/material.dart'; import 'package:gap/gap.dart'; import 'package:go_router/go_router.dart'; import 'package:internet_connection_checker/internet_connection_checker.dart'; +import 'package:pharmacy_mobile/blocs/caches/supplierlist/functions/cache_setsupplierlist.dart'; import 'package:pharmacy_mobile/functions/checkexisting_function.dart'; import 'package:pharmacy_mobile/tables/ref_suppliers.dart'; import 'package:pharmacy_mobile/widgets/buttonwithprogress_widget.dart'; @@ -22,26 +23,36 @@ class AddSupplierPage extends StatefulWidget { class _AddSupplierPageState extends State { final _formKey = GlobalKey(); final _nameController = TextEditingController(); - final _refsuppliers = RefSuppliers(); + final _refSuppliers = RefSuppliers(); late bool _isLoading = false; + Future _getSupplierListCache() async { + final supplierList = await _refSuppliers.getList(); + + if (supplierList.isNotEmpty) { + // ignore: use_build_context_synchronously + final setCache = await cacheSetSupplierList(context, supplierList); + if (!setCache) {} + } + } + void _saveSupplier() async { setState(() => _isLoading = true); try { if (await InternetConnectionChecker.instance.hasConnection) { - final existing = await checkExisting(_refsuppliers, _nameController); + final existing = await checkExisting(_refSuppliers, _nameController); if (existing && mounted) { showNotification(context, 'Supplier already listed', false); return; } - final post = await _refsuppliers.postSupplier(_nameController.text); - print('post: $post'); + final post = await _refSuppliers.postSupplier(_nameController.text); if (post && mounted) { + _getSupplierListCache(); showNotification(context, 'Supplier added to list', true); WidgetsBinding.instance.addPostFrameCallback((_) { diff --git a/lib/pages/add_type_page.dart b/lib/pages/add_type_page.dart index 6d07acc..24dfe82 100644 --- a/lib/pages/add_type_page.dart +++ b/lib/pages/add_type_page.dart @@ -1,6 +1,7 @@ import 'package:gap/gap.dart'; import 'package:flutter/material.dart'; import 'package:internet_connection_checker/internet_connection_checker.dart'; +import 'package:pharmacy_mobile/blocs/caches/typelist/functions/cache_settypelist.dart'; import 'package:pharmacy_mobile/functions/checkexisting_function.dart'; import 'package:pharmacy_mobile/tables/ref_types.dart'; import 'package:pharmacy_mobile/widgets/buttonwithprogress_widget.dart'; @@ -26,6 +27,16 @@ class _AddTypePageState extends State { bool _isLoading = false; + Future _getTypeListCache() async { + final typeList = await _refTypes.getList(); + + if (typeList.isNotEmpty) { + // ignore: use_build_context_synchronously + final setCache = await cacheSetTypeList(context, typeList); + if (!setCache) {} + } + } + void _saveType() async { setState(() => _isLoading = true); try { @@ -40,6 +51,7 @@ class _AddTypePageState extends State { final post = await _refTypes.postType(_typeController.text); if (post && mounted) { + _getTypeListCache(); showNotification(context, 'Medicine type saved', true); WidgetsBinding.instance.addPostFrameCallback((_) {