Browse Source

Polls improvements again

pull/61/merge
Ezerous 6 years ago
parent
commit
92f4b06296
No known key found for this signature in database GPG Key ID: 262B2954BBA319E3
  1. 28
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java
  2. 6
      app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ThmmyParser.java

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

@ -42,6 +42,7 @@ import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry; import com.github.mikephil.charting.data.BarEntry;
import com.squareup.picasso.Picasso; import com.squareup.picasso.Picasso;
import java.text.DecimalFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
@ -166,10 +167,13 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
boolean pollSupported = true; boolean pollSupported = true;
for (Poll.Entry entry : entries) { for (Poll.Entry entry : entries) {
if (ThmmyParser.containsHtml(entry.getEntryName())) pollSupported = false; if (ThmmyParser.containsHtml(entry.getEntryName())){
pollSupported = false;
break; break;
} }
if (ThmmyParser.containsHtml(poll.getQuestion())) pollSupported = false; }
if (ThmmyParser.containsHtml(poll.getQuestion()))
pollSupported = false;
if (!pollSupported) { if (!pollSupported) {
holder.optionsLayout.setVisibility(View.GONE); holder.optionsLayout.setVisibility(View.GONE);
holder.voteChart.setVisibility(View.GONE); holder.voteChart.setVisibility(View.GONE);
@ -193,6 +197,7 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
holder.errorTextview.setVisibility(View.GONE); holder.errorTextview.setVisibility(View.GONE);
final int primaryTextColor = context.getResources().getColor(R.color.primary_text); final int primaryTextColor = context.getResources().getColor(R.color.primary_text);
final int accentColor = context.getResources().getColor(R.color.accent);
if (poll.getAvailableVoteCount() > 1) { if (poll.getAvailableVoteCount() > 1) {
for (Poll.Entry entry : entries) { for (Poll.Entry entry : entries) {
@ -239,19 +244,21 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
//Showing results //Showing results
holder.optionsLayout.setVisibility(View.GONE); holder.optionsLayout.setVisibility(View.GONE);
List<BarEntry> valuesToCompare = new ArrayList<>(); List<BarEntry> valuesToCompare = new ArrayList<>();
int totalVotes = 0;
for (int i = 0; i < entries.length; i++) { for (int i = 0; i < entries.length; i++) {
valuesToCompare.add(new BarEntry(i, entries[i].getVotes())); valuesToCompare.add(new BarEntry(i, entries[i].getVotes()));
totalVotes += entries[i].getVotes();
} }
BarDataSet dataSet = new BarDataSet(valuesToCompare, "Vote Results"); BarDataSet dataSet = new BarDataSet(valuesToCompare, "Vote Results");
dataSet.setDrawValues(true); dataSet.setDrawValues(true);
dataSet.setColor(context.getResources().getColor(R.color.accent)); dataSet.setColor(accentColor);
dataSet.setValueTextColor(primaryTextColor); dataSet.setValueTextColor(accentColor);
YAxis yAxisLeft = holder.voteChart.getAxisLeft(); YAxis yAxisLeft = holder.voteChart.getAxisLeft();
yAxisLeft.setGranularity(1); yAxisLeft.setGranularity(1);
yAxisLeft.setTextColor(primaryTextColor); yAxisLeft.setTextColor(primaryTextColor);
yAxisLeft.setAxisMinimum(0); yAxisLeft.setAxisMinimum(0);
yAxisLeft.setSpaceTop(30f); yAxisLeft.setSpaceTop(40f);
YAxis yAxisRight = holder.voteChart.getAxisRight(); YAxis yAxisRight = holder.voteChart.getAxisRight();
yAxisRight.setEnabled(false); yAxisRight.setEnabled(false);
@ -265,6 +272,12 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
xAxis.setPosition(XAxis.XAxisPosition.TOP_INSIDE); xAxis.setPosition(XAxis.XAxisPosition.TOP_INSIDE);
BarData barData = new BarData(dataSet); BarData barData = new BarData(dataSet);
int finalSum = totalVotes;
barData.setValueFormatter((value, entry, dataSetIndex, viewPortHandler) -> {
DecimalFormat format = new DecimalFormat("###.#%");
double percentage = ((double) value / (double) finalSum);
return "" + (int) value + " (" + format.format(percentage) + ")";
});
holder.voteChart.setData(barData); holder.voteChart.setData(barData);
holder.voteChart.getLegend().setEnabled(false); holder.voteChart.getLegend().setEnabled(false);
holder.voteChart.getDescription().setEnabled(false); holder.voteChart.getDescription().setEnabled(false);
@ -509,9 +522,9 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
holder.overflowButton.setOnClickListener(view -> { holder.overflowButton.setOnClickListener(view -> {
//Inflates the popup menu content //Inflates the popup menu content
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (layoutInflater == null) { if (layoutInflater == null)
return; return;
}
View popUpContent = layoutInflater.inflate(R.layout.activity_topic_overflow_menu, null); View popUpContent = layoutInflater.inflate(R.layout.activity_topic_overflow_menu, null);
//Creates the PopupWindow //Creates the PopupWindow
@ -795,7 +808,6 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
voteChart = itemView.findViewById(R.id.vote_chart); voteChart = itemView.findViewById(R.id.vote_chart);
voteChart.setScaleEnabled(false); voteChart.setScaleEnabled(false);
voteChart.setTouchEnabled(false); voteChart.setTouchEnabled(false);
voteChart.setDrawValueAboveBar(false);
} }
} }

6
app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ThmmyParser.java

@ -162,7 +162,7 @@ public class ThmmyParser {
return tags.toArray(new BBTag[0]); return tags.toArray(new BBTag[0]);
} }
public static HtmlTag[] getHtmlTags(String html) { private static HtmlTag[] getHtmlTags(String html) {
Pattern htmlPattern = Pattern.compile("<(.+?)>"); Pattern htmlPattern = Pattern.compile("<(.+?)>");
LinkedList<HtmlTag> tags = new LinkedList<>(); LinkedList<HtmlTag> tags = new LinkedList<>();
@ -205,11 +205,11 @@ public class ThmmyParser {
return name.equals("b") || name.equals("i") || name.equals("span") || name.equals("del") || name.equals("a"); return name.equals("b") || name.equals("i") || name.equals("span") || name.equals("del") || name.equals("a");
} }
public static boolean isBBTagSupported(String name) { private static boolean isBBTagSupported(String name) {
return name.equals("b") || name.equals("i") || name.equals("u") || name.equals("s"); return name.equals("b") || name.equals("i") || name.equals("u") || name.equals("s");
} }
public static boolean isHtmlTag(String tagName) { private static boolean isHtmlTag(String tagName) {
for (String tag : ALL_HTML_TAGS) for (String tag : ALL_HTML_TAGS)
if (TextUtils.equals(tag, tagName)) return true; if (TextUtils.equals(tag, tagName)) return true;
return false; return false;

Loading…
Cancel
Save