Properly update text colors on theme change

and some code improvements
This commit is contained in:
Naveen 2022-12-11 14:45:47 +05:30
parent 716ebad21e
commit 7f9f925da4
2 changed files with 52 additions and 32 deletions

View File

@ -89,15 +89,18 @@ class MainActivity : SimpleActivity() {
super.onResume() super.onResume()
setupToolbar(main_toolbar) setupToolbar(main_toolbar)
getOrCreateConversationsAdapter().apply {
if (storedTextColor != getProperTextColor()) { if (storedTextColor != getProperTextColor()) {
(conversations_list.adapter as? ConversationsAdapter)?.updateTextColor(getProperTextColor()) updateTextColor(getProperTextColor())
} }
if (storedFontSize != config.fontSize) { if (storedFontSize != config.fontSize) {
(conversations_list.adapter as? ConversationsAdapter)?.updateFontSize() updateFontSize()
}
updateDrafts()
} }
(conversations_list.adapter as? ConversationsAdapter)?.updateDrafts()
updateTextColors(main_coordinator) updateTextColors(main_coordinator)
val properPrimaryColor = getProperPrimaryColor() 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<Conversation>) { private fun setupConversations(conversations: ArrayList<Conversation>) {
val hasConversations = conversations.isNotEmpty() val hasConversations = conversations.isNotEmpty()
val sortedConversations = conversations.sortedWith( val sortedConversations = conversations.sortedWith(
@ -301,37 +323,33 @@ class MainActivity : SimpleActivity() {
no_conversations_placeholder_2.beGone() 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 { try {
(currAdapter as ConversationsAdapter).updateConversations(sortedConversations) { getOrCreateConversationsAdapter().apply {
if (currAdapter.currentList.isEmpty()) { updateConversations(sortedConversations) {
if (currentList.isEmpty()) {
conversations_fastscroller.beGone() conversations_fastscroller.beGone()
no_conversations_placeholder.text = getString(R.string.no_conversations_found) no_conversations_placeholder.text = getString(R.string.no_conversations_found)
no_conversations_placeholder.beVisible() no_conversations_placeholder.beVisible()
no_conversations_placeholder_2.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)
}
} }
private fun launchNewConversation() { private fun launchNewConversation() {

View File

@ -1,5 +1,6 @@
package com.simplemobiletools.smsmessenger.adapters package com.simplemobiletools.smsmessenger.adapters
import android.annotation.SuppressLint
import android.content.Intent import android.content.Intent
import android.graphics.Typeface import android.graphics.Typeface
import android.os.Parcelable import android.os.Parcelable
@ -31,8 +32,9 @@ import com.simplemobiletools.smsmessenger.models.Conversation
import kotlinx.android.synthetic.main.item_conversation.view.* import kotlinx.android.synthetic.main.item_conversation.view.*
class ConversationsAdapter( class ConversationsAdapter(
activity: SimpleActivity, recyclerView: MyRecyclerView, itemClick: (Any) -> Unit activity: SimpleActivity, recyclerView: MyRecyclerView, onRefresh: () -> Unit, itemClick: (Any) -> Unit
) : MyRecyclerViewListAdapter<Conversation>(activity, recyclerView, ConversationDiffCallback(), itemClick), RecyclerViewFastScroller.OnPopupTextUpdate { ) : MyRecyclerViewListAdapter<Conversation>(activity, recyclerView, ConversationDiffCallback(), itemClick, onRefresh),
RecyclerViewFastScroller.OnPopupTextUpdate {
private var fontSize = activity.getTextSize() private var fontSize = activity.getTextSize()
private var drafts = HashMap<Long, String?>() private var drafts = HashMap<Long, String?>()