From de5a7c66a3aa2c76c8f3f3c0004aa763fe4cfa1d Mon Sep 17 00:00:00 2001 From: Patrick Alvin Alcala Date: Mon, 24 Mar 2025 11:50:39 +0800 Subject: [PATCH] add cache for stocks --- .../functions/cache_getstocklist.dart | 14 ++++++++ .../functions/cache_setstocklist.dart | 14 ++++++++ .../stocklist/stocklist_cache_bloc.dart | 14 ++++++++ .../stocklist/stocklist_cache_event.dart | 8 +++++ .../stocklist/stocklist_cache_state.dart | 5 +++ .../typelist/functions/cache_gettypelist.dart | 1 - lib/functions/getlist_cache_function.dart | 20 +++++++++++ lib/main.dart | 4 +++ lib/pages/delete_stock_page.dart | 33 ++++++++++++++++++- lib/pages/list_stocks_page.dart | 10 +++++- lib/pages/main_page.dart | 14 ++++++++ 11 files changed, 134 insertions(+), 3 deletions(-) create mode 100644 lib/blocs/caches/stocklist/functions/cache_getstocklist.dart create mode 100644 lib/blocs/caches/stocklist/functions/cache_setstocklist.dart create mode 100644 lib/blocs/caches/stocklist/stocklist_cache_bloc.dart create mode 100644 lib/blocs/caches/stocklist/stocklist_cache_event.dart create mode 100644 lib/blocs/caches/stocklist/stocklist_cache_state.dart create mode 100644 lib/functions/getlist_cache_function.dart diff --git a/lib/blocs/caches/stocklist/functions/cache_getstocklist.dart b/lib/blocs/caches/stocklist/functions/cache_getstocklist.dart new file mode 100644 index 0000000..9861b7b --- /dev/null +++ b/lib/blocs/caches/stocklist/functions/cache_getstocklist.dart @@ -0,0 +1,14 @@ +import 'package:flutter/widgets.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:pharmacy_mobile/blocs/caches/stocklist/stocklist_cache_bloc.dart'; +import 'package:pharmacy_mobile/blocs/caches/stocklist/stocklist_cache_event.dart'; + +Future cacheGetStockList(BuildContext context) async { + try { + final stockListCache = context.read(); + stockListCache.add(StockListCacheGet()); + return stockListCache.state.value; + } catch (e) { + return []; + } +} diff --git a/lib/blocs/caches/stocklist/functions/cache_setstocklist.dart b/lib/blocs/caches/stocklist/functions/cache_setstocklist.dart new file mode 100644 index 0000000..78d2f1b --- /dev/null +++ b/lib/blocs/caches/stocklist/functions/cache_setstocklist.dart @@ -0,0 +1,14 @@ +import 'package:flutter/widgets.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:pharmacy_mobile/blocs/caches/stocklist/stocklist_cache_bloc.dart'; +import 'package:pharmacy_mobile/blocs/caches/stocklist/stocklist_cache_event.dart'; + +Future cacheSetStockList(BuildContext context, List value) async { + try { + final stockListCache = context.read(); + stockListCache.add(StockListCacheSet(value)); + return true; + } catch (e) { + return false; + } +} diff --git a/lib/blocs/caches/stocklist/stocklist_cache_bloc.dart b/lib/blocs/caches/stocklist/stocklist_cache_bloc.dart new file mode 100644 index 0000000..fa8fe69 --- /dev/null +++ b/lib/blocs/caches/stocklist/stocklist_cache_bloc.dart @@ -0,0 +1,14 @@ +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:pharmacy_mobile/blocs/caches/stocklist/stocklist_cache_event.dart'; +import 'package:pharmacy_mobile/blocs/caches/stocklist/stocklist_cache_state.dart'; + +class StockListBloc extends Bloc { + StockListBloc() : super(StockListCacheState([])) { + on((event, emit) { + emit(StockListCacheState(event.value)); + }); + on((event, emit) { + emit(state); + }); + } +} diff --git a/lib/blocs/caches/stocklist/stocklist_cache_event.dart b/lib/blocs/caches/stocklist/stocklist_cache_event.dart new file mode 100644 index 0000000..b7d4867 --- /dev/null +++ b/lib/blocs/caches/stocklist/stocklist_cache_event.dart @@ -0,0 +1,8 @@ +abstract class StockListCacheEvent {} + +class StockListCacheSet extends StockListCacheEvent { + final List value; + StockListCacheSet(this.value); +} + +class StockListCacheGet extends StockListCacheEvent {} diff --git a/lib/blocs/caches/stocklist/stocklist_cache_state.dart b/lib/blocs/caches/stocklist/stocklist_cache_state.dart new file mode 100644 index 0000000..231178c --- /dev/null +++ b/lib/blocs/caches/stocklist/stocklist_cache_state.dart @@ -0,0 +1,5 @@ +class StockListCacheState { + final List value; + + StockListCacheState(this.value); +} diff --git a/lib/blocs/caches/typelist/functions/cache_gettypelist.dart b/lib/blocs/caches/typelist/functions/cache_gettypelist.dart index 49a10d3..f92294d 100644 --- a/lib/blocs/caches/typelist/functions/cache_gettypelist.dart +++ b/lib/blocs/caches/typelist/functions/cache_gettypelist.dart @@ -9,7 +9,6 @@ Future cacheGetTypeList(BuildContext context) async { typeListCache.add(TypeListCacheGet()); return typeListCache.state.value; } catch (e) { - print(e); return []; } } diff --git a/lib/functions/getlist_cache_function.dart b/lib/functions/getlist_cache_function.dart new file mode 100644 index 0000000..d914335 --- /dev/null +++ b/lib/functions/getlist_cache_function.dart @@ -0,0 +1,20 @@ +import 'package:flutter/widgets.dart'; +import 'package:pharmacy_mobile/blocs/caches/stocklist/functions/cache_getstocklist.dart'; + +Future getListCache(BuildContext context, String type) async { + late final List cache; + + switch (type) { + case 'stock': + cache = await cacheGetStockList(context); + break; + default: + return []; + } + + if (cache.isNotEmpty) { + return cache; + } else { + return []; + } +} diff --git a/lib/main.dart b/lib/main.dart index 7adab87..ceb710d 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -5,6 +5,7 @@ import 'package:pharmacy_mobile/blocs/caches/categorylist/categorylist_cache_blo 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/typelist/typelist_cache_bloc.dart'; import 'package:pharmacy_mobile/blocs/guest/guest_bloc.dart'; import 'package:pharmacy_mobile/blocs/user/user_bloc.dart'; @@ -144,6 +145,9 @@ class MyApp extends StatelessWidget { BlocProvider( create: (context) => MedicineListBloc(), ), + BlocProvider( + create: (context) => StockListBloc(), + ), ], child: MaterialApp.router( debugShowCheckedModeBanner: false, diff --git a/lib/pages/delete_stock_page.dart b/lib/pages/delete_stock_page.dart index 5a16606..1e07eaf 100644 --- a/lib/pages/delete_stock_page.dart +++ b/lib/pages/delete_stock_page.dart @@ -1,6 +1,9 @@ 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/stocklist/functions/cache_getstocklist.dart'; import 'package:pharmacy_mobile/functions/checkresult_function.dart'; +import 'package:pharmacy_mobile/functions/getlist_cache_function.dart'; import 'package:pharmacy_mobile/tables/stocks.dart'; import 'package:pharmacy_mobile/widgets/button_widget.dart'; import 'package:pharmacy_mobile/widgets/dropdown_widget.dart'; @@ -48,6 +51,17 @@ class _DeleteStockPageState extends State { } } + // Future _getStocksCache() async { + // final cache = await cacheGetStockList(context); + + // if (cache.isNotEmpty) { + // _stockList = cache; + // return true; + // } else { + // return false; + // } + // } + void _updateStock(dynamic stock) { _selectedStock = stock; _getQuantity(_selectedStock); @@ -98,9 +112,26 @@ class _DeleteStockPageState extends State { } } + void autoRun() async { + final cache = await getListCache(context, 'stock'); + + if (cache.isEmpty) { + if (await InternetConnectionChecker.instance.hasConnection) { + _getStocks(); + } else { + // ignore: use_build_context_synchronously + showNotification(context, 'Error: No Internet Connection', false); + } + } else { + setState(() { + _stockList = cache; + }); + } + } + @override void initState() { - _getStocks(); + autoRun(); super.initState(); } diff --git a/lib/pages/list_stocks_page.dart b/lib/pages/list_stocks_page.dart index be49ceb..4372210 100644 --- a/lib/pages/list_stocks_page.dart +++ b/lib/pages/list_stocks_page.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; import 'package:gap/gap.dart'; import 'package:intl/intl.dart'; +import 'package:pharmacy_mobile/functions/getlist_cache_function.dart'; import 'package:pharmacy_mobile/tables/stocks.dart'; import 'package:pharmacy_mobile/widgets/datatable_widget.dart'; import 'package:pharmacy_mobile/widgets/page_background_widget.dart'; @@ -63,7 +64,14 @@ class _ListStocksPageState extends State { setState(() { _isLoading = true; }); - _stockList = await _stocks.getList(); + + final cache = await getListCache(context, 'stock'); + + if (cache.isNotEmpty) { + _stockList = cache; + } else { + _stockList = await _stocks.getList(); + } setState(() { _isLoading = false; diff --git a/lib/pages/main_page.dart b/lib/pages/main_page.dart index 9daa456..dca62d2 100644 --- a/lib/pages/main_page.dart +++ b/lib/pages/main_page.dart @@ -9,12 +9,14 @@ 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/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/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_medicines.dart'; import 'package:pharmacy_mobile/tables/ref_types.dart'; +import 'package:pharmacy_mobile/tables/stocks.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'; @@ -37,6 +39,7 @@ class _MainPageState extends State { final _refTypes = RefTypes(); final _refManufacturers = RefManufacturers(); final _refMedicines = RefMedicines(); + final _stocks = Stocks(); late bool _isLoading = false; @@ -130,12 +133,23 @@ class _MainPageState extends State { } } + Future _getStockListCache() async { + final stockList = await _stocks.getList(); + + if (stockList.isNotEmpty) { + // ignore: use_build_context_synchronously + final setCache = await cacheSetStockList(context, stockList); + if (!setCache) {} + } + } + void autoRun() async { await _getCategoryListCache(); await _getGenericListCache(); await _getTypeListCache(); await _getManufacturerListCache(); await _getMedicineListCache(); + await _getStockListCache(); } @override