import 'package:diameter/components/detail.dart'; import 'package:diameter/components/dialogs.dart'; import 'package:diameter/components/forms.dart'; import 'package:diameter/config.dart'; import 'package:diameter/models/accuracy.dart'; import 'package:diameter/models/meal.dart'; import 'package:diameter/models/meal_category.dart'; import 'package:diameter/models/meal_portion_type.dart'; import 'package:diameter/models/meal_source.dart'; import 'package:diameter/navigation.dart'; import 'package:diameter/settings.dart'; import 'package:flutter/material.dart'; class MealDetailScreen extends StatefulWidget { static const String routeName = '/meal'; final Meal? meal; const MealDetailScreen({Key? key, this.meal}) : super(key: key); @override _MealDetailScreenState createState() => _MealDetailScreenState(); } class _MealDetailScreenState extends State { final GlobalKey _mealForm = GlobalKey(); final _valueController = TextEditingController(); final _carbsRatioController = TextEditingController(); final _portionSizeController = TextEditingController(); final _carbsPerPortionController = TextEditingController(); final _delayedBolusRateController = TextEditingController(); final _delayedBolusDurationController = TextEditingController(); final _notesController = TextEditingController(); String? _source; String? _category; String? _portionType; String? _portionSizeAccuracy; String? _carbsRatioAccuracy; late Future> _mealCategories; late Future> _mealPortionTypes; late Future> _mealSources; late Future> _portionSizeAccuracies; late Future> _carbsRatioAccuracies; @override void initState() { super.initState(); if (widget.meal != null) { _valueController.text = widget.meal!.value; _carbsRatioController.text = (widget.meal!.carbsRatio ?? '').toString(); _portionSizeController.text = (widget.meal!.portionSize ?? '').toString(); _carbsPerPortionController.text = (widget.meal!.carbsPerPortion ?? '').toString(); _delayedBolusRateController.text = (widget.meal!.delayedBolusRate ?? '').toString(); _delayedBolusDurationController.text = (widget.meal!.delayedBolusDuration ?? '').toString(); _notesController.text = widget.meal!.notes ?? ''; _source = widget.meal!.source; _category = widget.meal!.category; _portionType = widget.meal!.portionType; _portionSizeAccuracy = widget.meal!.portionSizeAccuracy; _carbsRatioAccuracy = widget.meal!.carbsRatioAccuracy; } _mealCategories = MealCategory.fetchAll(); _mealPortionTypes = MealPortionType.fetchAll(); _mealSources = MealSource.fetchAll(); _portionSizeAccuracies = Accuracy.fetchAllForPortionSize(); _carbsRatioAccuracies = Accuracy.fetchAllForCarbsRatio(); } void handleSaveAction() async { if (_mealForm.currentState!.validate()) { bool isNew = widget.meal == null; isNew ? await Meal.save( value: _valueController.text, source: _source, category: _category, portionType: _portionType, carbsRatio: double.tryParse(_carbsRatioController.text), portionSize: double.tryParse(_portionSizeController.text), carbsPerPortion: double.tryParse(_carbsPerPortionController.text), portionSizeAccuracy: _portionSizeAccuracy, carbsRatioAccuracy: _carbsRatioAccuracy, delayedBolusDuration: int.tryParse(_delayedBolusDurationController.text), delayedBolusRate: double.tryParse(_delayedBolusRateController.text), notes: _notesController.text, ) : await Meal.update( widget.meal!.objectId!, value: _valueController.text, source: _source, category: _category, portionType: _portionType, carbsRatio: double.tryParse(_carbsRatioController.text), portionSize: double.tryParse(_portionSizeController.text), carbsPerPortion: double.tryParse(_carbsPerPortionController.text), portionSizeAccuracy: _portionSizeAccuracy, carbsRatioAccuracy: _carbsRatioAccuracy, delayedBolusDuration: int.tryParse(_delayedBolusDurationController.text), delayedBolusRate: double.tryParse(_delayedBolusRateController.text), notes: _notesController.text, ); Navigator.pop(context, '${isNew ? 'New' : ''} Meal Saved'); } } void handleCancelAction() { bool isNew = widget.meal == null; if (showConfirmationDialogOnCancel && ((isNew && (_valueController.text != '' || _source != null || _category != null || _portionType != null || double.tryParse(_carbsRatioController.text) != null || double.tryParse(_portionSizeController.text) != null || double.tryParse(_carbsPerPortionController.text) != null || _carbsRatioAccuracy != null || _portionSizeAccuracy != null || int.tryParse(_delayedBolusDurationController.text) != null || double.tryParse(_delayedBolusRateController.text) != null || _notesController.text != '')) || (!isNew && (_valueController.text != widget.meal!.value || _source != widget.meal!.source || _category != widget.meal!.category || _portionType != widget.meal!.portionType || double.tryParse(_carbsRatioController.text) != widget.meal!.carbsRatio || double.tryParse(_portionSizeController.text) != widget.meal!.portionSize || double.tryParse(_carbsPerPortionController.text) != widget.meal!.carbsPerPortion || _carbsRatioAccuracy != widget.meal!.carbsRatioAccuracy || _portionSizeAccuracy != widget.meal!.portionSizeAccuracy || int.tryParse(_delayedBolusDurationController.text) != widget.meal!.delayedBolusDuration || double.tryParse(_delayedBolusRateController.text) != widget.meal!.delayedBolusRate || _notesController.text != (widget.meal!.notes ?? ''))))) { Dialogs.showCancelConfirmationDialog( context: context, isNew: isNew, onSave: handleSaveAction, ); } else { Navigator.pop(context); } } @override Widget build(BuildContext context) { bool isNew = widget.meal == null; return Scaffold( appBar: AppBar( title: Text(isNew ? 'New Meal' : widget.meal!.value), ), drawer: const Navigation(currentLocation: MealDetailScreen.routeName), body: SingleChildScrollView( child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ StyledForm( formState: _mealForm, fields: [ TextFormField( controller: _valueController, decoration: const InputDecoration( labelText: 'Name', ), validator: (value) { if (value!.trim().isEmpty) { return 'Empty name'; } return null; }, ), StyledFutureDropdownButton( selectedItem: _source, label: 'Meal Source', items: _mealSources, getItemValue: (item) => item.objectId, renderItem: (item) => Text(item.value), onChanged: (value) { setState(() { _source = value; }); }, ), // TODO: autofill the following fields on selecting a source StyledFutureDropdownButton( selectedItem: _category, label: 'Meal Category', items: _mealCategories, getItemValue: (item) => item.objectId, renderItem: (item) => Text(item.value), onChanged: (value) { setState(() { _category = value; }); }, ), StyledFutureDropdownButton( selectedItem: _portionType, label: 'Meal Portion Type', items: _mealPortionTypes, getItemValue: (item) => item.objectId, renderItem: (item) => Text(item.value), onChanged: (value) { setState(() { _portionType = value; }); }, ), // TODO: if 2 out of the 3 following fields are given, calc 3rd TextFormField( decoration: const InputDecoration( labelText: 'Carbs ratio', suffixText: '%', ), controller: _carbsRatioController, keyboardType: const TextInputType.numberWithOptions(decimal: true), ), TextFormField( decoration: InputDecoration( labelText: 'Portion size', suffixText: nutritionMeasurement == NutritionMeasurement.grams ? 'g' : nutritionMeasurement == NutritionMeasurement.ounces ? 'oz' : '', alignLabelWithHint: true, ), controller: _portionSizeController, keyboardType: const TextInputType.numberWithOptions(decimal: true), ), TextFormField( decoration: InputDecoration( labelText: 'Carbs per portion', suffixText: nutritionMeasurement == NutritionMeasurement.grams ? 'g' : nutritionMeasurement == NutritionMeasurement.ounces ? 'oz' : '', ), controller: _carbsPerPortionController, keyboardType: const TextInputType.numberWithOptions(decimal: true), ), StyledFutureDropdownButton( selectedItem: _carbsRatioAccuracy, label: 'Carbs Ratio Accuracy', items: _carbsRatioAccuracies, getItemValue: (item) => item.objectId, renderItem: (item) => Text(item.value), onChanged: (value) { setState(() { _carbsRatioAccuracy = value; }); }, ), // TODO: display according to time format TextFormField( decoration: const InputDecoration( labelText: 'Delayed Bolus Duration', suffixText: ' min', ), controller: _delayedBolusDurationController, keyboardType: const TextInputType.numberWithOptions(), ), TextFormField( decoration: const InputDecoration( labelText: 'Delayed Bolus Units', suffixText: ' U', ), controller: _delayedBolusRateController, keyboardType: const TextInputType.numberWithOptions(decimal: true), ), // TODO: autofill the following fields on selecting a source StyledFutureDropdownButton( selectedItem: _portionSizeAccuracy, label: 'Portion Size Accuracy', items: _portionSizeAccuracies, getItemValue: (item) => item.objectId, renderItem: (item) => Text(item.value), onChanged: (value) { setState(() { _portionSizeAccuracy = value; }); }, ), TextFormField( controller: _notesController, decoration: const InputDecoration( labelText: 'Notes', alignLabelWithHint: true, ), keyboardType: TextInputType.multiline, ), ], ), ], ), ), bottomNavigationBar: DetailBottomRow( onCancel: handleCancelAction, onSave: handleSaveAction, ), ); } }