diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java index eb87fcdc..4258d00f 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java +++ b/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.Html; import android.text.InputType; +import android.text.SpannableString; import android.text.TextUtils; import android.text.TextWatcher; import android.text.method.LinkMovementMethod; +import android.text.style.StyleSpan; import android.util.DisplayMetrics; import android.view.LayoutInflater; import android.view.View; @@ -171,7 +173,7 @@ class TopicAdapter extends RecyclerView.Adapter { boolean pollSupported = true; for (Poll.Entry entry : entries) { - if (ThmmyParser.containsHtml(entry.getEntryName())){ + if (ThmmyParser.containsHtml(entry.getEntryName())) { pollSupported = false; break; } @@ -204,9 +206,9 @@ class TopicAdapter extends RecyclerView.Adapter { final int accentColor = context.getResources().getColor(R.color.accent); if (poll.getAvailableVoteCount() > 1) { + // vote multiple options for (Poll.Entry entry : entries) { CheckBox checkBox = new CheckBox(context); - checkBox.setTextColor(primaryTextColor); checkBox.setMovementMethod(LinkMovementMethod.getInstance()); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { checkBox.setText(Html.fromHtml(entry.getEntryName(), Html.FROM_HTML_MODE_LEGACY)); @@ -220,6 +222,7 @@ class TopicAdapter extends RecyclerView.Adapter { holder.voteChart.setVisibility(View.GONE); holder.optionsLayout.setVisibility(View.VISIBLE); } else if (poll.getAvailableVoteCount() == 1) { + // vote single option RadioGroup radioGroup = new RadioGroup(context); for (int i = 0; i < entries.length; i++) { RadioButton radioButton = new RadioButton(context); @@ -238,8 +241,33 @@ class TopicAdapter extends RecyclerView.Adapter { holder.optionsLayout.addView(radioGroup); holder.voteChart.setVisibility(View.GONE); 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 { - //Showing results + // Showing results holder.optionsLayout.setVisibility(View.GONE); Arrays.sort(entries, (p1, p2) -> p1.getVotes() - p2.getVotes()); List valuesToCompare = new ArrayList<>(); @@ -274,7 +302,7 @@ class TopicAdapter extends RecyclerView.Adapter { barData.setValueFormatter((value, entry, dataSetIndex, viewPortHandler) -> { DecimalFormat format = new DecimalFormat("###.#%"); double percentage = 0; - if(finalSum!=0) + if (finalSum != 0) percentage = ((double) value / (double) finalSum); return "" + (int) value + " (" + format.format(percentage) + ")"; }); @@ -764,7 +792,7 @@ class TopicAdapter extends RecyclerView.Adapter { } } - private void loadAvatar(String imageUrl, ImageView imageView){ + private void loadAvatar(String imageUrl, ImageView imageView) { Picasso.with(context) .load(imageUrl) .fit() diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicParser.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicParser.java index 5cfcc2db..ca67957b 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicParser.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicParser.java @@ -484,7 +484,7 @@ public class TopicParser { try { String question; ArrayList entries = new ArrayList<>(); - int availableVoteCount = 0; + int availableVoteCount = 0, selectedEntryIndex = -1; String pollFormUrl = null, sc = null, removeVoteUrl = null, showVoteResultsUrl = null, showOptionsUrl = null; @@ -525,19 +525,30 @@ public class TopicParser { } } else { // poll in results mode + boolean pollResultsHidden = false; 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"); + + if (entryColumns.size() < 2) pollResultsHidden = true; + String optionName = entryColumns.first().html(); - String voteCountDescription = entryColumns.last().text(); - Matcher integerMatcher = integerPattern.matcher(voteCountDescription); int voteCount = 0; - if (integerMatcher.find()) { - voteCount = Integer.parseInt(voteCountDescription.substring(integerMatcher.start(), - integerMatcher.end())); + + if (pollResultsHidden) { + 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"); @@ -549,7 +560,7 @@ public class TopicParser { } } 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) { Timber.v(e, "Could not parse a poll"); } diff --git a/app/src/main/java/gr/thmmy/mthmmy/model/Poll.java b/app/src/main/java/gr/thmmy/mthmmy/model/Poll.java index e0d544f6..ffd98e4d 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/model/Poll.java +++ b/app/src/main/java/gr/thmmy/mthmmy/model/Poll.java @@ -7,11 +7,11 @@ public class Poll extends TopicItem { private final String question; private Entry[] entries; - private int availableVoteCount; + private int availableVoteCount, selectedEntryIndex = -1; private String pollFormUrl, sc, removeVoteUrl, showVoteResultsUrl, showOptionsUrl; 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.entries = entries; this.availableVoteCount = availableVoteCount; @@ -20,6 +20,7 @@ public class Poll extends TopicItem { this.removeVoteUrl = removeVoteUrl; this.showVoteResultsUrl = showVoteResultsUrl; this.showOptionsUrl = showOptionsUrl; + this.selectedEntryIndex = selectedEntryIndex; } private int totalVotes() { @@ -68,6 +69,10 @@ public class Poll extends TopicItem { return showOptionsUrl; } + public int getSelectedEntryIndex() { + return selectedEntryIndex; + } + public static class Entry { private final String entryName; private int votes; diff --git a/app/src/main/res/layout/activity_topic_poll.xml b/app/src/main/res/layout/activity_topic_poll.xml index 056ae2ca..285c740b 100644 --- a/app/src/main/res/layout/activity_topic_poll.xml +++ b/app/src/main/res/layout/activity_topic_poll.xml @@ -16,6 +16,7 @@ android:id="@+id/options_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" + android:layout_marginTop="8dp" android:orientation="vertical" />