2021-10-22 23:08:09 +00:00
|
|
|
import 'package:diameter/components/detail.dart';
|
|
|
|
import 'package:diameter/components/dialogs.dart';
|
|
|
|
import 'package:diameter/config.dart';
|
|
|
|
import 'package:diameter/navigation.dart';
|
|
|
|
import 'package:diameter/utils/date_time_utils.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:diameter/components/forms.dart';
|
|
|
|
import 'package:diameter/models/basal.dart';
|
|
|
|
import 'package:diameter/models/basal_profile.dart';
|
|
|
|
|
|
|
|
class BasalDetailScreen extends StatefulWidget {
|
|
|
|
static const String routeName = '/basal';
|
|
|
|
|
|
|
|
final BasalProfile basalProfile;
|
|
|
|
final Basal? basal;
|
2021-10-26 23:25:40 +00:00
|
|
|
final TimeOfDay? suggestedStartTime;
|
|
|
|
final TimeOfDay? suggestedEndTime;
|
2021-10-22 23:08:09 +00:00
|
|
|
|
2021-10-26 23:25:40 +00:00
|
|
|
const BasalDetailScreen(
|
|
|
|
{Key? key,
|
|
|
|
required this.basalProfile,
|
|
|
|
this.basal,
|
|
|
|
this.suggestedStartTime,
|
|
|
|
this.suggestedEndTime})
|
2021-10-22 23:08:09 +00:00
|
|
|
: super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
_BasalDetailScreenState createState() => _BasalDetailScreenState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _BasalDetailScreenState extends State<BasalDetailScreen> {
|
|
|
|
final GlobalKey<FormState> _basalForm = GlobalKey<FormState>();
|
|
|
|
TimeOfDay _startTime = const TimeOfDay(hour: 0, minute: 0);
|
|
|
|
TimeOfDay _endTime = const TimeOfDay(hour: 0, minute: 0);
|
|
|
|
|
|
|
|
final _startTimeController = TextEditingController(text: '');
|
|
|
|
final _endTimeController = TextEditingController(text: '');
|
|
|
|
final _unitsController = TextEditingController(text: '');
|
|
|
|
|
2021-10-27 19:00:28 +00:00
|
|
|
bool _isSaving = false;
|
|
|
|
|
2021-10-22 23:08:09 +00:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2021-10-26 23:25:40 +00:00
|
|
|
if (widget.suggestedStartTime != null) {
|
|
|
|
_startTime = widget.suggestedStartTime!;
|
|
|
|
}
|
|
|
|
if (widget.suggestedEndTime != null) {
|
|
|
|
_endTime = widget.suggestedEndTime!;
|
|
|
|
}
|
2021-10-22 23:08:09 +00:00
|
|
|
if (widget.basal != null) {
|
|
|
|
_startTime = TimeOfDay.fromDateTime(widget.basal!.startTime);
|
|
|
|
_endTime = TimeOfDay.fromDateTime(widget.basal!.endTime);
|
|
|
|
|
|
|
|
_unitsController.text = widget.basal!.units.toString();
|
|
|
|
}
|
|
|
|
updateStartTime();
|
|
|
|
updateEndTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateStartTime() {
|
|
|
|
_startTimeController.text = DateTimeUtils.displayTimeOfDay(_startTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateEndTime() {
|
|
|
|
_endTimeController.text = DateTimeUtils.displayTimeOfDay(_endTime);
|
|
|
|
}
|
|
|
|
|
2021-10-26 23:25:40 +00:00
|
|
|
Future<String?> validateTimePeriod() async {
|
|
|
|
String? error;
|
|
|
|
List<Basal> basalRates =
|
|
|
|
await Basal.fetchAllForBasalProfile(widget.basalProfile);
|
|
|
|
|
|
|
|
// check for duplicates
|
|
|
|
if (basalRates
|
|
|
|
.where((other) =>
|
|
|
|
(widget.basal == null ||
|
|
|
|
widget.basal!.objectId != other.objectId) &&
|
|
|
|
_startTime.hour == other.startTime.hour &&
|
|
|
|
_startTime.minute == other.startTime.minute)
|
|
|
|
.isNotEmpty) {
|
|
|
|
error = 'There\'s already a rate with this start time.';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (basalRates
|
|
|
|
.where((other) =>
|
|
|
|
(widget.basal == null ||
|
|
|
|
widget.basal!.objectId != other.objectId) &&
|
|
|
|
DateTimeUtils.convertTimeOfDayToDateTime(_startTime)
|
|
|
|
.isBefore(other.startTime) &&
|
|
|
|
DateTimeUtils.convertTimeOfDayToDateTime(_endTime)
|
|
|
|
.isAfter(other.startTime))
|
|
|
|
.isNotEmpty) {
|
|
|
|
error = 'This rate\'s time period overlaps with another one.';
|
|
|
|
}
|
|
|
|
|
|
|
|
return error == null
|
|
|
|
? null
|
|
|
|
: showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
return AlertDialog(
|
|
|
|
content: Text(error!),
|
|
|
|
actions: <Widget>[
|
|
|
|
TextButton(
|
|
|
|
onPressed: () => Navigator.pop(context, 'CANCEL'),
|
|
|
|
child: const Text('GO BACK TO EDITING'),
|
|
|
|
),
|
|
|
|
ElevatedButton(
|
|
|
|
onPressed: () => Navigator.pop(context, 'CONFIRM'),
|
|
|
|
child: const Text('SAVE AS IS'),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-10-22 23:08:09 +00:00
|
|
|
void handleSaveAction() async {
|
2021-10-27 19:00:28 +00:00
|
|
|
setState(() {
|
|
|
|
_isSaving = true;
|
|
|
|
});
|
2021-10-22 23:08:09 +00:00
|
|
|
if (_basalForm.currentState!.validate()) {
|
2021-10-26 23:25:40 +00:00
|
|
|
await validateTimePeriod().then((value) async {
|
|
|
|
if (value != 'CANCEL') {
|
|
|
|
bool isNew = widget.basal == null;
|
|
|
|
isNew
|
|
|
|
? await Basal.save(
|
|
|
|
startTime:
|
|
|
|
DateTimeUtils.convertTimeOfDayToDateTime(_startTime),
|
|
|
|
endTime: DateTimeUtils.convertTimeOfDayToDateTime(_endTime),
|
|
|
|
units: double.parse(_unitsController.text),
|
|
|
|
basalProfile: widget.basalProfile.objectId!,
|
|
|
|
)
|
|
|
|
: await Basal.update(
|
|
|
|
widget.basal!.objectId!,
|
|
|
|
startTime:
|
|
|
|
DateTimeUtils.convertTimeOfDayToDateTime(_startTime),
|
|
|
|
endTime: DateTimeUtils.convertTimeOfDayToDateTime(_endTime),
|
|
|
|
units: double.parse(_unitsController.text),
|
|
|
|
);
|
|
|
|
Navigator.pop(context, '${isNew ? 'New' : ''} Basal Rate saved');
|
|
|
|
}
|
|
|
|
});
|
2021-10-22 23:08:09 +00:00
|
|
|
}
|
2021-10-27 19:00:28 +00:00
|
|
|
setState(() {
|
|
|
|
_isSaving = false;
|
|
|
|
});
|
2021-10-22 23:08:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void handleCancelAction() {
|
|
|
|
bool isNew = widget.basal == null;
|
|
|
|
if (showConfirmationDialogOnCancel &&
|
|
|
|
((isNew &&
|
2021-10-26 23:25:40 +00:00
|
|
|
(_startTime.hour != (widget.suggestedStartTime?.hour ?? 0) ||
|
|
|
|
_endTime.hour != (widget.suggestedEndTime?.hour ?? 0) ||
|
|
|
|
_startTime.minute !=
|
|
|
|
(widget.suggestedStartTime?.minute ?? 0) ||
|
|
|
|
_endTime.minute != (widget.suggestedEndTime?.minute ?? 0) ||
|
2021-10-22 23:08:09 +00:00
|
|
|
double.tryParse(_unitsController.text) != null)) ||
|
|
|
|
(!isNew &&
|
|
|
|
(TimeOfDay.fromDateTime(widget.basal!.startTime) !=
|
|
|
|
_startTime ||
|
|
|
|
TimeOfDay.fromDateTime(widget.basal!.endTime) != _endTime ||
|
|
|
|
(double.tryParse(_unitsController.text) ?? 0) !=
|
|
|
|
widget.basal!.units)))) {
|
|
|
|
Dialogs.showCancelConfirmationDialog(
|
|
|
|
context: context,
|
|
|
|
isNew: isNew,
|
|
|
|
onSave: handleSaveAction,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
Navigator.pop(context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
bool isNew = widget.basal == null;
|
|
|
|
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
title: Text(
|
|
|
|
'${isNew ? 'New' : 'Edit'} Basal Rate for ${widget.basalProfile.name}'),
|
|
|
|
),
|
|
|
|
drawer: const Navigation(currentLocation: BasalDetailScreen.routeName),
|
|
|
|
body: Column(
|
|
|
|
children: [
|
|
|
|
StyledForm(
|
|
|
|
formState: _basalForm,
|
|
|
|
fields: [
|
|
|
|
Row(
|
|
|
|
children: [
|
|
|
|
Expanded(
|
2021-10-25 23:11:58 +00:00
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.only(right: 5),
|
|
|
|
child: StyledTimeOfDayFormField(
|
|
|
|
label: 'Start Time',
|
|
|
|
controller: _startTimeController,
|
|
|
|
time: _startTime,
|
|
|
|
onChanged: (newStartTime) {
|
|
|
|
if (newStartTime != null) {
|
|
|
|
setState(() {
|
|
|
|
_startTime = newStartTime;
|
|
|
|
});
|
|
|
|
updateStartTime();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
2021-10-22 23:08:09 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
Expanded(
|
2021-10-25 23:11:58 +00:00
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.only(left: 5),
|
|
|
|
child: StyledTimeOfDayFormField(
|
|
|
|
label: 'End Time',
|
|
|
|
controller: _endTimeController,
|
|
|
|
time: _endTime,
|
|
|
|
onChanged: (newEndTime) {
|
|
|
|
if (newEndTime != null) {
|
|
|
|
setState(() {
|
|
|
|
_endTime = newEndTime;
|
|
|
|
});
|
|
|
|
updateEndTime();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
2021-10-22 23:08:09 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
TextFormField(
|
|
|
|
controller: _unitsController,
|
|
|
|
keyboardType:
|
|
|
|
const TextInputType.numberWithOptions(decimal: true),
|
|
|
|
decoration: const InputDecoration(
|
|
|
|
labelText: 'Units',
|
|
|
|
suffixText: 'U',
|
|
|
|
),
|
|
|
|
validator: (value) {
|
|
|
|
if (value!.trim().isEmpty) {
|
|
|
|
return 'Empty amount of units';
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
bottomNavigationBar: DetailBottomRow(
|
|
|
|
onCancel: handleCancelAction,
|
2021-10-27 19:00:28 +00:00
|
|
|
onSave: _isSaving ? null : handleSaveAction,
|
2021-10-22 23:08:09 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|