deleteSelectedMessage method

Future<bool> deleteSelectedMessage(
  1. String? id
)

Implementation

Future<bool> deleteSelectedMessage(String? id) async {
  var key = AtKey()
    ..key = storageKey + (isGroupChat ? groupChatId! : '') + (chatWithAtSign ?? ' ').substring(1)
    ..sharedBy = currentAtSign!
    ..sharedWith = chatWithAtSign
    ..metadata = Metadata();
  key.metadata.ccd = true;

  try {
    for (var i = 0; i < chatHistoryMessages.length; i++) {
      var message = Message.fromJson(chatHistoryMessages[i]);
      if (message.id == id && message.contentType == MessageContentType.image) {
        // removing 'AtKey{' and ending '}'
        var savedKey = message.message?.substring(6, (message.message?.length ?? 1) - 1);
        var key = constructKey(savedKey ?? '');

        await atClientManager.atClient.delete(key);
      }
    }
    chatHistoryMessages.removeWhere((e) {
      var message = Message.fromJson(e);
      return message.id == id;
    });
    var result = await atClientManager.atClient.put(key, json.encode(chatHistoryMessages));
    await getChatHistory();
    return result;
  } catch (e) {
    return false;
  }
}