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

@ -0,0 +1,14 @@
import 'package:flutter/widgets.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:pharmacy_mobile/blocs/caches/manufacturerlist/manufacturerlist_cache_bloc.dart';
import 'package:pharmacy_mobile/blocs/caches/manufacturerlist/manufacturerlist_cache_event.dart';
Future<List> cacheGetManufacturerList(BuildContext context) async {
try {
final manufacturerListCache = context.read<ManufacturerListBloc>();
manufacturerListCache.add(ManufacturerListCacheGet());
return manufacturerListCache.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/manufacturerlist/manufacturerlist_cache_bloc.dart';
import 'package:pharmacy_mobile/blocs/caches/manufacturerlist/manufacturerlist_cache_event.dart';
Future<bool> cacheSetManufacturerList(BuildContext context, List value) async {
try {
final manufacturerListCache = context.read<ManufacturerListBloc>();
manufacturerListCache.add(ManufacturerListCacheSet(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/manufacturerlist/manufacturerlist_cache_event.dart';
import 'package:pharmacy_mobile/blocs/caches/manufacturerlist/manufacturerlist_cache_state.dart';
class ManufacturerListBloc extends Bloc<ManufacturerListCacheEvent, ManufacturerListCacheState> {
ManufacturerListBloc() : super(ManufacturerListCacheState([])) {
on<ManufacturerListCacheSet>((event, emit) {
emit(ManufacturerListCacheState(event.value));
});
on<ManufacturerListCacheGet>((event, emit) {
emit(state);
});
}
}

View file

@ -0,0 +1,8 @@
abstract class ManufacturerListCacheEvent {}
class ManufacturerListCacheSet extends ManufacturerListCacheEvent {
final List value;
ManufacturerListCacheSet(this.value);
}
class ManufacturerListCacheGet extends ManufacturerListCacheEvent {}

View file

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

View file

@ -3,6 +3,8 @@ 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/caches/manufacturerlist/manufacturerlist_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';
import 'package:pharmacy_mobile/pages/add_category_page.dart';
@ -132,6 +134,12 @@ class MyApp extends StatelessWidget {
BlocProvider(
create: (context) => GenericListBloc(),
),
BlocProvider(
create: (context) => TypeListBloc(),
),
BlocProvider(
create: (context) => ManufacturerListBloc(),
),
],
child: MaterialApp.router(
debugShowCheckedModeBanner: false,

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,14 +106,29 @@ 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();
print('generics: $generics, types: $types, manufacturers: $manufacturers');
if (!generics || !types || !manufacturers) {
if (await InternetConnectionChecker.instance.hasConnection) {
if (!generics) await _getGenerics();
if (!types) await _getTypes();
await _getGenerics();
await _getTypes();
await _getManufacturer();
// final sample = await _refMedicines.getList2();
@ -125,6 +143,9 @@ class _AddMedicinePageState extends State<AddMedicinePage> {
});
}
}
} else {
setState(() {});
}
}
void _updateGeneric(dynamic generic) async {

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,18 +100,26 @@ 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) {}
}
}
void autoRun() async {
await _getCategoryListCache();
await _getGenericListCache();
await _getTypeListCache();
await _getManufacturerListCache();
}
@override