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 509c13c1..32ef244b 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 @@ -15,6 +15,7 @@ import android.support.annotation.NonNull; import android.support.v4.content.res.ResourcesCompat; import android.support.v7.app.AlertDialog; import android.support.v7.content.res.AppCompatResources; +import android.support.v7.widget.AppCompatButton; import android.support.v7.widget.RecyclerView; import android.text.InputType; import android.text.TextUtils; @@ -27,6 +28,8 @@ import android.view.inputmethod.InputMethodManager; import android.webkit.WebResourceRequest; import android.webkit.WebView; import android.webkit.WebViewClient; +import android.widget.CheckBox; +import android.widget.CompoundButton; import android.widget.EditText; import android.widget.ImageButton; import android.widget.ImageView; @@ -38,6 +41,7 @@ import android.widget.TextView; import com.squareup.picasso.Picasso; import java.util.List; +import java.util.Map; import java.util.Objects; import gr.thmmy.mthmmy.R; @@ -135,6 +139,10 @@ class TopicAdapter extends RecyclerView.Adapter { editPostEdittext.requestFocus(); return new EditMessageViewHolder(view); + } else if (viewType == Poll.TYPE_POLL) { + View view = LayoutInflater.from(parent.getContext()). + inflate(R.layout.activity_topic_edit_row, parent, false); + return new PollViewHolder(view); } else { throw new IllegalArgumentException("Unknown view type"); } @@ -145,7 +153,18 @@ class TopicAdapter extends RecyclerView.Adapter { public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder currentHolder, final int position) { if (currentHolder.getItemViewType() == Poll.TYPE_POLL) { - + Poll poll = (Poll) topicItems.get(position); + Poll.Entry[] entries = poll.getEntries(); + PollViewHolder holder = (PollViewHolder) currentHolder; + holder.question.setText(poll.getQuestion()); + if (poll.getAvailableVoteCount() > 1) { + for (int i = 0; i < entries.length; i++) { + CheckBox checkBox = new CheckBox(context); + checkBox.setText(entries[i].getEntryName()); + checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> + viewModel.onVoteCheckboxClicked(holder.optionsLayout.indexOfChild(buttonView), isChecked)); + } + } } else { Post currentPost = (Post) topicItems.get(position); if (currentHolder instanceof PostViewHolder) { @@ -644,6 +663,23 @@ class TopicAdapter extends RecyclerView.Adapter { } } + static class PollViewHolder extends RecyclerView.ViewHolder { + final TextView question, errorTooManySelected; + final LinearLayout optionsLayout; + final AppCompatButton submitButton; + final AppCompatButton removeVotesButton; + + public PollViewHolder(View itemView) { + super(itemView); + + question = itemView.findViewById(R.id.question_textview); + optionsLayout = itemView.findViewById(R.id.options_layout); + submitButton = itemView.findViewById(R.id.submit_button); + removeVotesButton = itemView.findViewById(R.id.remove_vote_button); + errorTooManySelected = itemView.findViewById(R.id.error_too_many_checked); + } + } + /** * This class is used to handle link clicks in WebViews. When link url is one that the app can * handle internally, it does. Otherwise user is prompt to open the link in a browser. 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 85fa4c49..1d1b54bc 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/model/Poll.java +++ b/app/src/main/java/gr/thmmy/mthmmy/model/Poll.java @@ -3,21 +3,36 @@ package gr.thmmy.mthmmy.model; import java.text.DecimalFormat; public class Poll extends TopicItem { - public static int TYPE_POLL = 3; + public static final int TYPE_POLL = 3; private final String question; private Entry[] entries; private int availableVoteCount; - private String pollFormUrl, sc, removeVoteUrl; + private String pollFormUrl, sc, removeVoteUrl, showVoteResultsUrl; public Poll(String question, Entry[] entries, int availableVoteCount, String pollFormUrl, String sc, - String removeVoteUrl) { + String removeVoteUrl, String showVoteResultsUrl) { this.question = question; this.entries = entries; this.availableVoteCount = availableVoteCount; this.pollFormUrl = pollFormUrl; this.sc = sc; this.removeVoteUrl = removeVoteUrl; + this.showVoteResultsUrl = showVoteResultsUrl; + } + + public int totalVotes() { + int sum = 0; + for (Entry entry : entries) { + sum += entry.votes; + } + return sum; + } + + public String getVotePercentage(int index) { + DecimalFormat format = new DecimalFormat(".#"); + double percentage = 100 * ((double) entries[index].votes / (double) totalVotes()); + return format.format(percentage); } public String getQuestion() { @@ -44,21 +59,11 @@ public class Poll extends TopicItem { return removeVoteUrl; } - public int totalVotes() { - int sum = 0; - for (Entry entry : entries) { - sum += entry.votes; - } - return sum; - } - - public String getVotePercentage(int index) { - DecimalFormat format = new DecimalFormat(".#"); - double percentage = 100 * ((double) entries[index].votes / (double) totalVotes()); - return format.format(percentage); + public String getShowVoteResultsUrl() { + return showVoteResultsUrl; } - static class Entry { + public static class Entry { private final String entryName; private int votes; 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 66a80407..1a4c5adc 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java +++ b/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java @@ -5,6 +5,8 @@ import android.content.Context; import android.content.SharedPreferences; import android.os.AsyncTask; import android.preference.PreferenceManager; +import android.view.View; +import android.widget.CheckBox; import java.util.ArrayList; @@ -44,6 +46,7 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa * holds the adapter position of the post being edited */ private int postBeingEditedPosition; + private ArrayList selectedVoteIndices; private TopicTask currentTopicTask; private PrepareForEditTask currentPrepareForEditTask; @@ -245,6 +248,15 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa if (changePage && oldIndicatorIndex != this.pageIndicatorIndex.getValue()) performPageChange(); } + public void onVoteCheckboxClicked(int index, boolean checked) { + if (checked) { + selectedVoteIndices.add(index); + } else { + selectedVoteIndices.remove(index); + } + + } + // <-------------Just getters, setters and helper methods below here----------------> public MutableLiveData getTopicViewers() { diff --git a/app/src/main/res/layout/activity_topic_poll.xml b/app/src/main/res/layout/activity_topic_poll.xml new file mode 100644 index 00000000..3c68a7db --- /dev/null +++ b/app/src/main/res/layout/activity_topic_poll.xml @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c52cf546..d502d343 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -59,6 +59,9 @@ Network error retry This topic is either missing or off limits to you + Remove vote + Show results + You may only select %d options Username