286 lines
9.4 KiB
Dart
286 lines
9.4 KiB
Dart
import 'package:diameter/localization_keys.dart';
|
|
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';
|
|
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';
|
|
import 'package:flutter_translate/flutter_translate.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> {
|
|
final ScrollController _scrollController = ScrollController();
|
|
|
|
List<BolusProfile> _bolusProfiles = [];
|
|
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}) {
|
|
setState(() {
|
|
_bolusProfiles = BolusProfile.getAll();
|
|
});
|
|
|
|
updateBanner();
|
|
|
|
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();
|
|
|
|
setState(() {
|
|
banner = activeProfileCount != 1
|
|
? MaterialBanner(
|
|
content: Text(activeProfileCount == 0
|
|
? translate(LocalizationKeys.bolusProfile_warnings_noActive)
|
|
: translate(LocalizationKeys.bolusProfile_warnings_multipleActive)),
|
|
leading: const CircleAvatar(child: Icon(Icons.warning)),
|
|
forceActionsBelow: true,
|
|
actions: activeProfileCount == 0
|
|
? [
|
|
_bolusProfiles.isNotEmpty
|
|
? TextButton(
|
|
child: Text(translate(LocalizationKeys.bolusProfile_warnings_resolve_activate).toUpperCase()),
|
|
onPressed: handlePickActiveProfileAction,
|
|
)
|
|
: Container(),
|
|
TextButton(
|
|
child: Text(translate(LocalizationKeys.bolusProfile_warnings_resolve_create).toUpperCase()),
|
|
onPressed: () => onNew(true),
|
|
),
|
|
]
|
|
: [
|
|
TextButton(
|
|
child: Text(translate(LocalizationKeys.bolusProfile_warnings_resolve_pick).toUpperCase()),
|
|
onPressed: handlePickActiveProfileAction,
|
|
),
|
|
],
|
|
)
|
|
: Container();
|
|
});
|
|
}
|
|
|
|
void handleDuplicateAction(BolusProfile bolusProfile) async {
|
|
final copy = BolusProfile(
|
|
active: false,
|
|
name: translate(
|
|
LocalizationKeys.bolusProfile_copyOf,
|
|
args: {
|
|
"profileName": 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: translate(
|
|
LocalizationKeys.bolusProfile_copied,
|
|
args: {
|
|
"profileName": bolusProfile.name
|
|
},
|
|
),
|
|
);
|
|
}
|
|
|
|
void onDelete(BolusProfile bolusProfile) {
|
|
BolusProfile.remove(bolusProfile.id);
|
|
reload(message: translate(LocalizationKeys.bolusProfile_deleted));
|
|
}
|
|
|
|
void handleDeleteAction(BolusProfile bolusProfile) async {
|
|
if (Settings.get().showConfirmationDialogOnDelete) {
|
|
DialogUtils.showConfirmationDialog(
|
|
context: context,
|
|
onConfirm: () => onDelete(bolusProfile),
|
|
message: translate(LocalizationKeys.bolusProfile_confirmDelete),
|
|
);
|
|
} else {
|
|
onDelete(bolusProfile);
|
|
}
|
|
}
|
|
|
|
void onPickActive(BolusProfile? bolusProfile) {
|
|
if (bolusProfile != null) {
|
|
BolusProfile.setAllInactive;
|
|
bolusProfile.active = true;
|
|
BolusProfile.put(bolusProfile);
|
|
reload(
|
|
message: translate(
|
|
LocalizationKeys.bolusProfile_activated,
|
|
args: {
|
|
"profileName": bolusProfile.name
|
|
},
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
void handlePickActiveProfileAction() {
|
|
setState(() {
|
|
banner = MaterialBanner(
|
|
content: AutoCompleteDropdownButton(
|
|
controller: TextEditingController(text: ''),
|
|
items: _bolusProfiles,
|
|
label: translate(LocalizationKeys.bolusProfile_default),
|
|
onChanged: onPickActive,
|
|
),
|
|
leading: const CircleAvatar(child: Icon(Icons.info)),
|
|
forceActionsBelow: true,
|
|
actions: [
|
|
TextButton(
|
|
child: Text(translate(LocalizationKeys.bolusProfile_warnings_resolve_createInstead).toUpperCase()),
|
|
onPressed: () => onNew(true),
|
|
),
|
|
],
|
|
);
|
|
});
|
|
}
|
|
|
|
void showDetailScreen({BolusProfile? bolusProfile, bool active = false}) {
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) =>
|
|
BolusProfileDetailScreen(id: bolusProfile?.id ?? 0, active: active),
|
|
),
|
|
).then((result) => reload(message: result?[0]));
|
|
}
|
|
|
|
void onNew(bool active) {
|
|
showDetailScreen(active: active);
|
|
}
|
|
|
|
void onEdit(BolusProfile bolusProfile) {
|
|
showDetailScreen(bolusProfile: bolusProfile);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: Text(translate(LocalizationKeys.bolusProfile_title)),
|
|
actions: <Widget>[
|
|
IconButton(onPressed: reload, icon: const Icon(Icons.refresh))
|
|
],
|
|
),
|
|
drawer:
|
|
const Navigation(currentLocation: BolusProfileListScreen.routeName),
|
|
body: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
banner,
|
|
Expanded(
|
|
child: _bolusProfiles.isNotEmpty
|
|
? 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
|
|
? ' (${translate(LocalizationKeys.bolusProfile_default)})'
|
|
: bolusProfile.id == _activeProfile?.id
|
|
? ' (${translate(LocalizationKeys.bolusProfile_active)})'
|
|
: '';
|
|
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),
|
|
),
|
|
IconButton(
|
|
icon: const Icon(
|
|
Icons.delete,
|
|
color: Colors.blue,
|
|
),
|
|
onPressed: () =>
|
|
handleDeleteAction(bolusProfile),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
)
|
|
: Center(
|
|
child: Text(translate(LocalizationKeys.bolusProfile_empty)),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
floatingActionButton: FloatingActionButton(
|
|
onPressed: () => onNew(false),
|
|
child: const Icon(Icons.add),
|
|
),
|
|
);
|
|
}
|
|
}
|