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());
|
||||
return typeListCache.state.value;
|
||||
} catch (e) {
|
||||
print(e);
|
||||
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/genericlist/genericlist_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/guest/guest_bloc.dart';
|
||||
import 'package:pharmacy_mobile/blocs/user/user_bloc.dart';
|
||||
|
|
@ -140,6 +141,9 @@ class MyApp extends StatelessWidget {
|
|||
BlocProvider(
|
||||
create: (context) => ManufacturerListBloc(),
|
||||
),
|
||||
BlocProvider(
|
||||
create: (context) => MedicineListBloc(),
|
||||
),
|
||||
],
|
||||
child: MaterialApp.router(
|
||||
debugShowCheckedModeBanner: false,
|
||||
|
|
|
|||
|
|
@ -1,8 +1,10 @@
|
|||
import 'dart:io';
|
||||
import 'dart:typed_data';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:gap/gap.dart';
|
||||
import 'package:image_picker/image_picker.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/manufacturerlist/functions/cache_getmanufacturerlist.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 manufacturers = await _getManufacturerCache();
|
||||
|
||||
print('generics: $generics, types: $types, manufacturers: $manufacturers');
|
||||
|
||||
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);
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:gap/gap.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/checkresult_function.dart';
|
||||
import 'package:pharmacy_mobile/tables/ref_medicines.dart';
|
||||
|
|
@ -49,17 +50,37 @@ class _AddStockPageState extends State<AddStockPage> with WidgetsBindingObserver
|
|||
});
|
||||
}
|
||||
|
||||
void autoRun() async {
|
||||
if (await InternetConnectionChecker.instance.hasConnection) {
|
||||
_getMedicines();
|
||||
} else {
|
||||
if (mounted) {
|
||||
showNotification(context, 'Error: No Internet Connection', false);
|
||||
Future<bool> _getMedicinesCache() async {
|
||||
final cache = await cacheGetMedicineList(context);
|
||||
print('cache: $cache');
|
||||
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
context.push('/main');
|
||||
});
|
||||
if (cache.isNotEmpty) {
|
||||
_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:font_awesome_flutter/font_awesome_flutter.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/genericlist/functions/cache_setgenericlist.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/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_medicines.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';
|
||||
|
|
@ -32,6 +36,7 @@ class _MainPageState extends State<MainPage> {
|
|||
final _refGenericNames = RefGenericNames();
|
||||
final _refTypes = RefTypes();
|
||||
final _refManufacturers = RefManufacturers();
|
||||
final _refMedicines = RefMedicines();
|
||||
|
||||
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 {
|
||||
await _getCategoryListCache();
|
||||
await _getGenericListCache();
|
||||
await _getTypeListCache();
|
||||
await _getManufacturerListCache();
|
||||
await _getMedicineListCache();
|
||||
}
|
||||
|
||||
@override
|
||||
|
|
|
|||
|
|
@ -665,7 +665,7 @@ packages:
|
|||
source: hosted
|
||||
version: "1.1.0"
|
||||
path_provider:
|
||||
dependency: transitive
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: path_provider
|
||||
sha256: "50c5dd5b6e1aaf6fb3a78b33f6aa3afca52bf903a8a5298f53101fdaee55bbcd"
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@ dependencies:
|
|||
redacted: ^1.0.13
|
||||
flutter_bloc: ^9.1.0
|
||||
flutter_dotenv: ^5.2.1
|
||||
path_provider: ^2.1.5
|
||||
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue