actionSafetyCheck method
- List<
SafetyPlanFallback> fallbackItems, - VoidCallback primaryItem
Takes a list of fallbacks (options that a specific piece of code violates, and the desired fallback to complete instead). You should use this to check if the piece of code you wrote violated a SafetyPlan request.
IMPORTANT: if the parent widget does not subclass from AureusNotificationMaster, the notificationManager will not be able to show a widget. If you're only using Material, you should probably only use the 'executable code' fallback.
To use an action SafetyCheck, provide:
- fallbackItems: A list of rules that you may violate, and the fallback code you want to execute
- primaryItem: the action you want to take if the user's safetyPlan allows it.
For example, before sending a notification you would provide the context, the fallback items in case a user doesn't want notifications (the alt code to execute would be nothing), and the primary item would be scheduling / sending the notification.
Keep in mind this is a future, and requires the proper handling for async code. If you're a beginner, another area you might recognize async code from is a networking request. It's relatively similar to requesting data from an API!
So think of it as querying a user's "safety API" while giving the error handling directly to the API itself. The VoidCallback you're given is the executable code to run.
Implementation
Future<VoidCallback> actionSafetyCheck(
List<SafetyPlanFallback> fallbackItems, VoidCallback primaryItem) async {
VoidCallback executableCode = primaryItem;
for (var element in fallbackItems) {
if (await _SafetyPlanStorageLayer()._readSetting(element.safetyOption) ==
true) {
if (element.fallbackOption == SafetyFallBackOptions.alternateCode) {
executableCode = element.fallbackCode!;
} else if (element.fallbackOption ==
SafetyFallBackOptions.errorController) {
executableCode = () {
var safetyOption =
detailMetaData.retrieveDetails(element.safetyOption).name;
// Creates an alert controller object to show to the user.
var disabledAlertControllerObject =
AlertControllerObject.singleAction(
onCancellation: () => {
notificationMaster.resetRequests(),
},
alertTitle: "Blocked Action",
alertBody:
"You requested an action that was disabled by your Safety Plan settings. The setting blocking this action is $safetyOption. You can go into your settings to change your Safety Plan settings at any time.",
alertIcon: Assets.alertmessage,
actions: [
AlertControllerAction(
actionName: "Okay.",
actionSeverity: AlertControllerActionSeverity.standard,
onSelection: () => {
notificationMaster.resetRequests(),
}),
]);
notificationMaster
.sendAlertControllerRequest(disabledAlertControllerObject);
};
}
}
}
return executableCode;
}