import 'package:flutter/material.dart'; import 'package:muaawana/models/chat_v2/message.dart'; import 'package:muaawana/models/chat_v2/message_list.dart'; class UserConversation extends ChangeNotifier { List messagesList = []; List allConversations = []; String currentChatId=""; MessageListModel? selectedConversation; int notificationCount=0; bool showNotif=false; static final UserConversation userConversation = UserConversation._internal(); factory UserConversation() { return userConversation; } setCurrentChatId(String id , MessageListModel conversation){ selectedConversation = conversation; currentChatId=id; notifyListeners(); } addMessagesToCurrentConversation(List msg) { msg.forEach((element) { if (!messagesList.contains(element)) { messagesList.add(element); messagesList.sort((a, b) => DateTime.fromMillisecondsSinceEpoch(a.timestamp!) .compareTo(DateTime.fromMillisecondsSinceEpoch(b.timestamp!))); } }); notifyListeners(); } addOneMessage(MessageModel messageModel) { if (!messagesList.contains(messageModel)) { messagesList.add(messageModel); messagesList.sort((a, b) => DateTime.fromMillisecondsSinceEpoch(a.timestamp!) .compareTo(DateTime.fromMillisecondsSinceEpoch(b.timestamp!))); } allConversations.sort((a, b) => DateTime.fromMillisecondsSinceEpoch(a.lastmessagetimestmp!) .compareTo(DateTime.fromMillisecondsSinceEpoch(b.lastmessagetimestmp!))); allConversations=allConversations.reversed.toList(); notifyListeners(); } getAllConversations(List conversations){ allConversations = conversations; allConversations.sort((a, b) => DateTime.fromMillisecondsSinceEpoch(a.lastmessagetimestmp!) .compareTo(DateTime.fromMillisecondsSinceEpoch(b.lastmessagetimestmp!))); allConversations=allConversations.reversed.toList(); notifyListeners(); } updateConversation({required MessageListModel conversation,required String message,required int senderId}){ conversation.lastmessage=message; conversation.lastmessagesenderid=senderId; conversation.lastmessagetimestmp=DateTime.now().millisecondsSinceEpoch; allConversations.sort((a, b) => DateTime.fromMillisecondsSinceEpoch(a.lastmessagetimestmp!) .compareTo(DateTime.fromMillisecondsSinceEpoch(b.lastmessagetimestmp!))); allConversations=allConversations.reversed.toList(); notifyListeners(); } setConversationCount({ required MessageListModel message, required int count}){ message.count=0; message.count=count; notifyListeners(); } onDisposeDiscussion() { messagesList.clear(); } onDisposeConversation(){ allConversations.clear(); } UserConversation._internal(); showNotification(){ showNotif=true; notifyListeners(); } hideNotification(){ showNotif=false; notifyListeners(); } addAConversation(MessageListModel conversation){ allConversations.insert(0, conversation); notifyListeners(); } } final userConversationSingleton = UserConversation();