diameter/lib/screens/log/log_entry/log_bolus_detail.dart

706 lines
29 KiB
Dart
Raw Normal View History

2021-11-30 03:16:28 +00:00
import 'dart:math';
import 'package:diameter/components/detail.dart';
import 'package:diameter/components/dialogs.dart';
import 'package:diameter/components/dropdown.dart';
import 'package:diameter/components/forms.dart';
import 'package:diameter/models/bolus.dart';
import 'package:diameter/models/log_bolus.dart';
import 'package:diameter/models/log_entry.dart';
import 'package:diameter/models/log_meal.dart';
import 'package:diameter/models/settings.dart';
import 'package:diameter/navigation.dart';
2021-11-30 03:16:28 +00:00
import 'package:diameter/utils/utils.dart';
import 'package:flutter/material.dart';
2021-11-30 03:16:28 +00:00
enum BolusType {
meal,
glucose,
}
enum GlucoseParameter {
mgdlCurrent,
mgdlTarget,
mgdlCorrection,
mmolCurrent,
mmolTarget,
mmolCorrection,
}
class LogBolusDetailScreen extends StatefulWidget {
static const String routeName = '/log-bolus';
final int logEntryId;
final int id;
const LogBolusDetailScreen({Key? key, this.logEntryId = 0, this.id = 0})
: super(key: key);
@override
_LogBolusDetailScreenState createState() => _LogBolusDetailScreenState();
}
class _LogBolusDetailScreenState extends State<LogBolusDetailScreen> {
LogEntry? _logEntry;
LogBolus? _logBolus;
bool _isNew = true;
bool _isSaving = false;
final GlobalKey<FormState> _logBolusForm = GlobalKey<FormState>();
final _unitsController = TextEditingController(text: '');
final _carbsController = TextEditingController(text: '');
2021-11-30 03:16:28 +00:00
final _mgPerDlCurrentController = TextEditingController(text: '');
final _mgPerDlTargetController = TextEditingController(text: '');
final _mgPerDlCorrectionController = TextEditingController(text: '');
final _mmolPerLCurrentController = TextEditingController(text: '');
final _mmolPerLTargetController = TextEditingController(text: '');
final _mmolPerLCorrectionController = TextEditingController(text: '');
final _delayController = TextEditingController(text: '');
final _notesController = TextEditingController(text: '');
final _delayedUnitsController = TextEditingController(text: '');
final _immediateUnitsController = TextEditingController(text: '');
2021-11-25 18:25:13 +00:00
bool _setManually = false;
2021-11-30 03:16:28 +00:00
BolusType _bolusType = BolusType.meal;
LogMeal? _meal;
Bolus? _rate;
double _delayPercentage = 0;
List<LogMeal> _logMeals = [];
@override
void initState() {
super.initState();
reload();
_logEntry = LogEntry.get(widget.logEntryId);
_logMeals = LogMeal.getAllForEntry(widget.logEntryId);
if (widget.id != 0) {
_carbsController.text = (_logBolus!.carbs ?? '').toString();
_delayController.text = (_logBolus!.delay ?? '').toString();
_notesController.text = _logBolus!.notes ?? '';
2021-11-25 18:25:13 +00:00
_setManually = _logBolus!.setManually;
_meal = _logBolus!.meal.target;
2021-11-25 18:25:13 +00:00
_rate = _logBolus!.rate.target;
}
2021-11-25 18:25:13 +00:00
_rate ??= Bolus.getRateForTime(_logEntry?.time);
_mgPerDlCurrentController.text = (_logBolus?.mgPerDlCurrent ??
(LogEntry.hasUncorrectedGlucose(widget.logEntryId)
? _logEntry?.mgPerDl ?? 0
: 0))
.toString();
2021-11-30 03:16:28 +00:00
_mgPerDlTargetController.text =
(_logBolus?.mgPerDlTarget ?? Settings.get().moderateGlucoseMgPerDl).toString();
_mgPerDlCorrectionController.text = (_logBolus?.mgPerDlCorrection ??
max(
(int.tryParse(_mgPerDlCurrentController.text) ?? 0) -
(int.tryParse(_mgPerDlTargetController.text) ?? 0),
0))
.toString();
_mmolPerLCurrentController.text = (_logBolus?.mmolPerLCurrent ??
(LogEntry.hasUncorrectedGlucose(widget.logEntryId)
? _logEntry?.mmolPerL ?? 0
: 0))
.toString();
2021-11-30 03:16:28 +00:00
_mmolPerLTargetController.text =
(_logBolus?.mmolPerLTarget ?? Settings.get().moderateGlucoseMmolPerL).toString();
_mmolPerLCorrectionController.text = (_logBolus?.mmolPerLCorrection ??
max(
(double.tryParse(_mmolPerLCurrentController.text) ?? 0) -
(double.tryParse(_mmolPerLTargetController.text) ?? 0),
0))
.toString();
2021-11-30 03:16:28 +00:00
_unitsController.text = (_logBolus?.units ??
(_rate != null && !_setManually
? ((int.tryParse(_mgPerDlCorrectionController.text) ?? 0) /
((_rate!.mgPerDl ?? 0) / _rate!.units))
: 0))
.toString();
2021-11-30 03:16:28 +00:00
if (widget.id == 0 && LogEntry.hasUncorrectedGlucose(widget.logEntryId)) {
_bolusType = BolusType.glucose;
}
updateDelayedRatio();
}
void reload() {
if (widget.id != 0) {
setState(() {
_logBolus = LogBolus.get(widget.id);
});
}
_isNew = _logBolus == null;
}
void updateDelayedRatio() {
if (_unitsController.text != '') {
setState(() {
_delayedUnitsController.text =
((double.tryParse(_unitsController.text) ?? 0) *
_delayPercentage /
100)
.toString();
_immediateUnitsController.text =
((double.tryParse(_unitsController.text) ?? 0) *
(100 - _delayPercentage) /
100)
.toString();
});
}
}
2021-11-25 18:25:13 +00:00
void onSelectMeal(LogMeal meal) {
setState(() {
_meal = meal;
if (meal.carbsPerPortion != null) {
_carbsController.text = meal.carbsPerPortion.toString();
2021-11-25 18:25:13 +00:00
onChangeCarbs();
}
});
}
2021-11-25 18:25:13 +00:00
void onChangeCarbs() {
setState(() {
if (_rate != null && !_setManually) {
_unitsController.text = ((double.tryParse(_carbsController.text) ?? 0) /
(_rate!.carbs / _rate!.units))
.toString();
if (_unitsController.text != '') {
_delayedUnitsController.text =
((double.tryParse(_unitsController.text) ?? 0) *
_delayPercentage /
100)
.toString();
_immediateUnitsController.text =
((double.tryParse(_unitsController.text) ?? 0) *
(100 - _delayPercentage) /
100)
.toString();
}
}
});
}
2021-11-30 03:16:28 +00:00
void onChangeGlucose({GlucoseMeasurement? calculateFrom}) {
int? mgPerDlCurrent;
int? mgPerDlTarget;
int? mgPerDlCorrection;
double? mmolPerLCurrent;
double? mmolPerLTarget;
double? mmolPerLCorrection;
if (calculateFrom != GlucoseMeasurement.mmolPerL &&
_mgPerDlCurrentController.text != '' &&
_mgPerDlTargetController.text != '') {
mgPerDlCurrent = int.tryParse(_mgPerDlCurrentController.text);
mgPerDlTarget = int.tryParse(_mgPerDlTargetController.text);
mgPerDlCorrection = max((mgPerDlCurrent ?? 0) - (mgPerDlTarget ?? 0), 0);
}
if (calculateFrom != GlucoseMeasurement.mgPerDl &&
_mmolPerLCurrentController.text != '') {
mmolPerLCurrent = double.tryParse(_mmolPerLCurrentController.text);
mmolPerLTarget = double.tryParse(_mmolPerLTargetController.text);
mmolPerLCorrection =
max((mmolPerLCurrent ?? 0) - (mmolPerLTarget ?? 0), 0);
}
if ((mgPerDlCurrent != null && mmolPerLCurrent == null) ||
(mgPerDlTarget != null && mmolPerLTarget == null) ||
(mgPerDlCorrection != null && mmolPerLCorrection == null)) {
setState(() {
_mgPerDlCorrectionController.text = (mgPerDlCorrection ?? 0).toString();
_mmolPerLCurrentController.text =
Utils.convertMgPerDlToMmolPerL(mgPerDlCurrent ?? 0).toString();
_mmolPerLTargetController.text =
Utils.convertMgPerDlToMmolPerL(mgPerDlTarget ?? 0).toString();
_mmolPerLCorrectionController.text =
Utils.convertMgPerDlToMmolPerL(mgPerDlCorrection ?? 0).toString();
if (_rate != null && !_setManually) {
_unitsController.text = ((mgPerDlCorrection ?? 0) /
((_rate!.mgPerDl ?? 0) / _rate!.units))
.toString();
}
});
}
if ((mmolPerLCurrent != null && mgPerDlCurrent == null) ||
(mmolPerLTarget != null && mgPerDlTarget == null) ||
(mmolPerLCorrection != null && mgPerDlCorrection == null)) {
setState(() {
_mmolPerLCurrentController.text = (mmolPerLCorrection ?? 0).toString();
_mgPerDlCurrentController.text =
Utils.convertMmolPerLToMgPerDl(mmolPerLCurrent ?? 0).toString();
_mgPerDlTargetController.text =
Utils.convertMmolPerLToMgPerDl(mmolPerLTarget ?? 0).toString();
_mgPerDlCorrectionController.text =
Utils.convertMmolPerLToMgPerDl(mmolPerLCorrection ?? 0).toString();
if (_rate != null && !_setManually) {
_unitsController.text = ((mmolPerLCorrection ?? 0) /
((_rate!.mmolPerL ?? 0) / _rate!.units))
.toString();
}
});
}
}
void handleSaveAction() async {
setState(() {
_isSaving = true;
});
if (_logBolusForm.currentState!.validate()) {
LogBolus logBolus;
LogBolus? delayedBolus;
if ((int.tryParse(_delayController.text) ?? 0) != 0 &&
_delayPercentage != 0 &&
_delayPercentage != 100) {
logBolus = LogBolus(
id: widget.id,
units: double.tryParse(_immediateUnitsController.text) ?? 0,
setManually: _setManually,
notes: _notesController.text,
);
delayedBolus = LogBolus(
delay: int.tryParse(_delayController.text),
units: double.tryParse(_delayedUnitsController.text) ?? 0,
setManually: _setManually,
notes: _notesController.text,
);
} else {
logBolus = LogBolus(
id: widget.id,
units: double.tryParse(_unitsController.text) ?? 0,
delay: _delayPercentage == 100
? int.tryParse(_delayController.text)
: null,
setManually: _setManually,
notes: _notesController.text,
);
}
2021-11-30 03:16:28 +00:00
if (_bolusType == BolusType.meal) {
logBolus.carbs = double.tryParse(_carbsController.text);
if (delayedBolus != null) {
delayedBolus.carbs = double.tryParse(_carbsController.text);
}
2021-11-30 03:16:28 +00:00
logBolus.mgPerDlCurrent = null;
logBolus.mmolPerLCurrent = null;
} else {
logBolus.carbs = null;
logBolus.mgPerDlCurrent = int.tryParse(_mgPerDlCurrentController.text);
logBolus.mmolPerLCurrent =
double.tryParse(_mmolPerLCurrentController.text);
logBolus.mgPerDlTarget = int.tryParse(_mgPerDlTargetController.text);
logBolus.mmolPerLTarget =
double.tryParse(_mmolPerLTargetController.text);
logBolus.mgPerDlCorrection =
int.tryParse(_mgPerDlCorrectionController.text);
2021-11-30 03:16:28 +00:00
logBolus.mmolPerLCorrection =
double.tryParse(_mmolPerLCorrectionController.text);
if (delayedBolus != null) {
delayedBolus.mgPerDlCurrent =
int.tryParse(_mgPerDlCurrentController.text);
delayedBolus.mmolPerLCurrent =
double.tryParse(_mmolPerLCurrentController.text);
delayedBolus.mgPerDlTarget =
int.tryParse(_mgPerDlTargetController.text);
delayedBolus.mmolPerLTarget =
double.tryParse(_mmolPerLTargetController.text);
delayedBolus.mgPerDlCorrection =
int.tryParse(_mgPerDlCorrectionController.text);
delayedBolus.mmolPerLCorrection =
double.tryParse(_mmolPerLCorrectionController.text);
}
2021-11-30 03:16:28 +00:00
}
logBolus.logEntry.target = _logEntry;
logBolus.meal.target = _meal;
logBolus.rate.target = _rate;
LogBolus.put(logBolus);
if (delayedBolus != null) {
delayedBolus.logEntry.target = _logEntry;
delayedBolus.meal.target = _meal;
delayedBolus.rate.target = _rate;
LogBolus.put(delayedBolus);
}
Navigator.pop(context, '${_isNew ? 'New' : ''} Bolus Saved');
}
setState(() {
_isSaving = false;
});
}
void handleCancelAction() {
if (Settings.get().showConfirmationDialogOnCancel &&
((_isNew &&
(_carbsController.text != '' ||
(_bolusType == BolusType.glucose &&
(_mgPerDlCurrentController.text !=
(_logEntry?.mgPerDl.toString() ?? '') ||
_mmolPerLCurrentController.text !=
(_logEntry?.mmolPerL.toString() ?? ''))) ||
_mgPerDlTargetController.text !=
Settings.get().moderateGlucoseMgPerDl.toString() ||
_mmolPerLTargetController.text !=
Settings.get().moderateGlucoseMmolPerL.toString() ||
_delayController.text != '' ||
2021-11-25 18:25:13 +00:00
_setManually ||
_notesController.text != '')) ||
(!_isNew &&
(double.tryParse(_unitsController.text) != _logBolus!.units ||
2021-11-25 18:25:13 +00:00
double.tryParse(_carbsController.text) !=
_logBolus!.carbs ||
2021-11-30 03:16:28 +00:00
int.tryParse(_mgPerDlCurrentController.text) !=
_logBolus!.mgPerDlCurrent ||
int.tryParse(_mgPerDlTargetController.text) !=
_logBolus!.mgPerDlTarget ||
int.tryParse(_mgPerDlCorrectionController.text) !=
_logBolus!.mgPerDlCorrection ||
double.tryParse(_mmolPerLCurrentController.text) !=
_logBolus!.mmolPerLCurrent ||
double.tryParse(_mmolPerLTargetController.text) !=
_logBolus!.mmolPerLTarget ||
double.tryParse(_mmolPerLCorrectionController.text) !=
_logBolus!.mmolPerLCorrection ||
int.tryParse(_delayController.text) != _logBolus!.delay ||
2021-11-25 18:25:13 +00:00
_setManually != _logBolus!.setManually ||
_notesController.text != (_logBolus!.notes ?? ''))))) {
Dialogs.showCancelConfirmationDialog(
context: context,
isNew: _isNew,
onSave: handleSaveAction,
);
} else {
Navigator.pop(context);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(_isNew ? 'New Bolus' : 'Edit Bolus'),
),
drawer: const Navigation(currentLocation: LogBolusDetailScreen.routeName),
body: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
FormWrapper(
formState: _logBolusForm,
fields: [
Row(
children: [
Expanded(
child: TextFormField(
decoration: const InputDecoration(
labelText: 'Bolus Units',
suffixText: ' U',
),
controller: _unitsController,
onChanged: (_) {
setState(() {
_setManually = true;
});
updateDelayedRatio();
},
keyboardType: const TextInputType.numberWithOptions(
decimal: true),
),
),
Expanded(
child: BooleanFormField(
contentPadding: const EdgeInsets.only(
left: 10.0, right: 10.0, top: 10.0),
value: _setManually,
label: 'set manually',
onChanged: (value) {
setState(() {
_setManually = value;
});
},
),
),
],
2021-11-25 18:25:13 +00:00
),
2021-11-30 03:16:28 +00:00
Row(
children: [
Expanded(
child: RadioListTile(
title: const Text('for glucose'),
groupValue: _bolusType,
value: BolusType.glucose,
onChanged: (_) {
setState(() {
_bolusType = BolusType.glucose;
});
}),
),
Expanded(
child: RadioListTile(
title: const Text('for meal'),
groupValue: _bolusType,
value: BolusType.meal,
onChanged: (value) {
setState(() {
_bolusType = BolusType.meal;
});
}),
),
],
),
2021-11-30 03:16:28 +00:00
Column(
children: _bolusType == BolusType.glucose
? [
Row(
2021-12-06 00:11:22 +00:00
children: Settings.glucoseMeasurement == GlucoseMeasurement.mgPerDl ||
[GlucoseDisplayMode.both, GlucoseDisplayMode.bothForDetail].contains(Settings.glucoseDisplayMode)
2021-11-30 03:16:28 +00:00
? [
Expanded(
child: Padding(
padding:
const EdgeInsets.only(right: 5.0),
child: TextFormField(
decoration: const InputDecoration(
labelText: 'Current',
suffixText: 'mg/dl',
),
controller: _mgPerDlCurrentController,
onChanged: (_) => onChangeGlucose(
calculateFrom:
GlucoseMeasurement.mgPerDl),
keyboardType: const TextInputType
.numberWithOptions(),
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 5.0),
child: TextFormField(
decoration: const InputDecoration(
labelText: 'Target',
suffixText: 'mg/dl',
),
controller: _mgPerDlTargetController,
onChanged: (_) => onChangeGlucose(
calculateFrom:
GlucoseMeasurement.mgPerDl),
keyboardType: const TextInputType
.numberWithOptions(),
),
),
),
Expanded(
child: Padding(
padding:
const EdgeInsets.only(left: 5.0),
child: TextFormField(
decoration: const InputDecoration(
labelText: 'Correction',
suffixText: 'mg/dl',
),
controller:
_mgPerDlCorrectionController,
readOnly: true,
),
),
),
2021-12-06 00:11:22 +00:00
[GlucoseDisplayMode.both, GlucoseDisplayMode.bothForDetail].contains(Settings.glucoseDisplayMode)
2021-11-30 03:16:28 +00:00
? IconButton(
onPressed: () => onChangeGlucose(
calculateFrom:
GlucoseMeasurement
.mmolPerL),
icon: const Icon(Icons.calculate),
)
: Container(),
]
: [],
),
Row(
2021-12-06 00:11:22 +00:00
children: Settings.glucoseMeasurement == GlucoseMeasurement.mmolPerL ||
[GlucoseDisplayMode.both, GlucoseDisplayMode.bothForDetail].contains(Settings.glucoseDisplayMode)
2021-11-30 03:16:28 +00:00
? [
Expanded(
child: Padding(
padding:
const EdgeInsets.only(right: 5),
child: TextFormField(
decoration: const InputDecoration(
labelText: 'Current',
suffixText: 'mmol/l',
),
controller:
_mmolPerLCurrentController,
onChanged: (_) => onChangeGlucose(
calculateFrom:
GlucoseMeasurement.mmolPerL),
keyboardType: const TextInputType
.numberWithOptions(),
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 5.0),
child: TextFormField(
decoration: const InputDecoration(
labelText: 'Target',
suffixText: 'mmol/l',
),
controller: _mmolPerLTargetController,
onChanged: (_) => onChangeGlucose(
calculateFrom:
GlucoseMeasurement.mmolPerL),
keyboardType: const TextInputType
.numberWithOptions(),
),
),
),
Expanded(
child: Padding(
padding:
const EdgeInsets.only(left: 5.0),
child: TextFormField(
decoration: const InputDecoration(
labelText: 'Correction',
suffixText: 'mmol/l',
),
controller:
_mmolPerLCorrectionController,
readOnly: true,
),
),
),
2021-12-06 00:11:22 +00:00
[GlucoseDisplayMode.both, GlucoseDisplayMode.bothForDetail].contains(Settings.glucoseDisplayMode)
2021-11-30 03:16:28 +00:00
? IconButton(
onPressed: () => onChangeGlucose(
calculateFrom:
GlucoseMeasurement.mgPerDl),
icon: const Icon(Icons.calculate),
)
: Container(),
]
: [],
),
]
: [
AutoCompleteDropdownButton<LogMeal>(
selectedItem: _meal,
label: 'Meal',
items: _logMeals,
onChanged: (value) {
if (value != null) {
onSelectMeal(value);
}
},
),
TextFormField(
decoration: InputDecoration(
labelText: 'Carbs',
2021-12-06 00:11:22 +00:00
suffixText: Settings.nutritionMeasurementSuffix,
2021-11-30 03:16:28 +00:00
),
controller: _carbsController,
onChanged: (_) => onChangeCarbs(),
keyboardType: const TextInputType.numberWithOptions(
decimal: true),
),
],
2021-11-25 18:25:13 +00:00
),
TextFormField(
decoration: const InputDecoration(
labelText: 'Delayed Bolus Duration',
suffixText: ' min',
),
controller: _delayController,
onChanged: (value) => setState(() {}),
keyboardType: const TextInputType.numberWithOptions(),
),
(int.tryParse(_delayController.text) ?? 0) != 0
? Slider(
label: '${_delayPercentage.floor().toString()}%',
divisions: 100,
value: _delayPercentage,
min: 0,
max: 100,
onChanged: _delayController.text != ''
? (value) {
setState(() {
_delayPercentage = value;
});
updateDelayedRatio();
}
: null,
)
: Container(),
Row(
children: (int.tryParse(_delayController.text) ?? 0) != 0
? [
Expanded(
child: Padding(
padding: const EdgeInsets.only(right: 5.0),
child: TextFormField(
decoration: const InputDecoration(
labelText: 'Immediate Bolus',
suffixText: ' U',
),
controller: _immediateUnitsController,
readOnly: true,
enabled: (int.tryParse(_delayController.text) ??
0) !=
0,
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.only(left: 5.0),
child: TextFormField(
decoration: const InputDecoration(
labelText: 'Delayed Bolus',
suffixText: ' U',
),
controller: _delayedUnitsController,
readOnly: true,
enabled: (int.tryParse(_delayController.text) ??
0) !=
0,
),
),
),
]
: [],
),
TextFormField(
controller: _notesController,
decoration: const InputDecoration(
labelText: 'Notes',
alignLabelWithHint: true,
),
keyboardType: TextInputType.multiline,
),
],
),
],
),
),
bottomNavigationBar: DetailBottomRow(
onCancel: handleCancelAction,
onSave: _isSaving ? null : handleSaveAction,
),
);
}
}