Browse Source

cover multiple selection poll

pull/55/head
Thodoris1999 6 years ago
parent
commit
8ec74335a6
  1. 38
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java
  2. 37
      app/src/main/java/gr/thmmy/mthmmy/model/Poll.java
  3. 12
      app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java
  4. 46
      app/src/main/res/layout/activity_topic_poll.xml
  5. 3
      app/src/main/res/values/strings.xml

38
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<RecyclerView.ViewHolder> {
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<RecyclerView.ViewHolder> {
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<RecyclerView.ViewHolder> {
}
}
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.

37
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;

12
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<Integer> 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<String> getTopicViewers() {

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

@ -0,0 +1,46 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/question_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:id="@+id/options_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"/>
<TextView
android:id="@+id/error_too_many_checked"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/error_too_many_checked"
android:textColor="@color/red"
android:visibility="gone"/>
<android.support.v7.widget.AppCompatButton
android:id="@+id/submit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/submit"/>
<android.support.v7.widget.AppCompatButton
android:id="@+id/remove_vote_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/remove_vote_button"
android:visibility="gone"/>
<android.support.v7.widget.AppCompatButton
android:id="@+id/show_poll_results_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/show_vote_results_button"
android:visibility="gone"/>
</LinearLayout>

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

@ -59,6 +59,9 @@
<string name="generic_network_error">Network error</string>
<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="error_too_many_checked">You may only select %d options</string>
<!--Profile Activity-->
<string name="username">Username</string>

Loading…
Cancel
Save