Created
October 15, 2023 08:16
-
-
Save tsherdiwala/56fe37a31047526ee6b52cab3be2711a to your computer and use it in GitHub Desktop.
Creation of new message for Fanbib
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
private void createNewMessage(String message) { | |
String chatId = getIntent().getStringExtra("channelId"); | |
String loggedInUserId = Preferences.get(context, Preferences.KEY_USER_ID); | |
String loggedInUserName = ""; // TODO : get user name | |
DatabaseReference reference = FirebaseDatabase | |
.getInstance() | |
.getReference("/chatMessages/" + chatId + "/messages"); | |
long currentTime = System.currentTimeMillis(); | |
Map<String, Object> value = new HashMap<>(); | |
value.put("author", loggedInUserId); | |
value.put("messageText", message); | |
value.put("messageType", "text"); | |
value.put("time", currentTime); | |
reference.push().setValue(value); | |
FirebaseDatabase.getInstance().getReference("/chatroomUsers/" + chatId).get() | |
.addOnSuccessListener(new OnSuccessListener<DataSnapshot>() { | |
@Override | |
public void onSuccess(DataSnapshot dataSnapshot) { | |
Map<String, Object> valueMap = (Map<String, Object>) dataSnapshot.getValue(); | |
Iterator<String> keys = valueMap.keySet().iterator(); | |
Map<String, Object> value = new HashMap<>(); | |
value.put("lastMessage", message); | |
Map<String, Object> messageBy = new HashMap<>(); | |
messageBy.put("name", loggedInUserName); | |
messageBy.put("userId", Preferences.get(context, Preferences.KEY_USER_ID)); | |
value.put("lastMessageBy", messageBy); | |
value.put("lastMessageTime", currentTime); | |
for (Iterator<String> it = keys; it.hasNext(); ) { | |
String userId = it.next(); | |
String refPath = "/userChatRooms/" + userId + "/" + chatId; | |
String otherUserName = getIntent().getStringExtra("username"); | |
Map<String, Object> copyValue = new HashMap<String, Object>(value); | |
if (userId.equals(loggedInUserId)) { | |
// for the logged in user id set title as the other user id | |
copyValue.put("title", otherUserName); | |
} else { | |
// for non logged in user id use the user id of the logged in user | |
copyValue.put("title", loggedInUserName); | |
} | |
FirebaseDatabase | |
.getInstance() | |
.getReference(refPath) | |
.setValue(copyValue); | |
} | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment