Skip to content

Instantly share code, notes, and snippets.

@zeeshan-mehdi
Last active July 1, 2021 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeeshan-mehdi/2c777239f9f3e6f6ddb27b2ae8aec434 to your computer and use it in GitHub Desktop.
Save zeeshan-mehdi/2c777239f9f3e6f6ddb27b2ae8aec434 to your computer and use it in GitHub Desktop.
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