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 4a997e49..c21a5b05 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,7 +50,6 @@ 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; @@ -423,7 +422,7 @@ public class TopicActivity extends BaseActivity implements TopicAdapter.OnPostFo } else if (rect != null && event.getAction() == MotionEvent.ACTION_UP && autoIncrement) { autoIncrement = false; paginationEnabled(true); - viewModel.performPageChange(); + viewModel.loadPageIndicated(); } else if (rect != null && event.getAction() == MotionEvent.ACTION_MOVE) { if (!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())) { autoIncrement = false; @@ -467,7 +466,7 @@ public class TopicActivity extends BaseActivity implements TopicAdapter.OnPostFo } else if (event.getAction() == MotionEvent.ACTION_UP && autoDecrement) { autoDecrement = false; paginationEnabled(true); - viewModel.performPageChange(); + viewModel.loadPageIndicated(); } else if (event.getAction() == MotionEvent.ACTION_MOVE) { if (rect != null && !rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())) { @@ -602,12 +601,27 @@ public class TopicActivity extends BaseActivity implements TopicAdapter.OnPostFo }); 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"); + if (resultCode == NetworkResultCodes.SUCCESSFUL) { + Timber.i("Vote sent"); + viewModel.resetPage(); + } + else { + Timber.w("Failed to send vote"); + Toast.makeText(this, "Failed to send vote", Toast.LENGTH_LONG).show(); + } + }); + viewModel.setRemoveVoteTaskStartedListener(() -> progressBar.setVisibility(ProgressBar.VISIBLE)); + viewModel.setRemoveVoteTaskFinishedListener((resultCode, data) -> { + progressBar.setVisibility(View.GONE); + if (resultCode == NetworkResultCodes.SUCCESSFUL) { + Timber.i("Vote removed"); + viewModel.resetPage(); + } + else { + Timber.w("Failed to remove vote"); + Toast.makeText(this, "Failed to remove vote", Toast.LENGTH_LONG).show(); + } }); // observe the chages in data viewModel.getPageIndicatorIndex().observe(this, pageIndicatorIndex -> { 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 b7e1ccef..01759f34 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 @@ -200,6 +200,7 @@ class TopicAdapter extends RecyclerView.Adapter { YAxis yAxisLeft = holder.voteChart.getAxisLeft(); yAxisLeft.setGranularity(1f); yAxisLeft.setTextColor(context.getResources().getColor(R.color.primary_text)); + yAxisLeft.setAxisMinimum(0); YAxis yAxisRight = holder.voteChart.getAxisRight(); yAxisRight.setEnabled(false); @@ -223,10 +224,7 @@ class TopicAdapter extends RecyclerView.Adapter { holder.voteChart.setVisibility(View.VISIBLE); } if (poll.getRemoveVoteUrl() != null) { - holder.removeVotesButton.setOnClickListener(v -> { - viewModel.loadUrl(poll.getRemoveVoteUrl()); - viewModel.loadUrl(ParseHelpers.getBaseURL(viewModel.getTopicUrl()) + ".0"); - }); + holder.removeVotesButton.setOnClickListener(v -> viewModel.removeVote()); holder.removeVotesButton.setVisibility(View.VISIBLE); } else holder.removeVotesButton.setVisibility(View.GONE); if (poll.getShowVoteResultsUrl() != null) { diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/RemoveVoteTask.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/RemoveVoteTask.java new file mode 100644 index 00000000..f94ae027 --- /dev/null +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/RemoveVoteTask.java @@ -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 { + + @Override + protected Void performTask(Document document, Response response) { + return null; + } + + @Override + protected int getResultCode(Response response, Void data) { + return NetworkResultCodes.SUCCESSFUL; + } +} 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 index 886264d6..ec3773f7 100644 --- 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 @@ -3,7 +3,6 @@ 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; @@ -11,7 +10,6 @@ import okhttp3.MultipartBody; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; -import timber.log.Timber; public class SubmitVoteTask extends NetworkTask { @@ -29,7 +27,6 @@ public class SubmitVoteTask extends NetworkTask { for (int vote : votes) { postBodyBuilder.addFormDataPart("options[]", Integer.toString(vote)); } - Timber.d("response" + Arrays.toString(votes)); Request voteRequest = new Request.Builder() .url(input[0]) @@ -46,7 +43,6 @@ public class SubmitVoteTask extends NetworkTask { @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/viewmodel/TopicViewModel.java b/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java index 540d4917..1175980a 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java +++ b/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java @@ -18,6 +18,7 @@ import gr.thmmy.mthmmy.activities.topic.tasks.PrepareForEditResult; 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.RemoveVoteTask; import gr.thmmy.mthmmy.activities.topic.tasks.ReplyTask; import gr.thmmy.mthmmy.activities.topic.tasks.SubmitVoteTask; import gr.thmmy.mthmmy.activities.topic.tasks.TopicTask; @@ -28,7 +29,6 @@ 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; @@ -67,6 +67,8 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa private PrepareForReply.PrepareForReplyCallbacks prepareForReplyCallbacks; private ExternalAsyncTask.OnTaskStartedListener voteTaskStartedListener; private NetworkTask.OnNetworkTaskFinishedListener voteTaskFinishedListener; + private ExternalAsyncTask.OnTaskStartedListener removeVoteTaskStartedListener; + private NetworkTask.OnNetworkTaskFinishedListener removeVoteTaskFinishedListener; /** * Holds the value (index) of the page to be requested when a user interaction with bottom @@ -102,7 +104,17 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa loadUrl(topicUrl); } - public void performPageChange() { + /** + * In contrasto to {@link TopicViewModel#reloadPage()} this method gets rid of any arguements + * in the url before refreshing + */ + public void resetPage() { + if (topicUrl == null) throw new NullPointerException("No topic task has been requested yet!"); + Timber.i("Reseting page"); + loadUrl(ParseHelpers.getBaseURL(topicUrl) + "." + String.valueOf(currentPageIndex * 15)); + } + + public void loadPageIndicated() { if (pageIndicatorIndex.getValue() == null) throw new NullPointerException("No page has been loaded yet!"); int pageRequested = pageIndicatorIndex.getValue() - 1; @@ -136,6 +148,14 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa submitVoteTask.execute(poll.getPollFormUrl(), poll.getSc()); } + public void removeVote() { + if (topicItems.getValue() == null) throw new NullPointerException("Topic task has not finished yet!"); + RemoveVoteTask removeVoteTask = new RemoveVoteTask(); + removeVoteTask.setOnTaskStartedListener(removeVoteTaskStartedListener); + removeVoteTask.setOnNetworkTaskFinishedListener(removeVoteTaskFinishedListener); + removeVoteTask.execute(((Poll) topicItems.getValue().get(0)).getRemoveVoteUrl()); + } + public void prepareForReply() { if (replyPageUrl.getValue() == null) throw new NullPointerException("Topic task has not finished yet!"); @@ -255,7 +275,7 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa pageIndicatorIndex.setValue(pageIndicatorIndex.getValue() + step); } else pageIndicatorIndex.setValue(pageCount); - if (changePage && oldIndicatorIndex != pageIndicatorIndex.getValue()) performPageChange(); + if (changePage && oldIndicatorIndex != pageIndicatorIndex.getValue()) loadPageIndicated(); } public void decrementPageRequestValue(int step, boolean changePage) { @@ -266,7 +286,7 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa pageIndicatorIndex.setValue(pageIndicatorIndex.getValue() - step); } else pageIndicatorIndex.setValue(1); - if (changePage && oldIndicatorIndex != pageIndicatorIndex.getValue()) performPageChange(); + if (changePage && oldIndicatorIndex != pageIndicatorIndex.getValue()) loadPageIndicated(); } public void setPageIndicatorIndex(int pageIndicatorIndex, boolean changePage) { @@ -274,12 +294,20 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa throw new NullPointerException("No page has been loaded yet!"); int oldIndicatorIndex = this.pageIndicatorIndex.getValue(); this.pageIndicatorIndex.setValue(pageIndicatorIndex); - if (changePage && oldIndicatorIndex != this.pageIndicatorIndex.getValue()) performPageChange(); + if (changePage && oldIndicatorIndex != this.pageIndicatorIndex.getValue()) loadPageIndicated(); } // <-------------Just getters, setters and helper methods below here----------------> + public void setRemoveVoteTaskStartedListener(ExternalAsyncTask.OnTaskStartedListener removeVoteTaskStartedListener) { + this.removeVoteTaskStartedListener = removeVoteTaskStartedListener; + } + + public void setRemoveVoteTaskFinishedListener(NetworkTask.OnNetworkTaskFinishedListener removeVoteTaskFinishedListener) { + this.removeVoteTaskFinishedListener = removeVoteTaskFinishedListener; + } + public void setVoteTaskStartedListener(ExternalAsyncTask.OnTaskStartedListener voteTaskStartedListener) { this.voteTaskStartedListener = voteTaskStartedListener; }