Search in sources :

Example 1 with Message

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;
}
Also used : Message(garbagecollectors.com.unipool.models.Message) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 2 with Message

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;
}
Also used : Message(garbagecollectors.com.unipool.models.Message) ArrayList(java.util.ArrayList) PairUp(garbagecollectors.com.unipool.models.PairUp) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 3 with Message

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();
    });
}
Also used : Message(garbagecollectors.com.unipool.models.Message) DatabaseReference(com.google.firebase.database.DatabaseReference) HashMap(java.util.HashMap) ProgressDialog(android.app.ProgressDialog) DataSnapshot(com.google.firebase.database.DataSnapshot) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) ScrollView(android.widget.ScrollView) DatabaseError(com.google.firebase.database.DatabaseError) ChildEventListener(com.google.firebase.database.ChildEventListener)

Aggregations

Message (garbagecollectors.com.unipool.models.Message)3 HashMap (java.util.HashMap)3 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 ProgressDialog (android.app.ProgressDialog)1 View (android.view.View)1 ImageView (android.widget.ImageView)1 ScrollView (android.widget.ScrollView)1 TextView (android.widget.TextView)1 ChildEventListener (com.google.firebase.database.ChildEventListener)1 DataSnapshot (com.google.firebase.database.DataSnapshot)1 DatabaseError (com.google.firebase.database.DatabaseError)1 DatabaseReference (com.google.firebase.database.DatabaseReference)1 PairUp (garbagecollectors.com.unipool.models.PairUp)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1