219 lines
7.4 KiB
Dart
219 lines
7.4 KiB
Dart
import 'package:diameter/components/dialogs.dart';
|
|
import 'package:diameter/config.dart';
|
|
import 'package:diameter/navigation.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:diameter/components/progress_indicator.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> {
|
|
late Future<List<BolusProfile>?> _bolusProfiles;
|
|
Widget banner = Container();
|
|
bool pickActiveProfileMode = false;
|
|
|
|
void refresh({String? message}) {
|
|
setState(() {
|
|
pickActiveProfileMode = false;
|
|
_bolusProfiles = BolusProfile.fetchAll();
|
|
});
|
|
_bolusProfiles.then((list) => updateBanner(
|
|
list?.where((element) => element.active).length ?? 0,
|
|
list?.isNotEmpty ?? false));
|
|
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, bool isNotEmpty) {
|
|
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
|
|
? [
|
|
isNotEmpty
|
|
? 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 onDelete(BolusProfile bolusProfile) {
|
|
bolusProfile
|
|
.delete()
|
|
.then((_) => refresh(message: 'Bolus Profile deleted'));
|
|
}
|
|
|
|
void handleDeleteAction(BolusProfile bolusProfile) async {
|
|
if (showConfirmationDialogOnDelete) {
|
|
Dialogs.showConfirmationDialog(
|
|
context: context,
|
|
onConfirm: () => onDelete(bolusProfile),
|
|
message: 'Are you sure you want to delete this Bolus Profile?',
|
|
);
|
|
} else {
|
|
onDelete(bolusProfile);
|
|
}
|
|
}
|
|
|
|
void onPickActive(BolusProfile bolusProfile) {
|
|
BolusProfile.setAllInactive(exception: bolusProfile.objectId!).then((_) =>
|
|
refresh(
|
|
message:
|
|
'${bolusProfile.name} has been set as your active Profile'));
|
|
}
|
|
|
|
void handlePickActiveProfileAction() {
|
|
setState(() {
|
|
banner = MaterialBanner(
|
|
content: const Text('Click one of the profiles to active it.'),
|
|
leading: const CircleAvatar(child: Icon(Icons.info)),
|
|
forceActionsBelow: true,
|
|
actions: [
|
|
TextButton(
|
|
child: const Text('CREATE A NEW PROFILE INSTEAD'),
|
|
onPressed: () => onNew(true),
|
|
),
|
|
],
|
|
);
|
|
pickActiveProfileMode = true;
|
|
});
|
|
}
|
|
|
|
void showDetailScreen({BolusProfile? bolusProfile, bool active = false}) {
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) => BolusProfileDetailScreen(
|
|
bolusProfile: bolusProfile, active: active),
|
|
),
|
|
).then((message) => refresh(message: message));
|
|
}
|
|
|
|
void onNew(bool active) {
|
|
showDetailScreen(active: active);
|
|
}
|
|
|
|
void onEdit(BolusProfile bolusProfile) {
|
|
showDetailScreen(bolusProfile: bolusProfile);
|
|
}
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
refresh();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Bolus Profiles'),
|
|
actions: <Widget>[
|
|
IconButton(onPressed: refresh, icon: const Icon(Icons.refresh))
|
|
],
|
|
),
|
|
drawer:
|
|
const Navigation(currentLocation: BolusProfileListScreen.routeName),
|
|
body: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
banner,
|
|
Expanded(
|
|
child: FutureBuilder<List<BolusProfile>?>(
|
|
future: _bolusProfiles,
|
|
builder: (context, snapshot) {
|
|
return ViewWithProgressIndicator(
|
|
snapshot: snapshot,
|
|
child: snapshot.data == null || snapshot.data!.isEmpty
|
|
? Row(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: const [
|
|
Padding(
|
|
padding: EdgeInsets.all(10.0),
|
|
child: Text('No Bolus Profiles'),
|
|
),
|
|
])
|
|
: ListView.builder(
|
|
itemCount:
|
|
snapshot.data != null ? snapshot.data!.length : 0,
|
|
itemBuilder: (context, index) {
|
|
final bolusProfile = snapshot.data![index];
|
|
return ListTile(
|
|
tileColor: bolusProfile.active
|
|
? Colors.green.shade100
|
|
: null,
|
|
onTap: () {
|
|
pickActiveProfileMode
|
|
? onPickActive(bolusProfile)
|
|
: onEdit(bolusProfile);
|
|
},
|
|
title: Text(
|
|
bolusProfile.name,
|
|
),
|
|
subtitle: Text(bolusProfile.notes!),
|
|
trailing: Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
IconButton(
|
|
icon: const Icon(
|
|
Icons.delete,
|
|
color: Colors.blue,
|
|
),
|
|
onPressed: () =>
|
|
handleDeleteAction(bolusProfile),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
},
|
|
),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
],
|
|
),
|
|
floatingActionButton: FloatingActionButton(
|
|
onPressed: () => onNew(false),
|
|
child: const Icon(Icons.add),
|
|
),
|
|
);
|
|
}
|
|
}
|