sendShareLocationEvent method

Future<bool?> sendShareLocationEvent(
  1. String? atsign,
  2. bool isAcknowledgment, {
  3. int? minutes,
})

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]) &&
        (!KeyStreamService().isPastNotification(alreadyExists[1]))) {
      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()
      ..isAccepted = true
      ..isExited = false
      ..isSharing = true
      ..isAcknowledgment = isAcknowledgment;

    if ((minutes != null)) {
      locationNotificationModel.to =
          DateTime.now().add(Duration(minutes: minutes));
    }
    result = await NotifyAndPut().notifyAndPut(
      atKey,
      LocationNotificationModel.convertLocationNotificationToJson(
          locationNotificationModel),
    );
    _logger.finer('sendLocationNotification:$result');

    if (result) {
      await KeyStreamService().addDataToList(locationNotificationModel);
    }
    return result;
  } catch (e) {
    _logger.severe('sending share location failed $e');
    return false;
  }
}