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. 61
      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 class ForumTask extends NewParseTask<ArrayList<Category>> {
private HttpUrl forumUrl = SessionManager.forumUrl; //may change upon collapse/expand private HttpUrl forumUrl = SessionManager.forumUrl; //may change upon collapse/expand
public ForumTask(OnParseTaskStartedListener onParseTaskStartedListener, public ForumTask(OnTaskStartedListener onTaskStartedListener,
OnParseTaskFinishedListener<ArrayList<Category>> onParseTaskFinishedListener) { OnNetworkTaskFinishedListener<ArrayList<Category>> onParseTaskFinishedListener) {
super(onParseTaskStartedListener, onParseTaskFinishedListener); super(onTaskStartedListener, onParseTaskFinishedListener);
} }
@Override @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]"); Elements categoryBlocks = document.select(".tborder:not([style])>table[cellpadding=5]");
if (categoryBlocks.size() != 0) { if (categoryBlocks.size() != 0) {
ArrayList<Category> fetchedCategories = new ArrayList<>(); 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----------------------------------- //---------------------------------------ASYNC TASK-----------------------------------
private class RecentTask extends NewParseTask<ArrayList<TopicSummary>> { private class RecentTask extends NewParseTask<ArrayList<TopicSummary>> {
public RecentTask(OnParseTaskStartedListener onParseTaskStartedListener, public RecentTask(OnTaskStartedListener onTaskStartedListener,
OnParseTaskFinishedListener<ArrayList<TopicSummary>> onParseTaskFinishedListener) { OnNetworkTaskFinishedListener<ArrayList<TopicSummary>> onParseTaskFinishedListener) {
super(onParseTaskStartedListener, onParseTaskFinishedListener); super(onTaskStartedListener, onParseTaskFinishedListener);
} }
@Override @Override
protected ArrayList<TopicSummary> parse(Document document) throws ParseException { protected ArrayList<TopicSummary> parse(Document document, Response response) throws ParseException {
ArrayList<TopicSummary> fetchedRecent = new ArrayList<>(); ArrayList<TopicSummary> fetchedRecent = new ArrayList<>();
Elements recent = document.select("#block8 :first-child div"); Elements recent = document.select("#block8 :first-child div");
if (!recent.isEmpty()) { 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> { private class UnreadTask extends NewParseTask<Void> {
UnreadTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskFinishedListener<Void> onParseTaskFinishedListener) { UnreadTask(OnTaskStartedListener onTaskStartedListener, OnNetworkTaskFinishedListener<Void> onParseTaskFinishedListener) {
super(onParseTaskStartedListener, onParseTaskFinishedListener); super(onTaskStartedListener, onParseTaskFinishedListener);
} }
@Override @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)"); Elements unread = document.select("table.bordercolor[cellspacing=1] tr:not(.titlebg)");
if (!unread.isEmpty()) { if (!unread.isEmpty()) {
//topicSummaries.clear(); //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.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.parsing.ParseHelpers; import gr.thmmy.mthmmy.utils.parsing.ParseHelpers;
import gr.thmmy.mthmmy.viewmodel.TopicViewModel; import gr.thmmy.mthmmy.viewmodel.TopicViewModel;
import me.zhanghai.android.materialprogressbar.MaterialProgressBar; import me.zhanghai.android.materialprogressbar.MaterialProgressBar;
@ -598,6 +600,15 @@ public class TopicActivity extends BaseActivity implements TopicAdapter.OnPostFo
progressBar.setVisibility(ProgressBar.GONE); 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 // observe the chages in data
viewModel.getPageIndicatorIndex().observe(this, pageIndicatorIndex -> { viewModel.getPageIndicatorIndex().observe(this, pageIndicatorIndex -> {
if (pageIndicatorIndex == null) return; 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(); Poll.Entry[] entries = poll.getEntries();
PollViewHolder holder = (PollViewHolder) currentHolder; PollViewHolder holder = (PollViewHolder) currentHolder;
holder.question.setText(poll.getQuestion()); holder.question.setText(poll.getQuestion());
holder.optionsLayout.removeAllViews();
if (poll.getAvailableVoteCount() > 1) { if (poll.getAvailableVoteCount() > 1) {
for (Poll.Entry entry : entries) { for (Poll.Entry entry : entries) {
CheckBox checkBox = new CheckBox(context); CheckBox checkBox = new CheckBox(context);
@ -176,10 +177,11 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
holder.optionsLayout.setVisibility(View.VISIBLE); holder.optionsLayout.setVisibility(View.VISIBLE);
} else if (poll.getAvailableVoteCount() == 1) { } else if (poll.getAvailableVoteCount() == 1) {
RadioGroup radioGroup = new RadioGroup(context); RadioGroup radioGroup = new RadioGroup(context);
for (Poll.Entry entry : entries) { for (int i = 0; i < entries.length; i++) {
RadioButton radioButton = new RadioButton(context); RadioButton radioButton = new RadioButton(context);
radioButton.setText(entry.getEntryName()); radioButton.setId(i);
radioButton.setOnClickListener(v -> viewModel.onRadioButtonCLicked(radioGroup.indexOfChild(v))); radioButton.setText(entries[i].getEntryName());
radioButton.setTextColor(context.getResources().getColor(R.color.primary_text));
radioGroup.addView(radioButton); radioGroup.addView(radioButton);
} }
holder.optionsLayout.addView(radioGroup); holder.optionsLayout.addView(radioGroup);
@ -187,7 +189,6 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
holder.optionsLayout.setVisibility(View.VISIBLE); holder.optionsLayout.setVisibility(View.VISIBLE);
} else { } else {
//Showing results //Showing results
holder.optionsLayout.removeAllViews();
holder.optionsLayout.setVisibility(View.GONE); holder.optionsLayout.setVisibility(View.GONE);
List<BarEntry> valuesToCompare = new ArrayList<>(); List<BarEntry> valuesToCompare = new ArrayList<>();
for (int i = 0; i < entries.length; i++) { for (int i = 0; i < entries.length; i++) {
@ -221,8 +222,13 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
holder.voteChart.invalidate(); holder.voteChart.invalidate();
holder.voteChart.setVisibility(View.VISIBLE); holder.voteChart.setVisibility(View.VISIBLE);
} }
if (poll.getRemoveVoteUrl() != null) holder.removeVotesButton.setVisibility(View.VISIBLE); if (poll.getRemoveVoteUrl() != null) {
else holder.removeVotesButton.setVisibility(View.GONE); 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) { if (poll.getShowVoteResultsUrl() != null) {
holder.showPollResultsButton.setOnClickListener(v -> viewModel.loadUrl(poll.getShowVoteResultsUrl())); holder.showPollResultsButton.setOnClickListener(v -> viewModel.loadUrl(poll.getShowVoteResultsUrl()));
holder.showPollResultsButton.setVisibility(View.VISIBLE); 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.setOnClickListener(v -> viewModel.loadUrl(poll.getShowOptionsUrl()));
holder.hidePollResultsButton.setVisibility(View.VISIBLE); holder.hidePollResultsButton.setVisibility(View.VISIBLE);
} else holder.hidePollResultsButton.setVisibility(View.GONE); } else holder.hidePollResultsButton.setVisibility(View.GONE);
if (poll.getPollFormUrl() != null) holder.submitButton.setVisibility(View.VISIBLE); if (poll.getPollFormUrl() != null) {
else holder.submitButton.setVisibility(View.GONE); holder.submitButton.setOnClickListener(v -> viewModel.submitVote(holder.optionsLayout));
holder.submitButton.setVisibility(View.VISIBLE);
} else holder.submitButton.setVisibility(View.GONE);
} else { } else {
Post currentPost = (Post) topicItems.get(position); Post currentPost = (Post) topicItems.get(position);
if (currentHolder instanceof PostViewHolder) { 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 class DeleteTask extends NetworkTask<Void> {
public DeleteTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskFinishedListener<Void> onParseTaskFinishedListener) { public DeleteTask(OnTaskStartedListener onTaskStartedListener, OnNetworkTaskFinishedListener<Void> onParseTaskFinishedListener) {
super(onParseTaskStartedListener, onParseTaskFinishedListener); super(onTaskStartedListener, onParseTaskFinishedListener);
} }
@Override @Override
@ -28,7 +28,7 @@ public class DeleteTask extends NetworkTask<Void> {
} }
@Override @Override
protected Void performTask(Document document) { protected Void performTask(Document document, Response response) {
return null; 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> { public abstract class ExternalAsyncTask<U, V> extends AsyncTask<U, Void, V> {
protected OnParseTaskStartedListener onParseTaskStartedListener; protected OnTaskStartedListener onTaskStartedListener;
protected OnParseTaskCancelledListener onParseTaskCancelledListener; protected OnTaskCancelledListener onTaskCancelledListener;
protected OnParseTaskFinishedListener<V> onParseTaskFinishedListener; protected OnTaskFinishedListener<V> onTaskFinishedListener;
@Override @Override
protected void onPreExecute() { protected void onPreExecute() {
if (onParseTaskStartedListener != null) if (onTaskStartedListener != null)
onParseTaskStartedListener.onParseStart(); onTaskStartedListener.onTaskStarted();
else else
super.onPreExecute(); super.onPreExecute();
} }
@Override @Override
protected void onCancelled() { protected void onCancelled() {
if (onParseTaskCancelledListener != null) if (onTaskCancelledListener != null)
onParseTaskCancelledListener.onParseCancel(); onTaskCancelledListener.onTaskCanceled();
else else
super.onCancelled(); super.onCancelled();
} }
@Override @Override
protected void onCancelled(V v) { protected void onCancelled(V v) {
if (onParseTaskCancelledListener != null) if (onTaskCancelledListener != null)
onParseTaskCancelledListener.onParseCancel(); onTaskCancelledListener.onTaskCanceled();
else else
super.onCancelled(); super.onCancelled();
} }
@Override @Override
protected void onPostExecute(V v) { protected void onPostExecute(V v) {
if (onParseTaskFinishedListener != null) if (onTaskFinishedListener != null)
onParseTaskFinishedListener.onParseFinish(v); onTaskFinishedListener.onTaskFinished(v);
else else
super.onPostExecute(v); super.onPostExecute(v);
} }
public ExternalAsyncTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskCancelledListener onParseTaskCancelledListener, public ExternalAsyncTask(OnTaskStartedListener onTaskStartedListener, OnTaskCancelledListener onTaskCancelledListener,
OnParseTaskFinishedListener<V> onParseTaskFinishedListener) { OnTaskFinishedListener<V> onTaskFinishedListener) {
this.onParseTaskStartedListener = onParseTaskStartedListener; this.onTaskStartedListener = onTaskStartedListener;
this.onParseTaskCancelledListener = onParseTaskCancelledListener; this.onTaskCancelledListener = onTaskCancelledListener;
this.onParseTaskFinishedListener = onParseTaskFinishedListener; this.onTaskFinishedListener = onTaskFinishedListener;
} }
public ExternalAsyncTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskFinishedListener<V> onParseTaskFinishedListener) { public ExternalAsyncTask(OnTaskStartedListener onTaskStartedListener, OnTaskFinishedListener<V> onTaskFinishedListener) {
this.onParseTaskStartedListener = onParseTaskStartedListener; this.onTaskStartedListener = onTaskStartedListener;
this.onParseTaskFinishedListener = onParseTaskFinishedListener; this.onTaskFinishedListener = onTaskFinishedListener;
} }
public ExternalAsyncTask() { } public ExternalAsyncTask() { }
public void setOnParseTaskStartedListener(OnParseTaskStartedListener onParseTaskStartedListener) { public void setOnTaskStartedListener(OnTaskStartedListener onTaskStartedListener) {
this.onParseTaskStartedListener = onParseTaskStartedListener; this.onTaskStartedListener = onTaskStartedListener;
} }
public void setOnParseTaskCancelledListener(OnParseTaskCancelledListener onParseTaskCancelledListener) { public void setOnTaskCancelledListener(OnTaskCancelledListener onTaskCancelledListener) {
this.onParseTaskCancelledListener = onParseTaskCancelledListener; this.onTaskCancelledListener = onTaskCancelledListener;
} }
public void setOnParseTaskFinishedListener(OnParseTaskFinishedListener<V> onParseTaskFinishedListener) { public void setOnTaskFinishedListener(OnTaskFinishedListener<V> onTaskFinishedListener) {
this.onParseTaskFinishedListener = onParseTaskFinishedListener; this.onTaskFinishedListener = onTaskFinishedListener;
} }
public interface OnParseTaskStartedListener { public interface OnTaskStartedListener {
void onParseStart(); void onTaskStarted();
} }
public interface OnParseTaskCancelledListener { public interface OnTaskCancelledListener {
void onParseCancel(); void onTaskCanceled();
} }
public interface OnParseTaskFinishedListener<V> { public interface OnTaskFinishedListener<V> {
void onParseFinish(V result); 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>> { public abstract class NetworkTask<T> extends ExternalAsyncTask<String, Parcel<T>> {
protected OnParseTaskFinishedListener<T> onParseTaskFinishedListener; protected OnNetworkTaskFinishedListener<T> onNetworkTaskFinishedListener;
public NetworkTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskCancelledListener onParseTaskCancelledListener, public NetworkTask(OnTaskStartedListener onTaskStartedListener, OnTaskCancelledListener onTaskCancelledListener,
OnParseTaskFinishedListener<T> onParseTaskFinishedListener) { OnNetworkTaskFinishedListener<T> onNetworkTaskFinishedListener) {
super(onParseTaskStartedListener, onParseTaskCancelledListener, null); super(onTaskStartedListener, onTaskCancelledListener, null);
this.onParseTaskFinishedListener = onParseTaskFinishedListener; this.onNetworkTaskFinishedListener = onNetworkTaskFinishedListener;
} }
public NetworkTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskFinishedListener<T> onParseTaskFinishedListener) { public NetworkTask(OnTaskStartedListener onTaskStartedListener, OnNetworkTaskFinishedListener<T> onNetworkTaskFinishedListener) {
super(onParseTaskStartedListener, null); super(onTaskStartedListener, null);
this.onParseTaskFinishedListener = onParseTaskFinishedListener; this.onNetworkTaskFinishedListener = onNetworkTaskFinishedListener;
} }
public NetworkTask() {} public NetworkTask() {}
@ -49,7 +49,7 @@ public abstract class NetworkTask<T> extends ExternalAsyncTask<String, Parcel<T>
return new Parcel<>(NetworkResultCodes.NETWORK_ERROR, null); return new Parcel<>(NetworkResultCodes.NETWORK_ERROR, null);
} }
try { try {
T data = performTask(Jsoup.parse(responseBodyString)); T data = performTask(Jsoup.parse(responseBodyString), response);
int resultCode = getResultCode(response, data); int resultCode = getResultCode(response, data);
return new Parcel<>(resultCode, data); return new Parcel<>(resultCode, data);
} catch (ParseException pe) { } catch (ParseException pe) {
@ -63,8 +63,8 @@ public abstract class NetworkTask<T> extends ExternalAsyncTask<String, Parcel<T>
@Override @Override
protected void onPostExecute(Parcel<T> tParcel) { protected void onPostExecute(Parcel<T> tParcel) {
if (onParseTaskFinishedListener != null) if (onNetworkTaskFinishedListener != null)
onParseTaskFinishedListener.onParseFinish(tParcel.getResultCode(), tParcel.getData()); onNetworkTaskFinishedListener.onNetworkTaskFinished(tParcel.getResultCode(), tParcel.getData());
else else
super.onPostExecute(tParcel); super.onPostExecute(tParcel);
} }
@ -77,15 +77,15 @@ public abstract class NetworkTask<T> extends ExternalAsyncTask<String, Parcel<T>
return client.newCall(request).execute(); 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); protected abstract int getResultCode(Response response, T data);
public void setOnParseTaskFinishedListener(OnParseTaskFinishedListener<T> onParseTaskFinishedListener) { public void setOnNetworkTaskFinishedListener(OnNetworkTaskFinishedListener<T> onNetworkTaskFinishedListener) {
this.onParseTaskFinishedListener = onParseTaskFinishedListener; this.onNetworkTaskFinishedListener = onNetworkTaskFinishedListener;
} }
public interface OnParseTaskFinishedListener<T> { public interface OnNetworkTaskFinishedListener<T> {
void onParseFinish(int resultCode, T data); 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 org.jsoup.nodes.Document;
import gr.thmmy.mthmmy.utils.NetworkTask; import gr.thmmy.mthmmy.utils.NetworkTask;
import okhttp3.Response;
public abstract class NewParseTask<T> extends NetworkTask<T> { public abstract class NewParseTask<T> extends NetworkTask<T> {
public NewParseTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskCancelledListener onParseTaskCancelledListener, public NewParseTask(OnTaskStartedListener onTaskStartedListener, OnTaskCancelledListener onTaskCancelledListener,
OnParseTaskFinishedListener<T> onParseTaskFinishedListener) { OnNetworkTaskFinishedListener<T> onParseTaskFinishedListener) {
super(onParseTaskStartedListener, onParseTaskCancelledListener, onParseTaskFinishedListener); super(onTaskStartedListener, onTaskCancelledListener, onParseTaskFinishedListener);
} }
public NewParseTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskFinishedListener<T> onParseTaskFinishedListener) { public NewParseTask(OnTaskStartedListener onTaskStartedListener, OnNetworkTaskFinishedListener<T> onParseTaskFinishedListener) {
super(onParseTaskStartedListener, onParseTaskFinishedListener); super(onTaskStartedListener, onParseTaskFinishedListener);
} }
public NewParseTask() {} public NewParseTask() {}
@Override @Override
protected final T performTask(Document document) { protected final T performTask(Document document, Response response) {
try { try {
return parse(document); return parse(document, response);
} catch (Exception e) { } catch (Exception e) {
throw new ParseException("Parse failed.", 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 * @return the base URL of the given topic
*/ */
public static String getBaseURL(String topicURL) { 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()) if (baseUrlMatcher.find())
return topicURL.substring(baseUrlMatcher.start(), baseUrlMatcher.end()); return forumUrl + topicURL.substring(baseUrlMatcher.start(), baseUrlMatcher.end());
else return ""; else return "";
} }
} }

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

@ -5,6 +5,9 @@ import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import java.util.ArrayList; 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.PrepareForReply;
import gr.thmmy.mthmmy.activities.topic.tasks.PrepareForReplyResult; import gr.thmmy.mthmmy.activities.topic.tasks.PrepareForReplyResult;
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.TopicTask; import gr.thmmy.mthmmy.activities.topic.tasks.TopicTask;
import gr.thmmy.mthmmy.activities.topic.tasks.TopicTaskResult; import gr.thmmy.mthmmy.activities.topic.tasks.TopicTaskResult;
import gr.thmmy.mthmmy.base.BaseActivity; import gr.thmmy.mthmmy.base.BaseActivity;
import gr.thmmy.mthmmy.model.Poll;
import gr.thmmy.mthmmy.model.Post; 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.NetworkResultCodes;
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;
@ -44,7 +52,6 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa
* holds the adapter position of the post being edited * holds the adapter position of the post being edited
*/ */
private int postBeingEditedPosition; private int postBeingEditedPosition;
private ArrayList<Integer> selectedVoteIndices = new ArrayList<>();
private TopicTask currentTopicTask; private TopicTask currentTopicTask;
private PrepareForEditTask currentPrepareForEditTask; private PrepareForEditTask currentPrepareForEditTask;
@ -52,12 +59,14 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa
//callbacks for topic activity //callbacks for topic activity
private TopicTask.TopicTaskObserver topicTaskObserver; private TopicTask.TopicTaskObserver topicTaskObserver;
private DeleteTask.OnParseTaskStartedListener deleteTaskStartedListener; private ExternalAsyncTask.OnTaskStartedListener deleteTaskStartedListener;
private DeleteTask.OnParseTaskFinishedListener<Void> deleteTaskFinishedListener; private NetworkTask.OnNetworkTaskFinishedListener<Void> deleteTaskFinishedListener;
private ReplyTask.ReplyTaskCallbacks replyFinishListener; private ReplyTask.ReplyTaskCallbacks replyFinishListener;
private PrepareForEditTask.PrepareForEditCallbacks prepareForEditCallbacks; private PrepareForEditTask.PrepareForEditCallbacks prepareForEditCallbacks;
private EditTask.EditTaskCallbacks editTaskCallbacks; private EditTask.EditTaskCallbacks editTaskCallbacks;
private PrepareForReply.PrepareForReplyCallbacks prepareForReplyCallbacks; 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 * 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() { public void submitVote(LinearLayout optionsLayout) {
if (topicUrl == null) throw new NullPointerException("No topic task has been requested yet!"); if (topicItems.getValue() == null) throw new NullPointerException("Topic task has not finished yet!");
Timber.i("Viewing poll results"); ArrayList<Integer> votes = new ArrayList<>();
loadUrl(ParseHelpers.getBaseURL(topicUrl) + ";viewResults"); 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);
} }
}
public void loadBaseUrl() { int[] votesArray = new int[votes.size()];
if (topicUrl == null) throw new NullPointerException("No topic task has been requested yet!"); for (int i = 0; i < votes.size(); i++) votesArray[i] = votes.get(i);
Timber.i("Viewing poll results"); Poll poll = (Poll) topicItems.getValue().get(0);
loadUrl(ParseHelpers.getBaseURL(topicUrl)); SubmitVoteTask submitVoteTask = new SubmitVoteTask(votesArray);
submitVoteTask.setOnTaskStartedListener(voteTaskStartedListener);
submitVoteTask.setOnNetworkTaskFinishedListener(voteTaskFinishedListener);
submitVoteTask.execute(poll.getPollFormUrl(), poll.getSc());
} }
public void prepareForReply() { public void prepareForReply() {
@ -259,17 +277,16 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa
if (changePage && oldIndicatorIndex != this.pageIndicatorIndex.getValue()) performPageChange(); if (changePage && oldIndicatorIndex != this.pageIndicatorIndex.getValue()) performPageChange();
} }
public void onVoteCheckboxClicked(int index, boolean checked) { // <-------------Just getters, setters and helper methods below here---------------->
if (checked) selectedVoteIndices.add(index);
else selectedVoteIndices.remove(index);
}
public void onRadioButtonCLicked(int index) { public void setVoteTaskStartedListener(ExternalAsyncTask.OnTaskStartedListener voteTaskStartedListener) {
selectedVoteIndices.clear(); this.voteTaskStartedListener = voteTaskStartedListener;
selectedVoteIndices.add(index);
} }
// <-------------Just getters, setters and helper methods below here----------------> public void setVoteTaskFinishedListener(NetworkTask.OnNetworkTaskFinishedListener<Void> voteTaskFinishedListener) {
this.voteTaskFinishedListener = voteTaskFinishedListener;
}
public MutableLiveData<String> getTopicViewers() { public MutableLiveData<String> getTopicViewers() {
return topicViewers; 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; this.deleteTaskStartedListener = deleteTaskStartedListener;
} }
public void setDeleteTaskFinishedListener(DeleteTask.OnParseTaskFinishedListener<Void> deleteTaskFinishedListener) { public void setDeleteTaskFinishedListener(NetworkTask.OnNetworkTaskFinishedListener<Void> deleteTaskFinishedListener) {
this.deleteTaskFinishedListener = deleteTaskFinishedListener; this.deleteTaskFinishedListener = deleteTaskFinishedListener;
} }

Loading…
Cancel
Save