updateWithRequestLocationAcknowledge method

Future updateWithRequestLocationAcknowledge(
  1. LocationNotificationModel originalLocationNotificationModel, {
  2. bool rePrompt = false,
})

Updates originally created locationNotificationModel with originalLocationNotificationModel data If rePrompt is true, then will show dialog box on receiver's side.

Implementation

Future updateWithRequestLocationAcknowledge(
    LocationNotificationModel originalLocationNotificationModel,
    {bool rePrompt = false}) async {
  try {
    var locationNotificationModel = LocationNotificationModel.fromJson(
        jsonDecode(
            LocationNotificationModel.convertLocationNotificationToJson(
                originalLocationNotificationModel)));

    var atkeyMicrosecondId = locationNotificationModel.key!
        .split('requestlocation-')[1]
        .split('@')[0];

    var response =
        await AtLocationNotificationListener().atClientInstance!.getKeys(
              regex: 'requestlocation-$atkeyMicrosecondId',
            );

    var key = getAtKey(response[0]);

    /// in received location [atsignCreator] & [receiver] are interchanged
    key.sharedBy = locationNotificationModel.receiver;
    key.sharedWith = locationNotificationModel.atsignCreator;

    if ((locationNotificationModel.isAccepted) &&
        (locationNotificationModel.from != null) &&
        (locationNotificationModel.to != null)) {
      key.metadata.ttl = locationNotificationModel.to!
              .difference(locationNotificationModel.from!)
              .inMinutes *
          60000;
      key.metadata.ttr = locationNotificationModel.to!
              .difference(locationNotificationModel.from!)
              .inMinutes *
          60000;
      key.metadata.expiresAt = locationNotificationModel.to;
    }

    locationNotificationModel.isAcknowledgment = true;
    locationNotificationModel.rePrompt = rePrompt;

    var notification =
        LocationNotificationModel.convertLocationNotificationToJson(
            locationNotificationModel);
    var result;
    result = await NotifyAndPut().notifyAndPut(
      key,
      notification,
    );

    if (result) {
      /// only update
      for (var i = 0;
          i < KeyStreamService().allLocationNotifications.length;
          i++) {
        if (KeyStreamService()
            .allLocationNotifications[i]
            .locationNotificationModel!
            .key!
            .contains(atkeyMicrosecondId)) {
          KeyStreamService()
              .allLocationNotifications[i]
              .locationNotificationModel = locationNotificationModel;
          // _locationDataNotPresent = false;
        }
      }
      KeyStreamService().notifyListeners();
    }

    _logger.finer('update result - $result');

    return result;
  } catch (e) {
    return false;
  }
}