Browse Source

add functionality to show/ hide results

pull/55/head
Thodoris1999 6 years ago
parent
commit
8a508340ef
  1. 30
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java
  2. 10
      app/src/main/java/gr/thmmy/mthmmy/model/ThmmyPage.java
  3. 12
      app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseHelpers.java
  4. 12
      app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java
  5. 2
      app/src/main/res/layout/activity_topic_poll.xml
  6. 3
      app/src/main/res/values/strings.xml

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

@ -5,7 +5,6 @@ import android.annotation.TargetApi;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
@ -50,7 +49,6 @@ 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;
@ -223,9 +221,29 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
holder.voteChart.setVisibility(View.VISIBLE);
}
if (poll.getRemoveVoteUrl() != null) holder.removeVotesButton.setVisibility(View.VISIBLE);
if (poll.getShowVoteResultsUrl() != null) holder.showPollResultsButton.setVisibility(View.VISIBLE);
if (poll.getShowOptionsUrl() != null) holder.showPollOptionsButton.setVisibility(View.VISIBLE);
else holder.removeVotesButton.setVisibility(View.GONE);
if (poll.getShowVoteResultsUrl() != null) {
holder.showPollResultsButton.setOnClickListener(v -> {
if (holder.voteChart.getData() != null) {
// Chart has been already created, just make it visible
holder.voteChart.setVisibility(View.VISIBLE);
holder.showPollResultsButton.setVisibility(View.GONE);
holder.hidePollResultsButton.setVisibility(View.VISIBLE);
} else viewModel.viewVoteResults();
});
holder.showPollResultsButton.setVisibility(View.VISIBLE);
} else holder.showPollResultsButton.setVisibility(View.GONE);
if (poll.getShowOptionsUrl() != null) {
holder.hidePollResultsButton.setOnClickListener(v -> {
holder.voteChart.setVisibility(View.GONE);
holder.hidePollResultsButton.setVisibility(View.GONE);
holder.showPollResultsButton.setVisibility(View.VISIBLE);
});
holder.hidePollResultsButton.setVisibility(View.VISIBLE);
} else holder.hidePollResultsButton.setVisibility(View.GONE);
if (poll.getPollFormUrl() != null) holder.submitButton.setVisibility(View.VISIBLE);
else holder.submitButton.setVisibility(View.GONE);
} else {
Post currentPost = (Post) topicItems.get(position);
if (currentHolder instanceof PostViewHolder) {
@ -728,7 +746,7 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
final TextView question, errorTooManySelected;
final LinearLayout rootLayout;
final AppCompatButton submitButton;
final AppCompatButton removeVotesButton, showPollResultsButton, showPollOptionsButton;
final AppCompatButton removeVotesButton, showPollResultsButton, hidePollResultsButton;
final HorizontalBarChart voteChart;
public PollViewHolder(View itemView) {
@ -739,7 +757,7 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
submitButton = itemView.findViewById(R.id.submit_button);
removeVotesButton = itemView.findViewById(R.id.remove_vote_button);
showPollResultsButton = itemView.findViewById(R.id.show_poll_results_button);
showPollOptionsButton = itemView.findViewById(R.id.show_poll_options_button);
hidePollResultsButton = itemView.findViewById(R.id.show_poll_options_button);
errorTooManySelected = itemView.findViewById(R.id.error_too_many_checked);
voteChart = itemView.findViewById(R.id.vote_chart);
}

10
app/src/main/java/gr/thmmy/mthmmy/model/ThmmyPage.java

@ -3,6 +3,8 @@ package gr.thmmy.mthmmy.model;
import android.net.Uri;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import timber.log.Timber;
@ -180,10 +182,10 @@ public class ThmmyPage {
public static String getTopicId(String topicUrl) {
if (resolvePageCategory(Uri.parse(topicUrl)) == PageCategory.TOPIC) {
String returnString = topicUrl.substring(topicUrl.indexOf("topic=") + 6);
if (returnString.contains("."))
returnString = returnString.substring(0, returnString.indexOf("."));
return returnString;
Matcher topicIdMatcher = Pattern.compile("topic=[0-9]+").matcher(topicUrl);
if (topicIdMatcher.find()) {
return topicUrl.substring(topicIdMatcher.start() + 6, topicIdMatcher.end());
} else return null;
}
return null;
}

12
app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseHelpers.java

@ -5,6 +5,8 @@ import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import timber.log.Timber;
@ -179,11 +181,9 @@ public class ParseHelpers {
* @return the base URL of the given topic
*/
public static String getBaseURL(String topicURL) {
if (topicURL.substring(0, topicURL.lastIndexOf(".")).contains("topic="))
return topicURL.substring(0, topicURL.lastIndexOf("."));
else {
Timber.wtf(new ParseException("Could not parse base URL of topic"));
return "";
}
Matcher baseUrlMatcher = Pattern.compile(".+topic=[0-9]+").matcher(topicURL);
if (baseUrlMatcher.find())
return topicURL.substring(baseUrlMatcher.start(), baseUrlMatcher.end());
else return "";
}
}

12
app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java

@ -106,6 +106,18 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa
}
}
public void viewVoteResults() {
if (topicUrl == null) throw new NullPointerException("No topic task has been requested yet!");
Timber.i("Viewing poll results");
loadUrl(ParseHelpers.getBaseURL(topicUrl) + ";viewResults");
}
public void loadBaseUrl() {
if (topicUrl == null) throw new NullPointerException("No topic task has been requested yet!");
Timber.i("Viewing poll results");
loadUrl(ParseHelpers.getBaseURL(topicUrl));
}
public void prepareForReply() {
if (replyPageUrl.getValue() == null)
throw new NullPointerException("Topic task has not finished yet!");

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

@ -57,7 +57,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:text="@string/show_vote_results_button"
android:text="@string/show_vote_options_button"
android:visibility="gone" />
<android.support.v7.widget.AppCompatButton

3
app/src/main/res/values/strings.xml

@ -60,8 +60,9 @@
<string name="retry">retry</string>
<string name="unauthorized_topic_error">This topic is either missing or off limits to you</string>
<string name="remove_vote_button">Remove vote</string>
<string name="show_vote_results_button">Show results</string>
<string name="show_vote_results_button">show results</string>
<string name="error_too_many_checked">You may only select %d options</string>
<string name="show_vote_options_button">hide results</string>
<!--Profile Activity-->
<string name="username">Username</string>

Loading…
Cancel
Save