sendShareLocationEvent method
Sends a 'sharelocation' key to atsign with duration of minutes minute
Implementation
Future<bool?> sendShareLocationEvent(String? atsign, bool isAcknowledgment,
{int? minutes}) async {
try {
var alreadyExists = checkForAlreadyExisting(atsign);
var result;
if (alreadyExists[0]) {
var newLocationNotificationModel = LocationNotificationModel.fromJson(
jsonDecode(
LocationNotificationModel.convertLocationNotificationToJson(
alreadyExists[1])));
var isRejected = checkIfEventIsRejected(newLocationNotificationModel);
if (minutes != null) {
newLocationNotificationModel.to =
DateTime.now().add(Duration(minutes: minutes));
} else {
newLocationNotificationModel.to = null;
}
if (isRejected) {
newLocationNotificationModel.rePrompt = true;
}
var msg = isRejected
? 'Your share location request has been rejected by $atsign. Would you like to prompt them again & update your request ?'
: 'You already are sharing your location with $atsign. Would you like to update it ?';
await locationPromptDialog(
text: msg,
locationNotificationModel: newLocationNotificationModel,
isShareLocationData: true,
isRequestLocationData: false,
yesText: isRejected ? 'Yes! Re-Prompt' : 'Yes! Update',
noText: 'No');
return null;
}
AtKey atKey;
if (minutes != null) {
atKey = newAtKey((minutes * 60000),
'sharelocation-${DateTime.now().microsecondsSinceEpoch}', atsign,
ttl: (minutes * 60000),
expiresAt: DateTime.now().add(Duration(minutes: minutes)));
} else {
atKey = newAtKey(
60000,
'sharelocation-${DateTime.now().microsecondsSinceEpoch}',
atsign,
);
}
var locationNotificationModel = LocationNotificationModel()
..atsignCreator = AtLocationNotificationListener().currentAtSign
..key = atKey.key
..lat = 12
..long = 12
..receiver = atsign
..from = DateTime.now()
..isAcknowledgment = isAcknowledgment;
if ((minutes != null)) {
locationNotificationModel.to =
DateTime.now().add(Duration(minutes: minutes));
}
result = await AtLocationNotificationListener().atClientInstance!.put(
atKey,
LocationNotificationModel.convertLocationNotificationToJson(
locationNotificationModel),
isDedicated: MixedConstants.isDedicated,
);
print('sendLocationNotification:$result');
if (result) {
if (MixedConstants.isDedicated) {
await SyncSecondary().callSyncSecondary(SyncOperation.syncSecondary);
}
await KeyStreamService().addDataToList(locationNotificationModel);
}
return result;
} catch (e) {
print('sending share location failed $e');
return false;
}
}