add cache for stocks

This commit is contained in:
Patrick Alvin Alcala 2025-03-24 11:50:39 +08:00
parent 92df0af895
commit de5a7c66a3
11 changed files with 134 additions and 3 deletions

View file

@ -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<List> cacheGetStockList(BuildContext context) async {
try {
final stockListCache = context.read<StockListBloc>();
stockListCache.add(StockListCacheGet());
return stockListCache.state.value;
} catch (e) {
return [];
}
}

View file

@ -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<bool> cacheSetStockList(BuildContext context, List value) async {
try {
final stockListCache = context.read<StockListBloc>();
stockListCache.add(StockListCacheSet(value));
return true;
} catch (e) {
return false;
}
}

View file

@ -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<StockListCacheEvent, StockListCacheState> {
StockListBloc() : super(StockListCacheState([])) {
on<StockListCacheSet>((event, emit) {
emit(StockListCacheState(event.value));
});
on<StockListCacheGet>((event, emit) {
emit(state);
});
}
}

View file

@ -0,0 +1,8 @@
abstract class StockListCacheEvent {}
class StockListCacheSet extends StockListCacheEvent {
final List value;
StockListCacheSet(this.value);
}
class StockListCacheGet extends StockListCacheEvent {}

View file

@ -0,0 +1,5 @@
class StockListCacheState {
final List value;
StockListCacheState(this.value);
}

View file

@ -9,7 +9,6 @@ Future<List> cacheGetTypeList(BuildContext context) async {
typeListCache.add(TypeListCacheGet()); typeListCache.add(TypeListCacheGet());
return typeListCache.state.value; return typeListCache.state.value;
} catch (e) { } catch (e) {
print(e);
return []; return [];
} }
} }

View file

@ -0,0 +1,20 @@
import 'package:flutter/widgets.dart';
import 'package:pharmacy_mobile/blocs/caches/stocklist/functions/cache_getstocklist.dart';
Future<List> 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 [];
}
}

View file

@ -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/genericlist/genericlist_cache_bloc.dart';
import 'package:pharmacy_mobile/blocs/caches/manufacturerlist/manufacturerlist_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/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/caches/typelist/typelist_cache_bloc.dart';
import 'package:pharmacy_mobile/blocs/guest/guest_bloc.dart'; import 'package:pharmacy_mobile/blocs/guest/guest_bloc.dart';
import 'package:pharmacy_mobile/blocs/user/user_bloc.dart'; import 'package:pharmacy_mobile/blocs/user/user_bloc.dart';
@ -144,6 +145,9 @@ class MyApp extends StatelessWidget {
BlocProvider( BlocProvider(
create: (context) => MedicineListBloc(), create: (context) => MedicineListBloc(),
), ),
BlocProvider(
create: (context) => StockListBloc(),
),
], ],
child: MaterialApp.router( child: MaterialApp.router(
debugShowCheckedModeBanner: false, debugShowCheckedModeBanner: false,

View file

@ -1,6 +1,9 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:gap/gap.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/checkresult_function.dart';
import 'package:pharmacy_mobile/functions/getlist_cache_function.dart';
import 'package:pharmacy_mobile/tables/stocks.dart'; import 'package:pharmacy_mobile/tables/stocks.dart';
import 'package:pharmacy_mobile/widgets/button_widget.dart'; import 'package:pharmacy_mobile/widgets/button_widget.dart';
import 'package:pharmacy_mobile/widgets/dropdown_widget.dart'; import 'package:pharmacy_mobile/widgets/dropdown_widget.dart';
@ -48,6 +51,17 @@ class _DeleteStockPageState extends State<DeleteStockPage> {
} }
} }
// Future<bool> _getStocksCache() async {
// final cache = await cacheGetStockList(context);
// if (cache.isNotEmpty) {
// _stockList = cache;
// return true;
// } else {
// return false;
// }
// }
void _updateStock(dynamic stock) { void _updateStock(dynamic stock) {
_selectedStock = stock; _selectedStock = stock;
_getQuantity(_selectedStock); _getQuantity(_selectedStock);
@ -98,9 +112,26 @@ class _DeleteStockPageState extends State<DeleteStockPage> {
} }
} }
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 @override
void initState() { void initState() {
_getStocks(); autoRun();
super.initState(); super.initState();
} }

View file

@ -1,6 +1,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:gap/gap.dart'; import 'package:gap/gap.dart';
import 'package:intl/intl.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/tables/stocks.dart';
import 'package:pharmacy_mobile/widgets/datatable_widget.dart'; import 'package:pharmacy_mobile/widgets/datatable_widget.dart';
import 'package:pharmacy_mobile/widgets/page_background_widget.dart'; import 'package:pharmacy_mobile/widgets/page_background_widget.dart';
@ -63,7 +64,14 @@ class _ListStocksPageState extends State<ListStocksPage> {
setState(() { setState(() {
_isLoading = true; _isLoading = true;
}); });
_stockList = await _stocks.getList();
final cache = await getListCache(context, 'stock');
if (cache.isNotEmpty) {
_stockList = cache;
} else {
_stockList = await _stocks.getList();
}
setState(() { setState(() {
_isLoading = false; _isLoading = false;

View file

@ -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/genericlist/functions/cache_setgenericlist.dart';
import 'package:pharmacy_mobile/blocs/caches/manufacturerlist/functions/cache_setmanufacturerlist.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/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/blocs/caches/typelist/functions/cache_settypelist.dart';
import 'package:pharmacy_mobile/tables/ref_categories.dart'; import 'package:pharmacy_mobile/tables/ref_categories.dart';
import 'package:pharmacy_mobile/tables/ref_generic_names.dart'; import 'package:pharmacy_mobile/tables/ref_generic_names.dart';
import 'package:pharmacy_mobile/tables/ref_manufacturers.dart'; import 'package:pharmacy_mobile/tables/ref_manufacturers.dart';
import 'package:pharmacy_mobile/tables/ref_medicines.dart'; import 'package:pharmacy_mobile/tables/ref_medicines.dart';
import 'package:pharmacy_mobile/tables/ref_types.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/buttonwithprogress_widget.dart';
import 'package:pharmacy_mobile/widgets/menu_widget.dart'; import 'package:pharmacy_mobile/widgets/menu_widget.dart';
import 'package:pharmacy_mobile/widgets/page_background_widget.dart'; import 'package:pharmacy_mobile/widgets/page_background_widget.dart';
@ -37,6 +39,7 @@ class _MainPageState extends State<MainPage> {
final _refTypes = RefTypes(); final _refTypes = RefTypes();
final _refManufacturers = RefManufacturers(); final _refManufacturers = RefManufacturers();
final _refMedicines = RefMedicines(); final _refMedicines = RefMedicines();
final _stocks = Stocks();
late bool _isLoading = false; late bool _isLoading = false;
@ -130,12 +133,23 @@ class _MainPageState extends State<MainPage> {
} }
} }
Future<void> _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 { void autoRun() async {
await _getCategoryListCache(); await _getCategoryListCache();
await _getGenericListCache(); await _getGenericListCache();
await _getTypeListCache(); await _getTypeListCache();
await _getManufacturerListCache(); await _getManufacturerListCache();
await _getMedicineListCache(); await _getMedicineListCache();
await _getStockListCache();
} }
@override @override