Browse Source

finish submit/remove vote functionality

pull/55/head
Thodoris1999 6 years ago
parent
commit
53cecb5fbc
  1. 8
      app/src/main/java/gr/thmmy/mthmmy/activities/main/forum/ForumFragment.java
  2. 8
      app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java
  3. 6
      app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java
  4. 11
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java
  5. 24
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java
  6. 6
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/DeleteTask.java
  7. 52
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/SubmitVoteTask.java
  8. 62
      app/src/main/java/gr/thmmy/mthmmy/utils/ExternalAsyncTask.java
  9. 32
      app/src/main/java/gr/thmmy/mthmmy/utils/NetworkTask.java
  10. 17
      app/src/main/java/gr/thmmy/mthmmy/utils/parsing/NewParseTask.java
  11. 5
      app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseHelpers.java
  12. 63
      app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java

8
app/src/main/java/gr/thmmy/mthmmy/activities/main/forum/ForumFragment.java

@ -187,13 +187,13 @@ public class ForumFragment extends BaseFragment {
private class ForumTask extends NewParseTask<ArrayList<Category>> {
private HttpUrl forumUrl = SessionManager.forumUrl; //may change upon collapse/expand
public ForumTask(OnParseTaskStartedListener onParseTaskStartedListener,
OnParseTaskFinishedListener<ArrayList<Category>> onParseTaskFinishedListener) {
super(onParseTaskStartedListener, onParseTaskFinishedListener);
public ForumTask(OnTaskStartedListener onTaskStartedListener,
OnNetworkTaskFinishedListener<ArrayList<Category>> onParseTaskFinishedListener) {
super(onTaskStartedListener, onParseTaskFinishedListener);
}
@Override
protected ArrayList<Category> parse(Document document) throws ParseException {
protected ArrayList<Category> parse(Document document, Response response) throws ParseException {
Elements categoryBlocks = document.select(".tborder:not([style])>table[cellpadding=5]");
if (categoryBlocks.size() != 0) {
ArrayList<Category> fetchedCategories = new ArrayList<>();

8
app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java

@ -157,13 +157,13 @@ public class RecentFragment extends BaseFragment {
//---------------------------------------ASYNC TASK-----------------------------------
private class RecentTask extends NewParseTask<ArrayList<TopicSummary>> {
public RecentTask(OnParseTaskStartedListener onParseTaskStartedListener,
OnParseTaskFinishedListener<ArrayList<TopicSummary>> onParseTaskFinishedListener) {
super(onParseTaskStartedListener, onParseTaskFinishedListener);
public RecentTask(OnTaskStartedListener onTaskStartedListener,
OnNetworkTaskFinishedListener<ArrayList<TopicSummary>> onParseTaskFinishedListener) {
super(onTaskStartedListener, onParseTaskFinishedListener);
}
@Override
protected ArrayList<TopicSummary> parse(Document document) throws ParseException {
protected ArrayList<TopicSummary> parse(Document document, Response response) throws ParseException {
ArrayList<TopicSummary> fetchedRecent = new ArrayList<>();
Elements recent = document.select("#block8 :first-child div");
if (!recent.isEmpty()) {

6
app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java

@ -185,12 +185,12 @@ public class UnreadFragment extends BaseFragment {
private class UnreadTask extends NewParseTask<Void> {
UnreadTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskFinishedListener<Void> onParseTaskFinishedListener) {
super(onParseTaskStartedListener, onParseTaskFinishedListener);
UnreadTask(OnTaskStartedListener onTaskStartedListener, OnNetworkTaskFinishedListener<Void> onParseTaskFinishedListener) {
super(onTaskStartedListener, onParseTaskFinishedListener);
}
@Override
protected Void parse(Document document) throws ParseException {
protected Void parse(Document document, Response response) throws ParseException {
Elements unread = document.select("table.bordercolor[cellspacing=1] tr:not(.titlebg)");
if (!unread.isEmpty()) {
//topicSummaries.clear();

11
app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java

@ -50,8 +50,10 @@ import gr.thmmy.mthmmy.model.Post;
import gr.thmmy.mthmmy.model.ThmmyPage;
import gr.thmmy.mthmmy.model.TopicItem;
import gr.thmmy.mthmmy.utils.CustomLinearLayoutManager;
import gr.thmmy.mthmmy.utils.ExternalAsyncTask;
import gr.thmmy.mthmmy.utils.HTMLUtils;
import gr.thmmy.mthmmy.utils.NetworkResultCodes;
import gr.thmmy.mthmmy.utils.NetworkTask;
import gr.thmmy.mthmmy.utils.parsing.ParseHelpers;
import gr.thmmy.mthmmy.viewmodel.TopicViewModel;
import me.zhanghai.android.materialprogressbar.MaterialProgressBar;
@ -598,6 +600,15 @@ public class TopicActivity extends BaseActivity implements TopicAdapter.OnPostFo
progressBar.setVisibility(ProgressBar.GONE);
}
});
viewModel.setVoteTaskStartedListener(() -> progressBar.setVisibility(ProgressBar.VISIBLE));
viewModel.setVoteTaskFinishedListener((resultCode, data) -> {
if (resultCode == NetworkResultCodes.SUCCESSFUL)
Toast.makeText(this, "success", Toast.LENGTH_LONG).show();
else
Toast.makeText(this, "fail", Toast.LENGTH_LONG).show();
progressBar.setVisibility(View.GONE);
viewModel.loadUrl(ParseHelpers.getBaseURL(viewModel.getTopicUrl()) + ".0");
});
// observe the chages in data
viewModel.getPageIndicatorIndex().observe(this, pageIndicatorIndex -> {
if (pageIndicatorIndex == null) return;

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

@ -165,6 +165,7 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
Poll.Entry[] entries = poll.getEntries();
PollViewHolder holder = (PollViewHolder) currentHolder;
holder.question.setText(poll.getQuestion());
holder.optionsLayout.removeAllViews();
if (poll.getAvailableVoteCount() > 1) {
for (Poll.Entry entry : entries) {
CheckBox checkBox = new CheckBox(context);
@ -176,10 +177,11 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
holder.optionsLayout.setVisibility(View.VISIBLE);
} else if (poll.getAvailableVoteCount() == 1) {
RadioGroup radioGroup = new RadioGroup(context);
for (Poll.Entry entry : entries) {
for (int i = 0; i < entries.length; i++) {
RadioButton radioButton = new RadioButton(context);
radioButton.setText(entry.getEntryName());
radioButton.setOnClickListener(v -> viewModel.onRadioButtonCLicked(radioGroup.indexOfChild(v)));
radioButton.setId(i);
radioButton.setText(entries[i].getEntryName());
radioButton.setTextColor(context.getResources().getColor(R.color.primary_text));
radioGroup.addView(radioButton);
}
holder.optionsLayout.addView(radioGroup);
@ -187,7 +189,6 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
holder.optionsLayout.setVisibility(View.VISIBLE);
} else {
//Showing results
holder.optionsLayout.removeAllViews();
holder.optionsLayout.setVisibility(View.GONE);
List<BarEntry> valuesToCompare = new ArrayList<>();
for (int i = 0; i < entries.length; i++) {
@ -221,8 +222,13 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
holder.voteChart.invalidate();
holder.voteChart.setVisibility(View.VISIBLE);
}
if (poll.getRemoveVoteUrl() != null) holder.removeVotesButton.setVisibility(View.VISIBLE);
else holder.removeVotesButton.setVisibility(View.GONE);
if (poll.getRemoveVoteUrl() != null) {
holder.removeVotesButton.setOnClickListener(v -> {
viewModel.loadUrl(poll.getRemoveVoteUrl());
viewModel.loadUrl(ParseHelpers.getBaseURL(viewModel.getTopicUrl()) + ".0");
});
holder.removeVotesButton.setVisibility(View.VISIBLE);
} else holder.removeVotesButton.setVisibility(View.GONE);
if (poll.getShowVoteResultsUrl() != null) {
holder.showPollResultsButton.setOnClickListener(v -> viewModel.loadUrl(poll.getShowVoteResultsUrl()));
holder.showPollResultsButton.setVisibility(View.VISIBLE);
@ -232,8 +238,10 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
holder.hidePollResultsButton.setOnClickListener(v -> viewModel.loadUrl(poll.getShowOptionsUrl()));
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);
if (poll.getPollFormUrl() != null) {
holder.submitButton.setOnClickListener(v -> viewModel.submitVote(holder.optionsLayout));
holder.submitButton.setVisibility(View.VISIBLE);
} else holder.submitButton.setVisibility(View.GONE);
} else {
Post currentPost = (Post) topicItems.get(position);
if (currentHolder instanceof PostViewHolder) {

6
app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/DeleteTask.java

@ -12,8 +12,8 @@ import okhttp3.Response;
public class DeleteTask extends NetworkTask<Void> {
public DeleteTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskFinishedListener<Void> onParseTaskFinishedListener) {
super(onParseTaskStartedListener, onParseTaskFinishedListener);
public DeleteTask(OnTaskStartedListener onTaskStartedListener, OnNetworkTaskFinishedListener<Void> onParseTaskFinishedListener) {
super(onTaskStartedListener, onParseTaskFinishedListener);
}
@Override
@ -28,7 +28,7 @@ public class DeleteTask extends NetworkTask<Void> {
}
@Override
protected Void performTask(Document document) {
protected Void performTask(Document document, Response response) {
return null;
}

52
app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/SubmitVoteTask.java

@ -0,0 +1,52 @@
package gr.thmmy.mthmmy.activities.topic.tasks;
import org.jsoup.nodes.Document;
import java.io.IOException;
import java.util.Arrays;
import gr.thmmy.mthmmy.utils.NetworkResultCodes;
import gr.thmmy.mthmmy.utils.NetworkTask;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import timber.log.Timber;
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));
}
Timber.d("response" + Arrays.toString(votes));
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) {
Timber.d("response" + response);
return NetworkResultCodes.SUCCESSFUL;
}
}

62
app/src/main/java/gr/thmmy/mthmmy/utils/ExternalAsyncTask.java

@ -4,77 +4,77 @@ import android.os.AsyncTask;
public abstract class ExternalAsyncTask<U, V> extends AsyncTask<U, Void, V> {
protected OnParseTaskStartedListener onParseTaskStartedListener;
protected OnParseTaskCancelledListener onParseTaskCancelledListener;
protected OnParseTaskFinishedListener<V> onParseTaskFinishedListener;
protected OnTaskStartedListener onTaskStartedListener;
protected OnTaskCancelledListener onTaskCancelledListener;
protected OnTaskFinishedListener<V> onTaskFinishedListener;
@Override
protected void onPreExecute() {
if (onParseTaskStartedListener != null)
onParseTaskStartedListener.onParseStart();
if (onTaskStartedListener != null)
onTaskStartedListener.onTaskStarted();
else
super.onPreExecute();
}
@Override
protected void onCancelled() {
if (onParseTaskCancelledListener != null)
onParseTaskCancelledListener.onParseCancel();
if (onTaskCancelledListener != null)
onTaskCancelledListener.onTaskCanceled();
else
super.onCancelled();
}
@Override
protected void onCancelled(V v) {
if (onParseTaskCancelledListener != null)
onParseTaskCancelledListener.onParseCancel();
if (onTaskCancelledListener != null)
onTaskCancelledListener.onTaskCanceled();
else
super.onCancelled();
}
@Override
protected void onPostExecute(V v) {
if (onParseTaskFinishedListener != null)
onParseTaskFinishedListener.onParseFinish(v);
if (onTaskFinishedListener != null)
onTaskFinishedListener.onTaskFinished(v);
else
super.onPostExecute(v);
}
public ExternalAsyncTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskCancelledListener onParseTaskCancelledListener,
OnParseTaskFinishedListener<V> onParseTaskFinishedListener) {
this.onParseTaskStartedListener = onParseTaskStartedListener;
this.onParseTaskCancelledListener = onParseTaskCancelledListener;
this.onParseTaskFinishedListener = onParseTaskFinishedListener;
public ExternalAsyncTask(OnTaskStartedListener onTaskStartedListener, OnTaskCancelledListener onTaskCancelledListener,
OnTaskFinishedListener<V> onTaskFinishedListener) {
this.onTaskStartedListener = onTaskStartedListener;
this.onTaskCancelledListener = onTaskCancelledListener;
this.onTaskFinishedListener = onTaskFinishedListener;
}
public ExternalAsyncTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskFinishedListener<V> onParseTaskFinishedListener) {
this.onParseTaskStartedListener = onParseTaskStartedListener;
this.onParseTaskFinishedListener = onParseTaskFinishedListener;
public ExternalAsyncTask(OnTaskStartedListener onTaskStartedListener, OnTaskFinishedListener<V> onTaskFinishedListener) {
this.onTaskStartedListener = onTaskStartedListener;
this.onTaskFinishedListener = onTaskFinishedListener;
}
public ExternalAsyncTask() { }
public void setOnParseTaskStartedListener(OnParseTaskStartedListener onParseTaskStartedListener) {
this.onParseTaskStartedListener = onParseTaskStartedListener;
public void setOnTaskStartedListener(OnTaskStartedListener onTaskStartedListener) {
this.onTaskStartedListener = onTaskStartedListener;
}
public void setOnParseTaskCancelledListener(OnParseTaskCancelledListener onParseTaskCancelledListener) {
this.onParseTaskCancelledListener = onParseTaskCancelledListener;
public void setOnTaskCancelledListener(OnTaskCancelledListener onTaskCancelledListener) {
this.onTaskCancelledListener = onTaskCancelledListener;
}
public void setOnParseTaskFinishedListener(OnParseTaskFinishedListener<V> onParseTaskFinishedListener) {
this.onParseTaskFinishedListener = onParseTaskFinishedListener;
public void setOnTaskFinishedListener(OnTaskFinishedListener<V> onTaskFinishedListener) {
this.onTaskFinishedListener = onTaskFinishedListener;
}
public interface OnParseTaskStartedListener {
void onParseStart();
public interface OnTaskStartedListener {
void onTaskStarted();
}
public interface OnParseTaskCancelledListener {
void onParseCancel();
public interface OnTaskCancelledListener {
void onTaskCanceled();
}
public interface OnParseTaskFinishedListener<V> {
void onParseFinish(V result);
public interface OnTaskFinishedListener<V> {
void onTaskFinished(V result);
}
}

32
app/src/main/java/gr/thmmy/mthmmy/utils/NetworkTask.java

@ -14,17 +14,17 @@ import timber.log.Timber;
public abstract class NetworkTask<T> extends ExternalAsyncTask<String, Parcel<T>> {
protected OnParseTaskFinishedListener<T> onParseTaskFinishedListener;
protected OnNetworkTaskFinishedListener<T> onNetworkTaskFinishedListener;
public NetworkTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskCancelledListener onParseTaskCancelledListener,
OnParseTaskFinishedListener<T> onParseTaskFinishedListener) {
super(onParseTaskStartedListener, onParseTaskCancelledListener, null);
this.onParseTaskFinishedListener = onParseTaskFinishedListener;
public NetworkTask(OnTaskStartedListener onTaskStartedListener, OnTaskCancelledListener onTaskCancelledListener,
OnNetworkTaskFinishedListener<T> onNetworkTaskFinishedListener) {
super(onTaskStartedListener, onTaskCancelledListener, null);
this.onNetworkTaskFinishedListener = onNetworkTaskFinishedListener;
}
public NetworkTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskFinishedListener<T> onParseTaskFinishedListener) {
super(onParseTaskStartedListener, null);
this.onParseTaskFinishedListener = onParseTaskFinishedListener;
public NetworkTask(OnTaskStartedListener onTaskStartedListener, OnNetworkTaskFinishedListener<T> onNetworkTaskFinishedListener) {
super(onTaskStartedListener, null);
this.onNetworkTaskFinishedListener = onNetworkTaskFinishedListener;
}
public NetworkTask() {}
@ -49,7 +49,7 @@ public abstract class NetworkTask<T> extends ExternalAsyncTask<String, Parcel<T>
return new Parcel<>(NetworkResultCodes.NETWORK_ERROR, null);
}
try {
T data = performTask(Jsoup.parse(responseBodyString));
T data = performTask(Jsoup.parse(responseBodyString), response);
int resultCode = getResultCode(response, data);
return new Parcel<>(resultCode, data);
} catch (ParseException pe) {
@ -63,8 +63,8 @@ public abstract class NetworkTask<T> extends ExternalAsyncTask<String, Parcel<T>
@Override
protected void onPostExecute(Parcel<T> tParcel) {
if (onParseTaskFinishedListener != null)
onParseTaskFinishedListener.onParseFinish(tParcel.getResultCode(), tParcel.getData());
if (onNetworkTaskFinishedListener != null)
onNetworkTaskFinishedListener.onNetworkTaskFinished(tParcel.getResultCode(), tParcel.getData());
else
super.onPostExecute(tParcel);
}
@ -77,15 +77,15 @@ public abstract class NetworkTask<T> extends ExternalAsyncTask<String, Parcel<T>
return client.newCall(request).execute();
}
protected abstract T performTask(Document document);
protected abstract T performTask(Document document, Response response);
protected abstract int getResultCode(Response response, T data);
public void setOnParseTaskFinishedListener(OnParseTaskFinishedListener<T> onParseTaskFinishedListener) {
this.onParseTaskFinishedListener = onParseTaskFinishedListener;
public void setOnNetworkTaskFinishedListener(OnNetworkTaskFinishedListener<T> onNetworkTaskFinishedListener) {
this.onNetworkTaskFinishedListener = onNetworkTaskFinishedListener;
}
public interface OnParseTaskFinishedListener<T> {
void onParseFinish(int resultCode, T data);
public interface OnNetworkTaskFinishedListener<T> {
void onNetworkTaskFinished(int resultCode, T data);
}
}

17
app/src/main/java/gr/thmmy/mthmmy/utils/parsing/NewParseTask.java

@ -3,28 +3,29 @@ package gr.thmmy.mthmmy.utils.parsing;
import org.jsoup.nodes.Document;
import gr.thmmy.mthmmy.utils.NetworkTask;
import okhttp3.Response;
public abstract class NewParseTask<T> extends NetworkTask<T> {
public NewParseTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskCancelledListener onParseTaskCancelledListener,
OnParseTaskFinishedListener<T> onParseTaskFinishedListener) {
super(onParseTaskStartedListener, onParseTaskCancelledListener, onParseTaskFinishedListener);
public NewParseTask(OnTaskStartedListener onTaskStartedListener, OnTaskCancelledListener onTaskCancelledListener,
OnNetworkTaskFinishedListener<T> onParseTaskFinishedListener) {
super(onTaskStartedListener, onTaskCancelledListener, onParseTaskFinishedListener);
}
public NewParseTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskFinishedListener<T> onParseTaskFinishedListener) {
super(onParseTaskStartedListener, onParseTaskFinishedListener);
public NewParseTask(OnTaskStartedListener onTaskStartedListener, OnNetworkTaskFinishedListener<T> onParseTaskFinishedListener) {
super(onTaskStartedListener, onParseTaskFinishedListener);
}
public NewParseTask() {}
@Override
protected final T performTask(Document document) {
protected final T performTask(Document document, Response response) {
try {
return parse(document);
return parse(document, response);
} catch (Exception e) {
throw new ParseException("Parse failed.", e);
}
}
protected abstract T parse (Document document) throws ParseException;
protected abstract T parse (Document document, Response response) throws ParseException;
}

5
app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseHelpers.java

@ -181,9 +181,10 @@ public class ParseHelpers {
* @return the base URL of the given topic
*/
public static String getBaseURL(String topicURL) {
Matcher baseUrlMatcher = Pattern.compile(".+topic=[0-9]+").matcher(topicURL);
String forumUrl = "https://www.thmmy.gr/smf/index.php?";
Matcher baseUrlMatcher = Pattern.compile("topic=[0-9]+").matcher(topicURL);
if (baseUrlMatcher.find())
return topicURL.substring(baseUrlMatcher.start(), baseUrlMatcher.end());
return forumUrl + topicURL.substring(baseUrlMatcher.start(), baseUrlMatcher.end());
else return "";
}
}

63
app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java

@ -5,6 +5,9 @@ import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import java.util.ArrayList;
@ -16,12 +19,17 @@ import gr.thmmy.mthmmy.activities.topic.tasks.PrepareForEditTask;
import gr.thmmy.mthmmy.activities.topic.tasks.PrepareForReply;
import gr.thmmy.mthmmy.activities.topic.tasks.PrepareForReplyResult;
import gr.thmmy.mthmmy.activities.topic.tasks.ReplyTask;
import gr.thmmy.mthmmy.activities.topic.tasks.SubmitVoteTask;
import gr.thmmy.mthmmy.activities.topic.tasks.TopicTask;
import gr.thmmy.mthmmy.activities.topic.tasks.TopicTaskResult;
import gr.thmmy.mthmmy.base.BaseActivity;
import gr.thmmy.mthmmy.model.Poll;
import gr.thmmy.mthmmy.model.Post;
import gr.thmmy.mthmmy.model.TopicItem;
import gr.thmmy.mthmmy.session.SessionManager;
import gr.thmmy.mthmmy.utils.ExternalAsyncTask;
import gr.thmmy.mthmmy.utils.NetworkResultCodes;
import gr.thmmy.mthmmy.utils.NetworkTask;
import gr.thmmy.mthmmy.utils.parsing.ParseHelpers;
import timber.log.Timber;
@ -44,7 +52,6 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa
* holds the adapter position of the post being edited
*/
private int postBeingEditedPosition;
private ArrayList<Integer> selectedVoteIndices = new ArrayList<>();
private TopicTask currentTopicTask;
private PrepareForEditTask currentPrepareForEditTask;
@ -52,12 +59,14 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa
//callbacks for topic activity
private TopicTask.TopicTaskObserver topicTaskObserver;
private DeleteTask.OnParseTaskStartedListener deleteTaskStartedListener;
private DeleteTask.OnParseTaskFinishedListener<Void> deleteTaskFinishedListener;
private ExternalAsyncTask.OnTaskStartedListener deleteTaskStartedListener;
private NetworkTask.OnNetworkTaskFinishedListener<Void> deleteTaskFinishedListener;
private ReplyTask.ReplyTaskCallbacks replyFinishListener;
private PrepareForEditTask.PrepareForEditCallbacks prepareForEditCallbacks;
private EditTask.EditTaskCallbacks editTaskCallbacks;
private PrepareForReply.PrepareForReplyCallbacks prepareForReplyCallbacks;
private ExternalAsyncTask.OnTaskStartedListener voteTaskStartedListener;
private NetworkTask.OnNetworkTaskFinishedListener<Void> voteTaskFinishedListener;
/**
* Holds the value (index) of the page to be requested when a user interaction with bottom
@ -106,16 +115,25 @@ 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 submitVote(LinearLayout optionsLayout) {
if (topicItems.getValue() == null) throw new NullPointerException("Topic task has not finished yet!");
ArrayList<Integer> votes = new ArrayList<>();
if (optionsLayout.getChildAt(0) instanceof RadioGroup) {
RadioGroup optionsRadioGroup = (RadioGroup) optionsLayout.getChildAt(0);
votes.add(optionsRadioGroup.getCheckedRadioButtonId());
} else if (optionsLayout.getChildAt(0) instanceof CheckBox) {
for (int i = 0; i < optionsLayout.getChildCount(); i++) {
if (((CheckBox) optionsLayout.getChildAt(i)).isChecked())
votes.add(i);
}
}
int[] votesArray = new int[votes.size()];
for (int i = 0; i < votes.size(); i++) votesArray[i] = votes.get(i);
Poll poll = (Poll) topicItems.getValue().get(0);
SubmitVoteTask submitVoteTask = new SubmitVoteTask(votesArray);
submitVoteTask.setOnTaskStartedListener(voteTaskStartedListener);
submitVoteTask.setOnNetworkTaskFinishedListener(voteTaskFinishedListener);
submitVoteTask.execute(poll.getPollFormUrl(), poll.getSc());
}
public void prepareForReply() {
@ -259,17 +277,16 @@ 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 void onRadioButtonCLicked(int index) {
selectedVoteIndices.clear();
selectedVoteIndices.add(index);
public void setVoteTaskStartedListener(ExternalAsyncTask.OnTaskStartedListener voteTaskStartedListener) {
this.voteTaskStartedListener = voteTaskStartedListener;
}
// <-------------Just getters, setters and helper methods below here---------------->
public void setVoteTaskFinishedListener(NetworkTask.OnNetworkTaskFinishedListener<Void> voteTaskFinishedListener) {
this.voteTaskFinishedListener = voteTaskFinishedListener;
}
public MutableLiveData<String> getTopicViewers() {
return topicViewers;
@ -339,11 +356,11 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa
}
public void setDeleteTaskStartedListener(DeleteTask.OnParseTaskStartedListener deleteTaskStartedListener) {
public void setDeleteTaskStartedListener(ExternalAsyncTask.OnTaskStartedListener deleteTaskStartedListener) {
this.deleteTaskStartedListener = deleteTaskStartedListener;
}
public void setDeleteTaskFinishedListener(DeleteTask.OnParseTaskFinishedListener<Void> deleteTaskFinishedListener) {
public void setDeleteTaskFinishedListener(NetworkTask.OnNetworkTaskFinishedListener<Void> deleteTaskFinishedListener) {
this.deleteTaskFinishedListener = deleteTaskFinishedListener;
}

Loading…
Cancel
Save