Browse Source

polish poll tasks, set chart start at 0

pull/55/head
Thodoris1999 6 years ago
parent
commit
687e4a42f0
  1. 30
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java
  2. 6
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java
  3. 20
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/RemoveVoteTask.java
  4. 4
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/SubmitVoteTask.java
  5. 38
      app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java

30
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.ThmmyPage;
import gr.thmmy.mthmmy.model.TopicItem; import gr.thmmy.mthmmy.model.TopicItem;
import gr.thmmy.mthmmy.utils.CustomLinearLayoutManager; import gr.thmmy.mthmmy.utils.CustomLinearLayoutManager;
import gr.thmmy.mthmmy.utils.ExternalAsyncTask;
import gr.thmmy.mthmmy.utils.HTMLUtils; import gr.thmmy.mthmmy.utils.HTMLUtils;
import gr.thmmy.mthmmy.utils.NetworkResultCodes; import gr.thmmy.mthmmy.utils.NetworkResultCodes;
import gr.thmmy.mthmmy.utils.NetworkTask; 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) { } else if (rect != null && event.getAction() == MotionEvent.ACTION_UP && autoIncrement) {
autoIncrement = false; autoIncrement = false;
paginationEnabled(true); paginationEnabled(true);
viewModel.performPageChange(); viewModel.loadPageIndicated();
} else if (rect != null && event.getAction() == MotionEvent.ACTION_MOVE) { } else if (rect != null && event.getAction() == MotionEvent.ACTION_MOVE) {
if (!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())) { if (!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())) {
autoIncrement = false; autoIncrement = false;
@ -467,7 +466,7 @@ public class TopicActivity extends BaseActivity implements TopicAdapter.OnPostFo
} else if (event.getAction() == MotionEvent.ACTION_UP && autoDecrement) { } else if (event.getAction() == MotionEvent.ACTION_UP && autoDecrement) {
autoDecrement = false; autoDecrement = false;
paginationEnabled(true); paginationEnabled(true);
viewModel.performPageChange(); viewModel.loadPageIndicated();
} else if (event.getAction() == MotionEvent.ACTION_MOVE) { } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
if (rect != null && if (rect != null &&
!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())) { !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.setVoteTaskStartedListener(() -> progressBar.setVisibility(ProgressBar.VISIBLE));
viewModel.setVoteTaskFinishedListener((resultCode, data) -> { 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); 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 // observe the chages in data
viewModel.getPageIndicatorIndex().observe(this, pageIndicatorIndex -> { viewModel.getPageIndicatorIndex().observe(this, pageIndicatorIndex -> {

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

@ -200,6 +200,7 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
YAxis yAxisLeft = holder.voteChart.getAxisLeft(); YAxis yAxisLeft = holder.voteChart.getAxisLeft();
yAxisLeft.setGranularity(1f); yAxisLeft.setGranularity(1f);
yAxisLeft.setTextColor(context.getResources().getColor(R.color.primary_text)); yAxisLeft.setTextColor(context.getResources().getColor(R.color.primary_text));
yAxisLeft.setAxisMinimum(0);
YAxis yAxisRight = holder.voteChart.getAxisRight(); YAxis yAxisRight = holder.voteChart.getAxisRight();
yAxisRight.setEnabled(false); yAxisRight.setEnabled(false);
@ -223,10 +224,7 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
holder.voteChart.setVisibility(View.VISIBLE); holder.voteChart.setVisibility(View.VISIBLE);
} }
if (poll.getRemoveVoteUrl() != null) { if (poll.getRemoveVoteUrl() != null) {
holder.removeVotesButton.setOnClickListener(v -> { holder.removeVotesButton.setOnClickListener(v -> viewModel.removeVote());
viewModel.loadUrl(poll.getRemoveVoteUrl());
viewModel.loadUrl(ParseHelpers.getBaseURL(viewModel.getTopicUrl()) + ".0");
});
holder.removeVotesButton.setVisibility(View.VISIBLE); holder.removeVotesButton.setVisibility(View.VISIBLE);
} else holder.removeVotesButton.setVisibility(View.GONE); } else holder.removeVotesButton.setVisibility(View.GONE);
if (poll.getShowVoteResultsUrl() != null) { if (poll.getShowVoteResultsUrl() != null) {

20
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<Void> {
@Override
protected Void performTask(Document document, Response response) {
return null;
}
@Override
protected int getResultCode(Response response, Void data) {
return NetworkResultCodes.SUCCESSFUL;
}
}

4
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 org.jsoup.nodes.Document;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays;
import gr.thmmy.mthmmy.utils.NetworkResultCodes; import gr.thmmy.mthmmy.utils.NetworkResultCodes;
import gr.thmmy.mthmmy.utils.NetworkTask; import gr.thmmy.mthmmy.utils.NetworkTask;
@ -11,7 +10,6 @@ import okhttp3.MultipartBody;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
import okhttp3.Request; import okhttp3.Request;
import okhttp3.Response; import okhttp3.Response;
import timber.log.Timber;
public class SubmitVoteTask extends NetworkTask<Void> { public class SubmitVoteTask extends NetworkTask<Void> {
@ -29,7 +27,6 @@ public class SubmitVoteTask extends NetworkTask<Void> {
for (int vote : votes) { for (int vote : votes) {
postBodyBuilder.addFormDataPart("options[]", Integer.toString(vote)); postBodyBuilder.addFormDataPart("options[]", Integer.toString(vote));
} }
Timber.d("response" + Arrays.toString(votes));
Request voteRequest = new Request.Builder() Request voteRequest = new Request.Builder()
.url(input[0]) .url(input[0])
@ -46,7 +43,6 @@ public class SubmitVoteTask extends NetworkTask<Void> {
@Override @Override
protected int getResultCode(Response response, Void data) { protected int getResultCode(Response response, Void data) {
Timber.d("response" + response);
return NetworkResultCodes.SUCCESSFUL; return NetworkResultCodes.SUCCESSFUL;
} }
} }

38
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.PrepareForEditTask;
import gr.thmmy.mthmmy.activities.topic.tasks.PrepareForReply; import gr.thmmy.mthmmy.activities.topic.tasks.PrepareForReply;
import gr.thmmy.mthmmy.activities.topic.tasks.PrepareForReplyResult; 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.ReplyTask;
import gr.thmmy.mthmmy.activities.topic.tasks.SubmitVoteTask; import gr.thmmy.mthmmy.activities.topic.tasks.SubmitVoteTask;
import gr.thmmy.mthmmy.activities.topic.tasks.TopicTask; 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.model.TopicItem;
import gr.thmmy.mthmmy.session.SessionManager; import gr.thmmy.mthmmy.session.SessionManager;
import gr.thmmy.mthmmy.utils.ExternalAsyncTask; import gr.thmmy.mthmmy.utils.ExternalAsyncTask;
import gr.thmmy.mthmmy.utils.NetworkResultCodes;
import gr.thmmy.mthmmy.utils.NetworkTask; import gr.thmmy.mthmmy.utils.NetworkTask;
import gr.thmmy.mthmmy.utils.parsing.ParseHelpers; import gr.thmmy.mthmmy.utils.parsing.ParseHelpers;
import timber.log.Timber; import timber.log.Timber;
@ -67,6 +67,8 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa
private PrepareForReply.PrepareForReplyCallbacks prepareForReplyCallbacks; private PrepareForReply.PrepareForReplyCallbacks prepareForReplyCallbacks;
private ExternalAsyncTask.OnTaskStartedListener voteTaskStartedListener; private ExternalAsyncTask.OnTaskStartedListener voteTaskStartedListener;
private NetworkTask.OnNetworkTaskFinishedListener<Void> voteTaskFinishedListener; private NetworkTask.OnNetworkTaskFinishedListener<Void> voteTaskFinishedListener;
private ExternalAsyncTask.OnTaskStartedListener removeVoteTaskStartedListener;
private NetworkTask.OnNetworkTaskFinishedListener<Void> removeVoteTaskFinishedListener;
/** /**
* Holds the value (index) of the page to be requested when a user interaction with bottom * 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); 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) if (pageIndicatorIndex.getValue() == null)
throw new NullPointerException("No page has been loaded yet!"); throw new NullPointerException("No page has been loaded yet!");
int pageRequested = pageIndicatorIndex.getValue() - 1; int pageRequested = pageIndicatorIndex.getValue() - 1;
@ -136,6 +148,14 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa
submitVoteTask.execute(poll.getPollFormUrl(), poll.getSc()); 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() { public void prepareForReply() {
if (replyPageUrl.getValue() == null) if (replyPageUrl.getValue() == null)
throw new NullPointerException("Topic task has not finished yet!"); 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); pageIndicatorIndex.setValue(pageIndicatorIndex.getValue() + step);
} else } else
pageIndicatorIndex.setValue(pageCount); pageIndicatorIndex.setValue(pageCount);
if (changePage && oldIndicatorIndex != pageIndicatorIndex.getValue()) performPageChange(); if (changePage && oldIndicatorIndex != pageIndicatorIndex.getValue()) loadPageIndicated();
} }
public void decrementPageRequestValue(int step, boolean changePage) { public void decrementPageRequestValue(int step, boolean changePage) {
@ -266,7 +286,7 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa
pageIndicatorIndex.setValue(pageIndicatorIndex.getValue() - step); pageIndicatorIndex.setValue(pageIndicatorIndex.getValue() - step);
} else } else
pageIndicatorIndex.setValue(1); pageIndicatorIndex.setValue(1);
if (changePage && oldIndicatorIndex != pageIndicatorIndex.getValue()) performPageChange(); if (changePage && oldIndicatorIndex != pageIndicatorIndex.getValue()) loadPageIndicated();
} }
public void setPageIndicatorIndex(int pageIndicatorIndex, boolean changePage) { 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!"); throw new NullPointerException("No page has been loaded yet!");
int oldIndicatorIndex = this.pageIndicatorIndex.getValue(); int oldIndicatorIndex = this.pageIndicatorIndex.getValue();
this.pageIndicatorIndex.setValue(pageIndicatorIndex); 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----------------> // <-------------Just getters, setters and helper methods below here---------------->
public void setRemoveVoteTaskStartedListener(ExternalAsyncTask.OnTaskStartedListener removeVoteTaskStartedListener) {
this.removeVoteTaskStartedListener = removeVoteTaskStartedListener;
}
public void setRemoveVoteTaskFinishedListener(NetworkTask.OnNetworkTaskFinishedListener<Void> removeVoteTaskFinishedListener) {
this.removeVoteTaskFinishedListener = removeVoteTaskFinishedListener;
}
public void setVoteTaskStartedListener(ExternalAsyncTask.OnTaskStartedListener voteTaskStartedListener) { public void setVoteTaskStartedListener(ExternalAsyncTask.OnTaskStartedListener voteTaskStartedListener) {
this.voteTaskStartedListener = voteTaskStartedListener; this.voteTaskStartedListener = voteTaskStartedListener;
} }

Loading…
Cancel
Save