From 8a508340efe6c23786640a2dc0cfb764c41dd7e7 Mon Sep 17 00:00:00 2001 From: Thodoris1999 Date: Sat, 22 Sep 2018 23:19:34 +0300 Subject: [PATCH] add functionality to show/ hide results --- .../mthmmy/activities/topic/TopicAdapter.java | 30 +++++++++++++++---- .../java/gr/thmmy/mthmmy/model/ThmmyPage.java | 10 ++++--- .../mthmmy/utils/parsing/ParseHelpers.java | 12 ++++---- .../mthmmy/viewmodel/TopicViewModel.java | 12 ++++++++ .../main/res/layout/activity_topic_poll.xml | 2 +- app/src/main/res/values/strings.xml | 3 +- 6 files changed, 51 insertions(+), 18 deletions(-) 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 987528cf..c6c7ae53 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 @@ -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 { 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 { 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 { 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); } diff --git a/app/src/main/java/gr/thmmy/mthmmy/model/ThmmyPage.java b/app/src/main/java/gr/thmmy/mthmmy/model/ThmmyPage.java index bc451fd1..c500c50a 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/model/ThmmyPage.java +++ b/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; } diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseHelpers.java b/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseHelpers.java index 0034ffc4..804c9e85 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseHelpers.java +++ b/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 ""; } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java b/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java index a625f311..5bc1f894 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java +++ b/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!"); diff --git a/app/src/main/res/layout/activity_topic_poll.xml b/app/src/main/res/layout/activity_topic_poll.xml index e23b57cf..fd4f1f5f 100644 --- a/app/src/main/res/layout/activity_topic_poll.xml +++ b/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" /> retry This topic is either missing or off limits to you Remove vote - Show results + show results You may only select %d options + hide results Username