diff --git a/lib/pages/add_generics.dart b/lib/pages/add_generics.dart index 9eb391c..e361891 100644 --- a/lib/pages/add_generics.dart +++ b/lib/pages/add_generics.dart @@ -1,3 +1,5 @@ +import 'dart:developer'; + import 'package:gap/gap.dart'; import 'package:flutter/material.dart'; import 'package:internet_connection_checker/internet_connection_checker.dart'; @@ -25,7 +27,7 @@ class _AddGenericsPageState extends State { final _refGenericNames = RefGenericNames(); final _nameController = TextEditingController(); final _formKey = GlobalKey(); - // bool _isVisible = false; + bool _isLoading = false; late List _categoryList = []; @@ -33,12 +35,11 @@ class _AddGenericsPageState extends State { late String _categoryUUID = ''; void _getList() async { - // if (await InternetConnectionChecker.instance.hasConnection) { _categoryList = await _refCategories.getList(); if (_categoryList.isEmpty) { if (mounted) { - showNotification(context, 'Error: Empty', false); + showNotification(context, 'Error: No Categories Found', false); WidgetsBinding.instance.addPostFrameCallback((_) { if (mounted) { @@ -46,23 +47,14 @@ class _AddGenericsPageState extends State { } }); } + } else { + setState(() => {}); } - // } else { - // if (mounted) { - // showNotification(context, 'Error: No Internet Connection', false); - - // WidgetsBinding.instance.addPostFrameCallback((_) { - // if (mounted) { - // context.push('/main'); - // } - // }); - // } - // } } void autoRun() async { if (await InternetConnectionChecker.instance.hasConnection) { - _categoryList = await _refCategories.getList(); + _getList(); } else { if (mounted) { showNotification(context, 'Error: No Internet Connection', false); @@ -114,7 +106,7 @@ class _AddGenericsPageState extends State { @override void initState() { - // autoRun(); + autoRun(); super.initState(); } @@ -148,19 +140,39 @@ class _AddGenericsPageState extends State { children: [ InputWidget(label: 'Name', controller: _nameController), const Gap(16), - GestureDetector( - onTap: _getList, - child: DropDownWidget( - label: 'Category', - list: _categoryList, - listTitle: 'category_name', - onChanged: _updateCategory), - ), - const Gap(32), - if (_isLoading) - Center(child: CircularProgressIndicator(color: Colors.white)) + if (_categoryList.isEmpty) + Column( + children: [ + const Gap(8), + Row( + mainAxisAlignment: MainAxisAlignment.center, + crossAxisAlignment: CrossAxisAlignment.center, + spacing: 16, + children: [ + CircularProgressIndicator(color: Colors.white), + TextWidget( + text: 'Fetching Categories', + size: 16, + ) + ], + ), + ], + ) else - ButtonWidget(text: 'Add', onPressed: saveGeneric) + Column( + children: [ + DropDownWidget( + label: 'Category', + list: _categoryList, + listTitle: 'category_name', + onChanged: _updateCategory), + const Gap(32), + if (_isLoading) + Center(child: CircularProgressIndicator(color: Colors.white)) + else + ButtonWidget(text: 'Add', onPressed: saveGeneric) + ], + ), ], )), ) diff --git a/lib/pages/add_medicine.dart b/lib/pages/add_medicine.dart index aeaeeec..18aa71f 100644 --- a/lib/pages/add_medicine.dart +++ b/lib/pages/add_medicine.dart @@ -1,3 +1,5 @@ +import 'dart:developer'; + import 'package:flutter/material.dart'; import 'package:gap/gap.dart'; import 'package:internet_connection_checker/internet_connection_checker.dart'; @@ -8,13 +10,14 @@ import 'package:pharmacy_mobile/tables/ref_medicines.dart'; import 'package:pharmacy_mobile/tables/ref_types.dart'; import 'package:pharmacy_mobile/widgets/button_widget.dart'; import 'package:pharmacy_mobile/widgets/dropdown_widget.dart'; +import 'package:pharmacy_mobile/widgets/dropdown_wrapper_widget.dart'; import 'package:pharmacy_mobile/widgets/form_border_widget.dart'; import 'package:pharmacy_mobile/widgets/input_widget.dart'; import 'package:pharmacy_mobile/widgets/page_background_widget.dart'; import 'package:pharmacy_mobile/widgets/snackbar_widget.dart'; import 'package:pharmacy_mobile/widgets/text_widget.dart'; import 'package:pharmacy_mobile/widgets/title_widget.dart'; -import 'package:visibility_detector/visibility_detector.dart'; +import 'package:go_router/go_router.dart'; class AddMedicinePage extends StatefulWidget { const AddMedicinePage({super.key}); @@ -25,7 +28,6 @@ class AddMedicinePage extends StatefulWidget { class _AddMedicinePageState extends State { final _formKey = GlobalKey(); - // final _authService = AuthService(); final _refGenericNames = RefGenericNames(); final _refCategories = RefCategories(); final _refTypes = RefTypes(); @@ -33,7 +35,6 @@ class _AddMedicinePageState extends State { final _refMedicines = RefMedicines(); final _nameController = TextEditingController(); final FocusNode _focusNode = FocusNode(); - bool _isVisible = false; bool _isLoading = false; late List _genericNameList = []; @@ -44,14 +45,56 @@ class _AddMedicinePageState extends State { late List _manufactorerList = []; late String _selectedManufactorer = ''; + void _checkResult(List list, String name) { + if (list.isEmpty) { + if (mounted) { + showNotification(context, 'Error: No $name Found', false); + + WidgetsBinding.instance.addPostFrameCallback((_) { + if (mounted) { + context.push('/main'); + } + }); + } + } else { + setState(() => {}); + } + } + + void _getGenerics() async { + _genericNameList = await _refGenericNames.getList(); + _checkResult(_genericNameList, 'Generics'); + } + + void _getTypes() async { + _typeList = await _refTypes.getList(); + _checkResult(_typeList, 'Types'); + } + + void _getManufactorer() async { + _manufactorerList = await _refManufactorer.getList(); + _checkResult(_manufactorerList, 'Manufactorer'); + } + void autoRun() async { if (await InternetConnectionChecker.instance.hasConnection) { - _genericNameList = await _refGenericNames.getList(); - _typeList = await _refTypes.getList(); - _manufactorerList = await _refManufactorer.getList(); + _getGenerics(); + _getTypes(); + _getManufactorer(); + + setState(() {}); + + final sample = await _refMedicines.getList2(); + log(sample.toString()); } else { if (mounted) { showNotification(context, 'Error: No Internet Connection', false); + + WidgetsBinding.instance.addPostFrameCallback((_) { + if (mounted) { + context.push('/main'); + } + }); } } } @@ -82,7 +125,7 @@ class _AddMedicinePageState extends State { final medTypeUUID = await _refTypes.getUUID(_selectedType); final medManufactorerUUID = await _refManufactorer.getUUID(_selectedManufactorer); - await _refMedicines.postMedicine(medName, medGenericUUID, medManufactorerUUID, medTypeUUID); + await _refMedicines.postMedicine(medName, medManufactorerUUID, medGenericUUID, medTypeUUID); } else { if (mounted) { showNotification(context, 'Error: No Internet Connection', false); @@ -121,66 +164,64 @@ class _AddMedicinePageState extends State { @override Widget build(BuildContext context) { return Scaffold( - body: VisibilityDetector( - key: Key('AddMedicinePage'), - onVisibilityChanged: (visibilityInfo) { - if (visibilityInfo.visibleFraction > 0.5 && !_isVisible) { - setState(() { - _isVisible = true; - autoRun(); - }); - } - }, - child: PageBackgroundWidget( - child: Center( - child: Column( - children: [ - const Gap(96), - const TitleWidget(firstTextSize: 20, secondTextSize: 32), - const Gap(32), - const TextWidget(text: 'Add Medicine'), - const Gap(16), - FormBorderWidget( - color: 'green', - child: Form( - key: _formKey, - child: Center( - child: Column( - children: [ - InputWidget(label: 'Name', controller: _nameController), - const Gap(16), - Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - DropDownWidget( - label: 'Generic Name', - list: _genericNameList, - listTitle: 'generic_name', - onChanged: _updateGeneric), - const Gap(8), - TextWidget(text: _selectedCategory, size: 18), - ], - ), - const Gap(16), - DropDownWidget( - label: 'Type', list: _typeList, listTitle: 'type_name', onChanged: _updateType), - const Gap(16), - DropDownWidget( - label: 'Manufactorer', - list: _manufactorerList, - listTitle: 'manufactorer_name', - onChanged: _updateManufactorer), - const Gap(32), - if (_isLoading) - Center(child: CircularProgressIndicator(color: Colors.white)) - else - ButtonWidget(text: 'Save Medicine', onPressed: _saveMedicine) - ], - ), - )), - ) - ], - ), + body: PageBackgroundWidget( + child: Center( + child: Column( + children: [ + const Gap(96), + const TitleWidget(firstTextSize: 20, secondTextSize: 32), + const Gap(32), + const TextWidget(text: 'Add Medicine'), + const Gap(16), + FormBorderWidget( + color: 'green', + child: Form( + key: _formKey, + child: Center( + child: Column( + children: [ + InputWidget(label: 'Name', controller: _nameController), + const Gap(16), + DropdownWrapperWidget( + list: _genericNameList, + text: 'Generics', + widget: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + DropDownWidget( + label: 'Generic Name', + list: _genericNameList, + listTitle: 'generic_name', + onChanged: _updateGeneric), + const Gap(8), + TextWidget(text: _selectedCategory, size: 18), + ], + )), + const Gap(16), + DropdownWrapperWidget( + list: _typeList, + text: 'Types', + widget: DropDownWidget( + label: 'Type', list: _typeList, listTitle: 'type_name', onChanged: _updateType)), + const Gap(16), + DropdownWrapperWidget( + list: _manufactorerList, + text: 'Manufactorers', + widget: DropDownWidget( + label: 'Manufactorer', + list: _manufactorerList, + listTitle: 'manufactorer_name', + onChanged: _updateManufactorer)), + const Gap(32), + if (_isLoading) + Center(child: CircularProgressIndicator(color: Colors.white)) + else + ButtonWidget(text: 'Save Medicine', onPressed: _saveMedicine) + ], + ), + )), + ) + ], ), ), ), diff --git a/lib/pages/add_stock.dart b/lib/pages/add_stock.dart index 65531b5..39f6e64 100644 --- a/lib/pages/add_stock.dart +++ b/lib/pages/add_stock.dart @@ -10,7 +10,6 @@ import 'package:pharmacy_mobile/widgets/input_widget.dart'; import 'package:pharmacy_mobile/widgets/page_background_widget.dart'; import 'package:pharmacy_mobile/widgets/text_widget.dart'; import 'package:pharmacy_mobile/widgets/title_widget.dart'; -import 'package:visibility_detector/visibility_detector.dart'; // Import this package // import 'package:intl/intl.dart'; @@ -32,7 +31,6 @@ class _AddStockPageState extends State { late List _medicineList = []; late String _selectedMedicine = ''; late DateTime selectedDate = DateTime.now(); - bool _isVisible = false; void autoRun() async { _medicineList = await _refMedicines.getList(); @@ -63,61 +61,49 @@ class _AddStockPageState extends State { _selectedMedicine = ''; _quantityController.dispose(); _dateController.dispose(); - _isVisible = false; super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( - body: VisibilityDetector( - key: Key('AddStockPage'), - onVisibilityChanged: (visibilityInfo) { - if (visibilityInfo.visibleFraction > 0.5 && !_isVisible) { - setState(() { - _isVisible = true; - autoRun(); - }); - } - }, - child: PageBackgroundWidget( - child: Center( - child: Column( - children: [ - const Gap(96), - const TitleWidget(firstTextSize: 20, secondTextSize: 32), - const Gap(32), - const TextWidget(text: 'Add Stock'), - const Gap(16), - FormBorderWidget( - color: 'green', - child: Form( - key: _formKey, - child: Center( - child: Column( - children: [ - DropDownWidget( - label: 'Medicine Name', - list: _medicineList, - listTitle: 'medicine_name', - onChanged: _updateMedicine), - const Gap(16), - InputWidget(label: 'Quantity', controller: _quantityController), - const Gap(16), - DatePickerWidget( - label: 'Date Expiration', - controller: _dateController, - value: selectedDate, - ), - const Gap(16), - ButtonWidget(text: 'Add Stock', onPressed: saveStock) - ], - ), + body: PageBackgroundWidget( + child: Center( + child: Column( + children: [ + const Gap(96), + const TitleWidget(firstTextSize: 20, secondTextSize: 32), + const Gap(32), + const TextWidget(text: 'Add Stock'), + const Gap(16), + FormBorderWidget( + color: 'green', + child: Form( + key: _formKey, + child: Center( + child: Column( + children: [ + DropDownWidget( + label: 'Medicine Name', + list: _medicineList, + listTitle: 'medicine_name', + onChanged: _updateMedicine), + const Gap(16), + InputWidget(label: 'Quantity', controller: _quantityController), + const Gap(16), + DatePickerWidget( + label: 'Date Expiration', + controller: _dateController, + value: selectedDate, + ), + const Gap(16), + ButtonWidget(text: 'Add Stock', onPressed: saveStock) + ], ), ), - ) - ], - ), + ), + ) + ], ), ), ), diff --git a/lib/pages/add_type.dart b/lib/pages/add_type.dart index 369137f..e29b792 100644 --- a/lib/pages/add_type.dart +++ b/lib/pages/add_type.dart @@ -81,7 +81,7 @@ class _AddTypePageState extends State { child: Column( children: [ InputWidget(label: 'Type Name', controller: _typeController), - const Gap(16), + const Gap(32), if (_isLoading) Center(child: CircularProgressIndicator(color: Colors.white)) else diff --git a/lib/tables/ref_generic_names.dart b/lib/tables/ref_generic_names.dart index f50d481..c3e2b9c 100644 --- a/lib/tables/ref_generic_names.dart +++ b/lib/tables/ref_generic_names.dart @@ -27,4 +27,20 @@ class RefGenericNames { .from('ref_generic_names') .insert({'ref_generic_names_uuid': genericUUID, 'generic_name': name, 'ref_categories_uuid': uuid}); } + + Future getSample() async { + final data = await _supabase.from('ref_generic_names').select('''generic_name, ref_categories(category_name)'''); + return data.toList(); + } } + + +// let { data: ref_generic_names, error } = await supabase +// .from('ref_generic_names') +// .select(` +// some_column, +// other_table ( +// foreign_key +// ) +// `) + \ No newline at end of file diff --git a/lib/tables/ref_medicines.dart b/lib/tables/ref_medicines.dart index c23033e..9c72169 100644 --- a/lib/tables/ref_medicines.dart +++ b/lib/tables/ref_medicines.dart @@ -9,6 +9,13 @@ class RefMedicines { return data.toList(); } + Future getList2() async { + final data = await _supabase + .from('ref_medicines') + .select('''medicine_name, ref_manufactorers(manufactorer_name)''').order('medicine_name', ascending: true); + return data.toList(); + } + Future getUUID(String name) async { final data = await _supabase.from('ref_medicines').select('ref_medicines_uuid').eq('medicine_name', name); return data.first['ref_medicines_uuid']; diff --git a/lib/widgets/dropdown_wrapper_widget.dart b/lib/widgets/dropdown_wrapper_widget.dart new file mode 100644 index 0000000..911c94b --- /dev/null +++ b/lib/widgets/dropdown_wrapper_widget.dart @@ -0,0 +1,39 @@ +import 'package:flutter/material.dart'; +import 'package:gap/gap.dart'; +import 'package:pharmacy_mobile/widgets/text_widget.dart'; + +class DropdownWrapperWidget extends StatelessWidget { + final List list; + final String text; + final Widget widget; + + const DropdownWrapperWidget({ + super.key, + required this.list, + required this.text, + required this.widget, + }); + + @override + Widget build(BuildContext context) { + return (list.isEmpty) + ? Column( + children: [ + const Gap(8), + Row( + mainAxisAlignment: MainAxisAlignment.center, + crossAxisAlignment: CrossAxisAlignment.center, + spacing: 16, + children: [ + CircularProgressIndicator(color: Colors.white), + TextWidget( + text: 'Fetching $text', + size: 16, + ) + ], + ), + ], + ) + : widget; + } +} diff --git a/pubspec.lock b/pubspec.lock index 9d2d78b..cf6cf13 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -693,14 +693,6 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.4" - visibility_detector: - dependency: "direct main" - description: - name: visibility_detector - sha256: dd5cc11e13494f432d15939c3aa8ae76844c42b723398643ce9addb88a5ed420 - url: "https://pub.dev" - source: hosted - version: "0.4.0+2" vm_service: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 4f75a41..4e22f5c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -18,7 +18,6 @@ dependencies: font_awesome_flutter: ^10.8.0 uuid: ^4.5.1 intl: ^0.20.2 - visibility_detector: ^0.4.0+2 internet_connection_checker: ^3.0.1 dev_dependencies: