2019-06-22 21:55:03 +02:00
|
|
|
/* Copyright 2019 Conny Duck
|
|
|
|
*
|
|
|
|
* This file is a part of Tusky.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
|
|
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with Tusky; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
|
|
|
|
package com.keylesspalace.tusky.adapter
|
|
|
|
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
2021-09-17 22:12:17 +02:00
|
|
|
import com.keylesspalace.tusky.R
|
2021-03-07 19:24:01 +01:00
|
|
|
import com.keylesspalace.tusky.databinding.ItemPollBinding
|
2019-06-22 21:55:03 +02:00
|
|
|
import com.keylesspalace.tusky.entity.Emoji
|
2021-03-07 19:24:01 +01:00
|
|
|
import com.keylesspalace.tusky.util.BindingHolder
|
|
|
|
import com.keylesspalace.tusky.util.emojify
|
|
|
|
import com.keylesspalace.tusky.util.visible
|
2019-06-22 21:55:03 +02:00
|
|
|
import com.keylesspalace.tusky.viewdata.PollOptionViewData
|
2019-10-11 17:35:22 +02:00
|
|
|
import com.keylesspalace.tusky.viewdata.buildDescription
|
2019-06-22 21:55:03 +02:00
|
|
|
import com.keylesspalace.tusky.viewdata.calculatePercent
|
|
|
|
|
2021-06-28 21:13:24 +02:00
|
|
|
class PollAdapter : RecyclerView.Adapter<BindingHolder<ItemPollBinding>>() {
|
2019-06-22 21:55:03 +02:00
|
|
|
|
|
|
|
private var pollOptions: List<PollOptionViewData> = emptyList()
|
|
|
|
private var voteCount: Int = 0
|
2020-03-24 21:06:58 +01:00
|
|
|
private var votersCount: Int? = null
|
2019-06-22 21:55:03 +02:00
|
|
|
private var mode = RESULT
|
|
|
|
private var emojis: List<Emoji> = emptyList()
|
2020-10-02 21:18:39 +02:00
|
|
|
private var resultClickListener: View.OnClickListener? = null
|
2021-02-06 08:14:51 +01:00
|
|
|
private var animateEmojis = false
|
2019-06-22 21:55:03 +02:00
|
|
|
|
2020-03-24 21:06:58 +01:00
|
|
|
fun setup(
|
2021-06-28 21:13:24 +02:00
|
|
|
options: List<PollOptionViewData>,
|
|
|
|
voteCount: Int,
|
|
|
|
votersCount: Int?,
|
|
|
|
emojis: List<Emoji>,
|
|
|
|
mode: Int,
|
|
|
|
resultClickListener: View.OnClickListener?,
|
|
|
|
animateEmojis: Boolean
|
|
|
|
) {
|
2019-06-22 21:55:03 +02:00
|
|
|
this.pollOptions = options
|
|
|
|
this.voteCount = voteCount
|
2020-03-24 21:06:58 +01:00
|
|
|
this.votersCount = votersCount
|
2019-06-22 21:55:03 +02:00
|
|
|
this.emojis = emojis
|
|
|
|
this.mode = mode
|
2020-10-02 21:18:39 +02:00
|
|
|
this.resultClickListener = resultClickListener
|
2021-02-06 08:14:51 +01:00
|
|
|
this.animateEmojis = animateEmojis
|
2019-06-22 21:55:03 +02:00
|
|
|
notifyDataSetChanged()
|
|
|
|
}
|
|
|
|
|
2021-06-28 21:13:24 +02:00
|
|
|
fun getSelected(): List<Int> {
|
2019-06-22 21:55:03 +02:00
|
|
|
return pollOptions.filter { it.selected }
|
2021-06-28 21:13:24 +02:00
|
|
|
.map { pollOptions.indexOf(it) }
|
2019-06-22 21:55:03 +02:00
|
|
|
}
|
|
|
|
|
2021-03-07 19:24:01 +01:00
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BindingHolder<ItemPollBinding> {
|
|
|
|
val binding = ItemPollBinding.inflate(LayoutInflater.from(parent.context), parent, false)
|
|
|
|
return BindingHolder(binding)
|
2019-06-22 21:55:03 +02:00
|
|
|
}
|
|
|
|
|
2021-03-07 19:24:01 +01:00
|
|
|
override fun getItemCount() = pollOptions.size
|
2019-06-22 21:55:03 +02:00
|
|
|
|
2021-03-07 19:24:01 +01:00
|
|
|
override fun onBindViewHolder(holder: BindingHolder<ItemPollBinding>, position: Int) {
|
2019-06-22 21:55:03 +02:00
|
|
|
|
|
|
|
val option = pollOptions[position]
|
|
|
|
|
2021-03-07 19:24:01 +01:00
|
|
|
val resultTextView = holder.binding.statusPollOptionResult
|
|
|
|
val radioButton = holder.binding.statusPollRadioButton
|
|
|
|
val checkBox = holder.binding.statusPollCheckbox
|
|
|
|
|
|
|
|
resultTextView.visible(mode == RESULT)
|
|
|
|
radioButton.visible(mode == SINGLE)
|
|
|
|
checkBox.visible(mode == MULTIPLE)
|
2019-06-22 21:55:03 +02:00
|
|
|
|
2021-06-28 21:13:24 +02:00
|
|
|
when (mode) {
|
2019-06-22 21:55:03 +02:00
|
|
|
RESULT -> {
|
2020-03-24 21:06:58 +01:00
|
|
|
val percent = calculatePercent(option.votesCount, votersCount, voteCount)
|
2022-05-03 19:15:59 +02:00
|
|
|
resultTextView.text = buildDescription(option.title, percent, option.voted, resultTextView.context)
|
2021-06-28 21:13:24 +02:00
|
|
|
.emojify(emojis, resultTextView, animateEmojis)
|
2019-06-22 21:55:03 +02:00
|
|
|
|
|
|
|
val level = percent * 100
|
2021-09-17 22:12:17 +02:00
|
|
|
val optionColor = if (option.voted) {
|
|
|
|
R.color.colorBackgroundHighlight
|
|
|
|
} else {
|
|
|
|
R.color.colorBackgroundAccent
|
|
|
|
}
|
2019-06-22 21:55:03 +02:00
|
|
|
|
2021-03-07 19:24:01 +01:00
|
|
|
resultTextView.background.level = level
|
2022-08-04 16:48:26 +02:00
|
|
|
resultTextView.background.setTint(resultTextView.context.getColor(optionColor))
|
2021-03-07 19:24:01 +01:00
|
|
|
resultTextView.setOnClickListener(resultClickListener)
|
2019-06-22 21:55:03 +02:00
|
|
|
}
|
|
|
|
SINGLE -> {
|
2022-05-03 19:15:59 +02:00
|
|
|
radioButton.text = option.title.emojify(emojis, radioButton, animateEmojis)
|
2021-03-07 19:24:01 +01:00
|
|
|
radioButton.isChecked = option.selected
|
|
|
|
radioButton.setOnClickListener {
|
2019-06-22 21:55:03 +02:00
|
|
|
pollOptions.forEachIndexed { index, pollOption ->
|
2021-04-10 20:30:44 +02:00
|
|
|
pollOption.selected = index == holder.bindingAdapterPosition
|
2019-06-22 21:55:03 +02:00
|
|
|
notifyItemChanged(index)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MULTIPLE -> {
|
2022-05-03 19:15:59 +02:00
|
|
|
checkBox.text = option.title.emojify(emojis, checkBox, animateEmojis)
|
2021-03-07 19:24:01 +01:00
|
|
|
checkBox.isChecked = option.selected
|
|
|
|
checkBox.setOnCheckedChangeListener { _, isChecked ->
|
2021-04-10 20:30:44 +02:00
|
|
|
pollOptions[holder.bindingAdapterPosition].selected = isChecked
|
2019-06-22 21:55:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
const val RESULT = 0
|
|
|
|
const val SINGLE = 1
|
|
|
|
const val MULTIPLE = 2
|
|
|
|
}
|
|
|
|
}
|