Fixed reactions list in bottom sheet not properly updated if opened when changes happen

This commit is contained in:
Sylvain Berfini 2025-07-11 16:01:26 +02:00
parent 4ae046a166
commit 98cc173d2e
2 changed files with 16 additions and 15 deletions

View file

@ -1325,7 +1325,7 @@ open class ConversationFragment : SlidingPaneChildFragment() {
val model = MessageReactionsModel(chatMessageModel.chatMessage) { reactionsModel ->
coreContext.postOnMainThread {
if (reactionsModel.allReactions.isEmpty()) {
if (reactionsModel.allReactions.value.orEmpty().isEmpty()) {
Log.i("$TAG No reaction to display, closing bottom sheet")
val bottomSheetBehavior = BottomSheetBehavior.from(
binding.messageBottomSheet.root
@ -1391,7 +1391,7 @@ open class ConversationFragment : SlidingPaneChildFragment() {
@UiThread
private fun displayReactions(model: MessageReactionsModel) {
val totalCount = model.allReactions.size
val totalCount = model.allReactions.value.orEmpty().size
val label = getString(R.string.message_reactions_info_all_title, totalCount.toString())
val tabs = binding.messageBottomSheet.tabs
@ -1401,7 +1401,7 @@ open class ConversationFragment : SlidingPaneChildFragment() {
)
var index = 1
for (reaction in model.differentReactions.value.orEmpty()) {
for (reaction in model.differentReactions) {
val count = model.reactionsMap[reaction]
val tabLabel = getString(
R.string.message_reactions_info_emoji_title,
@ -1418,7 +1418,7 @@ open class ConversationFragment : SlidingPaneChildFragment() {
override fun onTabSelected(tab: TabLayout.Tab?) {
val filter = tab?.tag.toString()
if (filter.isEmpty()) {
bottomSheetAdapter.submitList(model.allReactions)
bottomSheetAdapter.submitList(model.allReactions.value.orEmpty())
} else {
bottomSheetAdapter.submitList(model.filterReactions(filter))
}
@ -1431,7 +1431,7 @@ open class ConversationFragment : SlidingPaneChildFragment() {
}
})
val initialList = model.allReactions
val initialList = model.allReactions.value.orEmpty()
bottomSheetAdapter.submitList(initialList)
Log.i("$TAG Submitted [${initialList.size}] items for default reactions list")
}

View file

@ -38,9 +38,9 @@ class MessageReactionsModel
private const val TAG = "[Message Reactions Model]"
}
val allReactions = arrayListOf<MessageBottomSheetParticipantModel>()
val allReactions = MutableLiveData<ArrayList<MessageBottomSheetParticipantModel>>()
val differentReactions = MutableLiveData<ArrayList<String>>()
val differentReactions = arrayListOf<String>()
val reactionsMap = HashMap<String, Int>()
@ -71,7 +71,7 @@ class MessageReactionsModel
fun filterReactions(emoji: String): ArrayList<MessageBottomSheetParticipantModel> {
val filteredList = arrayListOf<MessageBottomSheetParticipantModel>()
for (reaction in allReactions) {
for (reaction in allReactions.value.orEmpty()) {
if (reaction.value == emoji) {
filteredList.add(reaction)
}
@ -83,16 +83,17 @@ class MessageReactionsModel
@WorkerThread
private fun computeReactions() {
reactionsMap.clear()
allReactions.clear()
differentReactions.clear()
val differentReactionsList = arrayListOf<String>()
val allReactionsList = arrayListOf<MessageBottomSheetParticipantModel>()
for (reaction in chatMessage.reactions) {
val body = reaction.body
val count = reactionsMap.getOrDefault(body, 0)
reactionsMap[body] = count + 1
Log.i("$TAG Found reaction with body [$body] (count = ${count + 1}) from [${reaction.fromAddress.asStringUriOnly()}]")
val isOurOwn = reaction.fromAddress.weakEqual(chatMessage.chatRoom.localAddress)
allReactions.add(
allReactionsList.add(
MessageBottomSheetParticipantModel(
reaction.fromAddress,
reaction.body,
@ -111,15 +112,15 @@ class MessageReactionsModel
}
)
if (!differentReactionsList.contains(body)) {
differentReactionsList.add(body)
if (!differentReactions.contains(body)) {
differentReactions.add(body)
}
}
Log.i(
"$TAG [${differentReactionsList.size}] reactions found on a total of [${allReactions.size}]"
"$TAG [${differentReactions.size}] reactions found on a total of [${allReactionsList.size}]"
)
differentReactions.postValue(differentReactionsList)
allReactions.postValue(allReactionsList)
onReactionsUpdated?.invoke(this)
}
}