From 7f9f925da4787a15cc693294abbbbe526bb1d1e6 Mon Sep 17 00:00:00 2001 From: Naveen Date: Sun, 11 Dec 2022 14:45:47 +0530 Subject: [PATCH] Properly update text colors on theme change and some code improvements --- .../smsmessenger/activities/MainActivity.kt | 78 ++++++++++++------- .../adapters/ConversationsAdapter.kt | 6 +- 2 files changed, 52 insertions(+), 32 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/MainActivity.kt index b1a072b9..08753e03 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/MainActivity.kt @@ -89,15 +89,18 @@ class MainActivity : SimpleActivity() { super.onResume() setupToolbar(main_toolbar) - if (storedTextColor != getProperTextColor()) { - (conversations_list.adapter as? ConversationsAdapter)?.updateTextColor(getProperTextColor()) + getOrCreateConversationsAdapter().apply { + if (storedTextColor != getProperTextColor()) { + updateTextColor(getProperTextColor()) + } + + if (storedFontSize != config.fontSize) { + updateFontSize() + } + + updateDrafts() } - if (storedFontSize != config.fontSize) { - (conversations_list.adapter as? ConversationsAdapter)?.updateFontSize() - } - - (conversations_list.adapter as? ConversationsAdapter)?.updateDrafts() updateTextColors(main_coordinator) val properPrimaryColor = getProperPrimaryColor() @@ -285,6 +288,25 @@ class MainActivity : SimpleActivity() { } } + private fun getOrCreateConversationsAdapter(): ConversationsAdapter { + var currAdapter = conversations_list.adapter + if (currAdapter == null) { + hideKeyboard() + currAdapter = ConversationsAdapter( + activity = this, + recyclerView = conversations_list, + onRefresh = { notifyDatasetChanged() }, + itemClick = { handleConversationClick(it) } + ) + + conversations_list.adapter = currAdapter + if (areSystemAnimationsEnabled) { + conversations_list.scheduleLayoutAnimation() + } + } + return currAdapter as ConversationsAdapter + } + private fun setupConversations(conversations: ArrayList) { val hasConversations = conversations.isNotEmpty() val sortedConversations = conversations.sortedWith( @@ -301,36 +323,32 @@ class MainActivity : SimpleActivity() { no_conversations_placeholder_2.beGone() } - val currAdapter = conversations_list.adapter - if (currAdapter == null) { - hideKeyboard() - ConversationsAdapter(this, conversations_list) { - Intent(this, ThreadActivity::class.java).apply { - val conversation = it as Conversation - putExtra(THREAD_ID, conversation.threadId) - putExtra(THREAD_TITLE, conversation.title) - startActivity(this) - } - }.apply { - conversations_list.adapter = this - updateConversations(sortedConversations) - } - - if (areSystemAnimationsEnabled) { - conversations_list.scheduleLayoutAnimation() - } - } else { - try { - (currAdapter as ConversationsAdapter).updateConversations(sortedConversations) { - if (currAdapter.currentList.isEmpty()) { + try { + getOrCreateConversationsAdapter().apply { + updateConversations(sortedConversations) { + if (currentList.isEmpty()) { conversations_fastscroller.beGone() no_conversations_placeholder.text = getString(R.string.no_conversations_found) no_conversations_placeholder.beVisible() no_conversations_placeholder_2.beVisible() } } - } catch (ignored: Exception) { } + } catch (ignored: Exception) { + } + } + + @SuppressLint("NotifyDataSetChanged") + private fun notifyDatasetChanged() { + getOrCreateConversationsAdapter().notifyDataSetChanged() + } + + private fun handleConversationClick(any: Any) { + Intent(this, ThreadActivity::class.java).apply { + val conversation = any as Conversation + putExtra(THREAD_ID, conversation.threadId) + putExtra(THREAD_TITLE, conversation.title) + startActivity(this) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ConversationsAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ConversationsAdapter.kt index cdaffab0..4797800e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ConversationsAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ConversationsAdapter.kt @@ -1,5 +1,6 @@ package com.simplemobiletools.smsmessenger.adapters +import android.annotation.SuppressLint import android.content.Intent import android.graphics.Typeface import android.os.Parcelable @@ -31,8 +32,9 @@ import com.simplemobiletools.smsmessenger.models.Conversation import kotlinx.android.synthetic.main.item_conversation.view.* class ConversationsAdapter( - activity: SimpleActivity, recyclerView: MyRecyclerView, itemClick: (Any) -> Unit -) : MyRecyclerViewListAdapter(activity, recyclerView, ConversationDiffCallback(), itemClick), RecyclerViewFastScroller.OnPopupTextUpdate { + activity: SimpleActivity, recyclerView: MyRecyclerView, onRefresh: () -> Unit, itemClick: (Any) -> Unit +) : MyRecyclerViewListAdapter(activity, recyclerView, ConversationDiffCallback(), itemClick, onRefresh), + RecyclerViewFastScroller.OnPopupTextUpdate { private var fontSize = activity.getTextSize() private var drafts = HashMap()