fix caching on add medicines

This commit is contained in:
Patrick Alvin Alcala 2025-03-21 10:20:28 +08:00
parent 5c4c8ceca9
commit 518415aa4e
8 changed files with 116 additions and 26 deletions

View file

@ -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<AddMedicinePage> {
if (cache.isNotEmpty) {
_genericNameList = cache;
return true;
} else {
return false;
@ -90,6 +92,7 @@ class _AddMedicinePageState extends State<AddMedicinePage> {
if (cache.isNotEmpty) {
_typeList = cache;
return true;
} else {
return false;
@ -103,27 +106,45 @@ class _AddMedicinePageState extends State<AddMedicinePage> {
});
}
Future<bool> _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(() {});
}
}

View file

@ -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<MainPage> {
final _refCategories = RefCategories();
final _refGenericNames = RefGenericNames();
final _refTypes = RefTypes();
final _refManufacturers = RefManufacturers();
late bool _isLoading = false;
@ -77,10 +81,7 @@ class _MainPageState extends State<MainPage> {
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<MainPage> {
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<MainPage> {
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<void> _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<MainPage> {
await _getCategoryListCache();
await _getGenericListCache();
await _getTypeListCache();
await _getManufacturerListCache();
}
@override