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 com.squareup.picasso.Picasso;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
@ -166,10 +167,11 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
holder.question.setText(poll.getQuestion()); holder.question.setText(poll.getQuestion());
if (poll.getAvailableVoteCount() > 1) { if (poll.getAvailableVoteCount() > 1) {
LinearLayout optionsLayout = new LinearLayout(context); LinearLayout optionsLayout = new LinearLayout(context);
optionsLayout.setOrientation(LinearLayout.HORIZONTAL); optionsLayout.setOrientation(LinearLayout.VERTICAL);
for (Poll.Entry entry : entries) { for (Poll.Entry entry : entries) {
CheckBox checkBox = new CheckBox(context); CheckBox checkBox = new CheckBox(context);
checkBox.setText(entry.getEntryName()); checkBox.setText(entry.getEntryName());
checkBox.setTextColor(context.getResources().getColor(R.color.primary_text));
checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> checkBox.setOnCheckedChangeListener((buttonView, isChecked) ->
viewModel.onVoteCheckboxClicked(optionsLayout.indexOfChild(buttonView), isChecked)); viewModel.onVoteCheckboxClicked(optionsLayout.indexOfChild(buttonView), isChecked));
optionsLayout.addView(checkBox); optionsLayout.addView(checkBox);
@ -211,6 +213,7 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
holder.voteChart.getLegend().setEnabled(false); holder.voteChart.getLegend().setEnabled(false);
holder.voteChart.getDescription().setEnabled(false); holder.voteChart.getDescription().setEnabled(false);
holder.voteChart.invalidate(); holder.voteChart.invalidate();
holder.voteChart.setVisibility(View.VISIBLE);
} }
if (poll.getRemoveVoteUrl() != null) holder.removeVotesButton.setVisibility(View.VISIBLE); if (poll.getRemoveVoteUrl() != null) holder.removeVotesButton.setVisibility(View.VISIBLE);
if (poll.getShowVoteResultsUrl() != null) holder.showPollResultsButton.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; package gr.thmmy.mthmmy.activities.topic;
import android.graphics.Color; import android.graphics.Color;
import android.util.Log;
import org.jsoup.Jsoup; import org.jsoup.Jsoup;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
@ -500,20 +501,29 @@ public class TopicParser {
if (form != null) { if (form != null) {
// english poll in vote mode // english poll in vote mode
pollFormUrl = form.attr("action"); pollFormUrl = form.attr("action");
sc = form.select("input[name=sc]").first().attr("value");
Elements formInputs = form.select("input"); int rowIndex = -1;
for (int j = 0; j < formInputs.size(); j++) { Elements possibleEntriesRows = form.child(0).child(0).children();
if (formInputs.get(i).attr("name").equals("options[]")) { for (int j = 0; j < possibleEntriesRows.size(); j++) {
entries.add(new Poll.Entry(formInputs.get(i).text())); if (possibleEntriesRows.get(j).select("input").size() > 0) {
} else if (formInputs.get(i).attr("name").equals("sc")) { rowIndex = j;
sc = formInputs.get(i).attr("value"); 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); Element promptColumn = form.child(0).child(0).child(0);
String prompt = promptColumn.text(); String prompt = promptColumn.text();
Matcher integerMatcher = integerPattern.matcher(prompt); Matcher integerMatcher = integerPattern.matcher(prompt);
if (integerMatcher.find())
availableVoteCount = Integer.parseInt(prompt.substring(integerMatcher.start(), integerMatcher.end())); availableVoteCount = Integer.parseInt(prompt.substring(integerMatcher.start(), integerMatcher.end()));
else
availableVoteCount = 1;
Elements links = form.select("a"); Elements links = form.select("a");
if (links != null && links.size() > 0) { 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 <com.github.mikephil.charting.charts.HorizontalBarChart
android:id="@+id/vote_chart" android:id="@+id/vote_chart"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="200dp" /> android:layout_height="200dp"
android:visibility="gone"/>
<TextView <TextView
android:id="@+id/error_too_many_checked" android:id="@+id/error_too_many_checked"
@ -40,12 +41,14 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/remove_vote_button" android:text="@string/remove_vote_button"
android:visibility="gone" /> android:visibility="gone"
android:layout_marginEnd="16dp"/>
<android.support.v7.widget.AppCompatButton <android.support.v7.widget.AppCompatButton
android:id="@+id/show_poll_results_button" android:id="@+id/show_poll_results_button"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:text="@string/show_vote_results_button" android:text="@string/show_vote_results_button"
android:visibility="gone" /> android:visibility="gone" />
@ -53,6 +56,7 @@
android:id="@+id/show_poll_options_button" android:id="@+id/show_poll_options_button"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:text="@string/show_vote_results_button" android:text="@string/show_vote_results_button"
android:visibility="gone" /> android:visibility="gone" />
@ -60,7 +64,9 @@
android:id="@+id/submit_button" android:id="@+id/submit_button"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:text="@string/submit" android:text="@string/submit"
android:textColor="@color/accent"
android:visibility="gone"/> android:visibility="gone"/>
</LinearLayout> </LinearLayout>
</LinearLayout> </LinearLayout>
Loading…
Cancel
Save