added cache for types

This commit is contained in:
Patrick Alvin Alcala 2025-03-20 15:05:21 +08:00
parent bcf9823ffb
commit 5c4c8ceca9
8 changed files with 109 additions and 9 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/typelist/typelist_cache_bloc.dart';
import 'package:pharmacy_mobile/blocs/caches/typelist/typelist_cache_event.dart';
Future<List> cacheGetTypeList(BuildContext context) async {
try {
final typeListCache = context.read<TypeListBloc>();
typeListCache.add(TypeListCacheGet());
return typeListCache.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/typelist/typelist_cache_bloc.dart';
import 'package:pharmacy_mobile/blocs/caches/typelist/typelist_cache_event.dart';
Future<bool> cacheSetTypeList(BuildContext context, List value) async {
try {
final typeListCache = context.read<TypeListBloc>();
typeListCache.add(TypeListCacheSet(value));
return true;
} catch (e) {
return false;
}
}

View file

@ -0,0 +1,8 @@
import 'package:flutter/widgets.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:pharmacy_mobile/blocs/caches/typelist/typelist_cache_bloc.dart';
void cacheTypeListDispose(BuildContext context) async {
final cache = context.read<TypeListBloc>();
cache.close();
}

View file

@ -0,0 +1,14 @@
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:pharmacy_mobile/blocs/caches/typelist/typelist_cache_event.dart';
import 'package:pharmacy_mobile/blocs/caches/typelist/typelist_cache_state.dart';
class TypeListBloc extends Bloc<TypeListCacheEvent, TypeListCacheState> {
TypeListBloc() : super(TypeListCacheState([])) {
on<TypeListCacheSet>((event, emit) {
emit(TypeListCacheState(event.value));
});
on<TypeListCacheGet>((event, emit) {
emit(state);
});
}
}

View file

@ -0,0 +1,8 @@
abstract class TypeListCacheEvent {}
class TypeListCacheSet extends TypeListCacheEvent {
final List value;
TypeListCacheSet(this.value);
}
class TypeListCacheGet extends TypeListCacheEvent {}

View file

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

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/typelist/functions/cache_gettypelist.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';
@ -59,19 +60,24 @@ class _AddMedicinePageState extends State<AddMedicinePage> {
late String imageUrl = '';
Future<void> _getGenerics() async {
final cache = await cacheGetGenericList(context);
if (cache.isNotEmpty) {
_genericNameList = cache;
} else {
_genericNameList = await _refGenericNames.getList();
}
_genericNameList = await _refGenericNames.getList();
setState(() {
checkResult(context, _genericNameList, 'Generics');
});
}
Future<bool> _getGenericsCache() async {
final cache = await cacheGetGenericList(context);
if (cache.isNotEmpty) {
_genericNameList = cache;
return true;
} else {
return false;
}
}
Future<void> _getTypes() async {
_typeList = await _refTypes.getList();
setState(() {
@ -79,6 +85,17 @@ class _AddMedicinePageState extends State<AddMedicinePage> {
});
}
Future<bool> _getTypesCache() async {
final cache = await cacheGetTypeList(context);
if (cache.isNotEmpty) {
_typeList = cache;
return true;
} else {
return false;
}
}
Future<void> _getManufacturer() async {
_manufacturerList = await _refManufacturer.getList();
setState(() {
@ -87,9 +104,13 @@ class _AddMedicinePageState extends State<AddMedicinePage> {
}
void autoRun() async {
final generics = await _getGenericsCache();
final types = await _getTypesCache();
if (await InternetConnectionChecker.instance.hasConnection) {
await _getGenerics();
await _getTypes();
if (!generics) await _getGenerics();
if (!types) await _getTypes();
await _getManufacturer();
// final sample = await _refMedicines.getList2();

View file

@ -7,6 +7,7 @@ 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/tables/ref_categories.dart';
import 'package:pharmacy_mobile/tables/ref_generic_names.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';
import 'package:pharmacy_mobile/widgets/page_background_widget.dart';
@ -26,6 +27,7 @@ class _MainPageState extends State<MainPage> {
final _authService = AuthService();
final _refCategories = RefCategories();
final _refGenericNames = RefGenericNames();
final _refTypes = RefTypes();
late bool _isLoading = false;
@ -95,9 +97,23 @@ class _MainPageState extends State<MainPage> {
}
}
Future<void> _getTypeListCache() async {
final typeList = await _refTypes.getList();
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);
}
}
}
void autoRun() async {
await _getCategoryListCache();
await _getGenericListCache();
await _getTypeListCache();
}
@override