add active profile check on saving bolus/basal profiles
This commit is contained in:
		
							parent
							
								
									a38886d6f2
								
							
						
					
					
						commit
						d887f477f9
					
				
					 7 changed files with 172 additions and 11 deletions
				
			
		| 
						 | 
					@ -43,7 +43,20 @@ class BasalProfile {
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  static Future<void> setAllInactiveButOne(String? objectId) async {
 | 
					  static Future<int> getActiveCount() async {
 | 
				
			||||||
 | 
					    QueryBuilder<ParseObject> query =
 | 
				
			||||||
 | 
					        QueryBuilder<ParseObject>(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<void> setAllInactive({String? exception}) async {
 | 
				
			||||||
    QueryBuilder<ParseObject> query =
 | 
					    QueryBuilder<ParseObject> query =
 | 
				
			||||||
        QueryBuilder<ParseObject>(ParseObject('BasalProfile'));
 | 
					        QueryBuilder<ParseObject>(ParseObject('BasalProfile'));
 | 
				
			||||||
    final ParseResponse apiResponse = await query.query();
 | 
					    final ParseResponse apiResponse = await query.query();
 | 
				
			||||||
| 
						 | 
					@ -51,7 +64,7 @@ class BasalProfile {
 | 
				
			||||||
    if (apiResponse.success && apiResponse.results != null) {
 | 
					    if (apiResponse.success && apiResponse.results != null) {
 | 
				
			||||||
      for (var basalProfile in apiResponse.results as List<ParseObject>) {
 | 
					      for (var basalProfile in apiResponse.results as List<ParseObject>) {
 | 
				
			||||||
        basalProfile.set(
 | 
					        basalProfile.set(
 | 
				
			||||||
            'active', basalProfile.objectId == objectId ? true : false);
 | 
					            'active', basalProfile.objectId == exception ? true : false);
 | 
				
			||||||
        await basalProfile.save();
 | 
					        await basalProfile.save();
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -43,7 +43,20 @@ class BolusProfile {
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  static Future<void> setAllInactiveButOne(String? objectId) async {
 | 
					  static Future<int> getActiveCount() async {
 | 
				
			||||||
 | 
					    QueryBuilder<ParseObject> query =
 | 
				
			||||||
 | 
					        QueryBuilder<ParseObject>(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<void> setAllInactive({String? exception}) async {
 | 
				
			||||||
    QueryBuilder<ParseObject> query =
 | 
					    QueryBuilder<ParseObject> query =
 | 
				
			||||||
        QueryBuilder<ParseObject>(ParseObject('BolusProfile'));
 | 
					        QueryBuilder<ParseObject>(ParseObject('BolusProfile'));
 | 
				
			||||||
    final ParseResponse apiResponse = await query.query();
 | 
					    final ParseResponse apiResponse = await query.query();
 | 
				
			||||||
| 
						 | 
					@ -51,7 +64,7 @@ class BolusProfile {
 | 
				
			||||||
    if (apiResponse.success && apiResponse.results != null) {
 | 
					    if (apiResponse.success && apiResponse.results != null) {
 | 
				
			||||||
      for (var bolusProfile in apiResponse.results as List<ParseObject>) {
 | 
					      for (var bolusProfile in apiResponse.results as List<ParseObject>) {
 | 
				
			||||||
        bolusProfile.set(
 | 
					        bolusProfile.set(
 | 
				
			||||||
            'active', bolusProfile.objectId == objectId ? true : false);
 | 
					            'active', bolusProfile.objectId == exception ? true : false);
 | 
				
			||||||
        await bolusProfile.save();
 | 
					        await bolusProfile.save();
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -95,9 +95,77 @@ class _BasalProfileDetailScreenState extends State<BasalProfileDetailScreen> {
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  Future<void> 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 {
 | 
					  void handleSaveAction() async {
 | 
				
			||||||
    // TODO: if this would be the second active profile, prompt for deactivating that one
 | 
					 | 
				
			||||||
    if (_basalProfileForm.currentState!.validate()) {
 | 
					    if (_basalProfileForm.currentState!.validate()) {
 | 
				
			||||||
 | 
					      await checkActiveProfiles();
 | 
				
			||||||
      bool isNew = widget.basalProfile == null;
 | 
					      bool isNew = widget.basalProfile == null;
 | 
				
			||||||
      isNew
 | 
					      isNew
 | 
				
			||||||
          ? await BasalProfile.save(
 | 
					          ? await BasalProfile.save(
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -92,7 +92,7 @@ class _BasalProfileListScreenState extends State<BasalProfileListScreen> {
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  void onPickActive(BasalProfile basalProfile) {
 | 
					  void onPickActive(BasalProfile basalProfile) {
 | 
				
			||||||
    BasalProfile.setAllInactiveButOne(basalProfile.objectId!).then((_) =>
 | 
					    BasalProfile.setAllInactive(exception: basalProfile.objectId!).then((_) =>
 | 
				
			||||||
        refresh(
 | 
					        refresh(
 | 
				
			||||||
            message:
 | 
					            message:
 | 
				
			||||||
                '${basalProfile.name} has been set as your active Profile'));
 | 
					                '${basalProfile.name} has been set as your active Profile'));
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -8,7 +8,6 @@ import 'package:flutter/material.dart';
 | 
				
			||||||
import 'package:diameter/components/forms.dart';
 | 
					import 'package:diameter/components/forms.dart';
 | 
				
			||||||
import 'package:diameter/models/bolus_profile.dart';
 | 
					import 'package:diameter/models/bolus_profile.dart';
 | 
				
			||||||
import 'package:diameter/screens/bolus/bolus_list.dart';
 | 
					import 'package:diameter/screens/bolus/bolus_list.dart';
 | 
				
			||||||
import 'package:flutter/widgets.dart';
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
class BolusProfileDetailScreen extends StatefulWidget {
 | 
					class BolusProfileDetailScreen extends StatefulWidget {
 | 
				
			||||||
  static const String routeName = '/bolus-profile';
 | 
					  static const String routeName = '/bolus-profile';
 | 
				
			||||||
| 
						 | 
					@ -96,9 +95,77 @@ class _BolusProfileDetailScreenState extends State<BolusProfileDetailScreen> {
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  Future<void> 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 {
 | 
					  void handleSaveAction() async {
 | 
				
			||||||
    // TODO: if this would be the second active profile, prompt for deactivating that one
 | 
					 | 
				
			||||||
    if (_bolusProfileForm.currentState!.validate()) {
 | 
					    if (_bolusProfileForm.currentState!.validate()) {
 | 
				
			||||||
 | 
					      await checkActiveProfiles();
 | 
				
			||||||
      bool isNew = widget.bolusProfile == null;
 | 
					      bool isNew = widget.bolusProfile == null;
 | 
				
			||||||
      isNew
 | 
					      isNew
 | 
				
			||||||
          ? await BolusProfile.save(
 | 
					          ? await BolusProfile.save(
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -92,7 +92,7 @@ class _BolusProfileListScreenState extends State<BolusProfileListScreen> {
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  void onPickActive(BolusProfile bolusProfile) {
 | 
					  void onPickActive(BolusProfile bolusProfile) {
 | 
				
			||||||
    BolusProfile.setAllInactiveButOne(bolusProfile.objectId!).then((_) =>
 | 
					    BolusProfile.setAllInactive(exception: bolusProfile.objectId!).then((_) =>
 | 
				
			||||||
        refresh(
 | 
					        refresh(
 | 
				
			||||||
            message:
 | 
					            message:
 | 
				
			||||||
                '${bolusProfile.name} has been set as your active Profile'));
 | 
					                '${bolusProfile.name} has been set as your active Profile'));
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -28,7 +28,7 @@ packages:
 | 
				
			||||||
      name: characters
 | 
					      name: characters
 | 
				
			||||||
      url: "https://pub.dartlang.org"
 | 
					      url: "https://pub.dartlang.org"
 | 
				
			||||||
    source: hosted
 | 
					    source: hosted
 | 
				
			||||||
    version: "1.1.0"
 | 
					    version: "1.2.0"
 | 
				
			||||||
  charcode:
 | 
					  charcode:
 | 
				
			||||||
    dependency: transitive
 | 
					    dependency: transitive
 | 
				
			||||||
    description:
 | 
					    description:
 | 
				
			||||||
| 
						 | 
					@ -531,7 +531,7 @@ packages:
 | 
				
			||||||
      name: vector_math
 | 
					      name: vector_math
 | 
				
			||||||
      url: "https://pub.dartlang.org"
 | 
					      url: "https://pub.dartlang.org"
 | 
				
			||||||
    source: hosted
 | 
					    source: hosted
 | 
				
			||||||
    version: "2.1.0"
 | 
					    version: "2.1.1"
 | 
				
			||||||
  web_socket_channel:
 | 
					  web_socket_channel:
 | 
				
			||||||
    dependency: transitive
 | 
					    dependency: transitive
 | 
				
			||||||
    description:
 | 
					    description:
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue