446 lines
18 KiB
Dart
446 lines
18 KiB
Dart
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:diameter/utils/utils.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<MealDetailScreen> {
|
|
final GlobalKey<FormState> _mealForm = GlobalKey<FormState>();
|
|
final _valueController = TextEditingController(text: '');
|
|
final _carbsRatioController = TextEditingController(text: '');
|
|
final _portionSizeController = TextEditingController(text: '');
|
|
final _carbsPerPortionController = TextEditingController(text: '');
|
|
final _delayedBolusRateController = TextEditingController(text: '');
|
|
final _delayedBolusDurationController = TextEditingController(text: '');
|
|
final _notesController = TextEditingController(text: '');
|
|
String? _source;
|
|
String? _category;
|
|
String? _portionType;
|
|
String? _portionSizeAccuracy;
|
|
String? _carbsRatioAccuracy;
|
|
|
|
late Future<List<MealCategory>> _mealCategories;
|
|
late Future<List<MealPortionType>> _mealPortionTypes;
|
|
late Future<List<MealSource>> _mealSources;
|
|
late Future<List<Accuracy>> _portionSizeAccuracies;
|
|
late Future<List<Accuracy>> _carbsRatioAccuracies;
|
|
|
|
bool isSaving = false;
|
|
|
|
@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 {
|
|
setState(() {
|
|
isSaving = true;
|
|
});
|
|
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');
|
|
}
|
|
setState(() {
|
|
isSaving = false;
|
|
});
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
Future<void> onSelectMealSource(String? objectId) async {
|
|
if (objectId != null) {
|
|
MealSource? mealSource = await MealSource.get(objectId);
|
|
if (mealSource != null) {
|
|
setState(() {
|
|
_source = objectId;
|
|
if (mealSource.defaultCarbsRatioAccuracy != null) {
|
|
_carbsRatioAccuracy =
|
|
mealSource.defaultCarbsRatioAccuracy.toString();
|
|
}
|
|
if (mealSource.defaultPortionSizeAccuracy != null) {
|
|
_portionSizeAccuracy =
|
|
mealSource.defaultPortionSizeAccuracy.toString();
|
|
}
|
|
if (mealSource.defaultMealCategory != null) {
|
|
_category = mealSource.defaultMealCategory.toString();
|
|
}
|
|
if (mealSource.defaultMealPortionType != null) {
|
|
_portionType = mealSource.defaultMealPortionType.toString();
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
void calculateThirdMeasurementOfPortionCarbsRelation(
|
|
{PortionCarbsParameter? parameterToBeCalculated}) {
|
|
double? carbsRatio;
|
|
double? portionSize;
|
|
double? carbsPerPortion;
|
|
|
|
if (parameterToBeCalculated != PortionCarbsParameter.carbsRatio &&
|
|
_carbsRatioController.text != '') {
|
|
carbsRatio = double.tryParse(_carbsRatioController.text);
|
|
}
|
|
if (parameterToBeCalculated != PortionCarbsParameter.portionSize &&
|
|
_portionSizeController.text != '') {
|
|
portionSize = double.tryParse(_portionSizeController.text);
|
|
}
|
|
if (parameterToBeCalculated != PortionCarbsParameter.carbsPerPortion &&
|
|
_carbsRatioController.text != '') {
|
|
carbsPerPortion = double.tryParse(_carbsPerPortionController.text);
|
|
}
|
|
|
|
if (carbsRatio != null && portionSize != null && carbsPerPortion == null) {
|
|
setState(() {
|
|
_carbsPerPortionController.text =
|
|
Utils.calculateCarbsPerPortion(carbsRatio!, portionSize!)
|
|
.toString();
|
|
});
|
|
}
|
|
if (carbsRatio == null && portionSize != null && carbsPerPortion != null) {
|
|
setState(() {
|
|
_carbsRatioController.text =
|
|
Utils.calculateCarbsRatio(carbsPerPortion!, portionSize!)
|
|
.toString();
|
|
});
|
|
}
|
|
if (carbsRatio != null && portionSize == null && carbsPerPortion != null) {
|
|
setState(() {
|
|
_portionSizeController.text =
|
|
Utils.calculatePortionSize(carbsRatio!, carbsPerPortion!)
|
|
.toString();
|
|
});
|
|
}
|
|
}
|
|
|
|
@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: <Widget>[
|
|
StyledForm(
|
|
formState: _mealForm,
|
|
fields: [
|
|
TextFormField(
|
|
controller: _valueController,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Name',
|
|
),
|
|
validator: (value) {
|
|
if (value!.trim().isEmpty) {
|
|
return 'Empty name';
|
|
}
|
|
return null;
|
|
},
|
|
),
|
|
StyledFutureDropdownButton<MealSource>(
|
|
selectedItem: _source,
|
|
label: 'Meal Source',
|
|
items: _mealSources,
|
|
getItemValue: (item) => item.objectId,
|
|
renderItem: (item) => Text(item.value),
|
|
onChanged: (value) {
|
|
onSelectMealSource(value);
|
|
},
|
|
),
|
|
StyledFutureDropdownButton<MealCategory>(
|
|
selectedItem: _category,
|
|
label: 'Meal Category',
|
|
items: _mealCategories,
|
|
getItemValue: (item) => item.objectId,
|
|
renderItem: (item) => Text(item.value),
|
|
onChanged: (value) {
|
|
setState(() {
|
|
_category = value;
|
|
});
|
|
},
|
|
),
|
|
StyledFutureDropdownButton<MealPortionType>(
|
|
selectedItem: _portionType,
|
|
label: 'Meal Portion Type',
|
|
items: _mealPortionTypes,
|
|
getItemValue: (item) => item.objectId,
|
|
renderItem: (item) => Text(item.value),
|
|
onChanged: (value) {
|
|
setState(() {
|
|
_portionType = value;
|
|
});
|
|
},
|
|
),
|
|
Row(
|
|
children: [
|
|
Expanded(
|
|
child: TextFormField(
|
|
decoration: const InputDecoration(
|
|
labelText: 'Carbs ratio',
|
|
suffixText: '%',
|
|
),
|
|
controller: _carbsRatioController,
|
|
keyboardType: const TextInputType.numberWithOptions(
|
|
decimal: true),
|
|
onChanged: (_) =>
|
|
calculateThirdMeasurementOfPortionCarbsRelation(),
|
|
),
|
|
),
|
|
IconButton(
|
|
onPressed: () =>
|
|
calculateThirdMeasurementOfPortionCarbsRelation(
|
|
parameterToBeCalculated:
|
|
PortionCarbsParameter.carbsRatio),
|
|
icon: const Icon(Icons.calculate),
|
|
),
|
|
],
|
|
),
|
|
Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Expanded(
|
|
child: 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),
|
|
onChanged: (_) =>
|
|
calculateThirdMeasurementOfPortionCarbsRelation(),
|
|
),
|
|
),
|
|
IconButton(
|
|
onPressed: () =>
|
|
calculateThirdMeasurementOfPortionCarbsRelation(
|
|
parameterToBeCalculated:
|
|
PortionCarbsParameter.portionSize),
|
|
icon: const Icon(Icons.calculate),
|
|
),
|
|
],
|
|
),
|
|
StyledFutureDropdownButton<Accuracy>(
|
|
selectedItem: _portionSizeAccuracy,
|
|
label: 'Portion Size Accuracy',
|
|
items: _portionSizeAccuracies,
|
|
getItemValue: (item) => item.objectId,
|
|
renderItem: (item) => Text(item.value),
|
|
onChanged: (value) {
|
|
setState(() {
|
|
_portionSizeAccuracy = value;
|
|
});
|
|
},
|
|
),
|
|
Row(
|
|
children: [
|
|
Expanded(
|
|
child: TextFormField(
|
|
decoration: InputDecoration(
|
|
labelText: 'Carbs per portion',
|
|
suffixText:
|
|
nutritionMeasurement == NutritionMeasurement.grams
|
|
? 'g'
|
|
: nutritionMeasurement ==
|
|
NutritionMeasurement.ounces
|
|
? 'oz'
|
|
: '',
|
|
),
|
|
controller: _carbsPerPortionController,
|
|
keyboardType: const TextInputType.numberWithOptions(
|
|
decimal: true),
|
|
onChanged: (_) =>
|
|
calculateThirdMeasurementOfPortionCarbsRelation(),
|
|
),
|
|
),
|
|
IconButton(
|
|
onPressed: () =>
|
|
calculateThirdMeasurementOfPortionCarbsRelation(
|
|
parameterToBeCalculated:
|
|
PortionCarbsParameter.carbsPerPortion),
|
|
icon: const Icon(Icons.calculate),
|
|
),
|
|
],
|
|
),
|
|
StyledFutureDropdownButton<Accuracy>(
|
|
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),
|
|
),
|
|
TextFormField(
|
|
controller: _notesController,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Notes',
|
|
alignLabelWithHint: true,
|
|
),
|
|
keyboardType: TextInputType.multiline,
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
),
|
|
bottomNavigationBar: DetailBottomRow(
|
|
onCancel: handleCancelAction,
|
|
onSave: isSaving ? null : handleSaveAction,
|
|
),
|
|
);
|
|
}
|
|
}
|