diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/forum/ForumFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/forum/ForumFragment.java index ceb41e13..a85eca23 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/forum/ForumFragment.java +++ b/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> { private HttpUrl forumUrl = SessionManager.forumUrl; //may change upon collapse/expand - public ForumTask(OnParseTaskStartedListener onParseTaskStartedListener, - OnParseTaskFinishedListener> onParseTaskFinishedListener) { - super(onParseTaskStartedListener, onParseTaskFinishedListener); + public ForumTask(OnTaskStartedListener onTaskStartedListener, + OnNetworkTaskFinishedListener> onParseTaskFinishedListener) { + super(onTaskStartedListener, onParseTaskFinishedListener); } @Override - protected ArrayList parse(Document document) throws ParseException { + protected ArrayList parse(Document document, Response response) throws ParseException { Elements categoryBlocks = document.select(".tborder:not([style])>table[cellpadding=5]"); if (categoryBlocks.size() != 0) { ArrayList fetchedCategories = new ArrayList<>(); diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java index 98ad7f69..65b0d315 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java +++ b/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> { - public RecentTask(OnParseTaskStartedListener onParseTaskStartedListener, - OnParseTaskFinishedListener> onParseTaskFinishedListener) { - super(onParseTaskStartedListener, onParseTaskFinishedListener); + public RecentTask(OnTaskStartedListener onTaskStartedListener, + OnNetworkTaskFinishedListener> onParseTaskFinishedListener) { + super(onTaskStartedListener, onParseTaskFinishedListener); } @Override - protected ArrayList parse(Document document) throws ParseException { + protected ArrayList parse(Document document, Response response) throws ParseException { ArrayList fetchedRecent = new ArrayList<>(); Elements recent = document.select("#block8 :first-child div"); if (!recent.isEmpty()) { diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java index 0681c4af..0747b4bf 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java +++ b/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 { - UnreadTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskFinishedListener onParseTaskFinishedListener) { - super(onParseTaskStartedListener, onParseTaskFinishedListener); + UnreadTask(OnTaskStartedListener onTaskStartedListener, OnNetworkTaskFinishedListener 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(); diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java index b6201e9c..4a997e49 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java +++ b/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; 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 d0682f1e..b7e1ccef 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 @@ -165,6 +165,7 @@ class TopicAdapter extends RecyclerView.Adapter { 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 { 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 { holder.optionsLayout.setVisibility(View.VISIBLE); } else { //Showing results - holder.optionsLayout.removeAllViews(); holder.optionsLayout.setVisibility(View.GONE); List valuesToCompare = new ArrayList<>(); for (int i = 0; i < entries.length; i++) { @@ -221,8 +222,13 @@ class TopicAdapter extends RecyclerView.Adapter { 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 { 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) { diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/DeleteTask.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/DeleteTask.java index 41563ae5..b277d2bd 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/DeleteTask.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/DeleteTask.java @@ -12,8 +12,8 @@ import okhttp3.Response; public class DeleteTask extends NetworkTask { - public DeleteTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskFinishedListener onParseTaskFinishedListener) { - super(onParseTaskStartedListener, onParseTaskFinishedListener); + public DeleteTask(OnTaskStartedListener onTaskStartedListener, OnNetworkTaskFinishedListener onParseTaskFinishedListener) { + super(onTaskStartedListener, onParseTaskFinishedListener); } @Override @@ -28,7 +28,7 @@ public class DeleteTask extends NetworkTask { } @Override - protected Void performTask(Document document) { + protected Void performTask(Document document, Response response) { return null; } diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/SubmitVoteTask.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/SubmitVoteTask.java new file mode 100644 index 00000000..886264d6 --- /dev/null +++ b/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 { + + 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; + } +} diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/ExternalAsyncTask.java b/app/src/main/java/gr/thmmy/mthmmy/utils/ExternalAsyncTask.java index 82b535b6..d20d29dc 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/ExternalAsyncTask.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/ExternalAsyncTask.java @@ -4,77 +4,77 @@ import android.os.AsyncTask; public abstract class ExternalAsyncTask extends AsyncTask { - protected OnParseTaskStartedListener onParseTaskStartedListener; - protected OnParseTaskCancelledListener onParseTaskCancelledListener; - protected OnParseTaskFinishedListener onParseTaskFinishedListener; + protected OnTaskStartedListener onTaskStartedListener; + protected OnTaskCancelledListener onTaskCancelledListener; + protected OnTaskFinishedListener 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 onParseTaskFinishedListener) { - this.onParseTaskStartedListener = onParseTaskStartedListener; - this.onParseTaskCancelledListener = onParseTaskCancelledListener; - this.onParseTaskFinishedListener = onParseTaskFinishedListener; + public ExternalAsyncTask(OnTaskStartedListener onTaskStartedListener, OnTaskCancelledListener onTaskCancelledListener, + OnTaskFinishedListener onTaskFinishedListener) { + this.onTaskStartedListener = onTaskStartedListener; + this.onTaskCancelledListener = onTaskCancelledListener; + this.onTaskFinishedListener = onTaskFinishedListener; } - public ExternalAsyncTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskFinishedListener onParseTaskFinishedListener) { - this.onParseTaskStartedListener = onParseTaskStartedListener; - this.onParseTaskFinishedListener = onParseTaskFinishedListener; + public ExternalAsyncTask(OnTaskStartedListener onTaskStartedListener, OnTaskFinishedListener 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 onParseTaskFinishedListener) { - this.onParseTaskFinishedListener = onParseTaskFinishedListener; + public void setOnTaskFinishedListener(OnTaskFinishedListener 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 { - void onParseFinish(V result); + public interface OnTaskFinishedListener { + void onTaskFinished(V result); } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/NetworkTask.java b/app/src/main/java/gr/thmmy/mthmmy/utils/NetworkTask.java index 6793120a..cb613f26 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/NetworkTask.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/NetworkTask.java @@ -14,17 +14,17 @@ import timber.log.Timber; public abstract class NetworkTask extends ExternalAsyncTask> { - protected OnParseTaskFinishedListener onParseTaskFinishedListener; + protected OnNetworkTaskFinishedListener onNetworkTaskFinishedListener; - public NetworkTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskCancelledListener onParseTaskCancelledListener, - OnParseTaskFinishedListener onParseTaskFinishedListener) { - super(onParseTaskStartedListener, onParseTaskCancelledListener, null); - this.onParseTaskFinishedListener = onParseTaskFinishedListener; + public NetworkTask(OnTaskStartedListener onTaskStartedListener, OnTaskCancelledListener onTaskCancelledListener, + OnNetworkTaskFinishedListener onNetworkTaskFinishedListener) { + super(onTaskStartedListener, onTaskCancelledListener, null); + this.onNetworkTaskFinishedListener = onNetworkTaskFinishedListener; } - public NetworkTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskFinishedListener onParseTaskFinishedListener) { - super(onParseTaskStartedListener, null); - this.onParseTaskFinishedListener = onParseTaskFinishedListener; + public NetworkTask(OnTaskStartedListener onTaskStartedListener, OnNetworkTaskFinishedListener onNetworkTaskFinishedListener) { + super(onTaskStartedListener, null); + this.onNetworkTaskFinishedListener = onNetworkTaskFinishedListener; } public NetworkTask() {} @@ -49,7 +49,7 @@ public abstract class NetworkTask extends ExternalAsyncTask 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 extends ExternalAsyncTask @Override protected void onPostExecute(Parcel 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 extends ExternalAsyncTask 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 onParseTaskFinishedListener) { - this.onParseTaskFinishedListener = onParseTaskFinishedListener; + public void setOnNetworkTaskFinishedListener(OnNetworkTaskFinishedListener onNetworkTaskFinishedListener) { + this.onNetworkTaskFinishedListener = onNetworkTaskFinishedListener; } - public interface OnParseTaskFinishedListener { - void onParseFinish(int resultCode, T data); + public interface OnNetworkTaskFinishedListener { + void onNetworkTaskFinished(int resultCode, T data); } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/NewParseTask.java b/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/NewParseTask.java index 51749f2a..def14c2f 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/NewParseTask.java +++ b/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 extends NetworkTask { - public NewParseTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskCancelledListener onParseTaskCancelledListener, - OnParseTaskFinishedListener onParseTaskFinishedListener) { - super(onParseTaskStartedListener, onParseTaskCancelledListener, onParseTaskFinishedListener); + public NewParseTask(OnTaskStartedListener onTaskStartedListener, OnTaskCancelledListener onTaskCancelledListener, + OnNetworkTaskFinishedListener onParseTaskFinishedListener) { + super(onTaskStartedListener, onTaskCancelledListener, onParseTaskFinishedListener); } - public NewParseTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskFinishedListener onParseTaskFinishedListener) { - super(onParseTaskStartedListener, onParseTaskFinishedListener); + public NewParseTask(OnTaskStartedListener onTaskStartedListener, OnNetworkTaskFinishedListener 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; } diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseHelpers.java b/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseHelpers.java index 804c9e85..52669582 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseHelpers.java +++ b/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 ""; } } 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 5bc1f894..540d4917 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,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 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 deleteTaskFinishedListener; + private ExternalAsyncTask.OnTaskStartedListener deleteTaskStartedListener; + private NetworkTask.OnNetworkTaskFinishedListener deleteTaskFinishedListener; private ReplyTask.ReplyTaskCallbacks replyFinishListener; private PrepareForEditTask.PrepareForEditCallbacks prepareForEditCallbacks; private EditTask.EditTaskCallbacks editTaskCallbacks; private PrepareForReply.PrepareForReplyCallbacks prepareForReplyCallbacks; + private ExternalAsyncTask.OnTaskStartedListener voteTaskStartedListener; + private NetworkTask.OnNetworkTaskFinishedListener 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 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 voteTaskFinishedListener) { + this.voteTaskFinishedListener = voteTaskFinishedListener; + } public MutableLiveData 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 deleteTaskFinishedListener) { + public void setDeleteTaskFinishedListener(NetworkTask.OnNetworkTaskFinishedListener deleteTaskFinishedListener) { this.deleteTaskFinishedListener = deleteTaskFinishedListener; }