Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

addToStart Func not working #78

Closed
takawww opened this issue Aug 15, 2017 · 8 comments
Closed

addToStart Func not working #78

takawww opened this issue Aug 15, 2017 · 8 comments

Comments

@takawww
Copy link

takawww commented Aug 15, 2017

Hi all

I can add new message by using addToStart only in my first time init MessagesList Activity
After I close it and reopen the activity again, addToStart is no longer function.

Any hints?

Best Regards

@jirevwe
Copy link

jirevwe commented Aug 18, 2017

Are you still having this problem?
Can you log out any required info and paste them?

It's pretty straight-forward though.

Message message = new Message.Builder(Message.MessageType.TYPE_TEXT, Utilities.createMessageID())
                .user(SmartAlumni.currentUser)
                .message(message_string)
                .room(roomID)
                .build();
messagesAdapter.addToStart(message, true);

@takawww
Copy link
Author

takawww commented Aug 20, 2017

Hi,

I still have the issue whenever I re-open the activity which contain the MessagesList object
I init the activity by

Activity activity = (Activity) mContext
Intent activityChangeIntent = new Intent(activity, MsgBoxActivity.class);
activity.startActivity(activityChangeIntent);

In the activity, i have created a global observer to refresh the message list view by calling loadMessages and triggered by global timer.

protected void loadMessages() {
        new Handler().postDelayed(new Runnable() { //imitation of internet connection
            @Override
            public void run() {
                ArrayList<Message> messages = new ArrayList<>();
                for (int i = 0; i < MainActivity.msgboxObjs.size(); i++) {
                    if (MainActivity.msgboxObjs.get(i).respID.equals(LoginActivity.pollMan.selMsgGpID)) {
                        MsgBoxObj msgBoxObj = MainActivity.msgboxObjs.get(i);
                        MessageObj messageObj = LoginActivity.pollMan.selMsgGp;

                        if (!isFirst){
                            if (!msgBoxObj.isNew){
                                continue;
                            }
                        }

                        msgBoxObj.isNew = false;
                        Message message;
                        message = getMessage(messageObj, msgBoxObj);
                        messages.add(message);
                    }
                }

                Collections.sort(messages, new Comparator<Message>() {
                    @Override
                    public int compare(Message o1, Message o2) {
                        if (o1.getCreatedAt().after(o2.getCreatedAt())){
                            return -1;
                        }else{
                            return 1;
                        }

                    }
                });

                if (isFirst){
                    isFirst = false;
                    if (messages.size() > 0){
                        messagesAdapter.addToEnd(messages, false);
                    }
                }else{
                    for (int i=0;i<messages.size();i++){
                        messagesAdapter.addToStart(messages.get(i), true);
                  
                    }
                }





            }


        }, 1000);


    }

getMessage is function to create message object from my custom message object

public static Message getMessage(MessageObj messageObj, MsgBoxObj msgObj) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        Date dtCreateDate;

        if (msgObj.msgType.equals("O")){
            if (msgObj.msg.length() > 5){
                if (msgObj.msg.substring(0,5).equals("ATTA:")){
                    String url = LoginActivity.sysPath + "/" + LoginActivity.sysApp + "/upload/msg/" + messageObj.respID + "/" + msgObj.msg.substring(5);

                    User user = new User("0", messageObj.ownerName, messageObj.ownerImg, true);
                    Message message = new Message(msgObj.msgID, user, null);
                    try{
                        message.setImage(new Message.Image(url));
                    }catch (Exception ex){
                        ex.printStackTrace();
                    }

                    try {
                        dtCreateDate = format.parse(msgObj.createDate + msgObj.createTime);
                        message.setCreatedAt(dtCreateDate);
                    }catch (Exception ex){

                    }

                    return message;
                }else{
                    User user = new User("0", messageObj.ownerName, messageObj.ownerImg, true);
                    try {
                        dtCreateDate = format.parse(msgObj.createDate + msgObj.createTime);
                        return new Message(msgObj.msgID, user, msgObj.msg,dtCreateDate);
                    }catch (Exception ex){
                        return new Message(msgObj.msgID, user, msgObj.msg);
                    }

                }
            }else{
                User user = new User("0", messageObj.ownerName, messageObj.ownerImg, true);
                try {
                    dtCreateDate = format.parse(msgObj.createDate + msgObj.createTime);
                    return new Message(msgObj.msgID, user, msgObj.msg,dtCreateDate);
                }catch (Exception ex){
                    return new Message(msgObj.msgID, user, msgObj.msg);
                }

            }
        }else{
            if (msgObj.msg.length() > 5){
                if (msgObj.msg.substring(0,5).equals("ATTA:")){
                    String url = LoginActivity.sysPath + "/" + LoginActivity.sysApp + "/upload/msg/" + messageObj.respID + "/" + msgObj.msg.substring(5);

                    User user = new User("1", messageObj.userName, messageObj.userImg, true);
                    Message message = new Message(msgObj.msgID, user, null);
                    try{
                        message.setImage(new Message.Image(url));
                    }catch (Exception ex){
                        ex.printStackTrace();
                    }

                    try {
                        dtCreateDate = format.parse(msgObj.createDate + msgObj.createTime);
                        message.setCreatedAt(dtCreateDate);
                    }catch (Exception ex){

                    }
                    return message;
                }else{
                    User user = new User("1", messageObj.userName, messageObj.userImg, true);
                    try {
                        dtCreateDate = format.parse(msgObj.createDate + msgObj.createTime);
                        return new Message(msgObj.msgID, user, msgObj.msg, dtCreateDate);
                    }catch (Exception ex){
                        return new Message(msgObj.msgID, user, msgObj.msg);
                    }

                }
            }else{
                User user = new User("1", messageObj.userName, messageObj.userImg, true);
                try {
                    dtCreateDate = format.parse(msgObj.createDate + msgObj.createTime);
                    return new Message(msgObj.msgID, user, msgObj.msg, dtCreateDate);
                }catch (Exception ex){
                    return new Message(msgObj.msgID, user, msgObj.msg);
                }

            }
        }
    }

