506 lines
20 KiB
Dart
506 lines
20 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/log_entry.dart';
|
|
import 'package:diameter/models/log_meal.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 LogMealDetailScreen extends StatefulWidget {
|
|
static const String routeName = '/log-meal';
|
|
final LogEntry logEntry;
|
|
final LogMeal? logMeal;
|
|
const LogMealDetailScreen({Key? key, required this.logEntry, this.logMeal})
|
|
: super(key: key);
|
|
|
|
@override
|
|
_LogMealDetailScreenState createState() => _LogMealDetailScreenState();
|
|
}
|
|
|
|
class _LogMealDetailScreenState extends State<LogMealDetailScreen> {
|
|
final GlobalKey<FormState> _logMealForm = GlobalKey<FormState>();
|
|
final _valueController = TextEditingController(text: '');
|
|
final _carbsRatioController = TextEditingController(text: '');
|
|
final _portionSizeController = TextEditingController(text: '');
|
|
final _carbsPerPortionController = TextEditingController(text: '');
|
|
final _bolusController = TextEditingController(text: '');
|
|
final _delayedBolusRateController = TextEditingController(text: '');
|
|
final _delayedBolusDurationController = TextEditingController(text: '');
|
|
final _notesController = TextEditingController(text: '');
|
|
String? _meal;
|
|
String? _source;
|
|
String? _category;
|
|
String? _portionType;
|
|
String? _portionSizeAccuracy;
|
|
String? _carbsRatioAccuracy;
|
|
|
|
late Future<List<Meal>> _meals;
|
|
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.logMeal != null) {
|
|
_valueController.text = widget.logMeal!.value;
|
|
_carbsRatioController.text =
|
|
(widget.logMeal!.carbsRatio ?? '').toString();
|
|
_portionSizeController.text =
|
|
(widget.logMeal!.portionSize ?? '').toString();
|
|
_carbsPerPortionController.text =
|
|
(widget.logMeal!.carbsPerPortion ?? '').toString();
|
|
_bolusController.text = (widget.logMeal!.bolus ?? '').toString();
|
|
_delayedBolusRateController.text =
|
|
(widget.logMeal!.delayedBolusRate ?? '').toString();
|
|
_delayedBolusDurationController.text =
|
|
(widget.logMeal!.delayedBolusDuration ?? '').toString();
|
|
_notesController.text = widget.logMeal!.notes ?? '';
|
|
|
|
_meal = widget.logMeal!.meal;
|
|
_source = widget.logMeal!.source;
|
|
_category = widget.logMeal!.category;
|
|
_portionType = widget.logMeal!.portionType;
|
|
_portionSizeAccuracy = widget.logMeal!.portionSizeAccuracy;
|
|
_carbsRatioAccuracy = widget.logMeal!.carbsRatioAccuracy;
|
|
}
|
|
|
|
_meals = Meal.fetchAll();
|
|
_mealCategories = MealCategory.fetchAll();
|
|
_mealPortionTypes = MealPortionType.fetchAll();
|
|
_mealSources = MealSource.fetchAll();
|
|
_portionSizeAccuracies = Accuracy.fetchAllForPortionSize();
|
|
_carbsRatioAccuracies = Accuracy.fetchAllForCarbsRatio();
|
|
}
|
|
|
|
Future<void> onSelectMeal(String? objectId) async {
|
|
if (objectId != null) {
|
|
Meal? meal = await Meal.get(objectId);
|
|
if (meal != null) {
|
|
setState(() {
|
|
_meal = objectId;
|
|
_valueController.text = meal.value;
|
|
if (meal.carbsRatio != null) {
|
|
_carbsRatioController.text = meal.carbsRatio.toString();
|
|
}
|
|
if (meal.portionSize != null) {
|
|
_portionSizeController.text = meal.portionSize.toString();
|
|
}
|
|
if (meal.carbsPerPortion != null) {
|
|
_carbsPerPortionController.text = meal.carbsPerPortion.toString();
|
|
}
|
|
if (meal.delayedBolusRate != null) {
|
|
_delayedBolusRateController.text = meal.delayedBolusRate.toString();
|
|
}
|
|
if (meal.delayedBolusDuration != null) {
|
|
_delayedBolusDurationController.text =
|
|
meal.delayedBolusDuration.toString();
|
|
}
|
|
if (meal.source != null) {
|
|
_source = meal.source;
|
|
}
|
|
if (meal.category != null) {
|
|
_category = meal.category;
|
|
}
|
|
if (meal.portionType != null) {
|
|
_portionType = meal.portionType;
|
|
}
|
|
if (meal.portionSizeAccuracy != null) {
|
|
_portionSizeAccuracy = meal.portionSizeAccuracy;
|
|
}
|
|
if (meal.carbsRatioAccuracy != null) {
|
|
_carbsRatioAccuracy = meal.carbsRatioAccuracy;
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
void handleSaveAction() async {
|
|
setState(() {
|
|
_isSaving = true;
|
|
});
|
|
if (_logMealForm.currentState!.validate()) {
|
|
bool isNew = widget.logMeal == null;
|
|
isNew
|
|
? await LogMeal.save(
|
|
logEntry: widget.logEntry.objectId!,
|
|
meal: _meal,
|
|
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,
|
|
bolus: double.tryParse(_bolusController.text),
|
|
delayedBolusDuration:
|
|
int.tryParse(_delayedBolusDurationController.text),
|
|
delayedBolusRate:
|
|
double.tryParse(_delayedBolusRateController.text),
|
|
notes: _notesController.text,
|
|
)
|
|
: await LogMeal.update(
|
|
widget.logMeal!.objectId!,
|
|
meal: _meal,
|
|
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,
|
|
bolus: double.tryParse(_bolusController.text),
|
|
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.logMeal == null;
|
|
if (showConfirmationDialogOnCancel &&
|
|
((isNew &&
|
|
(_valueController.text != '' ||
|
|
_meal != null ||
|
|
_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 ||
|
|
double.tryParse(_bolusController.text) != null ||
|
|
int.tryParse(_delayedBolusDurationController.text) !=
|
|
null ||
|
|
double.tryParse(_delayedBolusRateController.text) != null ||
|
|
_notesController.text != '')) ||
|
|
(!isNew &&
|
|
(_valueController.text != widget.logMeal!.value ||
|
|
_meal != widget.logMeal!.meal ||
|
|
_source != widget.logMeal!.source ||
|
|
_category != widget.logMeal!.category ||
|
|
_portionType != widget.logMeal!.portionType ||
|
|
double.tryParse(_carbsRatioController.text) !=
|
|
widget.logMeal!.carbsRatio ||
|
|
double.tryParse(_portionSizeController.text) !=
|
|
widget.logMeal!.portionSize ||
|
|
double.tryParse(_carbsPerPortionController.text) !=
|
|
widget.logMeal!.carbsPerPortion ||
|
|
_carbsRatioAccuracy != widget.logMeal!.carbsRatioAccuracy ||
|
|
_portionSizeAccuracy !=
|
|
widget.logMeal!.portionSizeAccuracy ||
|
|
double.tryParse(_bolusController.text) != widget.logMeal!.bolus ||
|
|
int.tryParse(_delayedBolusDurationController.text) !=
|
|
widget.logMeal!.delayedBolusDuration ||
|
|
double.tryParse(_delayedBolusRateController.text) !=
|
|
widget.logMeal!.delayedBolusRate ||
|
|
_notesController.text != (widget.logMeal!.notes ?? ''))))) {
|
|
Dialogs.showCancelConfirmationDialog(
|
|
context: context,
|
|
isNew: isNew,
|
|
onSave: handleSaveAction,
|
|
);
|
|
} else {
|
|
Navigator.pop(context);
|
|
}
|
|
}
|
|
|
|
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.logMeal == null;
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: Text(isNew ? 'New Meal' : widget.logMeal!.value),
|
|
),
|
|
drawer: const Navigation(currentLocation: LogMealDetailScreen.routeName),
|
|
body: SingleChildScrollView(
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: <Widget>[
|
|
StyledForm(
|
|
formState: _logMealForm,
|
|
fields: [
|
|
TextFormField(
|
|
controller: _valueController,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Name',
|
|
),
|
|
validator: (value) {
|
|
if (value!.trim().isEmpty) {
|
|
return 'Empty name';
|
|
}
|
|
return null;
|
|
},
|
|
),
|
|
StyledFutureDropdownButton<Meal>(
|
|
selectedItem: _meal,
|
|
label: 'Meal',
|
|
items: _meals,
|
|
getItemValue: (item) => item.objectId,
|
|
renderItem: (item) => Text(item.value),
|
|
onChanged: (value) {
|
|
onSelectMeal(value);
|
|
},
|
|
),
|
|
StyledFutureDropdownButton<MealSource>(
|
|
selectedItem: _source,
|
|
label: 'Meal Source',
|
|
items: _mealSources,
|
|
getItemValue: (item) => item.objectId,
|
|
renderItem: (item) => Text(item.value),
|
|
onChanged: (value) {
|
|
setState(() {
|
|
_source = 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;
|
|
});
|
|
},
|
|
),
|
|
TextFormField(
|
|
decoration: const InputDecoration(
|
|
labelText: 'Bolus Units',
|
|
suffixText: ' U',
|
|
),
|
|
controller: _bolusController,
|
|
keyboardType:
|
|
const TextInputType.numberWithOptions(decimal: true),
|
|
),
|
|
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',
|
|
alignLabelWithHint: true,
|
|
),
|
|
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,
|
|
),
|
|
);
|
|
}
|
|
}
|