Only populate viewHolder with messages retrieved from database?

Viewed 1273

I'm following the Android Firebase Codelab.

Here's the project of the Friendly Chat app I'm attempting to modify: https://github.com/firebase/friendlychat-android/tree/master/android

I want the MessageViewHolder to be populated only after retrieving a message from the database. Unfortunately, right now, MessageViewHolder also gets populated locally (after the user presses 'SEND'). I don't want the latter to happen. That means the view holder gets updated twice (once locally and once after retrieval from the database) which is inefficient.

Side-node: Also, the user might think he's online when he presses 'SEND' and sees his MessageViewHolder get populated with his message. That's unwanted!

Please go to line 177 of MainActivity to see populateViewHolder method.

I've been trying to dodge the issue for the last couple of months, but now I really need the problem to be solved to continue working. What changes do I have to make to achieve what I want?

5 Answers

Here´s an optional solution. Why not take advantage of the fact that the chat message adapter fires twice. (as long as you dont use a transaction) First time the local call you save the message in local db with an id. The adapter presents the message in the chat with a progress wheel running like "sending message... When the second call fire the adapter pull the chat message from the db and sets it as "sent" and hence update the adapter with the sent message.

This way you can chat while offline just like Google hangout. This way if app restart/crash all not sent messages can have like a "send again" option. Also adapter can now easily show new messages highlighted and old(viewed) messages as normal.

Related