Regards

@takawww
Copy link
Author

takawww commented Aug 20, 2017

Hi,

The problem is temporary fixed

I have narrowed down the issue, which that it is happened inside global observer receive function.

private BroadcastReceiver msgBoxListViewRefreshReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            for (int i = 0; i < MainActivity.msgboxObjs.size(); i++) {
                if (MainActivity.msgboxObjs.get(i).respID.equals(LoginActivity.pollMan.selMsgGpID)) {
                    MsgBoxObj msgBoxObj = MainActivity.msgboxObjs.get(i);
                    MessageObj messageObj = LoginActivity.pollMan.selMsgGp;

                    if (! msgBoxObj.isNew) {
                        msgBoxObj.isNew = false;
                        
                        Message message;
                        message = getMessage(messageObj, msgBoxObj);
                        messagesAdapter.addToStart(message, true);
                    }
                }
            }
        }
    };

If i set a condition if(!msgObj.isNew){....addToStart(...) },
During my 2nd time to init the Activity, when global observer receive function is triggered,
addToStart can be fired, but no new message is shown in the messageList.

Now that I have change the condition to if (msgObj.createDate > lastMsgDate){...addToStart(...)},

private BroadcastReceiver msgBoxListViewRefreshReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            for (int i = 0; i < MainActivity.msgboxObjs.size(); i++) {
                if (MainActivity.msgboxObjs.get(i).respID.equals(LoginActivity.pollMan.selMsgGpID)) {
                    MsgBoxObj msgBoxObj = MainActivity.msgboxObjs.get(i);
                    MessageObj messageObj = LoginActivity.pollMan.selMsgGp;

                    if (Integer.parseInt(msgBoxObj.createDate) > Integer.parseInt(lastShownDate)) {
                        msgBoxObj.isNew = false;
                        lastShownDate = msgBoxObj.createDate;
                        lastShownTime = msgBoxObj.createTime;

                        Message message;
                        message = getMessage(messageObj, msgBoxObj);
                        messagesAdapter.addToStart(message, true);
                    }else{
                        if (Integer.parseInt(msgBoxObj.createDate) == Integer.parseInt(lastShownDate)) {
                            if (Integer.parseInt(msgBoxObj.createTime) > Integer.parseInt(lastShownTime)) {
                                msgBoxObj.isNew = false;
                                lastShownDate = msgBoxObj.createDate;
                                lastShownTime = msgBoxObj.createTime;

                                Message message;
                                message = getMessage(messageObj, msgBoxObj);
                                messagesAdapter.addToStart(message, true);
                            }
                        }
                    }
                }
            }
        }
    };

it is work!!!

Problem is so weird. and I have no conclusion of the root cause
but at least I can continue my work.

@ibmshaikh
Copy link

Please Someone help me how to add data in Dialog Please.

@jirevwe
Copy link

jirevwe commented Aug 20, 2017

Hmm... I looked at the code before didn't understand much though.

Though I thought the problem would be from the loadMessages() function, but since you say it's working, cool... @takawww you can close the issue if you've solved the problem

@jirevwe
Copy link

jirevwe commented Aug 20, 2017

@ibmshaikh create a new issue

@takawww
Copy link
Author

takawww commented Aug 20, 2017

Sure.. Thanks

@takawww takawww closed this as completed Aug 20, 2017
@Nizomjon1994
Copy link

@takawww Hi
Are you using rocket chat ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants