Browse Source

support poll state after voting and with results hidden

pull/61/merge
oogee 6 years ago
parent
commit
2759704921
  1. 38
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java
  2. 29
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicParser.java
  3. 9
      app/src/main/java/gr/thmmy/mthmmy/model/Poll.java
  4. 1
      app/src/main/res/layout/activity_topic_poll.xml

38
app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java

@ -14,9 +14,11 @@ import android.os.Bundle;
import android.text.Editable; import android.text.Editable;
import android.text.Html; import android.text.Html;
import android.text.InputType; import android.text.InputType;
import android.text.SpannableString;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.TextWatcher; import android.text.TextWatcher;
import android.text.method.LinkMovementMethod; import android.text.method.LinkMovementMethod;
import android.text.style.StyleSpan;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
@ -171,7 +173,7 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
boolean pollSupported = true; boolean pollSupported = true;
for (Poll.Entry entry : entries) { for (Poll.Entry entry : entries) {
if (ThmmyParser.containsHtml(entry.getEntryName())){ if (ThmmyParser.containsHtml(entry.getEntryName())) {
pollSupported = false; pollSupported = false;
break; break;
} }
@ -204,9 +206,9 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
final int accentColor = context.getResources().getColor(R.color.accent); final int accentColor = context.getResources().getColor(R.color.accent);
if (poll.getAvailableVoteCount() > 1) { if (poll.getAvailableVoteCount() > 1) {
// vote multiple options
for (Poll.Entry entry : entries) { for (Poll.Entry entry : entries) {
CheckBox checkBox = new CheckBox(context); CheckBox checkBox = new CheckBox(context);
checkBox.setTextColor(primaryTextColor);
checkBox.setMovementMethod(LinkMovementMethod.getInstance()); checkBox.setMovementMethod(LinkMovementMethod.getInstance());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
checkBox.setText(Html.fromHtml(entry.getEntryName(), Html.FROM_HTML_MODE_LEGACY)); checkBox.setText(Html.fromHtml(entry.getEntryName(), Html.FROM_HTML_MODE_LEGACY));
@ -220,6 +222,7 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
holder.voteChart.setVisibility(View.GONE); holder.voteChart.setVisibility(View.GONE);
holder.optionsLayout.setVisibility(View.VISIBLE); holder.optionsLayout.setVisibility(View.VISIBLE);
} else if (poll.getAvailableVoteCount() == 1) { } else if (poll.getAvailableVoteCount() == 1) {
// vote single option
RadioGroup radioGroup = new RadioGroup(context); RadioGroup radioGroup = new RadioGroup(context);
for (int i = 0; i < entries.length; i++) { for (int i = 0; i < entries.length; i++) {
RadioButton radioButton = new RadioButton(context); RadioButton radioButton = new RadioButton(context);
@ -238,8 +241,33 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
holder.optionsLayout.addView(radioGroup); holder.optionsLayout.addView(radioGroup);
holder.voteChart.setVisibility(View.GONE); holder.voteChart.setVisibility(View.GONE);
holder.optionsLayout.setVisibility(View.VISIBLE); holder.optionsLayout.setVisibility(View.VISIBLE);
} else if (poll.getSelectedEntryIndex() != -1) {
// vote already submitted but results are hidden
Poll.Entry[] entries1 = poll.getEntries();
for (int i = 0; i < entries1.length; i++) {
Poll.Entry entry = entries1[i];
TextView textView = new TextView(context);
textView.setMovementMethod(LinkMovementMethod.getInstance());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
textView.setText(Html.fromHtml(entry.getEntryName(), Html.FROM_HTML_MODE_LEGACY));
} else {
//noinspection deprecation
textView.setText(Html.fromHtml(entry.getEntryName()));
}
textView.setTextColor(primaryTextColor);
if (poll.getSelectedEntryIndex() == i) {
// apply bold to the selected entry
SpannableString spanString = new SpannableString(textView.getText() + " ✓");
spanString.setSpan(new StyleSpan(Typeface.BOLD), 0, spanString.length(), 0);
textView.setText(spanString);
textView.setTextColor(accentColor);
}
holder.optionsLayout.addView(textView);
}
holder.voteChart.setVisibility(View.GONE);
holder.optionsLayout.setVisibility(View.VISIBLE);
} else { } else {
//Showing results // Showing results
holder.optionsLayout.setVisibility(View.GONE); holder.optionsLayout.setVisibility(View.GONE);
Arrays.sort(entries, (p1, p2) -> p1.getVotes() - p2.getVotes()); Arrays.sort(entries, (p1, p2) -> p1.getVotes() - p2.getVotes());
List<BarEntry> valuesToCompare = new ArrayList<>(); List<BarEntry> valuesToCompare = new ArrayList<>();
@ -274,7 +302,7 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
barData.setValueFormatter((value, entry, dataSetIndex, viewPortHandler) -> { barData.setValueFormatter((value, entry, dataSetIndex, viewPortHandler) -> {
DecimalFormat format = new DecimalFormat("###.#%"); DecimalFormat format = new DecimalFormat("###.#%");
double percentage = 0; double percentage = 0;
if(finalSum!=0) if (finalSum != 0)
percentage = ((double) value / (double) finalSum); percentage = ((double) value / (double) finalSum);
return "" + (int) value + " (" + format.format(percentage) + ")"; return "" + (int) value + " (" + format.format(percentage) + ")";
}); });
@ -764,7 +792,7 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
} }
} }
private void loadAvatar(String imageUrl, ImageView imageView){ private void loadAvatar(String imageUrl, ImageView imageView) {
Picasso.with(context) Picasso.with(context)
.load(imageUrl) .load(imageUrl)
.fit() .fit()

29
app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicParser.java

@ -484,7 +484,7 @@ public class TopicParser {
try { try {
String question; String question;
ArrayList<Poll.Entry> entries = new ArrayList<>(); ArrayList<Poll.Entry> entries = new ArrayList<>();
int availableVoteCount = 0; int availableVoteCount = 0, selectedEntryIndex = -1;
String pollFormUrl = null, sc = null, removeVoteUrl = null, showVoteResultsUrl = null, String pollFormUrl = null, sc = null, removeVoteUrl = null, showVoteResultsUrl = null,
showOptionsUrl = null; showOptionsUrl = null;
@ -525,19 +525,30 @@ public class TopicParser {
} }
} else { } else {
// poll in results mode // poll in results mode
boolean pollResultsHidden = false;
Elements entryRows = pollColumn.select("table[cellspacing] tr"); Elements entryRows = pollColumn.select("table[cellspacing] tr");
for (Element entryRow : entryRows) { for (int i = 0; i < entryRows.size(); i++) {
Element entryRow = entryRows.get(i);
Elements entryColumns = entryRow.select("td"); Elements entryColumns = entryRow.select("td");
if (entryColumns.size() < 2) pollResultsHidden = true;
String optionName = entryColumns.first().html(); String optionName = entryColumns.first().html();
String voteCountDescription = entryColumns.last().text();
Matcher integerMatcher = integerPattern.matcher(voteCountDescription);
int voteCount = 0; int voteCount = 0;
if (integerMatcher.find()) {
voteCount = Integer.parseInt(voteCountDescription.substring(integerMatcher.start(), if (pollResultsHidden) {
integerMatcher.end())); if (entryColumns.first().attr("style").contains("font-weight: bold;"))
selectedEntryIndex = i;
} else {
String voteCountDescription = entryColumns.last().text();
Matcher integerMatcher = integerPattern.matcher(voteCountDescription);
if (integerMatcher.find()) {
voteCount = Integer.parseInt(voteCountDescription.substring(integerMatcher.start(),
integerMatcher.end()));
}
} }
entries.add(0, new Poll.Entry(optionName, voteCount)); entries.add(new Poll.Entry(optionName, voteCount));
} }
Elements links = pollColumn.select("td[style=padding-left: 15px;] > a"); Elements links = pollColumn.select("td[style=padding-left: 15px;] > a");
@ -549,7 +560,7 @@ public class TopicParser {
} }
} }
return new Poll(question, entries.toArray(new Poll.Entry[0]), availableVoteCount, return new Poll(question, entries.toArray(new Poll.Entry[0]), availableVoteCount,
pollFormUrl, sc, removeVoteUrl, showVoteResultsUrl, showOptionsUrl); pollFormUrl, sc, removeVoteUrl, showVoteResultsUrl, showOptionsUrl, selectedEntryIndex);
} catch (Exception e) { } catch (Exception e) {
Timber.v(e, "Could not parse a poll"); Timber.v(e, "Could not parse a poll");
} }

9
app/src/main/java/gr/thmmy/mthmmy/model/Poll.java

@ -7,11 +7,11 @@ public class Poll extends TopicItem {
private final String question; private final String question;
private Entry[] entries; private Entry[] entries;
private int availableVoteCount; private int availableVoteCount, selectedEntryIndex = -1;
private String pollFormUrl, sc, removeVoteUrl, showVoteResultsUrl, showOptionsUrl; private String pollFormUrl, sc, removeVoteUrl, showVoteResultsUrl, showOptionsUrl;
public Poll(String question, Entry[] entries, int availableVoteCount, String pollFormUrl, String sc, public Poll(String question, Entry[] entries, int availableVoteCount, String pollFormUrl, String sc,
String removeVoteUrl, String showVoteResultsUrl, String showOptionsUrl) { String removeVoteUrl, String showVoteResultsUrl, String showOptionsUrl, int selectedEntryIndex) {
this.question = question; this.question = question;
this.entries = entries; this.entries = entries;
this.availableVoteCount = availableVoteCount; this.availableVoteCount = availableVoteCount;
@ -20,6 +20,7 @@ public class Poll extends TopicItem {
this.removeVoteUrl = removeVoteUrl; this.removeVoteUrl = removeVoteUrl;
this.showVoteResultsUrl = showVoteResultsUrl; this.showVoteResultsUrl = showVoteResultsUrl;
this.showOptionsUrl = showOptionsUrl; this.showOptionsUrl = showOptionsUrl;
this.selectedEntryIndex = selectedEntryIndex;
} }
private int totalVotes() { private int totalVotes() {
@ -68,6 +69,10 @@ public class Poll extends TopicItem {
return showOptionsUrl; return showOptionsUrl;
} }
public int getSelectedEntryIndex() {
return selectedEntryIndex;
}
public static class Entry { public static class Entry {
private final String entryName; private final String entryName;
private int votes; private int votes;

1
app/src/main/res/layout/activity_topic_poll.xml

@ -16,6 +16,7 @@
android:id="@+id/options_layout" android:id="@+id/options_layout"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:orientation="vertical" /> android:orientation="vertical" />
<com.github.mikephil.charting.charts.HorizontalBarChart <com.github.mikephil.charting.charts.HorizontalBarChart

Loading…
Cancel
Save