requestLocationAcknowledgment method

Future<bool> requestLocationAcknowledgment(
  1. LocationNotificationModel originalLocationNotificationModel,
  2. bool isAccepted, {
  3. bool sendAck = false,
  4. int? minutes,
  5. bool? isSharing,
})

Sends a 'requestlocationacknowledged' key to originalLocationNotificationModel.receiver with isAccepted as isAccepted and duration of minutes minute

Implementation

Future<bool> requestLocationAcknowledgment(
    LocationNotificationModel originalLocationNotificationModel,
    bool isAccepted,
    {bool sendAck = false,
    int? minutes,
    bool? isSharing}) async {
  try {
    var locationNotificationModel = LocationNotificationModel.fromJson(
        jsonDecode(
            LocationNotificationModel.convertLocationNotificationToJson(
                originalLocationNotificationModel)));

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

    atKey = newAtKey(
      60000,
      'requestlocationacknowledged-$atkeyMicrosecondId',
      locationNotificationModel.receiver,
    );

    locationNotificationModel
      ..isAccepted = isAccepted
      ..isExited = !isAccepted
      ..lat = isAccepted ? 12 : 0
      ..long = isAccepted ? 12 : 0;

    if (isSharing != null) locationNotificationModel.isSharing = isSharing;

    if (isAccepted && (minutes != null)) {
      locationNotificationModel.from = DateTime.now();
      locationNotificationModel.to =
          DateTime.now().add(Duration(minutes: minutes));
    }

    bool? result;

    if (sendAck) {
      result = await NotifyAndPut().notifyAndPut(
        atKey,
        LocationNotificationModel.convertLocationNotificationToJson(
            locationNotificationModel),
      );
      _logger.finer('requestLocationAcknowledgment $result');
    }

    if (result == false) {
      CustomToast().show('Something went wrong , please try again.',
          AtLocationNotificationListener().navKey.currentContext!,
          isError: true);
    } else {
      await KeyStreamService()
          .mapUpdatedLocationDataToWidget(locationNotificationModel);
    }

    return result ?? true;
  } catch (e) {
    CustomToast().show('Something went wrong , please try again.',
        AtLocationNotificationListener().navKey.currentContext,
        isError: true);
    _logger.severe('Error in requestLocationAcknowledgment $e');
    return false;
  }
}