adding a helper function for updating conversations, if necessary

This commit is contained in:
tibbi 2020-05-30 18:28:34 +02:00
parent 7a7b1aab32
commit a013c110ec
2 changed files with 27 additions and 10 deletions

View File

@ -165,16 +165,7 @@ class MainActivity : SimpleActivity() {
conversations_list.beVisibleIf(hasConversations)
no_conversations_placeholder.beVisibleIf(!hasConversations)
no_conversations_placeholder_2.beVisibleIf(!hasConversations)
ConversationsAdapter(this, conversations, conversations_list, conversations_fastscroller) {
Intent(this, ThreadActivity::class.java).apply {
putExtra(THREAD_ID, (it as Conversation).system_id)
putExtra(THREAD_TITLE, it.title)
startActivity(this)
}
}.apply {
conversations_list.adapter = this
}
updateConversations(conversations)
}
}
@ -187,6 +178,23 @@ class MainActivity : SimpleActivity() {
}
}
private fun updateConversations(conversations: ArrayList<Conversation>) {
val currAdapter = conversations_list.adapter
if (currAdapter == null) {
ConversationsAdapter(this, conversations, conversations_list, conversations_fastscroller) {
Intent(this, ThreadActivity::class.java).apply {
putExtra(THREAD_ID, (it as Conversation).system_id)
putExtra(THREAD_TITLE, it.title)
startActivity(this)
}
}.apply {
conversations_list.adapter = this
}
} else {
(currAdapter as ConversationsAdapter).updateConversations(conversations)
}
}
private fun launchNewConversation() {
Intent(this, NewConversationActivity::class.java).apply {
startActivity(this)

View File

@ -173,6 +173,15 @@ class ConversationsAdapter(activity: SimpleActivity, var conversations: ArrayLis
}
}
fun updateConversations(newConversations: ArrayList<Conversation>) {
val oldHashCode = conversations.hashCode()
val newHashCode = newConversations.hashCode()
if (newHashCode != oldHashCode) {
conversations = newConversations
notifyDataSetChanged()
}
}
private fun setupView(view: View, conversation: Conversation) {
view.apply {
conversation_frame.isSelected = selectedKeys.contains(conversation.system_id)