updateRecentChat method

dynamic updateRecentChat({
  1. required String jid,
  2. bool changePosition = true,
})

Implementation

updateRecentChat({required String jid, bool changePosition = true}) async {
  //updateArchiveRecentChat(jid);
  getRecentChatOfJid(jid).then((recent) {
    final index = recentChats.indexWhere((chat) => chat.jid == jid);
    debugPrint("dashboard index--> $index");
    if (recent != null) {
      if (!recent.isChatArchived.checkNull()) {
        if (index.isNegative) {
          recentChats.insert(0, recent);
        } else {
          if (recentChats[index].isChatPinned.checkNull() ||
              !changePosition) {
            recentChats.replaceRange(index, index + 1, [recent]);
          } else {
            var lastPinnedChat = recentChats
                .lastIndexWhere((element) => element.isChatPinned!);
            var nxtIndex =
                lastPinnedChat.isNegative ? 0 : (lastPinnedChat + 1);
            /*if (recentChats[index].isChatPinned!) {
            recentChats.removeAt(index);
            recentChats.insert(index, recent);
          } else {
            recentChats.removeAt(index);
            recentChats.insert(nxtIndex, recent);
            recentChats.refresh();
          }*/
            recentChats.removeAt(index);
            recentChats.insert(nxtIndex, recent);
            recentChats.refresh();
          }
        }
      } else {
        if (!index.isNegative) {
          recentChats.removeAt(index);
        }
      }
      checkArchiveList(recent);
    } else {
      if (!index.isNegative) {
        recentChats.removeAt(index);
      }
    }
    recentChats.refresh();
  });
}