diameter/lib/screens/basal/basal_list.dart

178 lines
5.9 KiB
Dart
Raw Normal View History

2021-10-22 23:08:09 +00:00
import 'package:diameter/components/dialogs.dart';
import 'package:diameter/config.dart';
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);
}
}
String? checkBasalValidity(List<Basal> basalRates, int index) {
Basal basal = basalRates[index];
// check for gaps
if (index == 0 &&
(basal.startTime.toLocal().hour != 0 || basal.startTime.minute != 0)) {
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 &&
(basal.endTime.toLocal().hour != 0 || basal.endTime.minute != 0)) {
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'),
)
: ListView.builder(
shrinkWrap: true,
itemCount:
snapshot.data != null ? snapshot.data!.length : 0,
itemBuilder: (context, index) {
final basal = snapshot.data![index];
final error =
checkBasalValidity(snapshot.data!, index);
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
),
);
},
),
],
),
);
}
}