update the conversation if something changes, avoid recreating it

This commit is contained in:
tibbi 2021-01-01 20:45:10 +01:00
parent ac7d2afbb6
commit 71d7085ddb
2 changed files with 22 additions and 6 deletions

View File

@ -41,6 +41,7 @@ import com.simplemobiletools.smsmessenger.helpers.*
import com.simplemobiletools.smsmessenger.models.*
import com.simplemobiletools.smsmessenger.receivers.SmsStatusDeliveredReceiver
import com.simplemobiletools.smsmessenger.receivers.SmsStatusSentReceiver
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_thread.*
import kotlinx.android.synthetic.main.item_attachment.view.*
import kotlinx.android.synthetic.main.item_selected_contact.view.*
@ -221,8 +222,13 @@ class ThreadActivity : SimpleActivity() {
invalidateOptionsMenu()
runOnUiThread {
val adapter = ThreadAdapter(this, threadItems, thread_messages_list, thread_messages_fastscroller) {}
thread_messages_list.adapter = adapter
val currAdapter = thread_messages_list.adapter
if (currAdapter == null) {
val adapter = ThreadAdapter(this, threadItems, thread_messages_list, thread_messages_fastscroller) {}
thread_messages_list.adapter = adapter
} else {
(currAdapter as ThreadAdapter).updateMessages(threadItems)
}
}
SimpleContactsHelper(this).getAvailableContacts(false) { contacts ->

View File

@ -195,10 +195,13 @@ class ThreadAdapter(activity: SimpleActivity, var messages: ArrayList<ThreadItem
private fun isThreadDateTime(position: Int) = messages.getOrNull(position) is ThreadDateTime
override fun onViewRecycled(holder: ViewHolder) {
super.onViewRecycled(holder)
if (!activity.isDestroyed && !activity.isFinishing && holder.itemView.thread_message_sender_photo != null) {
Glide.with(activity).clear(holder.itemView.thread_message_sender_photo)
fun updateMessages(newMessages: ArrayList<ThreadItem>) {
val oldHashCode = messages.hashCode()
val newHashCode = newMessages.hashCode()
if (newHashCode != oldHashCode) {
messages = newMessages
notifyDataSetChanged()
recyclerView.scrollToPosition(messages.size-1)
}
}
@ -347,4 +350,11 @@ class ThreadAdapter(activity: SimpleActivity, var messages: ArrayList<ThreadItem
setTextColor(textColor)
}
}
override fun onViewRecycled(holder: ViewHolder) {
super.onViewRecycled(holder)
if (!activity.isDestroyed && !activity.isFinishing && holder.itemView.thread_message_sender_photo != null) {
Glide.with(activity).clear(holder.itemView.thread_message_sender_photo)
}
}
}