2021-10-22 23:08:09 +00:00
|
|
|
import 'package:diameter/components/dialogs.dart';
|
|
|
|
import 'package:diameter/config.dart';
|
2021-10-25 23:11:58 +00:00
|
|
|
import 'package:diameter/utils/date_time_utils.dart';
|
2021-10-22 23:08:09 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:diameter/components/progress_indicator.dart';
|
|
|
|
import 'package:diameter/models/basal.dart';
|
|
|
|
import 'package:diameter/models/basal_profile.dart';
|
|
|
|
import 'package:diameter/screens/basal/basal_detail.dart';
|
|
|
|
|
|
|
|
class BasalListScreen extends StatefulWidget {
|
|
|
|
final BasalProfile? basalProfile;
|
|
|
|
|
|
|
|
const BasalListScreen({Key? key, this.basalProfile}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
_BasalListScreenState createState() => _BasalListScreenState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _BasalListScreenState extends State<BasalListScreen> {
|
|
|
|
void refresh({String? message}) {
|
|
|
|
setState(() {
|
|
|
|
if (widget.basalProfile != null) {
|
|
|
|
widget.basalProfile!.basalRates =
|
|
|
|
Basal.fetchAllForBasalProfile(widget.basalProfile!);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
setState(() {
|
|
|
|
if (message != null) {
|
|
|
|
var snackBar = SnackBar(
|
|
|
|
content: Text(message),
|
|
|
|
duration: const Duration(seconds: 2),
|
|
|
|
);
|
|
|
|
ScaffoldMessenger.of(context)
|
|
|
|
..removeCurrentSnackBar()
|
|
|
|
..showSnackBar(snackBar);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void handleEditAction(Basal basal) {
|
|
|
|
Navigator.push(
|
|
|
|
context,
|
|
|
|
MaterialPageRoute(
|
|
|
|
builder: (context) => BasalDetailScreen(
|
|
|
|
basalProfile: widget.basalProfile!,
|
|
|
|
basal: basal,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
).then((message) => refresh(message: message));
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDelete(Basal basal) {
|
|
|
|
basal.delete().then((_) => refresh(message: 'Basal Rate deleted'));
|
|
|
|
}
|
|
|
|
|
|
|
|
void handleDeleteAction(Basal basal) async {
|
|
|
|
if (showConfirmationDialogOnDelete) {
|
|
|
|
Dialogs.showConfirmationDialog(
|
|
|
|
context: context,
|
|
|
|
onConfirm: () => onDelete(basal),
|
|
|
|
message: 'Are you sure you want to delete this Basal Rate?',
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
onDelete(basal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-26 23:25:40 +00:00
|
|
|
String? validateTimePeriod(List<Basal> basalRates, int index) {
|
2021-10-25 23:11:58 +00:00
|
|
|
Basal basal = basalRates[index];
|
|
|
|
|
|
|
|
// check for gaps
|
|
|
|
if (index == 0 &&
|
2021-10-26 23:25:40 +00:00
|
|
|
(basal.startTime.hour != 0 || basal.startTime.minute != 0)) {
|
2021-10-25 23:11:58 +00:00
|
|
|
return 'First Basal of the day needs to start at 00:00';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index > 0) {
|
|
|
|
var lastEndTime = basalRates[index - 1].endTime;
|
|
|
|
if (basal.startTime.isAfter(lastEndTime)) {
|
|
|
|
return 'There\'s a time gap between this and the previous rate';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index == basalRates.length - 1 &&
|
2021-10-26 23:25:40 +00:00
|
|
|
(basal.endTime.hour != 0 || basal.endTime.minute != 0)) {
|
2021-10-25 23:11:58 +00:00
|
|
|
return 'Last Basal of the day needs to end at 00:00';
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for duplicates
|
|
|
|
if (basalRates
|
|
|
|
.where((other) => basal != other && basal.startTime == other.startTime)
|
|
|
|
.isNotEmpty) {
|
|
|
|
return 'There are multiple rates with this start time';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (basalRates
|
|
|
|
.where((other) =>
|
|
|
|
basal.startTime.isBefore(other.startTime) &&
|
|
|
|
basal.endTime.isAfter(other.startTime))
|
|
|
|
.isNotEmpty) {
|
|
|
|
return 'This rate\'s time period overlaps with another one';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-22 23:08:09 +00:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return SingleChildScrollView(
|
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
FutureBuilder<List<Basal>>(
|
|
|
|
future: widget.basalProfile!.basalRates,
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
return ViewWithProgressIndicator(
|
|
|
|
snapshot: snapshot,
|
|
|
|
child: snapshot.data == null || snapshot.data!.isEmpty
|
|
|
|
? const Padding(
|
|
|
|
padding: EdgeInsets.all(10.0),
|
|
|
|
child: Text('No Basal Rates for this Profile'),
|
|
|
|
)
|
2021-10-25 23:11:58 +00:00
|
|
|
: ListView.builder(
|
|
|
|
shrinkWrap: true,
|
|
|
|
itemCount:
|
|
|
|
snapshot.data != null ? snapshot.data!.length : 0,
|
|
|
|
itemBuilder: (context, index) {
|
|
|
|
final basal = snapshot.data![index];
|
|
|
|
final error =
|
2021-10-26 23:25:40 +00:00
|
|
|
validateTimePeriod(snapshot.data!, index);
|
2021-10-25 23:11:58 +00:00
|
|
|
return ListTile(
|
|
|
|
tileColor:
|
|
|
|
error != null ? Colors.red.shade100 : null,
|
|
|
|
onTap: () {
|
|
|
|
handleEditAction(basal);
|
|
|
|
},
|
|
|
|
title: Row(
|
|
|
|
mainAxisSize: MainAxisSize.max,
|
|
|
|
children: [
|
|
|
|
Expanded(
|
|
|
|
child: Text(
|
|
|
|
'${DateTimeUtils.displayTime(basal.startTime)} - ${DateTimeUtils.displayTime(basal.endTime)}')),
|
|
|
|
const Spacer(),
|
|
|
|
Expanded(child: Text('${basal.units} U')),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
subtitle: error != null
|
|
|
|
? Text(error,
|
|
|
|
style: const TextStyle(color: Colors.red))
|
|
|
|
: Container(),
|
|
|
|
trailing: Row(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: [
|
|
|
|
IconButton(
|
|
|
|
icon: const Icon(
|
|
|
|
Icons.delete,
|
|
|
|
color: Colors.blue,
|
|
|
|
),
|
|
|
|
onPressed: () => handleDeleteAction(basal),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
2021-10-22 23:08:09 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|