From 0561d9a9b47094392bc36de6231cdf55fdd9ded3 Mon Sep 17 00:00:00 2001 From: Patrick Alvin Alcala Date: Tue, 22 Apr 2025 17:34:38 +0800 Subject: [PATCH] fix cache error --- lib/main.dart | 10 ++- lib/pages/add_medicine_page.dart | 115 ++++++++++++++++++++----------- lib/pages/main_page.dart | 28 ++++++++ 3 files changed, 112 insertions(+), 41 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 905e7d9..c12b6c1 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -2,10 +2,12 @@ import 'package:flutter/material.dart'; import 'package:flutter_dotenv/flutter_dotenv.dart'; import 'package:pharmacy_mobile/auth/auth_gate.dart'; import 'package:pharmacy_mobile/blocs/caches/categorylist/categorylist_cache_bloc.dart'; +import 'package:pharmacy_mobile/blocs/caches/distributorlist/distributorlist_cache_bloc.dart'; import 'package:pharmacy_mobile/blocs/caches/genericlist/genericlist_cache_bloc.dart'; import 'package:pharmacy_mobile/blocs/caches/manufacturerlist/manufacturerlist_cache_bloc.dart'; import 'package:pharmacy_mobile/blocs/caches/medicinelist/medicinelist_cache_bloc.dart'; import 'package:pharmacy_mobile/blocs/caches/stocklist/stocklist_cache_bloc.dart'; +import 'package:pharmacy_mobile/blocs/caches/supplierlist/supplierlist_cache_bloc.dart'; import 'package:pharmacy_mobile/blocs/caches/typelist/typelist_cache_bloc.dart'; import 'package:pharmacy_mobile/blocs/guest/guest_bloc.dart'; import 'package:pharmacy_mobile/blocs/language/language_bloc.dart'; @@ -114,7 +116,7 @@ final _router = GoRouter( child: child), ), ), - GoRoute( + GoRoute( name: 'addsupplier', path: '/addsupplier', pageBuilder: (BuildContext context, GoRouterState state) => CustomTransitionPage( @@ -287,6 +289,12 @@ class MyApp extends StatelessWidget { BlocProvider( create: (context) => LanguageBloc(), ), + BlocProvider( + create: (context) => DistributorListBloc(), + ), + BlocProvider( + create: (context) => SupplierListBloc(), + ), ], child: MaterialApp.router( debugShowCheckedModeBanner: false, diff --git a/lib/pages/add_medicine_page.dart b/lib/pages/add_medicine_page.dart index 681d158..d58ffd6 100644 --- a/lib/pages/add_medicine_page.dart +++ b/lib/pages/add_medicine_page.dart @@ -3,8 +3,10 @@ import 'package:flutter/material.dart'; import 'package:gap/gap.dart'; import 'package:image_picker/image_picker.dart'; import 'package:internet_connection_checker/internet_connection_checker.dart'; +import 'package:pharmacy_mobile/blocs/caches/distributorlist/functions/cache_getdistributorlist.dart'; import 'package:pharmacy_mobile/blocs/caches/genericlist/functions/cache_getgenericlist.dart'; import 'package:pharmacy_mobile/blocs/caches/manufacturerlist/functions/cache_getmanufacturerlist.dart'; +import 'package:pharmacy_mobile/blocs/caches/supplierlist/functions/cache_getsupplierlist.dart'; import 'package:pharmacy_mobile/blocs/caches/typelist/functions/cache_gettypelist.dart'; import 'package:pharmacy_mobile/functions/barcode_scan_function.dart'; import 'package:pharmacy_mobile/functions/checkresult_function.dart'; @@ -129,7 +131,7 @@ class _AddMedicinePageState extends State { }); } - Future _getManufacturerCache() async { + Future _getManufacturersCache() async { final cache = await cacheGetManufacturerList(context); if (cache.isNotEmpty) { @@ -141,51 +143,84 @@ class _AddMedicinePageState extends State { } } - void autoRun() async { - // final generics = await _getGenericsCache(); - // final types = await _getTypesCache(); - // final manufacturers = await _getManufacturerCache(); + Future _getDistributorsCache() async { + final cache = await cacheGetDistributorList(context); - // if (!generics || !types || !manufacturers) { - // if (await InternetConnectionChecker.instance.hasConnection) { - // await _getGenerics(); - // await _getTypes(); - // await _getManufacturers(); - // await _getDistributors(); - // } else { - // if (mounted) { - // showNotification(context, 'Error: No Internet Connection', false); + if (cache.isNotEmpty) { + _distributorList = cache; - // WidgetsBinding.instance.addPostFrameCallback((_) { - // if (mounted) { - // context.pop(); - // } - // }); - // } - // } - // } else { - // setState(() {}); - // } - - if (await InternetConnectionChecker.instance.hasConnection) { - await _getGenerics(); - await _getTypes(); - await _getManufacturers(); - await _getDistributors(); - await _getSuppliers(); + return true; } else { - if (mounted) { - showNotification(context, 'Error: No Internet Connection', false); - - WidgetsBinding.instance.addPostFrameCallback((_) { - if (mounted) { - context.pop(); - } - }); - } + return false; } } + Future _getSuppliersCache() async { + final cache = await cacheGetSupplierList(context); + + if (cache.isNotEmpty) { + _supplierList = cache; + + return true; + } else { + return false; + } + } + + void autoRun() async { + final generics = await _getGenericsCache(); + final types = await _getTypesCache(); + final manufacturers = await _getManufacturersCache(); + 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(); + await _getTypes(); + await _getManufacturers(); + await _getDistributors(); + await _getSuppliers(); + } else { + if (mounted) { + showNotification(context, 'Error: No Internet Connection', false); + + WidgetsBinding.instance.addPostFrameCallback((_) { + if (mounted) { + context.pop(); + } + }); + } + } + } else { + setState(() {}); + } + + // if (await InternetConnectionChecker.instance.hasConnection) { + // await _getGenerics(); + // await _getTypes(); + // await _getManufacturers(); + // await _getDistributors(); + // await _getSuppliers(); + // } else { + // if (mounted) { + // showNotification(context, 'Error: No Internet Connection', false); + + // WidgetsBinding.instance.addPostFrameCallback((_) { + // if (mounted) { + // context.pop(); + // } + // }); + // } + // } + } + void _updateGeneric(dynamic generic) async { _selectedGeneric = generic; final catuuid = await _refGenericNames.getCategoryUUID(_selectedGeneric); diff --git a/lib/pages/main_page.dart b/lib/pages/main_page.dart index 269162d..cddb7f8 100644 --- a/lib/pages/main_page.dart +++ b/lib/pages/main_page.dart @@ -4,15 +4,19 @@ import 'package:gap/gap.dart'; import 'package:go_router/go_router.dart'; import 'package:pharmacy_mobile/auth/auth_service.dart'; import 'package:pharmacy_mobile/blocs/caches/categorylist/functions/cache_setcategorylist.dart'; +import 'package:pharmacy_mobile/blocs/caches/distributorlist/functions/cache_setdistributorlist.dart'; import 'package:pharmacy_mobile/blocs/caches/genericlist/functions/cache_setgenericlist.dart'; import 'package:pharmacy_mobile/blocs/caches/manufacturerlist/functions/cache_setmanufacturerlist.dart'; import 'package:pharmacy_mobile/blocs/caches/medicinelist/functions/cache_setmedicinelist.dart'; import 'package:pharmacy_mobile/blocs/caches/stocklist/functions/cache_setstocklist.dart'; +import 'package:pharmacy_mobile/blocs/caches/supplierlist/functions/cache_setsupplierlist.dart'; import 'package:pharmacy_mobile/blocs/caches/typelist/functions/cache_settypelist.dart'; import 'package:pharmacy_mobile/tables/ref_categories.dart'; +import 'package:pharmacy_mobile/tables/ref_distributors.dart'; import 'package:pharmacy_mobile/tables/ref_generic_names.dart'; import 'package:pharmacy_mobile/tables/ref_manufacturers.dart'; import 'package:pharmacy_mobile/tables/ref_medicines.dart'; +import 'package:pharmacy_mobile/tables/ref_suppliers.dart'; import 'package:pharmacy_mobile/tables/ref_types.dart'; import 'package:pharmacy_mobile/tables/stocks.dart'; import 'package:pharmacy_mobile/widgets/buttonwithprogress_widget.dart'; @@ -38,6 +42,8 @@ class _MainPageState extends State { final _refManufacturers = RefManufacturers(); final _refMedicines = RefMedicines(); final _stocks = Stocks(); + final _refDistributors = RefDistributors(); + final _refSuppliers = RefSuppliers(); late bool _isLoading = false; @@ -123,6 +129,26 @@ class _MainPageState extends State { } } + Future _getDistributorListCache() async { + final distributorList = await _refDistributors.getList(); + + if (distributorList.isNotEmpty) { + // ignore: use_build_context_synchronously + final setCache = await cacheSetDistributorList(context, distributorList); + if (!setCache) {} + } + } + + 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 autoRun() async { await _getCategoryListCache(); await _getGenericListCache(); @@ -130,6 +156,8 @@ class _MainPageState extends State { await _getManufacturerListCache(); await _getMedicineListCache(); await _getStockListCache(); + await _getDistributorListCache(); + await _getSupplierListCache(); } @override