From d887f477f99b494ccec81ac6766fbb9a56feea6e Mon Sep 17 00:00:00 2001 From: spinel Date: Sat, 23 Oct 2021 02:19:25 +0200 Subject: [PATCH] add active profile check on saving bolus/basal profiles --- lib/models/basal_profile.dart | 17 ++++- lib/models/bolus_profile.dart | 17 ++++- lib/screens/basal/basal_profile_detail.dart | 70 +++++++++++++++++++- lib/screens/basal/basal_profiles_list.dart | 2 +- lib/screens/bolus/bolus_profile_detail.dart | 71 ++++++++++++++++++++- lib/screens/bolus/bolus_profile_list.dart | 2 +- pubspec.lock | 4 +- 7 files changed, 172 insertions(+), 11 deletions(-) diff --git a/lib/models/basal_profile.dart b/lib/models/basal_profile.dart index ba4e587..ce051cf 100644 --- a/lib/models/basal_profile.dart +++ b/lib/models/basal_profile.dart @@ -43,7 +43,20 @@ class BasalProfile { } } - static Future setAllInactiveButOne(String? objectId) async { + static Future getActiveCount() async { + QueryBuilder query = + QueryBuilder(ParseObject('BasalProfile')) + ..whereEqualTo('active', true); + final ParseResponse apiResponse = await query.query(); + + if (apiResponse.success && apiResponse.results != null) { + return apiResponse.results!.length; + } else { + return 0; + } + } + + static Future setAllInactive({String? exception}) async { QueryBuilder query = QueryBuilder(ParseObject('BasalProfile')); final ParseResponse apiResponse = await query.query(); @@ -51,7 +64,7 @@ class BasalProfile { if (apiResponse.success && apiResponse.results != null) { for (var basalProfile in apiResponse.results as List) { basalProfile.set( - 'active', basalProfile.objectId == objectId ? true : false); + 'active', basalProfile.objectId == exception ? true : false); await basalProfile.save(); } } diff --git a/lib/models/bolus_profile.dart b/lib/models/bolus_profile.dart index 335e60d..dcee764 100644 --- a/lib/models/bolus_profile.dart +++ b/lib/models/bolus_profile.dart @@ -43,7 +43,20 @@ class BolusProfile { } } - static Future setAllInactiveButOne(String? objectId) async { + static Future getActiveCount() async { + QueryBuilder query = + QueryBuilder(ParseObject('BolusProfile')) + ..whereEqualTo('active', true); + final ParseResponse apiResponse = await query.query(); + + if (apiResponse.success && apiResponse.results != null) { + return apiResponse.results!.length; + } else { + return 0; + } + } + + static Future setAllInactive({String? exception}) async { QueryBuilder query = QueryBuilder(ParseObject('BolusProfile')); final ParseResponse apiResponse = await query.query(); @@ -51,7 +64,7 @@ class BolusProfile { if (apiResponse.success && apiResponse.results != null) { for (var bolusProfile in apiResponse.results as List) { bolusProfile.set( - 'active', bolusProfile.objectId == objectId ? true : false); + 'active', bolusProfile.objectId == exception ? true : false); await bolusProfile.save(); } } diff --git a/lib/screens/basal/basal_profile_detail.dart b/lib/screens/basal/basal_profile_detail.dart index eab97f1..8d3768a 100644 --- a/lib/screens/basal/basal_profile_detail.dart +++ b/lib/screens/basal/basal_profile_detail.dart @@ -95,9 +95,77 @@ class _BasalProfileDetailScreenState extends State { }); } + Future checkActiveProfiles() async { + int _activeCount = await BasalProfile.getActiveCount(); + bool isNew = widget.basalProfile == null; + + if (_active && + (_activeCount > 1 || + _activeCount == 1 && (isNew || !widget.basalProfile!.active))) { + await showDialog( + context: context, + builder: (BuildContext context) { + return AlertDialog( + content: const Text( + 'There are already one or more active profiles. What would you like to do?'), + actions: [ + TextButton( + onPressed: () => Navigator.pop(context, 0), + child: const Text('IGNORE'), + ), + TextButton( + onPressed: () => Navigator.pop(context, 1), + child: + Text('DEACTIVATE ${_nameController.text.toUpperCase()}'), + ), + ElevatedButton( + onPressed: () => Navigator.pop(context, 2), + child: const Text('DEACTIVATE ALL OTHERS'), + ) + ], + ); + }).then((value) async { + if (value == 1) { + setState(() { + _active = false; + }); + } else if (value == 2) { + await BasalProfile.setAllInactive(); + } + }); + } else if (!_active && + ((isNew && _activeCount == 0) || + (_activeCount == 1 && widget.basalProfile!.active))) { + await showDialog( + context: context, + builder: (BuildContext context) { + return AlertDialog( + content: const Text( + 'There is currently no active profile. Would you like to set this one as active?'), + actions: [ + TextButton( + onPressed: () => Navigator.pop(context, 0), + child: const Text('IGNORE'), + ), + TextButton( + onPressed: () => Navigator.pop(context, 1), + child: const Text('ACTIVATE THIS PROFILE'), + ), + ], + ); + }).then((value) { + if (value == 1) { + setState(() { + _active = true; + }); + } + }); + } + } + void handleSaveAction() async { - // TODO: if this would be the second active profile, prompt for deactivating that one if (_basalProfileForm.currentState!.validate()) { + await checkActiveProfiles(); bool isNew = widget.basalProfile == null; isNew ? await BasalProfile.save( diff --git a/lib/screens/basal/basal_profiles_list.dart b/lib/screens/basal/basal_profiles_list.dart index 25315dc..b8463f7 100644 --- a/lib/screens/basal/basal_profiles_list.dart +++ b/lib/screens/basal/basal_profiles_list.dart @@ -92,7 +92,7 @@ class _BasalProfileListScreenState extends State { } void onPickActive(BasalProfile basalProfile) { - BasalProfile.setAllInactiveButOne(basalProfile.objectId!).then((_) => + BasalProfile.setAllInactive(exception: basalProfile.objectId!).then((_) => refresh( message: '${basalProfile.name} has been set as your active Profile')); diff --git a/lib/screens/bolus/bolus_profile_detail.dart b/lib/screens/bolus/bolus_profile_detail.dart index bea7225..4a97cae 100644 --- a/lib/screens/bolus/bolus_profile_detail.dart +++ b/lib/screens/bolus/bolus_profile_detail.dart @@ -8,7 +8,6 @@ import 'package:flutter/material.dart'; import 'package:diameter/components/forms.dart'; import 'package:diameter/models/bolus_profile.dart'; import 'package:diameter/screens/bolus/bolus_list.dart'; -import 'package:flutter/widgets.dart'; class BolusProfileDetailScreen extends StatefulWidget { static const String routeName = '/bolus-profile'; @@ -96,9 +95,77 @@ class _BolusProfileDetailScreenState extends State { }); } + Future checkActiveProfiles() async { + int _activeCount = await BolusProfile.getActiveCount(); + bool isNew = widget.bolusProfile == null; + + if (_active && + (_activeCount > 1 || + _activeCount == 1 && (isNew || !widget.bolusProfile!.active))) { + await showDialog( + context: context, + builder: (BuildContext context) { + return AlertDialog( + content: const Text( + 'There are already one or more active profiles. What would you like to do?'), + actions: [ + TextButton( + onPressed: () => Navigator.pop(context, 0), + child: const Text('IGNORE'), + ), + TextButton( + onPressed: () => Navigator.pop(context, 1), + child: + Text('DEACTIVATE ${_nameController.text.toUpperCase()}'), + ), + ElevatedButton( + onPressed: () => Navigator.pop(context, 2), + child: const Text('DEACTIVATE ALL OTHERS'), + ) + ], + ); + }).then((value) async { + if (value == 1) { + setState(() { + _active = false; + }); + } else if (value == 2) { + await BolusProfile.setAllInactive(); + } + }); + } else if (!_active && + ((isNew && _activeCount == 0) || + (_activeCount == 1 && widget.bolusProfile!.active))) { + await showDialog( + context: context, + builder: (BuildContext context) { + return AlertDialog( + content: const Text( + 'There is currently no active profile. Would you like to set this one as active?'), + actions: [ + TextButton( + onPressed: () => Navigator.pop(context, 0), + child: const Text('IGNORE'), + ), + TextButton( + onPressed: () => Navigator.pop(context, 1), + child: const Text('ACTIVATE THIS PROFILE'), + ), + ], + ); + }).then((value) { + if (value == 1) { + setState(() { + _active = true; + }); + } + }); + } + } + void handleSaveAction() async { - // TODO: if this would be the second active profile, prompt for deactivating that one if (_bolusProfileForm.currentState!.validate()) { + await checkActiveProfiles(); bool isNew = widget.bolusProfile == null; isNew ? await BolusProfile.save( diff --git a/lib/screens/bolus/bolus_profile_list.dart b/lib/screens/bolus/bolus_profile_list.dart index 629b5d3..a295451 100644 --- a/lib/screens/bolus/bolus_profile_list.dart +++ b/lib/screens/bolus/bolus_profile_list.dart @@ -92,7 +92,7 @@ class _BolusProfileListScreenState extends State { } void onPickActive(BolusProfile bolusProfile) { - BolusProfile.setAllInactiveButOne(bolusProfile.objectId!).then((_) => + BolusProfile.setAllInactive(exception: bolusProfile.objectId!).then((_) => refresh( message: '${bolusProfile.name} has been set as your active Profile')); diff --git a/pubspec.lock b/pubspec.lock index 206c9af..975a498 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -28,7 +28,7 @@ packages: name: characters url: "https://pub.dartlang.org" source: hosted - version: "1.1.0" + version: "1.2.0" charcode: dependency: transitive description: @@ -531,7 +531,7 @@ packages: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.1.0" + version: "2.1.1" web_socket_channel: dependency: transitive description: