sendRequestLocationEvent method

Future<bool?> sendRequestLocationEvent(
  1. String? atsign
)

Sends a 'requestlocation' key to atsign.

Implementation

Future<bool?> sendRequestLocationEvent(String? atsign) async {
  try {
    var alreadySharingLocation = checkForAlreadyExistingShareLocation(atsign);

    if (alreadySharingLocation[0]) {
      await locationPromptDialog(
        text: '$atsign is already sharing their location',
        isShareLocationData: false,
        isRequestLocationData: false,
        onlyText: true,
      );

      return null;
    }

    var alreadyExists = checkForAlreadyExisting(atsign);
    var result;

    if ((alreadyExists[0]) &&
        (!KeyStreamService().isPastNotification(alreadyExists[1]))) {
      var newLocationNotificationModel = LocationNotificationModel.fromJson(
          jsonDecode(
              LocationNotificationModel.convertLocationNotificationToJson(
                  alreadyExists[1])));

      var isNotResponded =
          checkIfEventIsNotResponded(newLocationNotificationModel);

      newLocationNotificationModel.rePrompt = true;

      if (isNotResponded) {
        await locationPromptDialog(
            text:
                'You have already requested $atsign\'s location but they have not yet responded. Would you like to prompt them again?',
            locationNotificationModel: newLocationNotificationModel,
            isShareLocationData: false,
            isRequestLocationData: true,
            yesText: 'Yes! Re-Prompt',
            noText: 'No');

        return null;
      }

      var isRejected = checkIfEventIsRejected(newLocationNotificationModel);
      if (isRejected) {
        await locationPromptDialog(
          text:
              'You have already requested $atsign\'s location and your request was rejected. Would you like to prompt them again?',
          locationNotificationModel: newLocationNotificationModel,
          isShareLocationData: false,
          isRequestLocationData: true,
          yesText: 'Yes! Re-Prompt',
          noText: 'No',
        );

        return null;
      }

      await locationPromptDialog(
        text: 'You have already requested $atsign',
        isShareLocationData: false,
        isRequestLocationData: false,
        onlyText: true,
      );

      return null;
    }

    var minutes = 24 * 60; // for a day

    var atKey = newAtKey(60000,
        'requestlocation-${DateTime.now().microsecondsSinceEpoch}', atsign,
        ttl: (minutes * 60000));

    var locationNotificationModel = LocationNotificationModel()
      ..atsignCreator = atsign
      ..key = atKey.key
      ..isRequest = true
      ..isSharing = true
      ..receiver = AtLocationNotificationListener()
          .atClientInstance!
          .getCurrentAtSign();

    result = await NotifyAndPut().notifyAndPut(
      atKey,
      LocationNotificationModel.convertLocationNotificationToJson(
          locationNotificationModel),
    );
    _logger.finer('requestLocationNotification:$result');

    if (result) {
      await KeyStreamService().addDataToList(locationNotificationModel);
    }
    return result;
  } catch (e) {
    _logger.finer('error in requestLocationNotification: $e');
    return false;
  }
}