2021-10-22 23:08:09 +00:00
|
|
|
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/basal_profile.dart';
|
|
|
|
import 'package:diameter/screens/basal/basal_profile_detail.dart';
|
|
|
|
|
|
|
|
class BasalProfileListScreen extends StatefulWidget {
|
|
|
|
static const String routeName = '/basal-profiles';
|
|
|
|
const BasalProfileListScreen({Key? key}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
_BasalProfileListScreenState createState() => _BasalProfileListScreenState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _BasalProfileListScreenState extends State<BasalProfileListScreen> {
|
|
|
|
late Future<List<BasalProfile>?> _basalProfiles;
|
|
|
|
Widget banner = Container();
|
|
|
|
bool pickActiveProfileMode = false;
|
|
|
|
|
|
|
|
void refresh({String? message}) {
|
|
|
|
setState(() {
|
|
|
|
pickActiveProfileMode = false;
|
|
|
|
_basalProfiles = BasalProfile.fetchAll();
|
|
|
|
});
|
|
|
|
_basalProfiles.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 Basal Profile.'
|
|
|
|
: 'More than one active Basal 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(BasalProfile basalProfile) {
|
|
|
|
basalProfile
|
|
|
|
.delete()
|
|
|
|
.then((_) => refresh(message: 'Basal Profile deleted'));
|
|
|
|
}
|
|
|
|
|
|
|
|
void handleDeleteAction(BasalProfile basalProfile) async {
|
|
|
|
if (showConfirmationDialogOnDelete) {
|
|
|
|
Dialogs.showConfirmationDialog(
|
|
|
|
context: context,
|
|
|
|
onConfirm: () => onDelete(basalProfile),
|
|
|
|
message: 'Are you sure you want to delete this Basal Profile?',
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
onDelete(basalProfile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void onPickActive(BasalProfile basalProfile) {
|
2021-10-23 00:19:25 +00:00
|
|
|
BasalProfile.setAllInactive(exception: basalProfile.objectId!).then((_) =>
|
2021-10-22 23:08:09 +00:00
|
|
|
refresh(
|
|
|
|
message:
|
|
|
|
'${basalProfile.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({BasalProfile? basalProfile, bool active = false}) {
|
|
|
|
Navigator.push(
|
|
|
|
context,
|
|
|
|
MaterialPageRoute(
|
|
|
|
builder: (context) => BasalProfileDetailScreen(
|
|
|
|
basalProfile: basalProfile, active: active),
|
|
|
|
),
|
|
|
|
).then((message) => refresh(message: message));
|
|
|
|
}
|
|
|
|
|
|
|
|
void onNew(bool active) {
|
|
|
|
showDetailScreen(active: active);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onEdit(BasalProfile basalProfile) {
|
|
|
|
showDetailScreen(basalProfile: basalProfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
title: const Text('Basal Profiles'),
|
|
|
|
actions: <Widget>[
|
|
|
|
IconButton(onPressed: refresh, icon: const Icon(Icons.refresh))
|
|
|
|
],
|
|
|
|
),
|
|
|
|
drawer:
|
|
|
|
const Navigation(currentLocation: BasalProfileListScreen.routeName),
|
|
|
|
body: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
banner,
|
|
|
|
Expanded(
|
|
|
|
child: FutureBuilder<List<BasalProfile>?>(
|
|
|
|
future: _basalProfiles,
|
|
|
|
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 Basal Profiles'),
|
|
|
|
),
|
|
|
|
])
|
|
|
|
: ListView.builder(
|
|
|
|
itemCount:
|
|
|
|
snapshot.data != null ? snapshot.data!.length : 0,
|
|
|
|
itemBuilder: (context, index) {
|
|
|
|
final basalProfile = snapshot.data![index];
|
|
|
|
return ListTile(
|
|
|
|
tileColor: basalProfile.active
|
|
|
|
? Colors.green.shade100
|
|
|
|
: null,
|
|
|
|
onTap: () {
|
|
|
|
pickActiveProfileMode
|
|
|
|
? onPickActive(basalProfile)
|
|
|
|
: onEdit(basalProfile);
|
|
|
|
},
|
|
|
|
title: Text(
|
|
|
|
basalProfile.name,
|
|
|
|
),
|
|
|
|
subtitle: Text(basalProfile.notes!),
|
|
|
|
trailing: Row(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: [
|
|
|
|
IconButton(
|
|
|
|
icon: const Icon(
|
|
|
|
Icons.delete,
|
|
|
|
color: Colors.blue,
|
|
|
|
),
|
|
|
|
onPressed: () =>
|
|
|
|
handleDeleteAction(basalProfile),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
floatingActionButton: FloatingActionButton(
|
|
|
|
onPressed: () => onNew(false),
|
|
|
|
child: const Icon(Icons.add),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|