added cache for categories
This commit is contained in:
parent
753c730588
commit
1aa7410e2e
14 changed files with 195 additions and 63 deletions
17
lib/blocs/caches/categorylist/categorylist_cache_bloc.dart
Normal file
17
lib/blocs/caches/categorylist/categorylist_cache_bloc.dart
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:pharmacy_mobile/blocs/caches/categorylist/categorylist_cache_event.dart';
|
||||
import 'package:pharmacy_mobile/blocs/caches/categorylist/categorylist_cache_state.dart';
|
||||
|
||||
class CategoryListBloc extends Bloc<CategoryListCacheEvent, CategoryListCacheState> {
|
||||
CategoryListBloc() : super(CategoryListCacheState([])) {
|
||||
on<CategoryListCacheSet>((event, emit) {
|
||||
emit(CategoryListCacheState(event.value));
|
||||
});
|
||||
on<CategoryListCacheGet>((event, emit) {
|
||||
emit(state);
|
||||
});
|
||||
on<CategoryListCacheCheck>((event, emit) {
|
||||
emit(state);
|
||||
});
|
||||
}
|
||||
}
|
||||
10
lib/blocs/caches/categorylist/categorylist_cache_event.dart
Normal file
10
lib/blocs/caches/categorylist/categorylist_cache_event.dart
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
abstract class CategoryListCacheEvent {}
|
||||
|
||||
class CategoryListCacheSet extends CategoryListCacheEvent {
|
||||
final List value;
|
||||
CategoryListCacheSet(this.value);
|
||||
}
|
||||
|
||||
class CategoryListCacheGet extends CategoryListCacheEvent {}
|
||||
|
||||
class CategoryListCacheCheck extends CategoryListCacheEvent {}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
class CategoryListCacheState {
|
||||
final List value;
|
||||
|
||||
CategoryListCacheState(this.value);
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:pharmacy_mobile/blocs/caches/categorylist/categorylist_cache_bloc.dart';
|
||||
import 'package:pharmacy_mobile/blocs/caches/categorylist/categorylist_cache_event.dart';
|
||||
|
||||
Future<List> cacheGetCategoryList(BuildContext context) async {
|
||||
try {
|
||||
final categoryListCache = context.read<CategoryListBloc>();
|
||||
categoryListCache.add(CategoryListCacheGet());
|
||||
return categoryListCache.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/categorylist/categorylist_cache_bloc.dart';
|
||||
import 'package:pharmacy_mobile/blocs/caches/categorylist/categorylist_cache_event.dart';
|
||||
|
||||
Future<bool> cacheSetCategoryList(BuildContext context, List value) async {
|
||||
try {
|
||||
final categoryListCache = context.read<CategoryListBloc>();
|
||||
categoryListCache.add(CategoryListCacheSet(value));
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
import 'package:flutter/material.dart';
|
||||
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/guest/guest_bloc.dart';
|
||||
import 'package:pharmacy_mobile/blocs/user/user_bloc.dart';
|
||||
import 'package:pharmacy_mobile/pages/add_category_page.dart';
|
||||
|
|
@ -124,6 +125,9 @@ class MyApp extends StatelessWidget {
|
|||
BlocProvider(
|
||||
create: (context) => UserBloc(),
|
||||
),
|
||||
BlocProvider(
|
||||
create: (context) => CategoryListBloc(),
|
||||
),
|
||||
],
|
||||
child: MaterialApp.router(
|
||||
debugShowCheckedModeBanner: false,
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import 'package:gap/gap.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:internet_connection_checker/internet_connection_checker.dart';
|
||||
import 'package:pharmacy_mobile/blocs/caches/categorylist/functions/cache_getcategorylist.dart';
|
||||
import 'package:pharmacy_mobile/functions/checkexisting_function.dart';
|
||||
import 'package:pharmacy_mobile/tables/ref_categories.dart';
|
||||
import 'package:pharmacy_mobile/tables/ref_generic_names.dart';
|
||||
|
|
@ -28,7 +29,8 @@ class _AddGenericsPageState extends State<AddGenericsPage> {
|
|||
final _nameController = TextEditingController();
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
|
||||
bool _isLoading = false;
|
||||
late bool _isLoading = false;
|
||||
late final List _categoryListCache = [];
|
||||
|
||||
late List _categoryList = [];
|
||||
late String _selectedCategory = '';
|
||||
|
|
@ -52,22 +54,34 @@ class _AddGenericsPageState extends State<AddGenericsPage> {
|
|||
}
|
||||
}
|
||||
|
||||
void autoRun() async {
|
||||
if (await InternetConnectionChecker.instance.hasConnection) {
|
||||
_getList();
|
||||
} else {
|
||||
if (mounted) {
|
||||
showNotification(context, 'Error: No Internet Connection', false);
|
||||
Future<void> _getCategory() async {
|
||||
final categoryListCache = await cacheGetCategoryList(context);
|
||||
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (mounted) {
|
||||
context.push('/main');
|
||||
}
|
||||
});
|
||||
if (categoryListCache.isNotEmpty) {
|
||||
setState(() {
|
||||
_categoryList = categoryListCache;
|
||||
});
|
||||
} else {
|
||||
if (await InternetConnectionChecker.instance.hasConnection) {
|
||||
_getList();
|
||||
} else {
|
||||
if (mounted) {
|
||||
showNotification(context, 'Error: No Internet Connection', false);
|
||||
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (mounted) {
|
||||
context.push('/main');
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void autoRun() async {
|
||||
_getCategory();
|
||||
}
|
||||
|
||||
void _updateCategory(dynamic category) {
|
||||
_selectedCategory = category;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ import 'package:gap/gap.dart';
|
|||
import 'package:go_router/go_router.dart';
|
||||
import 'package:internet_connection_checker/internet_connection_checker.dart';
|
||||
import 'package:pharmacy_mobile/functions/checkexisting_function.dart';
|
||||
import 'package:pharmacy_mobile/tables/ref_manufactorers.dart';
|
||||
import 'package:pharmacy_mobile/tables/ref_manufacturers.dart';
|
||||
import 'package:pharmacy_mobile/widgets/buttonwithprogress_widget.dart';
|
||||
import 'package:pharmacy_mobile/widgets/form_border_widget2.dart';
|
||||
import 'package:pharmacy_mobile/widgets/input_form_widget.dart';
|
||||
|
|
@ -16,33 +16,33 @@ class AddManufacturerPage extends StatefulWidget {
|
|||
const AddManufacturerPage({super.key});
|
||||
|
||||
@override
|
||||
State<AddManufacturerPage> createState() => _AddManufactorerPageState();
|
||||
State<AddManufacturerPage> createState() => _AddManufacturerPageState();
|
||||
}
|
||||
|
||||
class _AddManufactorerPageState extends State<AddManufacturerPage> {
|
||||
class _AddManufacturerPageState extends State<AddManufacturerPage> {
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
final _nameController = TextEditingController();
|
||||
final _addressController = TextEditingController();
|
||||
final _refManufactorers = RefManufactorers();
|
||||
final _refManufacturers = RefManufacturers();
|
||||
|
||||
late bool _isLoading = false;
|
||||
|
||||
void _saveManufactorer() async {
|
||||
void _saveManufacturer() async {
|
||||
setState(() => _isLoading = true);
|
||||
|
||||
try {
|
||||
if (await InternetConnectionChecker.instance.hasConnection) {
|
||||
final existing = await checkExisting(_refManufactorers, _nameController);
|
||||
final existing = await checkExisting(_refManufacturers, _nameController);
|
||||
|
||||
if (existing && mounted) {
|
||||
showNotification(context, 'Manufactorer already listed', false);
|
||||
showNotification(context, 'Manufacturer already listed', false);
|
||||
return;
|
||||
}
|
||||
|
||||
final post = await _refManufactorers.postManufactorer(_nameController.text, _addressController.text);
|
||||
final post = await _refManufacturers.postManufacturer(_nameController.text, _addressController.text);
|
||||
|
||||
if (post && mounted) {
|
||||
showNotification(context, 'Manufactorer added to list', true);
|
||||
showNotification(context, 'Manufacturer added to list', true);
|
||||
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (mounted) {
|
||||
|
|
@ -103,12 +103,12 @@ class _AddManufactorerPageState extends State<AddManufacturerPage> {
|
|||
// if (_isLoading)
|
||||
// const Center(child: CircularProgressIndicator(color: Colors.white))
|
||||
// else
|
||||
// ButtonWidget(text: 'Save Manufactorer', onPressed: _saveManufactorer)
|
||||
// ButtonWidget(text: 'Save Manufacturer', onPressed: _saveManufacturer)
|
||||
ButtonWithProgressWidget(
|
||||
trigger: _isLoading,
|
||||
progressText: 'Adding Manufacturer',
|
||||
buttonText: 'Save',
|
||||
onPressed: _saveManufactorer)
|
||||
onPressed: _saveManufacturer)
|
||||
],
|
||||
)))
|
||||
],
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ 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';
|
||||
import 'package:pharmacy_mobile/tables/ref_generic_names.dart';
|
||||
import 'package:pharmacy_mobile/tables/ref_manufactorers.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/storage.dart';
|
||||
|
|
@ -39,7 +39,7 @@ class _AddMedicinePageState extends State<AddMedicinePage> {
|
|||
final _refGenericNames = RefGenericNames();
|
||||
final _refCategories = RefCategories();
|
||||
final _refTypes = RefTypes();
|
||||
final _refManufactorer = RefManufactorers();
|
||||
final _refManufacturer = RefManufacturers();
|
||||
final _refMedicines = RefMedicines();
|
||||
final _storage = Storage();
|
||||
final _nameController = TextEditingController();
|
||||
|
|
@ -51,8 +51,8 @@ class _AddMedicinePageState extends State<AddMedicinePage> {
|
|||
late String _selectedCategory = '';
|
||||
late List _typeList = [];
|
||||
late String _selectedType = '';
|
||||
late List _manufactorerList = [];
|
||||
late String _selectedManufactorer = '';
|
||||
late List _manufacturerList = [];
|
||||
late String _selectedManufacturer = '';
|
||||
late String uuid = '';
|
||||
late bool imageUploaded = false;
|
||||
late String imageUrl = '';
|
||||
|
|
@ -71,10 +71,10 @@ class _AddMedicinePageState extends State<AddMedicinePage> {
|
|||
});
|
||||
}
|
||||
|
||||
Future<void> _getManufactorer() async {
|
||||
_manufactorerList = await _refManufactorer.getList();
|
||||
Future<void> _getManufacturer() async {
|
||||
_manufacturerList = await _refManufacturer.getList();
|
||||
setState(() {
|
||||
checkResult(context, _manufactorerList, 'Manufactorer');
|
||||
checkResult(context, _manufacturerList, 'Manufacturer');
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -82,7 +82,7 @@ class _AddMedicinePageState extends State<AddMedicinePage> {
|
|||
if (await InternetConnectionChecker.instance.hasConnection) {
|
||||
await _getGenerics();
|
||||
await _getTypes();
|
||||
await _getManufactorer();
|
||||
await _getManufacturer();
|
||||
|
||||
// final sample = await _refMedicines.getList2();
|
||||
} else {
|
||||
|
|
@ -110,8 +110,8 @@ class _AddMedicinePageState extends State<AddMedicinePage> {
|
|||
_selectedType = type;
|
||||
}
|
||||
|
||||
void _updateManufactorer(dynamic manufactorer) {
|
||||
_selectedManufactorer = manufactorer;
|
||||
void _updateManufacturer(dynamic manufacturer) {
|
||||
_selectedManufacturer = manufacturer;
|
||||
}
|
||||
|
||||
Future<void> _scanBarcode() async {
|
||||
|
|
@ -142,10 +142,10 @@ class _AddMedicinePageState extends State<AddMedicinePage> {
|
|||
final medName = _nameController.text;
|
||||
final medGenericUUID = await _refGenericNames.getUUID(_selectedGeneric);
|
||||
final medTypeUUID = await _refTypes.getUUID(_selectedType);
|
||||
final medManufactorerUUID = await _refManufactorer.getUUID(_selectedManufactorer);
|
||||
final medManufacturerUUID = await _refManufacturer.getUUID(_selectedManufacturer);
|
||||
|
||||
await _refMedicines.postMedicine(
|
||||
uuid, medName, medManufactorerUUID, medGenericUUID, medTypeUUID, encrpytedBarcode);
|
||||
uuid, medName, medManufacturerUUID, medGenericUUID, medTypeUUID, encrpytedBarcode);
|
||||
} else {
|
||||
if (mounted) {
|
||||
showNotification(context, 'Error: No Internet Connection', false);
|
||||
|
|
@ -177,13 +177,19 @@ class _AddMedicinePageState extends State<AddMedicinePage> {
|
|||
uploaded = await _storage.uploadImage(context, storageName, webpImage, '$imageName.webp');
|
||||
}
|
||||
|
||||
setState(() {
|
||||
if (!uploaded) {
|
||||
if (mounted) {
|
||||
showNotification(context, 'Image Upload failed, try again.', false);
|
||||
}
|
||||
if (!uploaded) {
|
||||
if (mounted) {
|
||||
showNotification(context, 'Image Upload failed, try again.', false);
|
||||
return;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (imageName.isNotEmpty) {
|
||||
final url = await _getImageUrl('$imageName.webp');
|
||||
setState(() {
|
||||
imageUrl = url;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Future<Uint8List> _webpConvert(Uint8List file) async {
|
||||
|
|
@ -197,6 +203,11 @@ class _AddMedicinePageState extends State<AddMedicinePage> {
|
|||
return result;
|
||||
}
|
||||
|
||||
Future<String> _getImageUrl(String imageName) async {
|
||||
final image = await _storage.getImageURL('ref_medicines_images', imageName);
|
||||
return image;
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
autoRun();
|
||||
|
|
@ -213,8 +224,8 @@ class _AddMedicinePageState extends State<AddMedicinePage> {
|
|||
_selectedCategory = '';
|
||||
_typeList = [];
|
||||
_selectedType = '';
|
||||
_manufactorerList = [];
|
||||
_selectedManufactorer = '';
|
||||
_manufacturerList = [];
|
||||
_selectedManufacturer = '';
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
|
|
@ -265,10 +276,10 @@ class _AddMedicinePageState extends State<AddMedicinePage> {
|
|||
),
|
||||
const Gap(16),
|
||||
DropDownWidget(
|
||||
label: 'Manufactorer',
|
||||
list: _manufactorerList,
|
||||
listTitle: 'manufactorer_name',
|
||||
onChanged: _updateManufactorer,
|
||||
label: 'Manufacturer',
|
||||
list: _manufacturerList,
|
||||
listTitle: 'manufacturer_name',
|
||||
onChanged: _updateManufacturer,
|
||||
),
|
||||
const Gap(16),
|
||||
InputFormWidget(label: 'Barcode', controller: _barcodeController),
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ class _CustomerSearchPageState extends State<CustomerSearchPage> {
|
|||
// late final List _medicinesList = [];
|
||||
|
||||
Future<void> _getURL() async {
|
||||
final image = await _storage.getPublicURL('ref_medicines_images', 'a3e430fe-86c1-4d46-9c6a-aed2dae57fef.webp');
|
||||
final image = await _storage.getImageURL('ref_medicines_images', 'a3e430fe-86c1-4d46-9c6a-aed2dae57fef.webp');
|
||||
setState(() {
|
||||
imageUrl = image;
|
||||
});
|
||||
|
|
|
|||
|
|
@ -3,6 +3,9 @@ import 'package:font_awesome_flutter/font_awesome_flutter.dart';
|
|||
import 'package:gap/gap.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:pharmacy_mobile/auth/auth_service.dart';
|
||||
import 'package:pharmacy_mobile/blocs/caches/categorylist/functions/cache_getcategorylist.dart';
|
||||
import 'package:pharmacy_mobile/blocs/caches/categorylist/functions/cache_setcategorylist.dart';
|
||||
import 'package:pharmacy_mobile/tables/ref_categories.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';
|
||||
|
|
@ -20,6 +23,7 @@ class MainPage extends StatefulWidget {
|
|||
|
||||
class _MainPageState extends State<MainPage> {
|
||||
final _authService = AuthService();
|
||||
final _refCategories = RefCategories();
|
||||
|
||||
late bool _isLoading = false;
|
||||
|
||||
|
|
@ -45,6 +49,50 @@ class _MainPageState extends State<MainPage> {
|
|||
}
|
||||
}
|
||||
|
||||
// void _getList() async {
|
||||
// _categoryList = await _refCategories.getList();
|
||||
|
||||
// if (_categoryList.isEmpty) {
|
||||
// if (mounted) {
|
||||
// showNotification(context, 'Error: No Categories Found', false);
|
||||
|
||||
// WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
// if (mounted) {
|
||||
// context.push('/main');
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
// } else {
|
||||
// setState(() => {});
|
||||
// }
|
||||
// }
|
||||
|
||||
Future<void> _getCategoryListCache() async {
|
||||
final categoryList = await _refCategories.getList();
|
||||
|
||||
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);
|
||||
} else {
|
||||
print('Caching Success');
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void autoRun() async {
|
||||
await _getCategoryListCache();
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
autoRun();
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return PopScope(
|
||||
|
|
|
|||
|
|
@ -1,12 +1,7 @@
|
|||
import 'dart:developer';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:gap/gap.dart';
|
||||
import 'package:internet_connection_checker/internet_connection_checker.dart';
|
||||
import 'package:pharmacy_mobile/auth/auth_service.dart';
|
||||
import 'package:pharmacy_mobile/blocs/guest/guest_bloc.dart';
|
||||
import 'package:pharmacy_mobile/blocs/guest/guest_event.dart';
|
||||
import 'package:pharmacy_mobile/widgets/buttonwithprogress_widget.dart';
|
||||
import 'package:pharmacy_mobile/widgets/glossy_container_widget.dart';
|
||||
import 'package:pharmacy_mobile/widgets/input_form_widget.dart';
|
||||
|
|
|
|||
|
|
@ -1,15 +1,15 @@
|
|||
import 'package:supabase_flutter/supabase_flutter.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
class RefManufactorers {
|
||||
class RefManufacturers {
|
||||
final SupabaseClient _supabase = Supabase.instance.client;
|
||||
|
||||
Future<List> getList() async {
|
||||
try {
|
||||
final data = await _supabase
|
||||
.from('ref_manufactorers')
|
||||
.select('manufactorer_name')
|
||||
.order('manufactorer_name', ascending: true);
|
||||
.from('ref_manufacturers')
|
||||
.select('manufacturer_name')
|
||||
.order('manufacturer_name', ascending: true);
|
||||
return data.toList();
|
||||
} catch (e) {
|
||||
return [];
|
||||
|
|
@ -19,20 +19,20 @@ class RefManufactorers {
|
|||
Future<String> getUUID(String name) async {
|
||||
try {
|
||||
final data =
|
||||
await _supabase.from('ref_manufactorers').select('ref_manufactorers_uuid').eq('manufactorer_name', name);
|
||||
return data.first['ref_manufactorers_uuid'].toString();
|
||||
await _supabase.from('ref_manufacturers').select('ref_manufacturers_uuid').eq('manufacturer_name', name);
|
||||
return data.first['ref_manufacturers_uuid'].toString();
|
||||
} catch (e) {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> postManufactorer(String name, String address) async {
|
||||
Future<bool> postManufacturer(String name, String address) async {
|
||||
try {
|
||||
final genericUUID = Uuid().v4();
|
||||
|
||||
await _supabase
|
||||
.from('ref_manufactorers')
|
||||
.insert({'ref_manufactorers_uuid': genericUUID, 'manufactorer_name': name, 'manufactorer_address': address});
|
||||
.from('ref_manufacturers')
|
||||
.insert({'ref_manufacturers_uuid': genericUUID, 'manufacturer_name': name, 'manufacturer_address': address});
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
|
|
@ -36,7 +36,7 @@ class Storage {
|
|||
}
|
||||
}
|
||||
|
||||
Future<String> getPublicURL(String storage, String name) async {
|
||||
Future<String> getImageURL(String storage, String name) async {
|
||||
try {
|
||||
final String file = _supabase.storage.from(storage).getPublicUrl(name);
|
||||
return file;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue