From 518415aa4edbded6b62d37aa9553fa30fc20cad7 Mon Sep 17 00:00:00 2001 From: Patrick Alvin Alcala Date: Fri, 21 Mar 2025 10:20:28 +0800 Subject: [PATCH] fix caching on add medicines --- .../functions/cache_getmanufacturerlist.dart | 14 ++++++ .../functions/cache_setmanufacturerlist.dart | 14 ++++++ .../manufacturerlist_cache_bloc.dart | 14 ++++++ .../manufacturerlist_cache_event.dart | 8 ++++ .../manufacturerlist_cache_state.dart | 5 ++ lib/main.dart | 8 ++++ lib/pages/add_medicine_page.dart | 47 ++++++++++++++----- lib/pages/main_page.dart | 32 ++++++++----- 8 files changed, 116 insertions(+), 26 deletions(-) create mode 100644 lib/blocs/caches/manufacturerlist/functions/cache_getmanufacturerlist.dart create mode 100644 lib/blocs/caches/manufacturerlist/functions/cache_setmanufacturerlist.dart create mode 100644 lib/blocs/caches/manufacturerlist/manufacturerlist_cache_bloc.dart create mode 100644 lib/blocs/caches/manufacturerlist/manufacturerlist_cache_event.dart create mode 100644 lib/blocs/caches/manufacturerlist/manufacturerlist_cache_state.dart diff --git a/lib/blocs/caches/manufacturerlist/functions/cache_getmanufacturerlist.dart b/lib/blocs/caches/manufacturerlist/functions/cache_getmanufacturerlist.dart new file mode 100644 index 0000000..278c105 --- /dev/null +++ b/lib/blocs/caches/manufacturerlist/functions/cache_getmanufacturerlist.dart @@ -0,0 +1,14 @@ +import 'package:flutter/widgets.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:pharmacy_mobile/blocs/caches/manufacturerlist/manufacturerlist_cache_bloc.dart'; +import 'package:pharmacy_mobile/blocs/caches/manufacturerlist/manufacturerlist_cache_event.dart'; + +Future cacheGetManufacturerList(BuildContext context) async { + try { + final manufacturerListCache = context.read(); + manufacturerListCache.add(ManufacturerListCacheGet()); + return manufacturerListCache.state.value; + } catch (e) { + return []; + } +} diff --git a/lib/blocs/caches/manufacturerlist/functions/cache_setmanufacturerlist.dart b/lib/blocs/caches/manufacturerlist/functions/cache_setmanufacturerlist.dart new file mode 100644 index 0000000..8f68216 --- /dev/null +++ b/lib/blocs/caches/manufacturerlist/functions/cache_setmanufacturerlist.dart @@ -0,0 +1,14 @@ +import 'package:flutter/widgets.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:pharmacy_mobile/blocs/caches/manufacturerlist/manufacturerlist_cache_bloc.dart'; +import 'package:pharmacy_mobile/blocs/caches/manufacturerlist/manufacturerlist_cache_event.dart'; + +Future cacheSetManufacturerList(BuildContext context, List value) async { + try { + final manufacturerListCache = context.read(); + manufacturerListCache.add(ManufacturerListCacheSet(value)); + return true; + } catch (e) { + return false; + } +} diff --git a/lib/blocs/caches/manufacturerlist/manufacturerlist_cache_bloc.dart b/lib/blocs/caches/manufacturerlist/manufacturerlist_cache_bloc.dart new file mode 100644 index 0000000..dd801b7 --- /dev/null +++ b/lib/blocs/caches/manufacturerlist/manufacturerlist_cache_bloc.dart @@ -0,0 +1,14 @@ +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:pharmacy_mobile/blocs/caches/manufacturerlist/manufacturerlist_cache_event.dart'; +import 'package:pharmacy_mobile/blocs/caches/manufacturerlist/manufacturerlist_cache_state.dart'; + +class ManufacturerListBloc extends Bloc { + ManufacturerListBloc() : super(ManufacturerListCacheState([])) { + on((event, emit) { + emit(ManufacturerListCacheState(event.value)); + }); + on((event, emit) { + emit(state); + }); + } +} diff --git a/lib/blocs/caches/manufacturerlist/manufacturerlist_cache_event.dart b/lib/blocs/caches/manufacturerlist/manufacturerlist_cache_event.dart new file mode 100644 index 0000000..5f2eaf9 --- /dev/null +++ b/lib/blocs/caches/manufacturerlist/manufacturerlist_cache_event.dart @@ -0,0 +1,8 @@ +abstract class ManufacturerListCacheEvent {} + +class ManufacturerListCacheSet extends ManufacturerListCacheEvent { + final List value; + ManufacturerListCacheSet(this.value); +} + +class ManufacturerListCacheGet extends ManufacturerListCacheEvent {} diff --git a/lib/blocs/caches/manufacturerlist/manufacturerlist_cache_state.dart b/lib/blocs/caches/manufacturerlist/manufacturerlist_cache_state.dart new file mode 100644 index 0000000..345e949 --- /dev/null +++ b/lib/blocs/caches/manufacturerlist/manufacturerlist_cache_state.dart @@ -0,0 +1,5 @@ +class ManufacturerListCacheState { + final List value; + + ManufacturerListCacheState(this.value); +} diff --git a/lib/main.dart b/lib/main.dart index 47ec9a3..e76b38c 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -3,6 +3,8 @@ 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/genericlist/genericlist_cache_bloc.dart'; +import 'package:pharmacy_mobile/blocs/caches/manufacturerlist/manufacturerlist_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/user/user_bloc.dart'; import 'package:pharmacy_mobile/pages/add_category_page.dart'; @@ -132,6 +134,12 @@ class MyApp extends StatelessWidget { BlocProvider( create: (context) => GenericListBloc(), ), + BlocProvider( + create: (context) => TypeListBloc(), + ), + BlocProvider( + create: (context) => ManufacturerListBloc(), + ), ], child: MaterialApp.router( debugShowCheckedModeBanner: false, diff --git a/lib/pages/add_medicine_page.dart b/lib/pages/add_medicine_page.dart index 9bfd3bf..08da86c 100644 --- a/lib/pages/add_medicine_page.dart +++ b/lib/pages/add_medicine_page.dart @@ -4,6 +4,7 @@ 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/genericlist/functions/cache_getgenericlist.dart'; +import 'package:pharmacy_mobile/blocs/caches/manufacturerlist/functions/cache_getmanufacturerlist.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'; @@ -72,6 +73,7 @@ class _AddMedicinePageState extends State { if (cache.isNotEmpty) { _genericNameList = cache; + return true; } else { return false; @@ -90,6 +92,7 @@ class _AddMedicinePageState extends State { if (cache.isNotEmpty) { _typeList = cache; + return true; } else { return false; @@ -103,27 +106,45 @@ class _AddMedicinePageState extends State { }); } + Future _getManufacturerCache() async { + final cache = await cacheGetManufacturerList(context); + + if (cache.isNotEmpty) { + _manufacturerList = cache; + + return true; + } else { + return false; + } + } + void autoRun() async { final generics = await _getGenericsCache(); final types = await _getTypesCache(); + final manufacturers = await _getManufacturerCache(); - if (await InternetConnectionChecker.instance.hasConnection) { - if (!generics) await _getGenerics(); - if (!types) await _getTypes(); + print('generics: $generics, types: $types, manufacturers: $manufacturers'); - await _getManufacturer(); + if (!generics || !types || !manufacturers) { + if (await InternetConnectionChecker.instance.hasConnection) { + await _getGenerics(); + await _getTypes(); + await _getManufacturer(); - // final sample = await _refMedicines.getList2(); - } else { - if (mounted) { - showNotification(context, 'Error: No Internet Connection', false); + // final sample = await _refMedicines.getList2(); + } else { + if (mounted) { + showNotification(context, 'Error: No Internet Connection', false); - WidgetsBinding.instance.addPostFrameCallback((_) { - if (mounted) { - context.push('/main'); - } - }); + WidgetsBinding.instance.addPostFrameCallback((_) { + if (mounted) { + context.push('/main'); + } + }); + } } + } else { + setState(() {}); } } diff --git a/lib/pages/main_page.dart b/lib/pages/main_page.dart index 93de666..4126cdb 100644 --- a/lib/pages/main_page.dart +++ b/lib/pages/main_page.dart @@ -5,8 +5,11 @@ 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/genericlist/functions/cache_setgenericlist.dart'; +import 'package:pharmacy_mobile/blocs/caches/manufacturerlist/functions/cache_setmanufacturerlist.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_generic_names.dart'; +import 'package:pharmacy_mobile/tables/ref_manufacturers.dart'; import 'package:pharmacy_mobile/tables/ref_types.dart'; import 'package:pharmacy_mobile/widgets/buttonwithprogress_widget.dart'; import 'package:pharmacy_mobile/widgets/menu_widget.dart'; @@ -28,6 +31,7 @@ class _MainPageState extends State { final _refCategories = RefCategories(); final _refGenericNames = RefGenericNames(); final _refTypes = RefTypes(); + final _refManufacturers = RefManufacturers(); late bool _isLoading = false; @@ -77,10 +81,7 @@ class _MainPageState extends State { if (categoryList.isNotEmpty) { // ignore: use_build_context_synchronously final setCache = await cacheSetCategoryList(context, categoryList); - if (!setCache) { - // ignore: use_build_context_synchronously - showNotification(context, 'Caching failed', false); - } + if (!setCache) {} } } @@ -90,10 +91,7 @@ class _MainPageState extends State { if (genericNameList.isNotEmpty) { // ignore: use_build_context_synchronously final setCache = await cacheSetGenericList(context, genericNameList); - if (!setCache) { - // ignore: use_build_context_synchronously - showNotification(context, 'Caching failed', false); - } + if (!setCache) {} } } @@ -102,11 +100,18 @@ class _MainPageState extends State { if (typeList.isNotEmpty) { // ignore: use_build_context_synchronously - final setCache = await cacheSetGenericList(context, typeList); - if (!setCache) { - // ignore: use_build_context_synchronously - showNotification(context, 'Caching failed', false); - } + final setCache = await cacheSetTypeList(context, typeList); + if (!setCache) {} + } + } + + Future _getManufacturerListCache() async { + final manufacturerList = await _refManufacturers.getList(); + + if (manufacturerList.isNotEmpty) { + // ignore: use_build_context_synchronously + final setCache = await cacheSetManufacturerList(context, manufacturerList); + if (!setCache) {} } } @@ -114,6 +119,7 @@ class _MainPageState extends State { await _getCategoryListCache(); await _getGenericListCache(); await _getTypeListCache(); + await _getManufacturerListCache(); } @override