diameter/lib/screens/bolus/bolus_profile_list.dart

266 lines
8.7 KiB
Dart
Raw Normal View History

import 'package:diameter/utils/dialog_utils.dart';
import 'package:diameter/components/forms/auto_complete_dropdown_button.dart';
import 'package:diameter/models/bolus.dart';
import 'package:diameter/models/settings.dart';
2021-10-22 23:08:09 +00:00
import 'package:diameter/navigation.dart';
import 'package:flutter/material.dart';
import 'package:diameter/models/bolus_profile.dart';
import 'package:diameter/screens/bolus/bolus_profile_detail.dart';
class BolusProfileListScreen extends StatefulWidget {
static const String routeName = '/bolus-profiles';
const BolusProfileListScreen({Key? key}) : super(key: key);
@override
_BolusProfileListScreenState createState() => _BolusProfileListScreenState();
}
class _BolusProfileListScreenState extends State<BolusProfileListScreen> {
2021-12-09 05:14:55 +00:00
final ScrollController _scrollController = ScrollController();
List<BolusProfile> _bolusProfiles = [];
2021-10-22 23:08:09 +00:00
Widget banner = Container();
final BolusProfile? _activeProfile = BolusProfile.getActive(DateTime.now());
@override
void initState() {
super.initState();
reload();
}
@override
void dispose() {
_scrollController.dispose();
super.dispose();
}
void reload({String? message}) {
2021-10-22 23:08:09 +00:00
setState(() {
_bolusProfiles = BolusProfile.getAll();
2021-10-22 23:08:09 +00:00
});
updateBanner();
2021-10-22 23:08:09 +00:00
setState(() {
if (message != null) {
var snackBar = SnackBar(
content: Text(message),
duration: const Duration(seconds: 2),
);
ScaffoldMessenger.of(context)
..removeCurrentSnackBar()
..showSnackBar(snackBar);
}
});
}
void updateBanner() {
int activeProfileCount = BolusProfile.activeCount();
2021-10-22 23:08:09 +00:00
setState(() {
banner = activeProfileCount != 1
? MaterialBanner(
content: Text(activeProfileCount == 0
? 'You currently do not have an active Bolus Profile.'
: 'More than one active Bolus Profile has been found.'),
leading: const CircleAvatar(child: Icon(Icons.warning)),
forceActionsBelow: true,
actions: activeProfileCount == 0
? [
_bolusProfiles.isNotEmpty
2021-10-22 23:08:09 +00:00
? TextButton(
child: const Text('ACTIVATE A PROFILE'),
onPressed: handlePickActiveProfileAction,
)
: Container(),
TextButton(
child: const Text('CREATE A NEW PROFILE'),
onPressed: () => onNew(true),
),
]
: [
TextButton(
child: const Text('PICK A PROFILE'),
onPressed: handlePickActiveProfileAction,
),
],
)
: Container();
});
}
void handleDuplicateAction(BolusProfile bolusProfile) async {
final copy = BolusProfile(
active: false,
name: 'Copy of ${bolusProfile.name}',
);
BolusProfile.put(copy);
final rates = Bolus.getAllForProfile(bolusProfile.id);
for (Bolus rate in rates) {
final bolus = Bolus(
endTime: rate.endTime,
startTime: rate.startTime,
units: rate.units,
carbs: rate.carbs,
mgPerDl: rate.mgPerDl,
mmolPerL: rate.mmolPerL,
);
bolus.bolusProfile.target = copy;
Bolus.put(bolus);
}
reload(message: 'Added copy of ${bolusProfile.name}');
}
2021-10-22 23:08:09 +00:00
void onDelete(BolusProfile bolusProfile) {
BolusProfile.remove(bolusProfile.id);
reload(message: 'Bolus Profile deleted');
2021-10-22 23:08:09 +00:00
}
void handleDeleteAction(BolusProfile bolusProfile) async {
if (Settings.get().showConfirmationDialogOnDelete) {
DialogUtils.showConfirmationDialog(
2021-10-22 23:08:09 +00:00
context: context,
onConfirm: () => onDelete(bolusProfile),
message: 'Are you sure you want to delete this Bolus Profile?',
);
} else {
onDelete(bolusProfile);
}
}
void onPickActive(BolusProfile? bolusProfile) {
if (bolusProfile != null) {
BolusProfile.setAllInactive;
bolusProfile.active = true;
BolusProfile.put(bolusProfile);
2021-12-09 05:14:55 +00:00
reload(
message: '${bolusProfile.name} has been set as your active Profile');
}
2021-10-22 23:08:09 +00:00
}
void handlePickActiveProfileAction() {
setState(() {
banner = MaterialBanner(
content: AutoCompleteDropdownButton(
2021-12-09 05:14:55 +00:00
controller: TextEditingController(text: ''),
items: _bolusProfiles,
label: 'Default Basal Profile',
onChanged: onPickActive,
),
2021-10-22 23:08:09 +00:00
leading: const CircleAvatar(child: Icon(Icons.info)),
forceActionsBelow: true,
actions: [
TextButton(
child: const Text('CREATE A NEW PROFILE INSTEAD'),
onPressed: () => onNew(true),
),
],
);
});
}
void showDetailScreen({BolusProfile? bolusProfile, bool active = false}) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
BolusProfileDetailScreen(id: bolusProfile?.id ?? 0, active: active),
2021-10-22 23:08:09 +00:00
),
2021-12-09 05:14:55 +00:00
).then((result) => reload(message: result?[0]));
2021-10-22 23:08:09 +00:00
}
void onNew(bool active) {
showDetailScreen(active: active);
}
void onEdit(BolusProfile bolusProfile) {
showDetailScreen(bolusProfile: bolusProfile);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Bolus Profiles'),
actions: <Widget>[
IconButton(onPressed: reload, icon: const Icon(Icons.refresh))
2021-10-22 23:08:09 +00:00
],
),
drawer:
const Navigation(currentLocation: BolusProfileListScreen.routeName),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
banner,
Expanded(
child: _bolusProfiles.isNotEmpty
2021-12-09 05:14:55 +00:00
? Scrollbar(
controller: _scrollController,
child: ListView.builder(
padding: const EdgeInsets.all(10.0),
controller: _scrollController,
itemCount: _bolusProfiles.length,
itemBuilder: (context, index) {
final bolusProfile = _bolusProfiles[index];
String activeProfileText = bolusProfile.active
? ' (Default Profile)'
: bolusProfile.id == _activeProfile?.id
? ' (Current Active Profile)'
: '';
return Card(
child: ListTile(
selected: bolusProfile.active ||
bolusProfile.id == _activeProfile?.id,
onTap: () => onEdit(bolusProfile),
title: Text(
bolusProfile.name.toUpperCase() +
activeProfileText,
style: Theme.of(context).textTheme.subtitle2,
),
subtitle: Padding(
padding: const EdgeInsets.only(top: 10.0),
child: Text(bolusProfile.notes ?? ''),
),
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: [
IconButton(
icon: const Icon(
Icons.copy,
color: Colors.blue,
),
onPressed: () =>
handleDuplicateAction(bolusProfile),
),
2021-12-09 05:14:55 +00:00
IconButton(
icon: const Icon(
Icons.delete,
color: Colors.blue,
),
onPressed: () =>
handleDeleteAction(bolusProfile),
),
],
),
2021-12-09 05:14:55 +00:00
),
);
},
),
)
: const Center(
child: Text('You have not created any Bolus Profiles yet!'),
),
2021-10-22 23:08:09 +00:00
),
],
),
floatingActionButton: FloatingActionButton(
onPressed: () => onNew(false),
child: const Icon(Icons.add),
),
);
}
}