updateRecentChat method

Future<bool> updateRecentChat({
  1. required String jid,
  2. bool changePosition = true,
  3. bool newInsertable = false,
})

Implementation

Future<bool> updateRecentChat(
    {required String jid,
    bool changePosition = true,
    bool newInsertable = false}) async {
  //updateArchiveRecentChat(jid);
  var recent = await getRecentChatOfJid(jid);
  final index = recentChats.indexWhere((chat) => chat.jid == jid);
  debugPrint("dashboard index--> $index");
  LogMessage.d("updateRecentChat", recent?.toJson());
  if (recent != null) {
    if (!recent.isChatArchived.checkNull()) {
      if (index.isNegative && newInsertable) {
        LogMessage.d("updateRecentChat", "New Insert");
        recentChats.insert(0, recent);
      } else {
        if (recentChats[index].isChatPinned.checkNull() || !changePosition) {
          // recentChats.removeAt(index);
          // recentChats.insert(index, recent);
          recentChats.replaceRange(index, index + 1, [recent]);
        } else {
          var lastPinnedChat =
              recentChats.lastIndexWhere((element) => element.isChatPinned!);
          var nxtIndex = lastPinnedChat.isNegative ? 0 : (lastPinnedChat + 1);
          LogMessage.d("updateRecentChat", "next Index $nxtIndex");
          recentChats.removeAt(index);
          recentChats.insert(nxtIndex, recent);
          // recentChats.refresh();
        }
      }
    } else {
      LogMessage.d("updateRecentChat", "Archived $index");
      if (!index.isNegative) {
        recentChats.removeAt(index);
      }
    }
    checkArchiveList(recent);
  } else {
    LogMessage.d("updateRecentChat", "recent chat null insert index $index");
    if (!index.isNegative) {
      recentChats.removeAt(index);
    }
  }
  recentChats.refresh();
  return true;
}