Last active
July 1, 2021 15:58
-
-
Save zeeshan-mehdi/2c777239f9f3e6f6ddb27b2ae8aec434 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
addMessage(Conversation _conversation, String text) { | |
Chat _chat = new Chat(text, DateTime.now().toUtc().millisecondsSinceEpoch, currentUser.value.id); | |
if (_conversation.id == null) { | |
_conversation.id = UniqueKey().toString(); | |
createConversation(_conversation); | |
} | |
_conversation.lastMessage = text; | |
_conversation.lastMessageTime = _chat.time; | |
_conversation.readByUsers = [currentUser.value.id]; | |
_chatRepository.addMessage(_conversation, _chat).then((value) { | |
_conversation.users.forEach((_user) { | |
if (_user.id != currentUser.value.id) { | |
sendNotification(text, S.of(state.context).newMessageFrom + " " + currentUser.value.name, _user); | |
} | |
}); | |
}); | |
} | |
//chat repository function thats saves to firestore | |
Future<void> addMessage(Conversation conversation, Chat chat) { | |
return FirebaseFirestore.instance.collection("conversations").doc(conversation.id).collection("chats").add(chat.toMap()).whenComplete(() { | |
updateConversation(conversation.id, conversation.toUpdatedMap()); | |
}).catchError((e) { | |
print(e.toString()); | |
}); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment