add cache for medicines
This commit is contained in:
parent
518415aa4e
commit
92df0af895
12 changed files with 110 additions and 14 deletions
|
|
@ -0,0 +1,14 @@
|
||||||
|
import 'package:flutter/widgets.dart';
|
||||||
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
|
import 'package:pharmacy_mobile/blocs/caches/medicinelist/medicinelist_cache_bloc.dart';
|
||||||
|
import 'package:pharmacy_mobile/blocs/caches/medicinelist/medicinelist_cache_event.dart';
|
||||||
|
|
||||||
|
Future<List> cacheGetMedicineList(BuildContext context) async {
|
||||||
|
try {
|
||||||
|
final medicineListCache = context.read<MedicineListBloc>();
|
||||||
|
medicineListCache.add(MedicineListCacheGet());
|
||||||
|
return medicineListCache.state.value;
|
||||||
|
} catch (e) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,14 @@
|
||||||
|
import 'package:flutter/widgets.dart';
|
||||||
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
|
import 'package:pharmacy_mobile/blocs/caches/medicinelist/medicinelist_cache_bloc.dart';
|
||||||
|
import 'package:pharmacy_mobile/blocs/caches/medicinelist/medicinelist_cache_event.dart';
|
||||||
|
|
||||||
|
Future<bool> cacheSetMedicineList(BuildContext context, List value) async {
|
||||||
|
try {
|
||||||
|
final medicineListCache = context.read<MedicineListBloc>();
|
||||||
|
medicineListCache.add(MedicineListCacheSet(value));
|
||||||
|
return true;
|
||||||
|
} catch (e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
14
lib/blocs/caches/medicinelist/medicinelist_cache_bloc.dart
Normal file
14
lib/blocs/caches/medicinelist/medicinelist_cache_bloc.dart
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
|
import 'package:pharmacy_mobile/blocs/caches/medicinelist/medicinelist_cache_event.dart';
|
||||||
|
import 'package:pharmacy_mobile/blocs/caches/medicinelist/medicinelist_cache_state.dart';
|
||||||
|
|
||||||
|
class MedicineListBloc extends Bloc<MedicineListCacheEvent, MedicineListCacheState> {
|
||||||
|
MedicineListBloc() : super(MedicineListCacheState([])) {
|
||||||
|
on<MedicineListCacheSet>((event, emit) {
|
||||||
|
emit(MedicineListCacheState(event.value));
|
||||||
|
});
|
||||||
|
on<MedicineListCacheGet>((event, emit) {
|
||||||
|
emit(state);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,8 @@
|
||||||
|
abstract class MedicineListCacheEvent {}
|
||||||
|
|
||||||
|
class MedicineListCacheSet extends MedicineListCacheEvent {
|
||||||
|
final List value;
|
||||||
|
MedicineListCacheSet(this.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
class MedicineListCacheGet extends MedicineListCacheEvent {}
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
class MedicineListCacheState {
|
||||||
|
final List value;
|
||||||
|
|
||||||
|
MedicineListCacheState(this.value);
|
||||||
|
}
|
||||||
|
|
@ -9,6 +9,7 @@ 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 [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ 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/categorylist/categorylist_cache_bloc.dart';
|
||||||
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/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';
|
||||||
|
|
@ -140,6 +141,9 @@ class MyApp extends StatelessWidget {
|
||||||
BlocProvider(
|
BlocProvider(
|
||||||
create: (context) => ManufacturerListBloc(),
|
create: (context) => ManufacturerListBloc(),
|
||||||
),
|
),
|
||||||
|
BlocProvider(
|
||||||
|
create: (context) => MedicineListBloc(),
|
||||||
|
),
|
||||||
],
|
],
|
||||||
child: MaterialApp.router(
|
child: MaterialApp.router(
|
||||||
debugShowCheckedModeBanner: false,
|
debugShowCheckedModeBanner: false,
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,10 @@
|
||||||
|
import 'dart:io';
|
||||||
import 'dart:typed_data';
|
import 'dart:typed_data';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:gap/gap.dart';
|
import 'package:gap/gap.dart';
|
||||||
import 'package:image_picker/image_picker.dart';
|
import 'package:image_picker/image_picker.dart';
|
||||||
import 'package:internet_connection_checker/internet_connection_checker.dart';
|
import 'package:internet_connection_checker/internet_connection_checker.dart';
|
||||||
|
import 'package:path_provider/path_provider.dart';
|
||||||
import 'package:pharmacy_mobile/blocs/caches/genericlist/functions/cache_getgenericlist.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/manufacturerlist/functions/cache_getmanufacturerlist.dart';
|
||||||
import 'package:pharmacy_mobile/blocs/caches/typelist/functions/cache_gettypelist.dart';
|
import 'package:pharmacy_mobile/blocs/caches/typelist/functions/cache_gettypelist.dart';
|
||||||
|
|
@ -123,15 +125,11 @@ class _AddMedicinePageState extends State<AddMedicinePage> {
|
||||||
final types = await _getTypesCache();
|
final types = await _getTypesCache();
|
||||||
final manufacturers = await _getManufacturerCache();
|
final manufacturers = await _getManufacturerCache();
|
||||||
|
|
||||||
print('generics: $generics, types: $types, manufacturers: $manufacturers');
|
|
||||||
|
|
||||||
if (!generics || !types || !manufacturers) {
|
if (!generics || !types || !manufacturers) {
|
||||||
if (await InternetConnectionChecker.instance.hasConnection) {
|
if (await InternetConnectionChecker.instance.hasConnection) {
|
||||||
await _getGenerics();
|
await _getGenerics();
|
||||||
await _getTypes();
|
await _getTypes();
|
||||||
await _getManufacturer();
|
await _getManufacturer();
|
||||||
|
|
||||||
// final sample = await _refMedicines.getList2();
|
|
||||||
} else {
|
} else {
|
||||||
if (mounted) {
|
if (mounted) {
|
||||||
showNotification(context, 'Error: No Internet Connection', false);
|
showNotification(context, 'Error: No Internet Connection', false);
|
||||||
|
|
|
||||||
|
|
@ -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:internet_connection_checker/internet_connection_checker.dart';
|
import 'package:internet_connection_checker/internet_connection_checker.dart';
|
||||||
|
import 'package:pharmacy_mobile/blocs/caches/medicinelist/functions/cache_getmedicinelist.dart';
|
||||||
import 'package:pharmacy_mobile/functions/barcode_scan_function.dart';
|
import 'package:pharmacy_mobile/functions/barcode_scan_function.dart';
|
||||||
import 'package:pharmacy_mobile/functions/checkresult_function.dart';
|
import 'package:pharmacy_mobile/functions/checkresult_function.dart';
|
||||||
import 'package:pharmacy_mobile/tables/ref_medicines.dart';
|
import 'package:pharmacy_mobile/tables/ref_medicines.dart';
|
||||||
|
|
@ -49,17 +50,37 @@ class _AddStockPageState extends State<AddStockPage> with WidgetsBindingObserver
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void autoRun() async {
|
Future<bool> _getMedicinesCache() async {
|
||||||
if (await InternetConnectionChecker.instance.hasConnection) {
|
final cache = await cacheGetMedicineList(context);
|
||||||
_getMedicines();
|
print('cache: $cache');
|
||||||
} else {
|
|
||||||
if (mounted) {
|
|
||||||
showNotification(context, 'Error: No Internet Connection', false);
|
|
||||||
|
|
||||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
if (cache.isNotEmpty) {
|
||||||
context.push('/main');
|
_medicineList = cache;
|
||||||
});
|
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void autoRun() async {
|
||||||
|
final medicines = await _getMedicinesCache();
|
||||||
|
print('medicines: $medicines');
|
||||||
|
|
||||||
|
if (!medicines) {
|
||||||
|
if (await InternetConnectionChecker.instance.hasConnection) {
|
||||||
|
_getMedicines();
|
||||||
|
} else {
|
||||||
|
if (mounted) {
|
||||||
|
showNotification(context, 'Error: No Internet Connection', false);
|
||||||
|
|
||||||
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
|
context.push('/main');
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
setState(() {});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
import 'dart:math';
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
|
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
|
||||||
import 'package:gap/gap.dart';
|
import 'package:gap/gap.dart';
|
||||||
|
|
@ -6,10 +8,12 @@ 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/categorylist/functions/cache_setcategorylist.dart';
|
||||||
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/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_types.dart';
|
import 'package:pharmacy_mobile/tables/ref_types.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';
|
||||||
|
|
@ -32,6 +36,7 @@ class _MainPageState extends State<MainPage> {
|
||||||
final _refGenericNames = RefGenericNames();
|
final _refGenericNames = RefGenericNames();
|
||||||
final _refTypes = RefTypes();
|
final _refTypes = RefTypes();
|
||||||
final _refManufacturers = RefManufacturers();
|
final _refManufacturers = RefManufacturers();
|
||||||
|
final _refMedicines = RefMedicines();
|
||||||
|
|
||||||
late bool _isLoading = false;
|
late bool _isLoading = false;
|
||||||
|
|
||||||
|
|
@ -115,11 +120,22 @@ class _MainPageState extends State<MainPage> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> _getMedicineListCache() async {
|
||||||
|
final medicineList = await _refMedicines.getList();
|
||||||
|
|
||||||
|
if (medicineList.isNotEmpty) {
|
||||||
|
// ignore: use_build_context_synchronously
|
||||||
|
final setCache = await cacheSetMedicineList(context, medicineList);
|
||||||
|
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
|
|
||||||
|
|
@ -665,7 +665,7 @@ packages:
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.1.0"
|
version: "1.1.0"
|
||||||
path_provider:
|
path_provider:
|
||||||
dependency: transitive
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
name: path_provider
|
name: path_provider
|
||||||
sha256: "50c5dd5b6e1aaf6fb3a78b33f6aa3afca52bf903a8a5298f53101fdaee55bbcd"
|
sha256: "50c5dd5b6e1aaf6fb3a78b33f6aa3afca52bf903a8a5298f53101fdaee55bbcd"
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,7 @@ dependencies:
|
||||||
redacted: ^1.0.13
|
redacted: ^1.0.13
|
||||||
flutter_bloc: ^9.1.0
|
flutter_bloc: ^9.1.0
|
||||||
flutter_dotenv: ^5.2.1
|
flutter_dotenv: ^5.2.1
|
||||||
|
path_provider: ^2.1.5
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue