use of garbagecollectors.com.unipool.models.Message in project UniPool by divya21raj.
the class UtilityMethods method getPersonalMessageMap.
public static TreeMap<Long, Message> getPersonalMessageMap(HashMap<String, HashMap<String, Message>> messages, String userId) {
TreeMap<Long, Message> messageMap = new TreeMap<>();
for (Map.Entry<String, HashMap<String, Message>> entry : messages.entrySet()) {
if (entry.getKey().contains(userId)) {
HashMap<String, Message> messagesHashMap = entry.getValue();
for (Map.Entry<String, Message> e : messagesHashMap.entrySet()) {
Message message = e.getValue();
messageMap.put(message.getCreatedAtTime(), message);
}
break;
}
}
return messageMap;
}
use of garbagecollectors.com.unipool.models.Message in project UniPool by divya21raj.
the class UtilityMethods method getMessageList.
public static List<Message> getMessageList(HashMap<String, ArrayList<Message>> messages, String userId) {
ArrayList<Message> personalMessageList;
HashMap<String, PairUp> pairUpList = BaseActivity.getFinalCurrentUser().getPairUps();
String pairUpId = null;
for (Map.Entry<String, PairUp> entry : pairUpList.entrySet()) {
PairUp pairUp = entry.getValue();
if (pairUp.getCreatorId().equals(userId) || pairUp.getRequesterId().equals(userId)) {
pairUpId = pairUp.getPairUpId();
break;
}
}
List messageList = messages.get(pairUpId);
if (messageList != null)
personalMessageList = new ArrayList<>(messages.get(pairUpId));
else {
personalMessageList = new ArrayList<>();
messages.put(pairUpId, personalMessageList);
}
return personalMessageList;
}
use of garbagecollectors.com.unipool.models.Message in project UniPool by divya21raj.
the class MessageListActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_message_list);
messagesLayout = findViewById(R.id.layout1);
sendButton = findViewById(R.id.sendButton);
messageArea = findViewById(R.id.message_edit_text);
scrollView = findViewById(R.id.scrollView);
personalMessageMap = new TreeMap<>();
messagesOnScreen = new ArrayList<>();
messageProgressDialog = new ProgressDialog(this);
messageProgressDialog.setCanceledOnTouchOutside(false);
messageProgressDialog.setMessage("Fetching your messages...");
messageProgressDialog.show();
setScrollViewToBottom();
DatabaseReference userMessageDatabaseReference = null;
try {
userMessageDatabaseReference = FirebaseDatabase.getInstance().getReference("messages/" + BaseActivity.getFinalCurrentUser().getUserId());
} catch (Exception e) {
// Could be NPE, fall back to locally stored USER_ID
userMessageDatabaseReference = FirebaseDatabase.getInstance().getReference("messages/" + Globals.USER_ID);
}
/*load from local*/
userMessageDatabaseReference.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Message message = dataSnapshot.getValue(Message.class);
if (message != null) {
UtilityMethods.putMessageInMap(BaseActivity.getMessages(), message);
if (!message.getMessageId().equals("def@ult") && (message.getSenderId().equals(chatUser.getUserId()) || message.getReceiverId().equals(chatUser.getUserId()))) {
personalMessageMap.containsKey(message.getCreatedAtTime());
if (!messagesOnScreen.contains(message.getMessageId())) {
showMessage(message);
}
} else if (message.getMessageId().equals("def@ult"))
messageProgressDialog.dismiss();
}
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Failed to read value
Log.w("Hello", "Failed to read value.", databaseError.toException());
Toast.makeText(getApplicationContext(), "Problems! Couldn't fetch messages...", Toast.LENGTH_LONG).show();
}
});
sendButton.setOnClickListener(view -> {
String typedMessage = messageArea.getText().toString();
if (!typedMessage.isEmpty()) {
Message message = new Message("", pairUp.getPairUpId(), typedMessage, BaseActivity.getFinalCurrentUser().getUserId(), chatUser.getUserId(), UtilityMethods.getCurrentTime());
// online update
UtilityMethods.putMessageOnDB(message, chatUser, BaseActivity.getFinalCurrentUser());
// local update
UtilityMethods.putMessageInMap(BaseActivity.getMessages(), message);
HashMap<String, String> notificationObject = new HashMap<>();
notificationObject.put("from", BaseActivity.getFinalCurrentUser().getUserId());
notificationObject.put("type", "chat");
Globals.notificationDatabaseReference.child(chatUser.getUserId()).push().setValue(notificationObject);
messageArea.setText("");
personalMessageMap.put(message.getCreatedAtTime(), message);
showMessage(message);
}
});
// detecting if keyboard on-screen
final View activityRootView = findViewById(R.id.activity_message_list);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
if (heightDiff > UtilityMethods.dpToPx(MessageListActivity.this, 200))
setScrollViewToBottom();
});
}
Aggregations