notifyAndPut method

Future<bool> notifyAndPut(
  1. AtKey atKey,
  2. dynamic value, {
  3. bool saveDataIfUndelivered = false,
})

Notifies and puts the value of an AtKey

Implementation

Future<bool> notifyAndPut(AtKey atKey, dynamic value,
    {bool saveDataIfUndelivered = false}) async {
  try {
    /// because .notify and .put will append the namespace
    /// and we dont want atKey.namespace.namespace
    atKey = removeNamespaceFromKey(atKey);

    if (!atKey.sharedBy!.contains('@')) {
      atKey.sharedBy = '@${atKey.sharedBy!}';
    }

    if (!atKey.sharedWith!.contains('@')) {
      atKey.sharedWith = '@${atKey.sharedWith!}';
    }

    var result = await AtClientManager.getInstance()
        .atClient
        .notificationService
        .notify(
          NotificationParams.forUpdate(
            atKey,
            value: value,
          ),
        );

    _logger.finer(
        'notifyAndPut result for $atKey - $result ${result.atClientException}');

    if ((saveDataIfUndelivered) ||
        (result.notificationStatusEnum == NotificationStatusEnum.delivered)) {
      /// because .notify and .put will append the namespace
      /// and we dont want atKey.namespace.namespace
      atKey = removeNamespaceFromKey(atKey);

      atKey.sharedWith = null;
      await AtClientManager.getInstance().atClient.put(
            atKey,
            value,
          );
      return true;
    }
    return false;
  } catch (e) {
    _logger.severe('Error in notifyAndPut $e');
    return false;
  }
}