From 173b878c3b158e5c203980e54f80a7c7fc7d10bf Mon Sep 17 00:00:00 2001 From: Konrad Pozniak Date: Sun, 28 Apr 2019 09:58:17 +0200 Subject: [PATCH] add support for multiple choice polls (#1212) --- .../tusky/adapter/StatusBaseViewHolder.java | 147 ++++++++++++------ .../com/keylesspalace/tusky/entity/Poll.kt | 2 +- app/src/main/res/layout/item_conversation.xml | 48 +++++- app/src/main/res/layout/item_status.xml | 48 +++++- .../main/res/layout/item_status_detailed.xml | 46 +++++- 5 files changed, 239 insertions(+), 52 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/adapter/StatusBaseViewHolder.java b/app/src/main/java/com/keylesspalace/tusky/adapter/StatusBaseViewHolder.java index 4ab22f94..e3df2a4f 100644 --- a/app/src/main/java/com/keylesspalace/tusky/adapter/StatusBaseViewHolder.java +++ b/app/src/main/java/com/keylesspalace/tusky/adapter/StatusBaseViewHolder.java @@ -8,6 +8,7 @@ import android.text.TextUtils; import android.view.View; import android.view.ViewGroup; import android.widget.Button; +import android.widget.CheckBox; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.RadioButton; @@ -36,6 +37,7 @@ import com.mikepenz.iconics.utils.Utils; import java.text.NumberFormat; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.List; @@ -80,6 +82,7 @@ public abstract class StatusBaseViewHolder extends RecyclerView.ViewHolder { private TextView pollDescription; private RadioGroup pollRadioGroup; private RadioButton[] pollRadioOptions; + private CheckBox[] pollCheckboxOptions; private Button pollButton; private boolean useAbsoluteTime; @@ -137,6 +140,12 @@ public abstract class StatusBaseViewHolder extends RecyclerView.ViewHolder { pollRadioGroup.findViewById(R.id.status_poll_radio_button_2), pollRadioGroup.findViewById(R.id.status_poll_radio_button_3) }; + pollCheckboxOptions = new CheckBox[] { + itemView.findViewById(R.id.status_poll_checkbox_0), + itemView.findViewById(R.id.status_poll_checkbox_1), + itemView.findViewById(R.id.status_poll_checkbox_2), + itemView.findViewById(R.id.status_poll_checkbox_3) + }; pollButton = itemView.findViewById(R.id.status_poll_button); @@ -760,62 +769,20 @@ public abstract class StatusBaseViewHolder extends RecyclerView.ViewHolder { pollDescription.setVisibility(View.GONE); pollRadioGroup.setVisibility(View.GONE); - for(RadioButton radioButton: pollRadioOptions) { - radioButton.setVisibility(View.GONE); + for(CheckBox checkBox: pollCheckboxOptions) { + checkBox.setVisibility(View.GONE); } pollButton.setVisibility(View.GONE); } else { Context context = pollDescription.getContext(); - List options = poll.getOptions(); if(poll.getExpired() || poll.getVoted()) { // no voting possible - for(int i = 0; i < Status.MAX_POLL_OPTIONS; i++) { - if(i < options.size()) { - long percent = calculatePollPercent(options.get(i).getVotesCount(), poll.getVotesCount()); - - String pollOptionText = context.getString(R.string.poll_option_format, percent, options.get(i).getTitle()); - pollResults[i].setText(CustomEmojiHelper.emojifyText(HtmlUtils.fromHtml(pollOptionText), emojis, pollResults[i])); - pollResults[i].setVisibility(View.VISIBLE); - - int level = (int) percent * 100; - - pollResults[i].getBackground().setLevel(level); - - } else { - pollResults[i].setVisibility(View.GONE); - } - } - - pollRadioGroup.setVisibility(View.GONE); - - for(RadioButton radioButton: pollRadioOptions) { - radioButton.setVisibility(View.GONE); - } - - pollButton.setVisibility(View.GONE); + setupPollResult(poll, emojis); } else { // voting possible - - for(TextView pollResult: pollResults) { - pollResult.setVisibility(View.GONE); - } - - pollRadioGroup.setVisibility(View.VISIBLE); - pollRadioGroup.clearCheck(); - pollButton.setVisibility(View.VISIBLE); - - for(int i = 0; i < Status.MAX_POLL_OPTIONS; i++) { - if(i < options.size()) { - pollRadioOptions[i].setText(CustomEmojiHelper.emojifyString(options.get(i).getTitle(), emojis, pollRadioOptions[i])); - pollRadioOptions[i].setVisibility(View.VISIBLE); - - } else { - pollRadioOptions[i].setVisibility(View.GONE); - } - } - + setupPollVoting(poll, emojis, listener); } pollDescription.setVisibility(View.VISIBLE); @@ -839,6 +806,94 @@ public abstract class StatusBaseViewHolder extends RecyclerView.ViewHolder { pollDescription.setText(pollInfo); + + } + } + + private void setupPollResult(Poll poll, List emojis) { + List options = poll.getOptions(); + + for(int i = 0; i < Status.MAX_POLL_OPTIONS; i++) { + if(i < options.size()) { + long percent = calculatePollPercent(options.get(i).getVotesCount(), poll.getVotesCount()); + + String pollOptionText = pollResults[i].getContext().getString(R.string.poll_option_format, percent, options.get(i).getTitle()); + pollResults[i].setText(CustomEmojiHelper.emojifyText(HtmlUtils.fromHtml(pollOptionText), emojis, pollResults[i])); + pollResults[i].setVisibility(View.VISIBLE); + + int level = (int) percent * 100; + + pollResults[i].getBackground().setLevel(level); + + } else { + pollResults[i].setVisibility(View.GONE); + } + } + + pollRadioGroup.setVisibility(View.GONE); + + for(CheckBox checkBox: pollCheckboxOptions) { + checkBox.setVisibility(View.GONE); + } + + pollButton.setVisibility(View.GONE); + } + + private void setupPollVoting(Poll poll, List emojis, StatusActionListener listener) { + List options = poll.getOptions(); + + pollButton.setVisibility(View.VISIBLE); + + for(TextView pollResult: pollResults) { + pollResult.setVisibility(View.GONE); + } + + if(poll.getMultiple()) { + + pollRadioGroup.setVisibility(View.GONE); + + for(int i = 0; i < Status.MAX_POLL_OPTIONS; i++) { + if(i < options.size()) { + pollCheckboxOptions[i].setText(CustomEmojiHelper.emojifyString(options.get(i).getTitle(), emojis, pollCheckboxOptions[i])); + pollCheckboxOptions[i].setVisibility(View.VISIBLE); + pollCheckboxOptions[i].setChecked(false); + } else { + pollCheckboxOptions[i].setVisibility(View.GONE); + } + } + + pollButton.setOnClickListener(v -> { + + List pollResult = new ArrayList<>(options.size()); + for(int i = 0; i < options.size(); i++) { + if(pollCheckboxOptions[i].isChecked()) { + pollResult.add(i); + } + } + if(pollResult.size() == 0) { + return; + } + + listener.onVoteInPoll(getAdapterPosition(), pollResult); + }); + } else { + + for(CheckBox pollCheckbox: pollCheckboxOptions) { + pollCheckbox.setVisibility(View.GONE); + } + + pollRadioGroup.setVisibility(View.VISIBLE); + pollRadioGroup.clearCheck(); + + for(int i = 0; i < Status.MAX_POLL_OPTIONS; i++) { + if(i < options.size()) { + pollRadioOptions[i].setText(CustomEmojiHelper.emojifyString(options.get(i).getTitle(), emojis, pollRadioOptions[i])); + pollRadioOptions[i].setVisibility(View.VISIBLE); + } else { + pollRadioOptions[i].setVisibility(View.GONE); + } + } + pollButton.setOnClickListener(v -> { int selectedRadioButtonIndex; diff --git a/app/src/main/java/com/keylesspalace/tusky/entity/Poll.kt b/app/src/main/java/com/keylesspalace/tusky/entity/Poll.kt index 55193404..b5a1fa76 100644 --- a/app/src/main/java/com/keylesspalace/tusky/entity/Poll.kt +++ b/app/src/main/java/com/keylesspalace/tusky/entity/Poll.kt @@ -22,7 +22,7 @@ data class Poll( } } - return copy(options = newOptions, votesCount = votesCount + 1, voted = true) + return copy(options = newOptions, votesCount = votesCount + choices.size, voted = true) } } diff --git a/app/src/main/res/layout/item_conversation.xml b/app/src/main/res/layout/item_conversation.xml index 04d40799..5274889c 100644 --- a/app/src/main/res/layout/item_conversation.xml +++ b/app/src/main/res/layout/item_conversation.xml @@ -455,11 +455,56 @@ tools:text="Option 4" /> + + + + + + + + + app:layout_constraintTop_toBottomOf="@id/status_poll_checkbox_3" /> + + + + + + + + + app:layout_constraintTop_toBottomOf="@id/status_poll_checkbox_3" /> + + + + + + + + + app:layout_constraintTop_toBottomOf="@id/status_poll_checkbox_3" />