From 4938597f2ed778d4a0929c4fd830dc2618983f44 Mon Sep 17 00:00:00 2001 From: Thodoris1999 Date: Tue, 11 Sep 2018 23:06:22 +0300 Subject: [PATCH] improvements and implement parse task in delete post --- .../activities/topic/TopicActivity.java | 29 ++++------ .../activities/topic/tasks/DeleteTask.java | 53 ++++++------------- .../mthmmy/utils/parsing/NetworkTask.java | 19 ++++--- .../mthmmy/utils/parsing/NewParseTask.java | 4 +- .../mthmmy/utils/parsing/ParseException.java | 2 +- .../mthmmy/viewmodel/TopicViewModel.java | 16 ++++-- 6 files changed, 51 insertions(+), 72 deletions(-) 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 953f7ad8..7f8b248a 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 @@ -12,7 +12,6 @@ import android.os.Bundle; import android.os.Handler; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; -import android.support.v4.content.ContextCompat; import android.support.v4.content.res.ResourcesCompat; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatDelegate; @@ -39,7 +38,6 @@ import android.widget.Toast; import java.util.ArrayList; import gr.thmmy.mthmmy.R; -import gr.thmmy.mthmmy.activities.topic.tasks.DeleteTask; import gr.thmmy.mthmmy.activities.topic.tasks.EditTask; import gr.thmmy.mthmmy.activities.topic.tasks.PrepareForEditTask; import gr.thmmy.mthmmy.activities.topic.tasks.PrepareForReply; @@ -52,6 +50,7 @@ import gr.thmmy.mthmmy.model.Post; import gr.thmmy.mthmmy.model.ThmmyPage; import gr.thmmy.mthmmy.utils.CustomLinearLayoutManager; import gr.thmmy.mthmmy.utils.HTMLUtils; +import gr.thmmy.mthmmy.utils.parsing.Parcel; import gr.thmmy.mthmmy.utils.parsing.ParseHelpers; import gr.thmmy.mthmmy.viewmodel.TopicViewModel; import me.zhanghai.android.materialprogressbar.MaterialProgressBar; @@ -498,23 +497,15 @@ public class TopicActivity extends BaseActivity implements TopicAdapter.OnPostFo progressBar.setVisibility(ProgressBar.GONE); } }); - viewModel.setDeleteTaskCallbacks(new DeleteTask.DeleteTaskCallbacks() { - @Override - public void onDeleteTaskStarted() { - progressBar.setVisibility(ProgressBar.VISIBLE); - } - - @Override - public void onDeleteTaskFinished(boolean result) { - progressBar.setVisibility(ProgressBar.GONE); - - if (result) { - Timber.i("Post deleted successfully"); - viewModel.reloadPage(); - } else { - Timber.w("Failed to delete post"); - Toast.makeText(getBaseContext(), "Delete failed!", Toast.LENGTH_SHORT).show(); - } + viewModel.setDeleteTaskStartedListener(() -> progressBar.setVisibility(ProgressBar.VISIBLE)); + viewModel.setDeleteTaskFinishedListener((resultCode, data) -> { + progressBar.setVisibility(ProgressBar.GONE); + if (resultCode == Parcel.ResultCode.SUCCESSFUL) { + Timber.i("Post deleted successfully"); + viewModel.reloadPage(); + } else { + Timber.w("Failed to delete post"); + Toast.makeText(getBaseContext(), "Delete failed!", Toast.LENGTH_SHORT).show(); } }); viewModel.setReplyFinishListener(new ReplyTask.ReplyTaskCallbacks() { 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 0a9486b1..40a2df5b 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 @@ -1,62 +1,39 @@ package gr.thmmy.mthmmy.activities.topic.tasks; -import android.os.AsyncTask; +import org.jsoup.nodes.Document; import java.io.IOException; -import gr.thmmy.mthmmy.activities.topic.Posting; -import gr.thmmy.mthmmy.base.BaseApplication; +import gr.thmmy.mthmmy.utils.parsing.NetworkTask; +import gr.thmmy.mthmmy.utils.parsing.Parcel; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; -import timber.log.Timber; -public class DeleteTask extends AsyncTask { - private DeleteTaskCallbacks listener; +public class DeleteTask extends NetworkTask { - public DeleteTask(DeleteTaskCallbacks listener) { - this.listener = listener; + public DeleteTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskFinishedListener onParseTaskFinishedListener) { + super(onParseTaskStartedListener, onParseTaskFinishedListener); } @Override - protected void onPreExecute() { - listener.onDeleteTaskStarted(); - } - - @Override - protected Boolean doInBackground(String... args) { + protected Response sendRequest(OkHttpClient client, String... input) throws IOException { Request delete = new Request.Builder() - .url(args[0]) + .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") .build(); - - try { - OkHttpClient client = BaseApplication.getInstance().getClient(); - client.newCall(delete).execute(); - Response response = client.newCall(delete).execute(); - //Response response = client.newCall(delete).execute(); - switch (Posting.replyStatus(response)) { - case SUCCESSFUL: - BaseApplication.getInstance().logFirebaseAnalyticsEvent("post_deletion", null); - return true; - default: - Timber.e("Something went wrong. Request string: %s", delete.toString()); - return false; - } - } catch (IOException e) { - Timber.e(e, "Delete failed."); - return false; - } + client.newCall(delete).execute(); + return client.newCall(delete).execute(); } @Override - protected void onPostExecute(Boolean result) { - listener.onDeleteTaskFinished(result); + protected Void performTask(Document document) { + return null; } - public interface DeleteTaskCallbacks { - void onDeleteTaskStarted(); - void onDeleteTaskFinished(boolean result); + @Override + protected int getResultCode(Response response, Void data) { + return Parcel.ResultCode.SUCCESSFUL; } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/NetworkTask.java b/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/NetworkTask.java index a63f954e..3507d130 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/NetworkTask.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/NetworkTask.java @@ -7,7 +7,6 @@ import java.io.IOException; import gr.thmmy.mthmmy.base.BaseApplication; import okhttp3.OkHttpClient; -import okhttp3.Request; import okhttp3.Response; import timber.log.Timber; @@ -30,15 +29,21 @@ public abstract class NetworkTask extends ExternalAsyncTask @Override protected final Parcel doInBackground(String... input) { - Response response = sendRequest(BaseApplication.getInstance().getClient(), input); + Response response; + try { + response = sendRequest(BaseApplication.getInstance().getClient(), input); + } catch (IOException e) { + Timber.e(e, "Error connecting to thmmy.gr"); + return new Parcel<>(Parcel.ResultCode.NETWORK_ERROR, null); + } String responseBodyString; try { responseBodyString = response.body().string(); } catch (NullPointerException npe) { - Timber.e(npe, "Invalid response. Detatails: https://square.github.io/okhttp/3.x/okhttp/okhttp3/Response.html#body--"); + Timber.wtf(npe, "Invalid response. Detatails: https://square.github.io/okhttp/3.x/okhttp/okhttp3/Response.html#body--"); return new Parcel<>(Parcel.ResultCode.NETWORK_ERROR, null); } catch (IOException e) { - Timber.e(e); + Timber.e(e, "Error getting response body string"); return new Parcel<>(Parcel.ResultCode.NETWORK_ERROR, null); } try { @@ -62,9 +67,9 @@ public abstract class NetworkTask extends ExternalAsyncTask super.onPostExecute(tParcel); } - protected abstract Response sendRequest(OkHttpClient client, String... input); + protected abstract Response sendRequest(OkHttpClient client, String... input) throws IOException; - protected abstract T performTask(Document document) throws ParseException; + protected abstract T performTask(Document document); protected abstract int getResultCode(Response response, T data); @@ -73,6 +78,6 @@ public abstract class NetworkTask extends ExternalAsyncTask } public interface OnParseTaskFinishedListener { - void onParseFinish(int resultCOde, T data); + void onParseFinish(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 98449b64..c9f6ea2e 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 @@ -16,7 +16,7 @@ public abstract class NewParseTask extends NetworkTask { public NewParseTask() {} @Override - protected final T performTask(Document document) throws ParseException { + protected final T performTask(Document document) { try { return parse(document); } catch (Exception e) { @@ -24,5 +24,5 @@ public abstract class NewParseTask extends NetworkTask { } } - protected abstract T parse (Document document); + protected abstract T parse (Document document) throws ParseException; } diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseException.java b/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseException.java index abda1f38..942e789b 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseException.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseException.java @@ -3,7 +3,7 @@ package gr.thmmy.mthmmy.utils.parsing; /** * ParseException is to be used for errors while parsing. */ -public class ParseException extends Exception { +public class ParseException extends RuntimeException { public ParseException() {} public ParseException(String message) 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 96cecf4a..78284b11 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java +++ b/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java @@ -9,8 +9,8 @@ import android.preference.PreferenceManager; import java.util.ArrayList; import gr.thmmy.mthmmy.activities.settings.SettingsActivity; -import gr.thmmy.mthmmy.activities.topic.tasks.DeleteTask; import gr.thmmy.mthmmy.activities.topic.tasks.EditTask; +import gr.thmmy.mthmmy.activities.topic.tasks.DeleteTask; import gr.thmmy.mthmmy.activities.topic.tasks.PrepareForEditResult; import gr.thmmy.mthmmy.activities.topic.tasks.PrepareForEditTask; import gr.thmmy.mthmmy.activities.topic.tasks.PrepareForReply; @@ -50,7 +50,8 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa //callbacks for topic activity private TopicTask.TopicTaskObserver topicTaskObserver; - private DeleteTask.DeleteTaskCallbacks deleteTaskCallbacks; + private DeleteTask.OnParseTaskStartedListener deleteTaskStartedListener; + private DeleteTask.OnParseTaskFinishedListener deleteTaskFinishedListener; private ReplyTask.ReplyTaskCallbacks replyFinishListener; private PrepareForEditTask.PrepareForEditCallbacks prepareForEditCallbacks; private EditTask.EditTaskCallbacks editTaskCallbacks; @@ -133,7 +134,7 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa public void deletePost(String postDeleteUrl) { Timber.i("Deleting post"); - new DeleteTask(deleteTaskCallbacks).execute(postDeleteUrl); + new DeleteTask(deleteTaskStartedListener, deleteTaskFinishedListener).execute(postDeleteUrl); } public void prepareForEdit(int position, String postEditURL) { @@ -313,8 +314,13 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa this.topicTaskObserver = topicTaskObserver; } - public void setDeleteTaskCallbacks(DeleteTask.DeleteTaskCallbacks deleteTaskCallbacks) { - this.deleteTaskCallbacks = deleteTaskCallbacks; + + public void setDeleteTaskStartedListener(DeleteTask.OnParseTaskStartedListener deleteTaskStartedListener) { + this.deleteTaskStartedListener = deleteTaskStartedListener; + } + + public void setDeleteTaskFinishedListener(DeleteTask.OnParseTaskFinishedListener deleteTaskFinishedListener) { + this.deleteTaskFinishedListener = deleteTaskFinishedListener; } public void setReplyFinishListener(ReplyTask.ReplyTaskCallbacks replyFinishListener) {