Code review fixes.

This commit is contained in:
Onuray Sahin 2021-12-13 17:37:11 +03:00
parent c7ad50a4d6
commit f6dcda6841
2 changed files with 17 additions and 18 deletions

View File

@ -18,6 +18,7 @@ package im.vector.app.features.home.room.detail.timeline.item
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.view.children
import com.airbnb.epoxy.EpoxyAttribute
import com.airbnb.epoxy.EpoxyModelClass
import im.vector.app.R
@ -58,23 +59,29 @@ abstract class PollItem : AbsMessageItem<PollItem.Holder>() {
holder.questionTextView.text = pollContent?.pollCreationInfo?.question?.question
holder.totalVotesTextView.text = totalVotesText
val cachedViews = mutableMapOf<String, PollOptionItem>()
holder.optionsContainer.children.filterIsInstance<PollOptionItem>().forEach { existingPollItemView ->
cachedViews[existingPollItemView.getTag(STUB_ID)?.toString() ?: ""] = existingPollItemView
}
holder.optionsContainer.removeAllViews()
pollContent?.pollCreationInfo?.answers?.forEachIndexed { index, option ->
val optionName = option.answer ?: ""
val tag = relatedEventId + option.id
holder.optionsContainer.addView(
PollOptionItem(holder.view.context).apply {
val pollOptionItem: PollOptionItem = (cachedViews[tag] ?: PollOptionItem(holder.view.context))
.apply {
setTag(STUB_ID, tag)
render(
state = optionViewStates?.getOrNull(index) ?: PollOptionViewState.DisabledOptionWithInvisibleVotes(optionName),
callback = object : PollOptionItem.Callback {
override fun onOptionClicked() {
callback?.onTimelineItemAction(RoomDetailAction.VoteToPoll(relatedEventId, option.id ?: ""))
}
}
state = optionViewStates?.getOrNull(index) ?: PollOptionViewState.DisabledOptionWithInvisibleVotes(optionName)
)
}
)
pollOptionItem.setOnClickListener {
callback?.onTimelineItemAction(RoomDetailAction.VoteToPoll(relatedEventId, option.id ?: ""))
}
holder.optionsContainer.addView(pollOptionItem)
}
}

View File

@ -32,12 +32,7 @@ class PollOptionItem @JvmOverloads constructor(
defStyleAttr: Int = 0
) : ConstraintLayout(context, attrs, defStyleAttr) {
interface Callback {
fun onOptionClicked()
}
private lateinit var views: ItemPollOptionBinding
private var callback: Callback? = null
init {
setupViews()
@ -46,12 +41,9 @@ class PollOptionItem @JvmOverloads constructor(
private fun setupViews() {
inflate(context, R.layout.item_poll_option, this)
views = ItemPollOptionBinding.bind(this)
views.root.setOnClickListener { callback?.onOptionClicked() }
}
fun render(state: PollOptionViewState, callback: Callback) {
this.callback = callback
fun render(state: PollOptionViewState) {
views.optionNameTextView.text = state.name