Set onClick listener for poll result view. (#1946)

Fixes #1861
main
Levi Bard 4 years ago committed by Alibek Omarov
parent c527b14eec
commit 8ee0420678
  1. 18
      app/src/main/java/com/keylesspalace/tusky/adapter/PollAdapter.kt
  2. 23
      app/src/main/java/com/keylesspalace/tusky/adapter/StatusBaseViewHolder.java
  3. 2
      app/src/main/java/com/keylesspalace/tusky/components/notifications/NotificationHelper.java
  4. 10
      app/src/main/java/com/keylesspalace/tusky/entity/Poll.kt
  5. 13
      app/src/main/java/com/keylesspalace/tusky/util/StatusViewHelper.kt
  6. 5
      app/src/main/java/com/keylesspalace/tusky/viewdata/PollViewData.kt
  7. 1
      app/src/test/java/com/keylesspalace/tusky/FilterTest.kt

@ -34,14 +34,24 @@ class PollAdapter: RecyclerView.Adapter<PollViewHolder>() {
private var pollOptions: List<PollOptionViewData> = emptyList()
private var voteCount: Int = 0
private var votersCount: Int? = null
private var mode = RESULT
private var emojis: List<Emoji> = emptyList()
fun setup(options: List<PollOptionViewData>, voteCount: Int, emojis: List<Emoji>, mode: Int) {
private var resultClickListener: View.OnClickListener? = null
fun setup(
options: List<PollOptionViewData>,
voteCount: Int,
votersCount: Int?,
emojis: List<Emoji>,
mode: Int,
resultClickListener: View.OnClickListener?) {
this.pollOptions = options
this.voteCount = voteCount
this.votersCount = votersCount
this.emojis = emojis
this.mode = mode
this.resultClickListener = resultClickListener
notifyDataSetChanged()
}
@ -69,7 +79,7 @@ class PollAdapter: RecyclerView.Adapter<PollViewHolder>() {
when(mode) {
RESULT -> {
val percent = calculatePercent(option.votesCount, voteCount)
val percent = calculatePercent(option.votesCount, votersCount, voteCount)
val emojifiedPollOptionText = buildDescription(option.title, percent, holder.resultTextView.context)
.emojify(emojis, holder.resultTextView)
holder.resultTextView.text = EmojiCompat.get().process(emojifiedPollOptionText)
@ -77,7 +87,7 @@ class PollAdapter: RecyclerView.Adapter<PollViewHolder>() {
val level = percent * 100
holder.resultTextView.background.level = level
holder.resultTextView.setOnClickListener(resultClickListener)
}
SINGLE -> {
val emojifiedPollOptionText = option.title.emojify(emojis, holder.radioButton)

@ -946,7 +946,7 @@ public abstract class StatusBaseViewHolder extends RecyclerView.ViewHolder {
List<PollOptionViewData> options = poll.getOptions();
for (int i = 0; i < args.length; i++) {
if (i < options.size()) {
int percent = PollViewDataKt.calculatePercent(options.get(i).getVotesCount(), poll.getVotesCount());
int percent = PollViewDataKt.calculatePercent(options.get(i).getVotesCount(), poll.getVotersCount(), poll.getVotesCount());
args[i] = buildDescription(options.get(i).getTitle(), percent, context);
} else {
args[i] = "";
@ -989,12 +989,18 @@ public abstract class StatusBaseViewHolder extends RecyclerView.ViewHolder {
if (expired || poll.getVoted()) {
// no voting possible
pollAdapter.setup(poll.getOptions(), poll.getVotesCount(), emojis, PollAdapter.RESULT);
View.OnClickListener viewThreadListener = v -> {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
listener.onViewThread(position);
}
};
pollAdapter.setup(poll.getOptions(), poll.getVotesCount(), poll.getVotersCount(), emojis, PollAdapter.RESULT, viewThreadListener);
pollButton.setVisibility(View.GONE);
} else {
// voting possible
pollAdapter.setup(poll.getOptions(), poll.getVotesCount(), emojis, poll.getMultiple() ? PollAdapter.MULTIPLE : PollAdapter.SINGLE);
pollAdapter.setup(poll.getOptions(), poll.getVotesCount(), poll.getVotersCount(), emojis, poll.getMultiple() ? PollAdapter.MULTIPLE : PollAdapter.SINGLE, null);
pollButton.setVisibility(View.VISIBLE);
@ -1021,8 +1027,15 @@ public abstract class StatusBaseViewHolder extends RecyclerView.ViewHolder {
private CharSequence getPollInfoText(long timestamp, PollViewData poll,
StatusDisplayOptions statusDisplayOptions,
Context context) {
String votes = numberFormat.format(poll.getVotesCount());
String votesText = context.getResources().getQuantityString(R.plurals.poll_info_votes, poll.getVotesCount(), votes);
String votesText;
if(poll.getVotersCount() == null) {
String voters = numberFormat.format(poll.getVotesCount());
votesText = context.getResources().getQuantityString(R.plurals.poll_info_votes, poll.getVotesCount(), voters);
} else {
String voters = numberFormat.format(poll.getVotersCount());
votesText = context.getResources().getQuantityString(R.plurals.poll_info_people, poll.getVotersCount(), voters);
}
CharSequence pollDurationInfo;
if (poll.getExpired()) {
pollDurationInfo = context.getString(R.string.poll_info_closed);

@ -716,7 +716,7 @@ public class NotificationHelper {
Poll poll = notification.getStatus().getPoll();
for(PollOption option: poll.getOptions()) {
builder.append(buildDescription(option.getTitle(),
PollViewDataKt.calculatePercent(option.getVotesCount(), poll.getVotesCount()),
PollViewDataKt.calculatePercent(option.getVotesCount(), poll.getVotersCount(), poll.getVoterCount()),
context));
builder.append('\n');
}

@ -9,6 +9,7 @@ data class Poll(
val expired: Boolean,
val multiple: Boolean,
@SerializedName("votes_count") val votesCount: Int,
@SerializedName("voters_count") val votersCount: Int?, // nullable for compatibility with Pleroma
val options: List<PollOption>,
val voted: Boolean
) {
@ -22,7 +23,12 @@ data class Poll(
}
}
return copy(options = newOptions, votesCount = votesCount + choices.size, voted = true)
return copy(
options = newOptions,
votesCount = votesCount + choices.size,
votersCount = votersCount?.plus(1),
voted = true
)
}
fun toNewPoll(creationDate: Date) = NewPoll(
@ -38,4 +44,4 @@ data class Poll(
data class PollOption(
val title: String,
@SerializedName("votes_count") val votesCount: Int
)
)

@ -24,7 +24,6 @@ import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.DrawableRes
import androidx.appcompat.content.res.AppCompatResources
import androidx.core.content.ContextCompat
import com.bumptech.glide.Glide
import com.keylesspalace.tusky.R
import com.keylesspalace.tusky.entity.Attachment
@ -272,9 +271,13 @@ class StatusViewHelper(private val itemView: View) {
private fun getPollInfoText(timestamp: Long, poll: PollViewData, pollDescription: TextView, useAbsoluteTime: Boolean): CharSequence {
val context = pollDescription.context
val votes = NumberFormat.getNumberInstance().format(poll.votesCount.toLong())
val votesText = context.resources.getQuantityString(R.plurals.poll_info_votes, poll.votesCount, votes)
val pollDurationInfo: CharSequence
val votesText = if(poll.votersCount == null) {
val votes = NumberFormat.getNumberInstance().format(poll.votesCount.toLong())
context.resources.getQuantityString(R.plurals.poll_info_votes, poll.votesCount, votes)
} else {
val votes = NumberFormat.getNumberInstance().format(poll.votersCount.toLong())
context.resources.getQuantityString(R.plurals.poll_info_people, poll.votersCount, votes)
}
pollDurationInfo = if (poll.expired) {
context.getString(R.string.poll_info_closed)
} else {
@ -294,7 +297,7 @@ class StatusViewHelper(private val itemView: View) {
for (i in 0 until Status.MAX_POLL_OPTIONS) {
if (i < options.size) {
val percent = calculatePercent(options[i].votesCount, poll.votesCount)
val percent = calculatePercent(options[i].votesCount, poll.votersCount, poll.votesCount)
val pollOptionText = buildDescription(options[i].title, percent, pollResults[i].context)
pollResults[i].text = pollOptionText.emojify(emojis, pollResults[i])

@ -31,6 +31,7 @@ data class PollViewData(
val expired: Boolean,
val multiple: Boolean,
val votesCount: Int,
val votersCount: Int?,
val options: List<PollOptionViewData>,
var voted: Boolean
)
@ -41,10 +42,11 @@ data class PollOptionViewData(
var selected: Boolean
)
fun calculatePercent(fraction: Int, total: Int): Int {
fun calculatePercent(fraction: Int, totalVoters: Int?, totalVotes: Int): Int {
return if (fraction == 0) {
0
} else {
val total = totalVoters ?: totalVotes
(fraction / total.toDouble() * 100).roundToInt()
}
}
@ -63,6 +65,7 @@ fun Poll?.toViewData(): PollViewData? {
expired,
multiple,
votesCount,
votersCount,
options.map { it.toViewData() },
voted
)

@ -224,6 +224,7 @@ class FilterTest {
expired = false,
multiple = false,
votesCount = 0,
votersCount = 0,
options = pollOptions.map {
PollOption(it, 0)
},

Loading…
Cancel
Save