From 5c4c8ceca904124075c0a48b07157ee4e7ba56db Mon Sep 17 00:00:00 2001 From: Patrick Alvin Alcala Date: Thu, 20 Mar 2025 15:05:21 +0800 Subject: [PATCH] added cache for types --- .../typelist/functions/cache_gettypelist.dart | 14 +++++++ .../typelist/functions/cache_settypelist.dart | 14 +++++++ .../functions/cache_typelist_dispose.dart | 8 ++++ .../caches/typelist/typelist_cache_bloc.dart | 14 +++++++ .../caches/typelist/typelist_cache_event.dart | 8 ++++ .../caches/typelist/typelist_cache_state.dart | 5 +++ lib/pages/add_medicine_page.dart | 39 ++++++++++++++----- lib/pages/main_page.dart | 16 ++++++++ 8 files changed, 109 insertions(+), 9 deletions(-) create mode 100644 lib/blocs/caches/typelist/functions/cache_gettypelist.dart create mode 100644 lib/blocs/caches/typelist/functions/cache_settypelist.dart create mode 100644 lib/blocs/caches/typelist/functions/cache_typelist_dispose.dart create mode 100644 lib/blocs/caches/typelist/typelist_cache_bloc.dart create mode 100644 lib/blocs/caches/typelist/typelist_cache_event.dart create mode 100644 lib/blocs/caches/typelist/typelist_cache_state.dart diff --git a/lib/blocs/caches/typelist/functions/cache_gettypelist.dart b/lib/blocs/caches/typelist/functions/cache_gettypelist.dart new file mode 100644 index 0000000..f92294d --- /dev/null +++ b/lib/blocs/caches/typelist/functions/cache_gettypelist.dart @@ -0,0 +1,14 @@ +import 'package:flutter/widgets.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:pharmacy_mobile/blocs/caches/typelist/typelist_cache_bloc.dart'; +import 'package:pharmacy_mobile/blocs/caches/typelist/typelist_cache_event.dart'; + +Future cacheGetTypeList(BuildContext context) async { + try { + final typeListCache = context.read(); + typeListCache.add(TypeListCacheGet()); + return typeListCache.state.value; + } catch (e) { + return []; + } +} diff --git a/lib/blocs/caches/typelist/functions/cache_settypelist.dart b/lib/blocs/caches/typelist/functions/cache_settypelist.dart new file mode 100644 index 0000000..eaa413f --- /dev/null +++ b/lib/blocs/caches/typelist/functions/cache_settypelist.dart @@ -0,0 +1,14 @@ +import 'package:flutter/widgets.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:pharmacy_mobile/blocs/caches/typelist/typelist_cache_bloc.dart'; +import 'package:pharmacy_mobile/blocs/caches/typelist/typelist_cache_event.dart'; + +Future cacheSetTypeList(BuildContext context, List value) async { + try { + final typeListCache = context.read(); + typeListCache.add(TypeListCacheSet(value)); + return true; + } catch (e) { + return false; + } +} diff --git a/lib/blocs/caches/typelist/functions/cache_typelist_dispose.dart b/lib/blocs/caches/typelist/functions/cache_typelist_dispose.dart new file mode 100644 index 0000000..6cf9051 --- /dev/null +++ b/lib/blocs/caches/typelist/functions/cache_typelist_dispose.dart @@ -0,0 +1,8 @@ +import 'package:flutter/widgets.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:pharmacy_mobile/blocs/caches/typelist/typelist_cache_bloc.dart'; + +void cacheTypeListDispose(BuildContext context) async { + final cache = context.read(); + cache.close(); +} diff --git a/lib/blocs/caches/typelist/typelist_cache_bloc.dart b/lib/blocs/caches/typelist/typelist_cache_bloc.dart new file mode 100644 index 0000000..ee44f7a --- /dev/null +++ b/lib/blocs/caches/typelist/typelist_cache_bloc.dart @@ -0,0 +1,14 @@ +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:pharmacy_mobile/blocs/caches/typelist/typelist_cache_event.dart'; +import 'package:pharmacy_mobile/blocs/caches/typelist/typelist_cache_state.dart'; + +class TypeListBloc extends Bloc { + TypeListBloc() : super(TypeListCacheState([])) { + on((event, emit) { + emit(TypeListCacheState(event.value)); + }); + on((event, emit) { + emit(state); + }); + } +} diff --git a/lib/blocs/caches/typelist/typelist_cache_event.dart b/lib/blocs/caches/typelist/typelist_cache_event.dart new file mode 100644 index 0000000..9b4ae3e --- /dev/null +++ b/lib/blocs/caches/typelist/typelist_cache_event.dart @@ -0,0 +1,8 @@ +abstract class TypeListCacheEvent {} + +class TypeListCacheSet extends TypeListCacheEvent { + final List value; + TypeListCacheSet(this.value); +} + +class TypeListCacheGet extends TypeListCacheEvent {} diff --git a/lib/blocs/caches/typelist/typelist_cache_state.dart b/lib/blocs/caches/typelist/typelist_cache_state.dart new file mode 100644 index 0000000..949408b --- /dev/null +++ b/lib/blocs/caches/typelist/typelist_cache_state.dart @@ -0,0 +1,5 @@ +class TypeListCacheState { + final List value; + + TypeListCacheState(this.value); +} diff --git a/lib/pages/add_medicine_page.dart b/lib/pages/add_medicine_page.dart index fd75231..9bfd3bf 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/typelist/functions/cache_gettypelist.dart'; import 'package:pharmacy_mobile/functions/barcode_scan_function.dart'; import 'package:pharmacy_mobile/functions/checkresult_function.dart'; import 'package:pharmacy_mobile/tables/ref_categories.dart'; @@ -59,19 +60,24 @@ class _AddMedicinePageState extends State { late String imageUrl = ''; Future _getGenerics() async { - final cache = await cacheGetGenericList(context); - - if (cache.isNotEmpty) { - _genericNameList = cache; - } else { - _genericNameList = await _refGenericNames.getList(); - } + _genericNameList = await _refGenericNames.getList(); setState(() { checkResult(context, _genericNameList, 'Generics'); }); } + Future _getGenericsCache() async { + final cache = await cacheGetGenericList(context); + + if (cache.isNotEmpty) { + _genericNameList = cache; + return true; + } else { + return false; + } + } + Future _getTypes() async { _typeList = await _refTypes.getList(); setState(() { @@ -79,6 +85,17 @@ class _AddMedicinePageState extends State { }); } + Future _getTypesCache() async { + final cache = await cacheGetTypeList(context); + + if (cache.isNotEmpty) { + _typeList = cache; + return true; + } else { + return false; + } + } + Future _getManufacturer() async { _manufacturerList = await _refManufacturer.getList(); setState(() { @@ -87,9 +104,13 @@ class _AddMedicinePageState extends State { } void autoRun() async { + final generics = await _getGenericsCache(); + final types = await _getTypesCache(); + if (await InternetConnectionChecker.instance.hasConnection) { - await _getGenerics(); - await _getTypes(); + if (!generics) await _getGenerics(); + if (!types) await _getTypes(); + await _getManufacturer(); // final sample = await _refMedicines.getList2(); diff --git a/lib/pages/main_page.dart b/lib/pages/main_page.dart index 32d95af..93de666 100644 --- a/lib/pages/main_page.dart +++ b/lib/pages/main_page.dart @@ -7,6 +7,7 @@ import 'package:pharmacy_mobile/blocs/caches/categorylist/functions/cache_setcat import 'package:pharmacy_mobile/blocs/caches/genericlist/functions/cache_setgenericlist.dart'; import 'package:pharmacy_mobile/tables/ref_categories.dart'; import 'package:pharmacy_mobile/tables/ref_generic_names.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'; import 'package:pharmacy_mobile/widgets/page_background_widget.dart'; @@ -26,6 +27,7 @@ class _MainPageState extends State { final _authService = AuthService(); final _refCategories = RefCategories(); final _refGenericNames = RefGenericNames(); + final _refTypes = RefTypes(); late bool _isLoading = false; @@ -95,9 +97,23 @@ class _MainPageState extends State { } } + Future _getTypeListCache() async { + final typeList = await _refTypes.getList(); + + 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); + } + } + } + void autoRun() async { await _getCategoryListCache(); await _getGenericListCache(); + await _getTypeListCache(); } @override