From bcf9823ffb6a095690745c022fe04e82943be4f3 Mon Sep 17 00:00:00 2001 From: Patrick Alvin Alcala Date: Thu, 20 Mar 2025 14:17:01 +0800 Subject: [PATCH] added cache for generics --- .../categorylist/categorylist_cache_bloc.dart | 3 --- .../categorylist_cache_event.dart | 2 -- .../functions/cache_categorylist_dispose.dart | 8 ++++++++ .../functions/cache_genericlist_dispose.dart | 8 ++++++++ .../functions/cache_getgenericlist.dart | 14 ++++++++++++++ .../functions/cache_setgenericlist.dart | 14 ++++++++++++++ .../genericlist/genericlist_cache_bloc.dart | 14 ++++++++++++++ .../genericlist/genericlist_cache_event.dart | 8 ++++++++ .../genericlist/genericlist_cache_state.dart | 5 +++++ .../guest/functions/bloc_guest_dispose.dart | 8 ++++++++ lib/main.dart | 4 ++++ lib/pages/add_medicine_page.dart | 10 +++++++++- lib/pages/main_page.dart | 19 ++++++++++++++++--- 13 files changed, 108 insertions(+), 9 deletions(-) create mode 100644 lib/blocs/caches/categorylist/functions/cache_categorylist_dispose.dart create mode 100644 lib/blocs/caches/genericlist/functions/cache_genericlist_dispose.dart create mode 100644 lib/blocs/caches/genericlist/functions/cache_getgenericlist.dart create mode 100644 lib/blocs/caches/genericlist/functions/cache_setgenericlist.dart create mode 100644 lib/blocs/caches/genericlist/genericlist_cache_bloc.dart create mode 100644 lib/blocs/caches/genericlist/genericlist_cache_event.dart create mode 100644 lib/blocs/caches/genericlist/genericlist_cache_state.dart create mode 100644 lib/blocs/guest/functions/bloc_guest_dispose.dart diff --git a/lib/blocs/caches/categorylist/categorylist_cache_bloc.dart b/lib/blocs/caches/categorylist/categorylist_cache_bloc.dart index e048372..c9cccd2 100644 --- a/lib/blocs/caches/categorylist/categorylist_cache_bloc.dart +++ b/lib/blocs/caches/categorylist/categorylist_cache_bloc.dart @@ -10,8 +10,5 @@ class CategoryListBloc extends Bloc((event, emit) { emit(state); }); - on((event, emit) { - emit(state); - }); } } diff --git a/lib/blocs/caches/categorylist/categorylist_cache_event.dart b/lib/blocs/caches/categorylist/categorylist_cache_event.dart index 2833987..4cf2da2 100644 --- a/lib/blocs/caches/categorylist/categorylist_cache_event.dart +++ b/lib/blocs/caches/categorylist/categorylist_cache_event.dart @@ -6,5 +6,3 @@ class CategoryListCacheSet extends CategoryListCacheEvent { } class CategoryListCacheGet extends CategoryListCacheEvent {} - -class CategoryListCacheCheck extends CategoryListCacheEvent {} diff --git a/lib/blocs/caches/categorylist/functions/cache_categorylist_dispose.dart b/lib/blocs/caches/categorylist/functions/cache_categorylist_dispose.dart new file mode 100644 index 0000000..ec872d4 --- /dev/null +++ b/lib/blocs/caches/categorylist/functions/cache_categorylist_dispose.dart @@ -0,0 +1,8 @@ +import 'package:flutter/widgets.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:pharmacy_mobile/blocs/caches/categorylist/categorylist_cache_bloc.dart'; + +void cacheCategoryListDispose(BuildContext context) async { + final cache = context.read(); + cache.close(); +} diff --git a/lib/blocs/caches/genericlist/functions/cache_genericlist_dispose.dart b/lib/blocs/caches/genericlist/functions/cache_genericlist_dispose.dart new file mode 100644 index 0000000..9d24dc4 --- /dev/null +++ b/lib/blocs/caches/genericlist/functions/cache_genericlist_dispose.dart @@ -0,0 +1,8 @@ +import 'package:flutter/widgets.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:pharmacy_mobile/blocs/caches/genericlist/genericlist_cache_bloc.dart'; + +void cacheGenericListDispose(BuildContext context) async { + final cache = context.read(); + cache.close(); +} diff --git a/lib/blocs/caches/genericlist/functions/cache_getgenericlist.dart b/lib/blocs/caches/genericlist/functions/cache_getgenericlist.dart new file mode 100644 index 0000000..cc73519 --- /dev/null +++ b/lib/blocs/caches/genericlist/functions/cache_getgenericlist.dart @@ -0,0 +1,14 @@ +import 'package:flutter/widgets.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:pharmacy_mobile/blocs/caches/genericlist/genericlist_cache_bloc.dart'; +import 'package:pharmacy_mobile/blocs/caches/genericlist/genericlist_cache_event.dart'; + +Future cacheGetGenericList(BuildContext context) async { + try { + final genericListCache = context.read(); + genericListCache.add(GenericListCacheGet()); + return genericListCache.state.value; + } catch (e) { + return []; + } +} diff --git a/lib/blocs/caches/genericlist/functions/cache_setgenericlist.dart b/lib/blocs/caches/genericlist/functions/cache_setgenericlist.dart new file mode 100644 index 0000000..bb3a521 --- /dev/null +++ b/lib/blocs/caches/genericlist/functions/cache_setgenericlist.dart @@ -0,0 +1,14 @@ +import 'package:flutter/widgets.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:pharmacy_mobile/blocs/caches/genericlist/genericlist_cache_bloc.dart'; +import 'package:pharmacy_mobile/blocs/caches/genericlist/genericlist_cache_event.dart'; + +Future cacheSetGenericList(BuildContext context, List value) async { + try { + final genericListCache = context.read(); + genericListCache.add(GenericListCacheSet(value)); + return true; + } catch (e) { + return false; + } +} diff --git a/lib/blocs/caches/genericlist/genericlist_cache_bloc.dart b/lib/blocs/caches/genericlist/genericlist_cache_bloc.dart new file mode 100644 index 0000000..153c293 --- /dev/null +++ b/lib/blocs/caches/genericlist/genericlist_cache_bloc.dart @@ -0,0 +1,14 @@ +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:pharmacy_mobile/blocs/caches/genericlist/genericlist_cache_event.dart'; +import 'package:pharmacy_mobile/blocs/caches/genericlist/genericlist_cache_state.dart'; + +class GenericListBloc extends Bloc { + GenericListBloc() : super(GenericListCacheState([])) { + on((event, emit) { + emit(GenericListCacheState(event.value)); + }); + on((event, emit) { + emit(state); + }); + } +} diff --git a/lib/blocs/caches/genericlist/genericlist_cache_event.dart b/lib/blocs/caches/genericlist/genericlist_cache_event.dart new file mode 100644 index 0000000..3308aa1 --- /dev/null +++ b/lib/blocs/caches/genericlist/genericlist_cache_event.dart @@ -0,0 +1,8 @@ +abstract class GenericListCacheEvent {} + +class GenericListCacheSet extends GenericListCacheEvent { + final List value; + GenericListCacheSet(this.value); +} + +class GenericListCacheGet extends GenericListCacheEvent {} diff --git a/lib/blocs/caches/genericlist/genericlist_cache_state.dart b/lib/blocs/caches/genericlist/genericlist_cache_state.dart new file mode 100644 index 0000000..8643b1d --- /dev/null +++ b/lib/blocs/caches/genericlist/genericlist_cache_state.dart @@ -0,0 +1,5 @@ +class GenericListCacheState { + final List value; + + GenericListCacheState(this.value); +} diff --git a/lib/blocs/guest/functions/bloc_guest_dispose.dart b/lib/blocs/guest/functions/bloc_guest_dispose.dart new file mode 100644 index 0000000..460d738 --- /dev/null +++ b/lib/blocs/guest/functions/bloc_guest_dispose.dart @@ -0,0 +1,8 @@ +import 'package:flutter/widgets.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:pharmacy_mobile/blocs/guest/guest_bloc.dart'; + +void blocGuestDispose(BuildContext context) async { + final guestBloc = context.read(); + guestBloc.close(); +} diff --git a/lib/main.dart b/lib/main.dart index d16c248..47ec9a3 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -2,6 +2,7 @@ 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/genericlist/genericlist_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'; @@ -128,6 +129,9 @@ class MyApp extends StatelessWidget { BlocProvider( create: (context) => CategoryListBloc(), ), + BlocProvider( + create: (context) => GenericListBloc(), + ), ], child: MaterialApp.router( debugShowCheckedModeBanner: false, diff --git a/lib/pages/add_medicine_page.dart b/lib/pages/add_medicine_page.dart index 08f1628..fd75231 100644 --- a/lib/pages/add_medicine_page.dart +++ b/lib/pages/add_medicine_page.dart @@ -3,6 +3,7 @@ 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/genericlist/functions/cache_getgenericlist.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'; @@ -58,7 +59,14 @@ class _AddMedicinePageState extends State { late String imageUrl = ''; Future _getGenerics() async { - _genericNameList = await _refGenericNames.getList(); + final cache = await cacheGetGenericList(context); + + if (cache.isNotEmpty) { + _genericNameList = cache; + } else { + _genericNameList = await _refGenericNames.getList(); + } + setState(() { checkResult(context, _genericNameList, 'Generics'); }); diff --git a/lib/pages/main_page.dart b/lib/pages/main_page.dart index c47a9f2..32d95af 100644 --- a/lib/pages/main_page.dart +++ b/lib/pages/main_page.dart @@ -3,9 +3,10 @@ import 'package:font_awesome_flutter/font_awesome_flutter.dart'; 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_getcategorylist.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/tables/ref_categories.dart'; +import 'package:pharmacy_mobile/tables/ref_generic_names.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'; @@ -24,6 +25,7 @@ class MainPage extends StatefulWidget { class _MainPageState extends State { final _authService = AuthService(); final _refCategories = RefCategories(); + final _refGenericNames = RefGenericNames(); late bool _isLoading = false; @@ -76,15 +78,26 @@ class _MainPageState extends State { if (!setCache) { // ignore: use_build_context_synchronously showNotification(context, 'Caching failed', false); - } else { - print('Caching Success'); + } + } + } + Future _getGenericListCache() async { + final genericNameList = await _refGenericNames.getList(); + + 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); } } } void autoRun() async { await _getCategoryListCache(); + await _getGenericListCache(); } @override