mirror of https://github.com/ThmmyNoLife/mTHMMY
oogee
6 years ago
committed by
GitHub
22 changed files with 1081 additions and 503 deletions
@ -0,0 +1,20 @@ |
|||
package gr.thmmy.mthmmy.activities.topic.tasks; |
|||
|
|||
import org.jsoup.nodes.Document; |
|||
|
|||
import gr.thmmy.mthmmy.utils.NetworkResultCodes; |
|||
import gr.thmmy.mthmmy.utils.NetworkTask; |
|||
import okhttp3.Response; |
|||
|
|||
public class RemoveVoteTask extends NetworkTask<Void> { |
|||
|
|||
@Override |
|||
protected Void performTask(Document document, Response response) { |
|||
return null; |
|||
} |
|||
|
|||
@Override |
|||
protected int getResultCode(Response response, Void data) { |
|||
return NetworkResultCodes.SUCCESSFUL; |
|||
} |
|||
} |
@ -0,0 +1,48 @@ |
|||
package gr.thmmy.mthmmy.activities.topic.tasks; |
|||
|
|||
import org.jsoup.nodes.Document; |
|||
|
|||
import java.io.IOException; |
|||
|
|||
import gr.thmmy.mthmmy.utils.NetworkResultCodes; |
|||
import gr.thmmy.mthmmy.utils.NetworkTask; |
|||
import okhttp3.MultipartBody; |
|||
import okhttp3.OkHttpClient; |
|||
import okhttp3.Request; |
|||
import okhttp3.Response; |
|||
|
|||
public class SubmitVoteTask extends NetworkTask<Void> { |
|||
|
|||
private int[] votes; |
|||
|
|||
public SubmitVoteTask(int... votes) { |
|||
this.votes = votes; |
|||
} |
|||
|
|||
@Override |
|||
protected Response sendRequest(OkHttpClient client, String... input) throws IOException { |
|||
MultipartBody.Builder postBodyBuilder = new MultipartBody.Builder() |
|||
.setType(MultipartBody.FORM) |
|||
.addFormDataPart("sc", input[1]); |
|||
for (int vote : votes) { |
|||
postBodyBuilder.addFormDataPart("options[]", Integer.toString(vote)); |
|||
} |
|||
|
|||
Request voteRequest = new Request.Builder() |
|||
.url(input[0]) |
|||
.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36") |
|||
.post(postBodyBuilder.build()) |
|||
.build(); |
|||
return client.newCall(voteRequest).execute(); |
|||
} |
|||
|
|||
@Override |
|||
protected Void performTask(Document document, Response response) { |
|||
return null; |
|||
} |
|||
|
|||
@Override |
|||
protected int getResultCode(Response response, Void data) { |
|||
return NetworkResultCodes.SUCCESSFUL; |
|||
} |
|||
} |
@ -0,0 +1,108 @@ |
|||
package gr.thmmy.mthmmy.model; |
|||
|
|||
import java.text.DecimalFormat; |
|||
|
|||
public class Poll extends TopicItem { |
|||
public static final int TYPE_POLL = 3; |
|||
|
|||
private final String question; |
|||
private Entry[] entries; |
|||
private int availableVoteCount; |
|||
private String pollFormUrl, sc, removeVoteUrl, showVoteResultsUrl, showOptionsUrl; |
|||
|
|||
public Poll(String question, Entry[] entries, int availableVoteCount, String pollFormUrl, String sc, |
|||
String removeVoteUrl, String showVoteResultsUrl, String showOptionsUrl) { |
|||
this.question = question; |
|||
this.entries = entries; |
|||
this.availableVoteCount = availableVoteCount; |
|||
this.pollFormUrl = pollFormUrl; |
|||
this.sc = sc; |
|||
this.removeVoteUrl = removeVoteUrl; |
|||
this.showVoteResultsUrl = showVoteResultsUrl; |
|||
this.showOptionsUrl = showOptionsUrl; |
|||
} |
|||
|
|||
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() { |
|||
return question; |
|||
} |
|||
|
|||
public Entry[] getEntries() { |
|||
return entries; |
|||
} |
|||
|
|||
public int getAvailableVoteCount() { |
|||
return availableVoteCount; |
|||
} |
|||
|
|||
public String getPollFormUrl() { |
|||
return pollFormUrl; |
|||
} |
|||
|
|||
public String getSc() { |
|||
return sc; |
|||
} |
|||
|
|||
public String getRemoveVoteUrl() { |
|||
return removeVoteUrl; |
|||
} |
|||
|
|||
public String getShowVoteResultsUrl() { |
|||
return showVoteResultsUrl; |
|||
} |
|||
|
|||
public String getShowOptionsUrl() { |
|||
return showOptionsUrl; |
|||
} |
|||
|
|||
public static class Entry { |
|||
private final String entryName; |
|||
private int votes; |
|||
|
|||
public Entry(String entryName, int votes) { |
|||
this.entryName = entryName; |
|||
this.votes = votes; |
|||
} |
|||
|
|||
/** |
|||
* Constructor for entry with unknown number of votes |
|||
* |
|||
* @param entryName |
|||
* The name of the entry |
|||
*/ |
|||
public Entry(String entryName) { |
|||
this.entryName = entryName; |
|||
votes = -1; |
|||
} |
|||
|
|||
public String getEntryName() { |
|||
return entryName; |
|||
} |
|||
|
|||
public int getVotes() { |
|||
return votes; |
|||
} |
|||
|
|||
public void setVotes(int votes) { |
|||
this.votes = votes; |
|||
} |
|||
|
|||
@Override |
|||
public String toString() { |
|||
return "Vote label:" + entryName + ", num votes:" + votes; |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,5 @@ |
|||
package gr.thmmy.mthmmy.model; |
|||
|
|||
public abstract class TopicItem { |
|||
|
|||
} |
@ -0,0 +1,78 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<LinearLayout |
|||
xmlns:android="http://schemas.android.com/apk/res/android" |
|||
android:layout_width="match_parent" |
|||
android:layout_height="wrap_content" |
|||
android:layout_margin="16dp" |
|||
android:orientation="vertical"> |
|||
|
|||
<TextView |
|||
android:id="@+id/question_textview" |
|||
android:layout_width="wrap_content" |
|||
android:layout_height="wrap_content" |
|||
android:textColor="@color/primary_text" /> |
|||
|
|||
<LinearLayout |
|||
android:id="@+id/options_layout" |
|||
android:layout_width="wrap_content" |
|||
android:layout_height="wrap_content" |
|||
android:orientation="vertical" /> |
|||
|
|||
<com.github.mikephil.charting.charts.HorizontalBarChart |
|||
android:id="@+id/vote_chart" |
|||
android:layout_width="match_parent" |
|||
android:layout_height="wrap_content" |
|||
android:visibility="gone"/> |
|||
|
|||
<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" /> |
|||
|
|||
<!-- controls --> |
|||
<LinearLayout |
|||
android:layout_width="match_parent" |
|||
android:layout_height="wrap_content"> |
|||
|
|||
<View |
|||
android:layout_width="0dp" |
|||
android:layout_height="wrap_content" |
|||
android:layout_weight="1"/> |
|||
|
|||
<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:layout_marginEnd="16dp"/> |
|||
|
|||
<android.support.v7.widget.AppCompatButton |
|||
android:id="@+id/show_poll_results_button" |
|||
android:layout_width="wrap_content" |
|||
android:layout_height="wrap_content" |
|||
android:layout_marginEnd="16dp" |
|||
android:text="@string/show_vote_results_button" |
|||
android:visibility="gone" /> |
|||
|
|||
<android.support.v7.widget.AppCompatButton |
|||
android:id="@+id/show_poll_options_button" |
|||
android:layout_width="wrap_content" |
|||
android:layout_height="wrap_content" |
|||
android:layout_marginEnd="16dp" |
|||
android:text="@string/show_vote_options_button" |
|||
android:visibility="gone" /> |
|||
|
|||
<android.support.v7.widget.AppCompatButton |
|||
android:id="@+id/submit_button" |
|||
android:layout_width="wrap_content" |
|||
android:layout_height="wrap_content" |
|||
android:layout_marginEnd="16dp" |
|||
android:text="@string/submit" |
|||
android:textColor="@color/accent" |
|||
android:visibility="gone"/> |
|||
</LinearLayout> |
|||
</LinearLayout> |
Loading…
Reference in new issue