add cache for stocks
This commit is contained in:
parent
92df0af895
commit
de5a7c66a3
11 changed files with 134 additions and 3 deletions
14
lib/blocs/caches/stocklist/functions/cache_getstocklist.dart
Normal file
14
lib/blocs/caches/stocklist/functions/cache_getstocklist.dart
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:pharmacy_mobile/blocs/caches/stocklist/stocklist_cache_bloc.dart';
|
||||
import 'package:pharmacy_mobile/blocs/caches/stocklist/stocklist_cache_event.dart';
|
||||
|
||||
Future<List> cacheGetStockList(BuildContext context) async {
|
||||
try {
|
||||
final stockListCache = context.read<StockListBloc>();
|
||||
stockListCache.add(StockListCacheGet());
|
||||
return stockListCache.state.value;
|
||||
} catch (e) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
14
lib/blocs/caches/stocklist/functions/cache_setstocklist.dart
Normal file
14
lib/blocs/caches/stocklist/functions/cache_setstocklist.dart
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:pharmacy_mobile/blocs/caches/stocklist/stocklist_cache_bloc.dart';
|
||||
import 'package:pharmacy_mobile/blocs/caches/stocklist/stocklist_cache_event.dart';
|
||||
|
||||
Future<bool> cacheSetStockList(BuildContext context, List value) async {
|
||||
try {
|
||||
final stockListCache = context.read<StockListBloc>();
|
||||
stockListCache.add(StockListCacheSet(value));
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
14
lib/blocs/caches/stocklist/stocklist_cache_bloc.dart
Normal file
14
lib/blocs/caches/stocklist/stocklist_cache_bloc.dart
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:pharmacy_mobile/blocs/caches/stocklist/stocklist_cache_event.dart';
|
||||
import 'package:pharmacy_mobile/blocs/caches/stocklist/stocklist_cache_state.dart';
|
||||
|
||||
class StockListBloc extends Bloc<StockListCacheEvent, StockListCacheState> {
|
||||
StockListBloc() : super(StockListCacheState([])) {
|
||||
on<StockListCacheSet>((event, emit) {
|
||||
emit(StockListCacheState(event.value));
|
||||
});
|
||||
on<StockListCacheGet>((event, emit) {
|
||||
emit(state);
|
||||
});
|
||||
}
|
||||
}
|
||||
8
lib/blocs/caches/stocklist/stocklist_cache_event.dart
Normal file
8
lib/blocs/caches/stocklist/stocklist_cache_event.dart
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
abstract class StockListCacheEvent {}
|
||||
|
||||
class StockListCacheSet extends StockListCacheEvent {
|
||||
final List value;
|
||||
StockListCacheSet(this.value);
|
||||
}
|
||||
|
||||
class StockListCacheGet extends StockListCacheEvent {}
|
||||
5
lib/blocs/caches/stocklist/stocklist_cache_state.dart
Normal file
5
lib/blocs/caches/stocklist/stocklist_cache_state.dart
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
class StockListCacheState {
|
||||
final List value;
|
||||
|
||||
StockListCacheState(this.value);
|
||||
}
|
||||
|
|
@ -9,7 +9,6 @@ Future<List> cacheGetTypeList(BuildContext context) async {
|
|||
typeListCache.add(TypeListCacheGet());
|
||||
return typeListCache.state.value;
|
||||
} catch (e) {
|
||||
print(e);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
20
lib/functions/getlist_cache_function.dart
Normal file
20
lib/functions/getlist_cache_function.dart
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import 'package:flutter/widgets.dart';
|
||||
import 'package:pharmacy_mobile/blocs/caches/stocklist/functions/cache_getstocklist.dart';
|
||||
|
||||
Future<List> getListCache(BuildContext context, String type) async {
|
||||
late final List cache;
|
||||
|
||||
switch (type) {
|
||||
case 'stock':
|
||||
cache = await cacheGetStockList(context);
|
||||
break;
|
||||
default:
|
||||
return [];
|
||||
}
|
||||
|
||||
if (cache.isNotEmpty) {
|
||||
return cache;
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
|
@ -5,6 +5,7 @@ import 'package:pharmacy_mobile/blocs/caches/categorylist/categorylist_cache_blo
|
|||
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/stocklist/stocklist_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';
|
||||
|
|
@ -144,6 +145,9 @@ class MyApp extends StatelessWidget {
|
|||
BlocProvider(
|
||||
create: (context) => MedicineListBloc(),
|
||||
),
|
||||
BlocProvider(
|
||||
create: (context) => StockListBloc(),
|
||||
),
|
||||
],
|
||||
child: MaterialApp.router(
|
||||
debugShowCheckedModeBanner: false,
|
||||
|
|
|
|||
|
|
@ -1,6 +1,9 @@
|
|||
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/stocklist/functions/cache_getstocklist.dart';
|
||||
import 'package:pharmacy_mobile/functions/checkresult_function.dart';
|
||||
import 'package:pharmacy_mobile/functions/getlist_cache_function.dart';
|
||||
import 'package:pharmacy_mobile/tables/stocks.dart';
|
||||
import 'package:pharmacy_mobile/widgets/button_widget.dart';
|
||||
import 'package:pharmacy_mobile/widgets/dropdown_widget.dart';
|
||||
|
|
@ -48,6 +51,17 @@ class _DeleteStockPageState extends State<DeleteStockPage> {
|
|||
}
|
||||
}
|
||||
|
||||
// Future<bool> _getStocksCache() async {
|
||||
// final cache = await cacheGetStockList(context);
|
||||
|
||||
// if (cache.isNotEmpty) {
|
||||
// _stockList = cache;
|
||||
// return true;
|
||||
// } else {
|
||||
// return false;
|
||||
// }
|
||||
// }
|
||||
|
||||
void _updateStock(dynamic stock) {
|
||||
_selectedStock = stock;
|
||||
_getQuantity(_selectedStock);
|
||||
|
|
@ -98,9 +112,26 @@ class _DeleteStockPageState extends State<DeleteStockPage> {
|
|||
}
|
||||
}
|
||||
|
||||
void autoRun() async {
|
||||
final cache = await getListCache(context, 'stock');
|
||||
|
||||
if (cache.isEmpty) {
|
||||
if (await InternetConnectionChecker.instance.hasConnection) {
|
||||
_getStocks();
|
||||
} else {
|
||||
// ignore: use_build_context_synchronously
|
||||
showNotification(context, 'Error: No Internet Connection', false);
|
||||
}
|
||||
} else {
|
||||
setState(() {
|
||||
_stockList = cache;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
_getStocks();
|
||||
autoRun();
|
||||
super.initState();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:gap/gap.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:pharmacy_mobile/functions/getlist_cache_function.dart';
|
||||
import 'package:pharmacy_mobile/tables/stocks.dart';
|
||||
import 'package:pharmacy_mobile/widgets/datatable_widget.dart';
|
||||
import 'package:pharmacy_mobile/widgets/page_background_widget.dart';
|
||||
|
|
@ -63,7 +64,14 @@ class _ListStocksPageState extends State<ListStocksPage> {
|
|||
setState(() {
|
||||
_isLoading = true;
|
||||
});
|
||||
_stockList = await _stocks.getList();
|
||||
|
||||
final cache = await getListCache(context, 'stock');
|
||||
|
||||
if (cache.isNotEmpty) {
|
||||
_stockList = cache;
|
||||
} else {
|
||||
_stockList = await _stocks.getList();
|
||||
}
|
||||
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
|
|
|
|||
|
|
@ -9,12 +9,14 @@ 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/blocs/caches/manufacturerlist/functions/cache_setmanufacturerlist.dart';
|
||||
import 'package:pharmacy_mobile/blocs/caches/medicinelist/functions/cache_setmedicinelist.dart';
|
||||
import 'package:pharmacy_mobile/blocs/caches/stocklist/functions/cache_setstocklist.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/tables/stocks.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';
|
||||
|
|
@ -37,6 +39,7 @@ class _MainPageState extends State<MainPage> {
|
|||
final _refTypes = RefTypes();
|
||||
final _refManufacturers = RefManufacturers();
|
||||
final _refMedicines = RefMedicines();
|
||||
final _stocks = Stocks();
|
||||
|
||||
late bool _isLoading = false;
|
||||
|
||||
|
|
@ -130,12 +133,23 @@ class _MainPageState extends State<MainPage> {
|
|||
}
|
||||
}
|
||||
|
||||
Future<void> _getStockListCache() async {
|
||||
final stockList = await _stocks.getList();
|
||||
|
||||
if (stockList.isNotEmpty) {
|
||||
// ignore: use_build_context_synchronously
|
||||
final setCache = await cacheSetStockList(context, stockList);
|
||||
if (!setCache) {}
|
||||
}
|
||||
}
|
||||
|
||||
void autoRun() async {
|
||||
await _getCategoryListCache();
|
||||
await _getGenericListCache();
|
||||
await _getTypeListCache();
|
||||
await _getManufacturerListCache();
|
||||
await _getMedicineListCache();
|
||||
await _getStockListCache();
|
||||
}
|
||||
|
||||
@override
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue