From db75338039d6347a95d70b74551e39df59aec23a Mon Sep 17 00:00:00 2001 From: Thodoris1999 Date: Tue, 11 Sep 2018 18:04:46 +0300 Subject: [PATCH] improvements --- .../utils/parsing/ExternalAsyncTask.java | 80 +++++++++++++++++++ .../mthmmy/utils/parsing/NetworkTask.java | 65 +++++++++++++++ .../mthmmy/utils/parsing/NewParseTask.java | 71 ++++------------ .../gr/thmmy/mthmmy/utils/parsing/Parcel.java | 51 ++++++++++++ 4 files changed, 211 insertions(+), 56 deletions(-) create mode 100644 app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ExternalAsyncTask.java create mode 100644 app/src/main/java/gr/thmmy/mthmmy/utils/parsing/NetworkTask.java create mode 100644 app/src/main/java/gr/thmmy/mthmmy/utils/parsing/Parcel.java diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ExternalAsyncTask.java b/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ExternalAsyncTask.java new file mode 100644 index 00000000..6f5814e0 --- /dev/null +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ExternalAsyncTask.java @@ -0,0 +1,80 @@ +package gr.thmmy.mthmmy.utils.parsing; + +import android.os.AsyncTask; + +public abstract class ExternalAsyncTask extends AsyncTask { + + private OnParseTaskStartedListener onParseTaskStartedListener; + private OnParseTaskCancelledListener onParseTaskCancelledListener; + private OnParseTaskFinishedListener onParseTaskFinishedListener; + + @Override + protected void onPreExecute() { + if (onParseTaskStartedListener != null) + onParseTaskStartedListener.onParseStart(); + else + super.onPreExecute(); + } + + @Override + protected void onCancelled() { + if (onParseTaskCancelledListener != null) + onParseTaskCancelledListener.onParseCancel(); + else + super.onCancelled(); + } + + @Override + protected void onCancelled(V v) { + if (onParseTaskCancelledListener != null) + onParseTaskCancelledListener.onParseCancel(); + else + super.onCancelled(); + } + + @Override + protected void onPostExecute(V v) { + if (onParseTaskFinishedListener != null) + onParseTaskFinishedListener.onParseFinish(v); + else + super.onPostExecute(v); + } + + public ExternalAsyncTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskCancelledListener onParseTaskCancelledListener, + OnParseTaskFinishedListener onParseTaskFinishedListener) { + this.onParseTaskStartedListener = onParseTaskStartedListener; + this.onParseTaskCancelledListener = onParseTaskCancelledListener; + this.onParseTaskFinishedListener = onParseTaskFinishedListener; + } + + public ExternalAsyncTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskFinishedListener onParseTaskFinishedListener) { + this.onParseTaskStartedListener = onParseTaskStartedListener; + this.onParseTaskFinishedListener = onParseTaskFinishedListener; + } + + public ExternalAsyncTask() { } + + public void setOnParseTaskStartedListener(OnParseTaskStartedListener onParseTaskStartedListener) { + this.onParseTaskStartedListener = onParseTaskStartedListener; + } + + public void setOnParseTaskCancelledListener(OnParseTaskCancelledListener onParseTaskCancelledListener) { + this.onParseTaskCancelledListener = onParseTaskCancelledListener; + } + + public void setOnParseTaskFinishedListener(OnParseTaskFinishedListener onParseTaskFinishedListener) { + this.onParseTaskFinishedListener = onParseTaskFinishedListener; + } + + public interface OnParseTaskStartedListener { + void onParseStart(); + } + + public interface OnParseTaskCancelledListener { + void onParseCancel(); + } + + public interface OnParseTaskFinishedListener { + void onParseFinish(V result); + } +} 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 new file mode 100644 index 00000000..d2bd3d57 --- /dev/null +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/NetworkTask.java @@ -0,0 +1,65 @@ +package gr.thmmy.mthmmy.utils.parsing; + +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; + +import java.io.IOException; + +import gr.thmmy.mthmmy.base.BaseApplication; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; +import timber.log.Timber; + +public abstract class NetworkTask extends ExternalAsyncTask> { + + private OnParseTaskFinishedListener onParseTaskFinishedListener; + + @Override + protected final Parcel doInBackground(String... input) { + Request request = createRequest(input); + Response response = getResponse(request, BaseApplication.getInstance().getClient()); + String responseBodyString; + try { + responseBodyString = response.body().string(); + } catch (IOException e) { + Timber.e(e); + return new Parcel<>(Parcel.ResultCode.NETWORK_ERROR, null); + } + try { + T data = performTask(Jsoup.parse(responseBodyString)); + int resultCode = getResultCode(response, data); + return new Parcel<>(resultCode, data); + } catch (ParseException pe) { + Timber.e(pe); + return new Parcel<>(Parcel.ResultCode.PARSE_ERROR, null); + } catch (Exception e) { + Timber.e(e); + return new Parcel<>(Parcel.ResultCode.PERFORM_TASK_ERROR, null); + } + } + + @Override + protected void onPostExecute(Parcel tParcel) { + if (onParseTaskFinishedListener != null) + onParseTaskFinishedListener.onParseFinish(tParcel.getResultCode(), tParcel.getData()); + else + super.onPostExecute(tParcel); + } + + abstract Request createRequest(String... input); + + abstract Response getResponse(Request request, OkHttpClient client); + + abstract T performTask(Document document) throws ParseException; + + abstract int getResultCode(Response response, T data); + + public void setOnParseTaskFinishedListener(OnParseTaskFinishedListener onParseTaskFinishedListener) { + this.onParseTaskFinishedListener = onParseTaskFinishedListener; + } + + public interface OnParseTaskFinishedListener { + 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 ab6047e8..4c23c241 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 @@ -1,68 +1,27 @@ package gr.thmmy.mthmmy.utils.parsing; -import android.os.AsyncTask; +import org.jsoup.nodes.Document; -public abstract class NewParseTask extends AsyncTask { +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; - private OnParseTaskStartedListener onParseTaskStartedListener; - private OnParseTaskCancelledListener onParseTaskCancelledListener; - private OnParseTaskFinishedListener onParseTaskFinishedListener; +public abstract class NewParseTask extends NetworkTask { @Override - protected void onPreExecute() { - if (onParseTaskStartedListener != null) - onParseTaskStartedListener.onParseStart(); - else - super.onPreExecute(); + final T performTask(Document document) throws ParseException { + try { + return parse(document); + } catch (Exception e) { + throw new ParseException("Parse failed."); + } } - @Override - protected void onCancelled() { - if (onParseTaskCancelledListener != null) - onParseTaskCancelledListener.onParseCancel(); - else - super.onCancelled(); - } - - @Override - protected void onCancelled(V v) { - if (onParseTaskCancelledListener != null) - onParseTaskCancelledListener.onParseCancel(); - else - super.onCancelled(); - } - - @Override - protected void onPostExecute(V v) { - if (onParseTaskFinishedListener != null) - onParseTaskFinishedListener.onParseFinish(v); - else - super.onPostExecute(v); - } + abstract Request createRequest(String... input); - public NewParseTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskCancelledListener onParseTaskCancelledListener, - OnParseTaskFinishedListener onParseTaskFinishedListener) { - this.onParseTaskStartedListener = onParseTaskStartedListener; - this.onParseTaskCancelledListener = onParseTaskCancelledListener; - this.onParseTaskFinishedListener = onParseTaskFinishedListener; - } - - public NewParseTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskFinishedListener onParseTaskFinishedListener) { - this.onParseTaskStartedListener = onParseTaskStartedListener; - this.onParseTaskFinishedListener = onParseTaskFinishedListener; - } - - public NewParseTask() { } + abstract Response getResponse(Request request, OkHttpClient client); - public interface OnParseTaskStartedListener { - void onParseStart(); - } - - public interface OnParseTaskCancelledListener { - void onParseCancel(); - } + abstract T parse (Document document); - public interface OnParseTaskFinishedListener { - void onParseFinish(V result); - } + abstract int getResultCode(Response response, T data); } diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/Parcel.java b/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/Parcel.java new file mode 100644 index 00000000..6e1e0f7f --- /dev/null +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/Parcel.java @@ -0,0 +1,51 @@ +package gr.thmmy.mthmmy.utils.parsing; + +public class Parcel { + + private int resultCode; + private T data; + + public Parcel(int resultCode, T data) { + this.resultCode = resultCode; + this.data = data; + } + + public int getResultCode() { + return resultCode; + } + + public T getData() { + return data; + } + + public class ResultCode { + /** + * The request was successful + */ + public static final int SUCCESSFUL = 0; + /** + * Error 404, page was not found + */ + public static final int NOT_FOUND = 1; + /** + * User session ended while posting the reply + */ + public static final int SESSION_ENDED = 2; + /** + * Exception occured while parsing + */ + public static final int PARSE_ERROR = 3; + /** + * Other undefined of unidentified error + */ + public static final int OTHER_ERROR = 4; + /** + * Failed to connect to thmmy.gr + */ + public static final int NETWORK_ERROR = 5; + /** + * Error while excecuting NetworkTask's performTask() + */ + public static final int PERFORM_TASK_ERROR = 6; + } +}