sendRequestLocationEvent method

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

Sends a 'requestlocation' key to atsign.

Implementation

Future<bool?> sendRequestLocationEvent(String? atsign) async {
  try {
    var alreadyExists = checkForAlreadyExisting(atsign);
    var result;

    if (alreadyExists[0]) {
      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. But your request has not been responded yet. 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. But your request has been 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 atKey = newAtKey(60000,
        'requestlocation-${DateTime.now().microsecondsSinceEpoch}', atsign);

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

    result = await AtLocationNotificationListener().atClientInstance!.put(
          atKey,
          LocationNotificationModel.convertLocationNotificationToJson(
              locationNotificationModel),
          isDedicated: MixedConstants.isDedicated,
        );
    print('requestLocationNotification:$result');

    if (result) {
      if (MixedConstants.isDedicated) {
        await SyncSecondary().callSyncSecondary(SyncOperation.syncSecondary);
      }
      await KeyStreamService().addDataToList(locationNotificationModel);
    }
    return result;
  } catch (e) {
    return false;
  }
}