Browse Source

fix vote mode parsing in polls

pull/55/head
Thodoris1999 6 years ago
parent
commit
3189cc9365
  1. 5
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java
  2. 22
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicParser.java
  3. 10
      app/src/main/res/layout/activity_topic_poll.xml

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

@ -48,6 +48,7 @@ import com.github.mikephil.charting.data.BarEntry;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
@ -166,10 +167,11 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
holder.question.setText(poll.getQuestion());
if (poll.getAvailableVoteCount() > 1) {
LinearLayout optionsLayout = new LinearLayout(context);
optionsLayout.setOrientation(LinearLayout.HORIZONTAL);
optionsLayout.setOrientation(LinearLayout.VERTICAL);
for (Poll.Entry entry : entries) {
CheckBox checkBox = new CheckBox(context);
checkBox.setText(entry.getEntryName());
checkBox.setTextColor(context.getResources().getColor(R.color.primary_text));
checkBox.setOnCheckedChangeListener((buttonView, isChecked) ->
viewModel.onVoteCheckboxClicked(optionsLayout.indexOfChild(buttonView), isChecked));
optionsLayout.addView(checkBox);
@ -211,6 +213,7 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
holder.voteChart.getLegend().setEnabled(false);
holder.voteChart.getDescription().setEnabled(false);
holder.voteChart.invalidate();
holder.voteChart.setVisibility(View.VISIBLE);
}
if (poll.getRemoveVoteUrl() != null) holder.removeVotesButton.setVisibility(View.VISIBLE);
if (poll.getShowVoteResultsUrl() != null) holder.showPollResultsButton.setVisibility(View.VISIBLE);

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

@ -1,6 +1,7 @@
package gr.thmmy.mthmmy.activities.topic;
import android.graphics.Color;
import android.util.Log;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
@ -500,20 +501,29 @@ public class TopicParser {
if (form != null) {
// english poll in vote mode
pollFormUrl = form.attr("action");
sc = form.select("input[name=sc]").first().attr("value");
Elements formInputs = form.select("input");
for (int j = 0; j < formInputs.size(); j++) {
if (formInputs.get(i).attr("name").equals("options[]")) {
entries.add(new Poll.Entry(formInputs.get(i).text()));
} else if (formInputs.get(i).attr("name").equals("sc")) {
sc = formInputs.get(i).attr("value");
int rowIndex = -1;
Elements possibleEntriesRows = form.child(0).child(0).children();
for (int j = 0; j < possibleEntriesRows.size(); j++) {
if (possibleEntriesRows.get(j).select("input").size() > 0) {
rowIndex = j;
break;
}
}
String entriesRaw = form.child(0).child(0).child(rowIndex).child(0).html();
Matcher entryMatcher = Pattern.compile(">[^<]+<br").matcher(entriesRaw);
while (entryMatcher.find()) {
entries.add(new Poll.Entry(entriesRaw.substring(entryMatcher.start() + 1, entryMatcher.end() - 3).trim()));
}
Element promptColumn = form.child(0).child(0).child(0);
String prompt = promptColumn.text();
Matcher integerMatcher = integerPattern.matcher(prompt);
if (integerMatcher.find())
availableVoteCount = Integer.parseInt(prompt.substring(integerMatcher.start(), integerMatcher.end()));
else
availableVoteCount = 1;
Elements links = form.select("a");
if (links != null && links.size() > 0) {

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

@ -15,7 +15,8 @@
<com.github.mikephil.charting.charts.HorizontalBarChart
android:id="@+id/vote_chart"
android:layout_width="match_parent"
android:layout_height="200dp" />
android:layout_height="200dp"
android:visibility="gone"/>
<TextView
android:id="@+id/error_too_many_checked"
@ -40,12 +41,14 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/remove_vote_button"
android:visibility="gone" />
android:visibility="gone"
android:layout_marginEnd="16dp"/>
<android.support.v7.widget.AppCompatButton
android:id="@+id/show_poll_results_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:text="@string/show_vote_results_button"
android:visibility="gone" />
@ -53,6 +56,7 @@
android:id="@+id/show_poll_options_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:text="@string/show_vote_results_button"
android:visibility="gone" />
@ -60,7 +64,9 @@
android:id="@+id/submit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:text="@string/submit"
android:textColor="@color/accent"
android:visibility="gone"/>
</LinearLayout>
</LinearLayout>
Loading…
Cancel
Save