From 4f8e2e079dde0c8364d046af8b2997a40dcdb50d Mon Sep 17 00:00:00 2001 From: Thodoris1999 Date: Mon, 10 Sep 2018 23:16:46 +0300 Subject: [PATCH 01/11] new parse task --- .../mthmmy/utils/parsing/NewParseTask.java | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 app/src/main/java/gr/thmmy/mthmmy/utils/parsing/NewParseTask.java 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 new file mode 100644 index 00000000..5af4a172 --- /dev/null +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/NewParseTask.java @@ -0,0 +1,66 @@ +package gr.thmmy.mthmmy.utils.parsing; + +import android.os.AsyncTask; + +public abstract class NewParseTask 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(); + else + super.onPostExecute(v); + } + + 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(); + } +} From d7bd211de618dbbca9b864a2a014f2f2bf73c374 Mon Sep 17 00:00:00 2001 From: Thodoris1999 Date: Mon, 10 Sep 2018 23:20:14 +0300 Subject: [PATCH 02/11] constructors instead of setters --- .../thmmy/mthmmy/utils/parsing/NewParseTask.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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 5af4a172..ec1da28e 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 @@ -40,6 +40,20 @@ public abstract class NewParseTask extends AsyncTask { super.onPostExecute(v); } + 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() { } + public void setOnParseTaskStartedListener(OnParseTaskStartedListener onParseTaskStartedListener) { this.onParseTaskStartedListener = onParseTaskStartedListener; } From bb7537f65ac15289c9a1e709d20a88324d6d01fe Mon Sep 17 00:00:00 2001 From: Thodoris1999 Date: Mon, 10 Sep 2018 23:32:27 +0300 Subject: [PATCH 03/11] fixes --- .../mthmmy/utils/parsing/NewParseTask.java | 24 +++++-------------- 1 file changed, 6 insertions(+), 18 deletions(-) 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 ec1da28e..ab6047e8 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 @@ -6,7 +6,7 @@ public abstract class NewParseTask extends AsyncTask { private OnParseTaskStartedListener onParseTaskStartedListener; private OnParseTaskCancelledListener onParseTaskCancelledListener; - private OnParseTaskFinishedListener onParseTaskFinishedListener; + private OnParseTaskFinishedListener onParseTaskFinishedListener; @Override protected void onPreExecute() { @@ -35,37 +35,25 @@ public abstract class NewParseTask extends AsyncTask { @Override protected void onPostExecute(V v) { if (onParseTaskFinishedListener != null) - onParseTaskFinishedListener.onParseFinish(); + onParseTaskFinishedListener.onParseFinish(v); else super.onPostExecute(v); } public NewParseTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskCancelledListener onParseTaskCancelledListener, - OnParseTaskFinishedListener onParseTaskFinishedListener) { + OnParseTaskFinishedListener onParseTaskFinishedListener) { this.onParseTaskStartedListener = onParseTaskStartedListener; this.onParseTaskCancelledListener = onParseTaskCancelledListener; this.onParseTaskFinishedListener = onParseTaskFinishedListener; } - public NewParseTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskFinishedListener onParseTaskFinishedListener) { + public NewParseTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskFinishedListener onParseTaskFinishedListener) { this.onParseTaskStartedListener = onParseTaskStartedListener; this.onParseTaskFinishedListener = onParseTaskFinishedListener; } public NewParseTask() { } - 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(); } @@ -74,7 +62,7 @@ public abstract class NewParseTask extends AsyncTask { void onParseCancel(); } - public interface OnParseTaskFinishedListener { - void onParseFinish(); + public interface OnParseTaskFinishedListener { + void onParseFinish(V result); } } From db75338039d6347a95d70b74551e39df59aec23a Mon Sep 17 00:00:00 2001 From: Thodoris1999 Date: Tue, 11 Sep 2018 18:04:46 +0300 Subject: [PATCH 04/11] 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; + } +} From fff52d1ec6061e936136b604bbe9164480640e81 Mon Sep 17 00:00:00 2001 From: Thodoris1999 Date: Tue, 11 Sep 2018 18:13:49 +0300 Subject: [PATCH 05/11] add parse exception constructor --- .../main/java/gr/thmmy/mthmmy/utils/parsing/NewParseTask.java | 2 +- .../java/gr/thmmy/mthmmy/utils/parsing/ParseException.java | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) 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 4c23c241..0d19c098 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 @@ -13,7 +13,7 @@ public abstract class NewParseTask extends NetworkTask { try { return parse(document); } catch (Exception e) { - throw new ParseException("Parse failed."); + throw new ParseException("Parse failed.", e); } } 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 fea7be6b..abda1f38 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 @@ -10,4 +10,8 @@ public class ParseException extends Exception { { super(message); } + + public ParseException(String message, Throwable cause) { + super(message, cause); + } } From e31227f62e3262031a2c16276c02e0af78c6a572 Mon Sep 17 00:00:00 2001 From: Thodoris1999 Date: Tue, 11 Sep 2018 18:36:08 +0300 Subject: [PATCH 06/11] override constructors --- .../mthmmy/utils/parsing/NetworkTask.java | 13 ++++++++++++ .../mthmmy/utils/parsing/NewParseTask.java | 21 ++++++++++--------- 2 files changed, 24 insertions(+), 10 deletions(-) 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 d2bd3d57..cfceb904 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 @@ -15,6 +15,19 @@ public abstract class NetworkTask extends ExternalAsyncTask private OnParseTaskFinishedListener onParseTaskFinishedListener; + public NetworkTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskCancelledListener onParseTaskCancelledListener, + OnParseTaskFinishedListener onParseTaskFinishedListener) { + super(onParseTaskStartedListener, onParseTaskCancelledListener, null); + this.onParseTaskFinishedListener = onParseTaskFinishedListener; + } + + public NetworkTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskFinishedListener onParseTaskFinishedListener) { + super(onParseTaskStartedListener, null); + this.onParseTaskFinishedListener = onParseTaskFinishedListener; + } + + public NetworkTask() {} + @Override protected final Parcel doInBackground(String... input) { Request request = createRequest(input); 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 0d19c098..0a0591de 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 @@ -2,12 +2,19 @@ package gr.thmmy.mthmmy.utils.parsing; import org.jsoup.nodes.Document; -import okhttp3.OkHttpClient; -import okhttp3.Request; -import okhttp3.Response; - public abstract class NewParseTask extends NetworkTask { + public NewParseTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskCancelledListener onParseTaskCancelledListener, + OnParseTaskFinishedListener onParseTaskFinishedListener) { + super(onParseTaskStartedListener, onParseTaskCancelledListener, onParseTaskFinishedListener); + } + + public NewParseTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskFinishedListener onParseTaskFinishedListener) { + super(onParseTaskStartedListener, onParseTaskFinishedListener); + } + + public NewParseTask() {} + @Override final T performTask(Document document) throws ParseException { try { @@ -17,11 +24,5 @@ public abstract class NewParseTask extends NetworkTask { } } - abstract Request createRequest(String... input); - - abstract Response getResponse(Request request, OkHttpClient client); - abstract T parse (Document document); - - abstract int getResultCode(Response response, T data); } From 4b79360ad1f3207cd2dfe3bc97b0c6d94bddc3b5 Mon Sep 17 00:00:00 2001 From: Thodoris1999 Date: Tue, 11 Sep 2018 19:33:01 +0300 Subject: [PATCH 07/11] fix variable and method scopes --- .../thmmy/mthmmy/utils/parsing/ExternalAsyncTask.java | 6 +++--- .../gr/thmmy/mthmmy/utils/parsing/NetworkTask.java | 10 +++++----- .../gr/thmmy/mthmmy/utils/parsing/NewParseTask.java | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-) 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 index 6f5814e0..93bc4224 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ExternalAsyncTask.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ExternalAsyncTask.java @@ -4,9 +4,9 @@ import android.os.AsyncTask; public abstract class ExternalAsyncTask extends AsyncTask { - private OnParseTaskStartedListener onParseTaskStartedListener; - private OnParseTaskCancelledListener onParseTaskCancelledListener; - private OnParseTaskFinishedListener onParseTaskFinishedListener; + protected OnParseTaskStartedListener onParseTaskStartedListener; + protected OnParseTaskCancelledListener onParseTaskCancelledListener; + protected OnParseTaskFinishedListener onParseTaskFinishedListener; @Override protected void onPreExecute() { 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 cfceb904..a137ebdb 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 @@ -13,7 +13,7 @@ import timber.log.Timber; public abstract class NetworkTask extends ExternalAsyncTask> { - private OnParseTaskFinishedListener onParseTaskFinishedListener; + protected OnParseTaskFinishedListener onParseTaskFinishedListener; public NetworkTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskCancelledListener onParseTaskCancelledListener, OnParseTaskFinishedListener onParseTaskFinishedListener) { @@ -60,13 +60,13 @@ public abstract class NetworkTask extends ExternalAsyncTask super.onPostExecute(tParcel); } - abstract Request createRequest(String... input); + protected abstract Request createRequest(String... input); - abstract Response getResponse(Request request, OkHttpClient client); + protected abstract Response getResponse(Request request, OkHttpClient client); - abstract T performTask(Document document) throws ParseException; + protected abstract T performTask(Document document) throws ParseException; - abstract int getResultCode(Response response, T data); + protected abstract int getResultCode(Response response, T data); public void setOnParseTaskFinishedListener(OnParseTaskFinishedListener onParseTaskFinishedListener) { this.onParseTaskFinishedListener = onParseTaskFinishedListener; 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 0a0591de..98449b64 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 - final T performTask(Document document) throws ParseException { + protected final T performTask(Document document) throws ParseException { try { return parse(document); } catch (Exception e) { @@ -24,5 +24,5 @@ public abstract class NewParseTask extends NetworkTask { } } - abstract T parse (Document document); + protected abstract T parse (Document document); } From f400960548cb266f92b68ec20d0e1bba4882c01c Mon Sep 17 00:00:00 2001 From: Thodoris1999 Date: Tue, 11 Sep 2018 21:18:22 +0300 Subject: [PATCH 08/11] merge createRequest and getResponse --- .../gr/thmmy/mthmmy/utils/parsing/NetworkTask.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) 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 a137ebdb..a63f954e 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 @@ -30,11 +30,13 @@ public abstract class NetworkTask extends ExternalAsyncTask @Override protected final Parcel doInBackground(String... input) { - Request request = createRequest(input); - Response response = getResponse(request, BaseApplication.getInstance().getClient()); + Response response = sendRequest(BaseApplication.getInstance().getClient(), input); 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--"); + return new Parcel<>(Parcel.ResultCode.NETWORK_ERROR, null); } catch (IOException e) { Timber.e(e); return new Parcel<>(Parcel.ResultCode.NETWORK_ERROR, null); @@ -60,9 +62,7 @@ public abstract class NetworkTask extends ExternalAsyncTask super.onPostExecute(tParcel); } - protected abstract Request createRequest(String... input); - - protected abstract Response getResponse(Request request, OkHttpClient client); + protected abstract Response sendRequest(OkHttpClient client, String... input); protected abstract T performTask(Document document) throws ParseException; From 4938597f2ed778d4a0929c4fd830dc2618983f44 Mon Sep 17 00:00:00 2001 From: Thodoris1999 Date: Tue, 11 Sep 2018 23:06:22 +0300 Subject: [PATCH 09/11] 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) { From 5129c06cd4cf2f3d65df3d1881d82e6404ef6e21 Mon Sep 17 00:00:00 2001 From: Thodoris1999 Date: Wed, 12 Sep 2018 13:29:16 +0300 Subject: [PATCH 10/11] add default request in NetworkTask, refactor main activity --- .../activities/main/forum/ForumFragment.java | 89 ++++++++++--------- .../main/recent/RecentFragment.java | 70 ++++++++------- .../main/unread/UnreadFragment.java | 71 +++++++++------ .../mthmmy/utils/parsing/NetworkTask.java | 9 +- 4 files changed, 142 insertions(+), 97 deletions(-) diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/forum/ForumFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/forum/ForumFragment.java index 3b6366b4..f98dcd60 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/forum/ForumFragment.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/main/forum/ForumFragment.java @@ -10,6 +10,7 @@ import android.view.View; import android.view.ViewGroup; import android.widget.ProgressBar; import android.widget.RelativeLayout; +import android.widget.Toast; import com.bignerdranch.expandablerecyclerview.ExpandableRecyclerAdapter; @@ -17,21 +18,26 @@ import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; +import java.io.IOException; import java.util.ArrayList; import java.util.List; import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.base.BaseActivity; +import gr.thmmy.mthmmy.base.BaseApplication; import gr.thmmy.mthmmy.base.BaseFragment; import gr.thmmy.mthmmy.model.Board; import gr.thmmy.mthmmy.model.Category; import gr.thmmy.mthmmy.session.SessionManager; import gr.thmmy.mthmmy.utils.CustomRecyclerView; +import gr.thmmy.mthmmy.utils.parsing.NewParseTask; +import gr.thmmy.mthmmy.utils.parsing.Parcel; import gr.thmmy.mthmmy.utils.parsing.ParseException; -import gr.thmmy.mthmmy.utils.parsing.ParseTask; import me.zhanghai.android.materialprogressbar.MaterialProgressBar; import okhttp3.HttpUrl; +import okhttp3.OkHttpClient; import okhttp3.Request; +import okhttp3.Response; import timber.log.Timber; /** @@ -83,7 +89,7 @@ public class ForumFragment extends BaseFragment { public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); if (categories.isEmpty()) { - forumTask = new ForumTask(); + forumTask = new ForumTask(this::onForumTaskStarted, this::onForumTaskFinished); forumTask.execute(); } @@ -106,7 +112,7 @@ public class ForumFragment extends BaseFragment { if (BaseActivity.getSessionManager().isLoggedIn()) { if (forumTask.getStatus() == AsyncTask.Status.RUNNING) forumTask.cancel(true); - forumTask = new ForumTask(); + forumTask = new ForumTask(ForumFragment.this::onForumTaskStarted, ForumFragment.this::onForumTaskFinished); forumTask.setUrl(categories.get(parentPosition).getCategoryURL()); forumTask.execute(); } @@ -117,7 +123,7 @@ public class ForumFragment extends BaseFragment { if (BaseActivity.getSessionManager().isLoggedIn()) { if (forumTask.getStatus() == AsyncTask.Status.RUNNING) forumTask.cancel(true); - forumTask = new ForumTask(); + forumTask = new ForumTask(ForumFragment.this::onForumTaskStarted, ForumFragment.this::onForumTaskFinished); forumTask.setUrl(categories.get(parentPosition).getCategoryURL()); forumTask.execute(); } @@ -135,16 +141,12 @@ public class ForumFragment extends BaseFragment { swipeRefreshLayout = rootView.findViewById(R.id.swiperefresh); swipeRefreshLayout.setProgressBackgroundColorSchemeResource(R.color.primary); swipeRefreshLayout.setColorSchemeResources(R.color.accent); - swipeRefreshLayout.setOnRefreshListener( - new SwipeRefreshLayout.OnRefreshListener() { - @Override - public void onRefresh() { - if (forumTask != null && forumTask.getStatus() != AsyncTask.Status.RUNNING) { - forumTask = new ForumTask(); - forumTask.execute(SessionManager.indexUrl.toString()); - } + swipeRefreshLayout.setOnRefreshListener(() -> { + if (forumTask != null && forumTask.getStatus() != AsyncTask.Status.RUNNING) { + forumTask = new ForumTask(ForumFragment.this::onForumTaskStarted, ForumFragment.this::onForumTaskFinished); + //forumTask.execute(SessionManager.indexUrl.toString()); + forumTask.execute(); } - } ); @@ -163,33 +165,38 @@ public class ForumFragment extends BaseFragment { void onForumFragmentInteraction(Board board); } - //---------------------------------------ASYNC TASK----------------------------------- + public void onForumTaskStarted() { + progressBar.setVisibility(ProgressBar.VISIBLE); + } - private class ForumTask extends ParseTask { - private HttpUrl forumUrl = SessionManager.forumUrl; //may change upon collapse/expand + public void onForumTaskFinished(int resultCode, ArrayList fetchedCategories) { + if (resultCode == Parcel.ResultCode.SUCCESSFUL) { + categories.clear(); + categories.addAll(fetchedCategories); + forumAdapter.notifyParentDataSetChanged(false); + } else if (resultCode == Parcel.ResultCode.NETWORK_ERROR) { + Toast.makeText(BaseApplication.getInstance().getApplicationContext(), "Network error", Toast.LENGTH_SHORT).show(); + } - private final List fetchedCategories; + progressBar.setVisibility(ProgressBar.INVISIBLE); + swipeRefreshLayout.setRefreshing(false); + } - ForumTask() { - fetchedCategories = new ArrayList<>(); - } + //---------------------------------------ASYNC TASK----------------------------------- - protected void onPreExecute() { - progressBar.setVisibility(ProgressBar.VISIBLE); - } + private class ForumTask extends NewParseTask> { + private HttpUrl forumUrl = SessionManager.forumUrl; //may change upon collapse/expand - @Override - protected Request prepareRequest(String... params) { - return new Request.Builder() - .url(forumUrl) - .build(); + public ForumTask(OnParseTaskStartedListener onParseTaskStartedListener, + OnParseTaskFinishedListener> onParseTaskFinishedListener) { + super(onParseTaskStartedListener, onParseTaskFinishedListener); } - @Override - public void parse(Document document) throws ParseException { + protected ArrayList parse(Document document) throws ParseException { Elements categoryBlocks = document.select(".tborder:not([style])>table[cellpadding=5]"); if (categoryBlocks.size() != 0) { + ArrayList fetchedCategories = new ArrayList<>(); for (Element categoryBlock : categoryBlocks) { Element categoryElement = categoryBlock.select("td[colspan=2]>[name]").first(); String categoryUrl = categoryElement.attr("href"); @@ -207,24 +214,26 @@ public class ForumFragment extends BaseFragment { fetchedCategories.add(category); } - categories.clear(); - categories.addAll(fetchedCategories); - fetchedCategories.clear(); + return fetchedCategories; } else throw new ParseException("Parsing failed"); } @Override - protected void postExecution(ParseTask.ResultCode result) { - if (result == ResultCode.SUCCESS) - forumAdapter.notifyParentDataSetChanged(false); + protected Response sendRequest(OkHttpClient client, String... input) throws IOException { + Request request = new Request.Builder() + .url(forumUrl) + .build(); + return client.newCall(request).execute(); + } - progressBar.setVisibility(ProgressBar.INVISIBLE); - swipeRefreshLayout.setRefreshing(false); + @Override + protected int getResultCode(Response response, ArrayList data) { + return Parcel.ResultCode.SUCCESSFUL; } - public void setUrl(String string) //TODO delete and simplify e.g. in prepareRequest possible? - { + //TODO delete and simplify e.g. in prepareRequest possible? + public void setUrl(String string) { forumUrl = HttpUrl.parse(string); } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java index aee35e37..31b3421d 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java @@ -10,23 +10,30 @@ import android.view.View; import android.view.ViewGroup; import android.widget.ProgressBar; import android.widget.RelativeLayout; +import android.widget.Toast; import org.jsoup.nodes.Document; import org.jsoup.select.Elements; +import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import gr.thmmy.mthmmy.R; +import gr.thmmy.mthmmy.base.BaseApplication; import gr.thmmy.mthmmy.base.BaseFragment; import gr.thmmy.mthmmy.model.TopicSummary; import gr.thmmy.mthmmy.session.SessionManager; import gr.thmmy.mthmmy.utils.CustomRecyclerView; +import gr.thmmy.mthmmy.utils.parsing.NewParseTask; +import gr.thmmy.mthmmy.utils.parsing.Parcel; import gr.thmmy.mthmmy.utils.parsing.ParseException; -import gr.thmmy.mthmmy.utils.parsing.ParseTask; import me.zhanghai.android.materialprogressbar.MaterialProgressBar; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; import timber.log.Timber; @@ -79,7 +86,7 @@ public class RecentFragment extends BaseFragment { public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); if (topicSummaries.isEmpty()) { - recentTask = new RecentTask(); + recentTask = new RecentTask(this::onRecentTaskStarted, this::onRecentTaskFinished); recentTask.execute(SessionManager.indexUrl.toString()); } @@ -109,16 +116,11 @@ public class RecentFragment extends BaseFragment { swipeRefreshLayout = rootView.findViewById(R.id.swiperefresh); swipeRefreshLayout.setProgressBackgroundColorSchemeResource(R.color.primary); swipeRefreshLayout.setColorSchemeResources(R.color.accent); - swipeRefreshLayout.setOnRefreshListener( - new SwipeRefreshLayout.OnRefreshListener() { - @Override - public void onRefresh() { - if (recentTask != null && recentTask.getStatus() != AsyncTask.Status.RUNNING) { - recentTask = new RecentTask(); - recentTask.execute(SessionManager.indexUrl.toString()); - } + swipeRefreshLayout.setOnRefreshListener(() -> { + if (recentTask != null && recentTask.getStatus() != AsyncTask.Status.RUNNING) { + recentTask = new RecentTask(this::onRecentTaskStarted, this::onRecentTaskFinished); + recentTask.execute(SessionManager.indexUrl.toString()); } - } ); } @@ -138,18 +140,34 @@ public class RecentFragment extends BaseFragment { void onRecentFragmentInteraction(TopicSummary topicSummary); } + private void onRecentTaskStarted() { + progressBar.setVisibility(ProgressBar.VISIBLE); + } + + private void onRecentTaskFinished(int resultCode, ArrayList fetchedRecent) { + if (resultCode == Parcel.ResultCode.SUCCESSFUL) { + topicSummaries.clear(); + topicSummaries.addAll(fetchedRecent); + recentAdapter.notifyDataSetChanged(); + } else if (resultCode == Parcel.ResultCode.NETWORK_ERROR) { + Toast.makeText(BaseApplication.getInstance().getApplicationContext(), "Network error", Toast.LENGTH_SHORT).show(); + } + + progressBar.setVisibility(ProgressBar.INVISIBLE); + swipeRefreshLayout.setRefreshing(false); + } + //---------------------------------------ASYNC TASK----------------------------------- - private class RecentTask extends ParseTask { - private List fetchedRecent; + private class RecentTask extends NewParseTask> { - @Override - protected void onPreExecute() { - progressBar.setVisibility(ProgressBar.VISIBLE); - fetchedRecent = new ArrayList<>(); + public RecentTask(OnParseTaskStartedListener onParseTaskStartedListener, + OnParseTaskFinishedListener> onParseTaskFinishedListener) { + super(onParseTaskStartedListener, onParseTaskFinishedListener); } @Override - public void parse(Document document) throws ParseException { + protected ArrayList parse(Document document) throws ParseException { + ArrayList fetchedRecent = new ArrayList<>(); Elements recent = document.select("#block8 :first-child div"); if (!recent.isEmpty()) { for (int i = 0; i < recent.size(); i += 3) { @@ -174,7 +192,7 @@ public class RecentFragment extends BaseFragment { dateTime.contains(" πμ") || dateTime.contains(" μμ")) { dateTime = dateTime.replaceAll(":[0-5][0-9] ", " "); } else { - dateTime=dateTime.substring(0,dateTime.lastIndexOf(":")); + dateTime = dateTime.substring(0, dateTime.lastIndexOf(":")); } if (!dateTime.contains(",")) { dateTime = dateTime.replaceAll(".+? ([0-9])", "$1"); @@ -184,22 +202,14 @@ public class RecentFragment extends BaseFragment { fetchedRecent.add(new TopicSummary(link, title, lastUser, dateTime)); } - return; + return fetchedRecent; } throw new ParseException("Parsing failed"); } @Override - protected void postExecution(ParseTask.ResultCode result) { - if (result == ResultCode.SUCCESS) - { - topicSummaries.clear(); - topicSummaries.addAll(fetchedRecent); - recentAdapter.notifyDataSetChanged(); - } - - progressBar.setVisibility(ProgressBar.INVISIBLE); - swipeRefreshLayout.setRefreshing(false); + protected int getResultCode(Response response, ArrayList data) { + return Parcel.ResultCode.SUCCESSFUL; } } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java index b18874d5..0c4f5417 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java @@ -22,13 +22,19 @@ import java.util.ArrayList; import java.util.List; import gr.thmmy.mthmmy.R; +import gr.thmmy.mthmmy.base.BaseApplication; import gr.thmmy.mthmmy.base.BaseFragment; import gr.thmmy.mthmmy.model.TopicSummary; import gr.thmmy.mthmmy.session.SessionManager; import gr.thmmy.mthmmy.utils.CustomRecyclerView; +import gr.thmmy.mthmmy.utils.parsing.NewParseTask; +import gr.thmmy.mthmmy.utils.parsing.Parcel; +import gr.thmmy.mthmmy.utils.parsing.ParseException; import gr.thmmy.mthmmy.utils.parsing.ParseTask; import me.zhanghai.android.materialprogressbar.MaterialProgressBar; +import okhttp3.OkHttpClient; import okhttp3.Request; +import okhttp3.Response; import timber.log.Timber; /** @@ -83,7 +89,7 @@ public class UnreadFragment extends BaseFragment { public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); if (topicSummaries.isEmpty()) { - unreadTask = new UnreadTask(); + unreadTask = new UnreadTask(this::onUnreadTaskStarted, this::onUnreadTaskFinished); assert SessionManager.unreadUrl != null; unreadTask.execute(SessionManager.unreadUrl.toString()); } @@ -126,7 +132,7 @@ public class UnreadFragment extends BaseFragment { topicSummaries.clear(); numberOfPages = 0; loadedPages = 0; - unreadTask = new UnreadTask(); + unreadTask = new UnreadTask(this::onUnreadTaskStarted, this::onUnreadTaskFinished); assert SessionManager.unreadUrl != null; unreadTask.execute(SessionManager.unreadUrl.toString()); } @@ -151,13 +157,42 @@ public class UnreadFragment extends BaseFragment { } //---------------------------------------ASYNC TASK----------------------------------- - private class UnreadTask extends ParseTask { - protected void onPreExecute() { - progressBar.setVisibility(ProgressBar.VISIBLE); + + private void onUnreadTaskStarted() { + progressBar.setVisibility(ProgressBar.VISIBLE); + } + + private void onUnreadTaskFinished(int resultCode, Void data) { + if (resultCode == Parcel.ResultCode.SUCCESSFUL) { + unreadAdapter.notifyDataSetChanged(); + + ++loadedPages; + if (loadedPages < numberOfPages) { + unreadTask = new UnreadTask(this::onUnreadTaskStarted, this::onUnreadTaskFinished); + assert SessionManager.unreadUrl != null; + unreadTask.execute(SessionManager.unreadUrl.toString() + ";start=" + loadedPages * 20); + } + else { + progressBar.setVisibility(ProgressBar.INVISIBLE); + swipeRefreshLayout.setRefreshing(false); + } + } + else{ + progressBar.setVisibility(ProgressBar.INVISIBLE); + swipeRefreshLayout.setRefreshing(false); + if (resultCode == Parcel.ResultCode.NETWORK_ERROR) + Toast.makeText(BaseApplication.getInstance().getApplicationContext(), "Network error", Toast.LENGTH_SHORT).show(); + } + } + + private class UnreadTask extends NewParseTask { + + UnreadTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskFinishedListener onParseTaskFinishedListener) { + super(onParseTaskStartedListener, onParseTaskFinishedListener); } @Override - public void parse(Document document) { + protected Void parse(Document document) throws ParseException { Elements unread = document.select("table.bordercolor[cellspacing=1] tr:not(.titlebg)"); if (!unread.isEmpty()) { //topicSummaries.clear(); @@ -217,28 +252,12 @@ public class UnreadFragment extends BaseFragment { } topicSummaries.add(new TopicSummary(null, null, null, message)); } + return null; } @Override - protected void postExecution(ParseTask.ResultCode result) { - if (result == ResultCode.SUCCESS) { - unreadAdapter.notifyDataSetChanged(); - - ++loadedPages; - if (loadedPages < numberOfPages) { - unreadTask = new UnreadTask(); - assert SessionManager.unreadUrl != null; - unreadTask.execute(SessionManager.unreadUrl.toString() + ";start=" + loadedPages * 20); - } - else { - progressBar.setVisibility(ProgressBar.INVISIBLE); - swipeRefreshLayout.setRefreshing(false); - } - } - else{ - progressBar.setVisibility(ProgressBar.INVISIBLE); - swipeRefreshLayout.setRefreshing(false); - } + protected int getResultCode(Response response, Void data) { + return Parcel.ResultCode.SUCCESSFUL; } } @@ -282,7 +301,7 @@ public class UnreadFragment extends BaseFragment { , "Fatal error!\n Task aborted...", Toast.LENGTH_LONG).show(); } else { if (unreadTask != null && unreadTask.getStatus() != AsyncTask.Status.RUNNING) { - unreadTask = new UnreadTask(); + unreadTask = new UnreadTask(UnreadFragment.this::onUnreadTaskStarted, UnreadFragment.this::onUnreadTaskFinished); assert SessionManager.unreadUrl != null; unreadTask.execute(SessionManager.unreadUrl.toString()); } 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 3507d130..c638f284 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,6 +7,7 @@ import java.io.IOException; import gr.thmmy.mthmmy.base.BaseApplication; import okhttp3.OkHttpClient; +import okhttp3.Request; import okhttp3.Response; import timber.log.Timber; @@ -67,7 +68,13 @@ public abstract class NetworkTask extends ExternalAsyncTask super.onPostExecute(tParcel); } - protected abstract Response sendRequest(OkHttpClient client, String... input) throws IOException; + protected Response sendRequest(OkHttpClient client, String... input) throws IOException { + String url = input[0]; + Request request = new Request.Builder() + .url(url) + .build(); + return client.newCall(request).execute(); + } protected abstract T performTask(Document document); From 0107f36d59e8fd0e67836648fe73a32b01610fe0 Mon Sep 17 00:00:00 2001 From: Thodoris1999 Date: Wed, 12 Sep 2018 13:36:26 +0300 Subject: [PATCH 11/11] externalize result codes, change project structure --- .../activities/main/forum/ForumFragment.java | 8 +-- .../main/recent/RecentFragment.java | 11 ++-- .../main/unread/UnreadFragment.java | 10 ++-- .../activities/topic/TopicActivity.java | 4 +- .../activities/topic/tasks/DeleteTask.java | 6 +-- .../{parsing => }/ExternalAsyncTask.java | 2 +- .../mthmmy/utils/NetworkResultCodes.java | 32 ++++++++++++ .../utils/{parsing => }/NetworkTask.java | 13 ++--- .../java/gr/thmmy/mthmmy/utils/Parcel.java | 20 ++++++++ .../mthmmy/utils/parsing/NewParseTask.java | 2 + .../gr/thmmy/mthmmy/utils/parsing/Parcel.java | 51 ------------------- 11 files changed, 79 insertions(+), 80 deletions(-) rename app/src/main/java/gr/thmmy/mthmmy/utils/{parsing => }/ExternalAsyncTask.java (98%) create mode 100644 app/src/main/java/gr/thmmy/mthmmy/utils/NetworkResultCodes.java rename app/src/main/java/gr/thmmy/mthmmy/utils/{parsing => }/NetworkTask.java (87%) create mode 100644 app/src/main/java/gr/thmmy/mthmmy/utils/Parcel.java delete mode 100644 app/src/main/java/gr/thmmy/mthmmy/utils/parsing/Parcel.java diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/forum/ForumFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/forum/ForumFragment.java index f98dcd60..ceb41e13 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/forum/ForumFragment.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/main/forum/ForumFragment.java @@ -30,8 +30,8 @@ import gr.thmmy.mthmmy.model.Board; import gr.thmmy.mthmmy.model.Category; import gr.thmmy.mthmmy.session.SessionManager; import gr.thmmy.mthmmy.utils.CustomRecyclerView; +import gr.thmmy.mthmmy.utils.NetworkResultCodes; import gr.thmmy.mthmmy.utils.parsing.NewParseTask; -import gr.thmmy.mthmmy.utils.parsing.Parcel; import gr.thmmy.mthmmy.utils.parsing.ParseException; import me.zhanghai.android.materialprogressbar.MaterialProgressBar; import okhttp3.HttpUrl; @@ -170,11 +170,11 @@ public class ForumFragment extends BaseFragment { } public void onForumTaskFinished(int resultCode, ArrayList fetchedCategories) { - if (resultCode == Parcel.ResultCode.SUCCESSFUL) { + if (resultCode == NetworkResultCodes.SUCCESSFUL) { categories.clear(); categories.addAll(fetchedCategories); forumAdapter.notifyParentDataSetChanged(false); - } else if (resultCode == Parcel.ResultCode.NETWORK_ERROR) { + } else if (resultCode == NetworkResultCodes.NETWORK_ERROR) { Toast.makeText(BaseApplication.getInstance().getApplicationContext(), "Network error", Toast.LENGTH_SHORT).show(); } @@ -229,7 +229,7 @@ public class ForumFragment extends BaseFragment { @Override protected int getResultCode(Response response, ArrayList data) { - return Parcel.ResultCode.SUCCESSFUL; + return NetworkResultCodes.SUCCESSFUL; } //TODO delete and simplify e.g. in prepareRequest possible? diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java index 31b3421d..98ad7f69 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java @@ -15,7 +15,6 @@ import android.widget.Toast; import org.jsoup.nodes.Document; import org.jsoup.select.Elements; -import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; @@ -27,12 +26,10 @@ import gr.thmmy.mthmmy.base.BaseFragment; import gr.thmmy.mthmmy.model.TopicSummary; import gr.thmmy.mthmmy.session.SessionManager; import gr.thmmy.mthmmy.utils.CustomRecyclerView; +import gr.thmmy.mthmmy.utils.NetworkResultCodes; import gr.thmmy.mthmmy.utils.parsing.NewParseTask; -import gr.thmmy.mthmmy.utils.parsing.Parcel; import gr.thmmy.mthmmy.utils.parsing.ParseException; import me.zhanghai.android.materialprogressbar.MaterialProgressBar; -import okhttp3.OkHttpClient; -import okhttp3.Request; import okhttp3.Response; import timber.log.Timber; @@ -145,11 +142,11 @@ public class RecentFragment extends BaseFragment { } private void onRecentTaskFinished(int resultCode, ArrayList fetchedRecent) { - if (resultCode == Parcel.ResultCode.SUCCESSFUL) { + if (resultCode == NetworkResultCodes.SUCCESSFUL) { topicSummaries.clear(); topicSummaries.addAll(fetchedRecent); recentAdapter.notifyDataSetChanged(); - } else if (resultCode == Parcel.ResultCode.NETWORK_ERROR) { + } else if (resultCode == NetworkResultCodes.NETWORK_ERROR) { Toast.makeText(BaseApplication.getInstance().getApplicationContext(), "Network error", Toast.LENGTH_SHORT).show(); } @@ -209,7 +206,7 @@ public class RecentFragment extends BaseFragment { @Override protected int getResultCode(Response response, ArrayList data) { - return Parcel.ResultCode.SUCCESSFUL; + return NetworkResultCodes.SUCCESSFUL; } } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java index 0c4f5417..0681c4af 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java @@ -27,12 +27,10 @@ import gr.thmmy.mthmmy.base.BaseFragment; import gr.thmmy.mthmmy.model.TopicSummary; import gr.thmmy.mthmmy.session.SessionManager; import gr.thmmy.mthmmy.utils.CustomRecyclerView; +import gr.thmmy.mthmmy.utils.NetworkResultCodes; import gr.thmmy.mthmmy.utils.parsing.NewParseTask; -import gr.thmmy.mthmmy.utils.parsing.Parcel; import gr.thmmy.mthmmy.utils.parsing.ParseException; -import gr.thmmy.mthmmy.utils.parsing.ParseTask; import me.zhanghai.android.materialprogressbar.MaterialProgressBar; -import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import timber.log.Timber; @@ -163,7 +161,7 @@ public class UnreadFragment extends BaseFragment { } private void onUnreadTaskFinished(int resultCode, Void data) { - if (resultCode == Parcel.ResultCode.SUCCESSFUL) { + if (resultCode == NetworkResultCodes.SUCCESSFUL) { unreadAdapter.notifyDataSetChanged(); ++loadedPages; @@ -180,7 +178,7 @@ public class UnreadFragment extends BaseFragment { else{ progressBar.setVisibility(ProgressBar.INVISIBLE); swipeRefreshLayout.setRefreshing(false); - if (resultCode == Parcel.ResultCode.NETWORK_ERROR) + if (resultCode == NetworkResultCodes.NETWORK_ERROR) Toast.makeText(BaseApplication.getInstance().getApplicationContext(), "Network error", Toast.LENGTH_SHORT).show(); } } @@ -257,7 +255,7 @@ public class UnreadFragment extends BaseFragment { @Override protected int getResultCode(Response response, Void data) { - return Parcel.ResultCode.SUCCESSFUL; + return NetworkResultCodes.SUCCESSFUL; } } 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 7f8b248a..00e16471 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,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.NetworkResultCodes; import gr.thmmy.mthmmy.utils.parsing.ParseHelpers; import gr.thmmy.mthmmy.viewmodel.TopicViewModel; import me.zhanghai.android.materialprogressbar.MaterialProgressBar; @@ -500,7 +500,7 @@ public class TopicActivity extends BaseActivity implements TopicAdapter.OnPostFo viewModel.setDeleteTaskStartedListener(() -> progressBar.setVisibility(ProgressBar.VISIBLE)); viewModel.setDeleteTaskFinishedListener((resultCode, data) -> { progressBar.setVisibility(ProgressBar.GONE); - if (resultCode == Parcel.ResultCode.SUCCESSFUL) { + if (resultCode == NetworkResultCodes.SUCCESSFUL) { Timber.i("Post deleted successfully"); viewModel.reloadPage(); } else { 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 40a2df5b..41563ae5 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 @@ -4,8 +4,8 @@ import org.jsoup.nodes.Document; import java.io.IOException; -import gr.thmmy.mthmmy.utils.parsing.NetworkTask; -import gr.thmmy.mthmmy.utils.parsing.Parcel; +import gr.thmmy.mthmmy.utils.NetworkResultCodes; +import gr.thmmy.mthmmy.utils.NetworkTask; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; @@ -34,6 +34,6 @@ public class DeleteTask extends NetworkTask { @Override protected int getResultCode(Response response, Void data) { - return Parcel.ResultCode.SUCCESSFUL; + return NetworkResultCodes.SUCCESSFUL; } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ExternalAsyncTask.java b/app/src/main/java/gr/thmmy/mthmmy/utils/ExternalAsyncTask.java similarity index 98% rename from app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ExternalAsyncTask.java rename to app/src/main/java/gr/thmmy/mthmmy/utils/ExternalAsyncTask.java index 93bc4224..82b535b6 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ExternalAsyncTask.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/ExternalAsyncTask.java @@ -1,4 +1,4 @@ -package gr.thmmy.mthmmy.utils.parsing; +package gr.thmmy.mthmmy.utils; import android.os.AsyncTask; diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/NetworkResultCodes.java b/app/src/main/java/gr/thmmy/mthmmy/utils/NetworkResultCodes.java new file mode 100644 index 00000000..90cd8771 --- /dev/null +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/NetworkResultCodes.java @@ -0,0 +1,32 @@ +package gr.thmmy.mthmmy.utils; + +public class NetworkResultCodes { + /** + * 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; +} diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/NetworkTask.java b/app/src/main/java/gr/thmmy/mthmmy/utils/NetworkTask.java similarity index 87% rename from app/src/main/java/gr/thmmy/mthmmy/utils/parsing/NetworkTask.java rename to app/src/main/java/gr/thmmy/mthmmy/utils/NetworkTask.java index c638f284..6793120a 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/NetworkTask.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/NetworkTask.java @@ -1,4 +1,4 @@ -package gr.thmmy.mthmmy.utils.parsing; +package gr.thmmy.mthmmy.utils; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; @@ -6,6 +6,7 @@ import org.jsoup.nodes.Document; import java.io.IOException; import gr.thmmy.mthmmy.base.BaseApplication; +import gr.thmmy.mthmmy.utils.parsing.ParseException; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; @@ -35,17 +36,17 @@ public abstract class NetworkTask extends ExternalAsyncTask 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); + return new Parcel<>(NetworkResultCodes.NETWORK_ERROR, null); } String responseBodyString; try { responseBodyString = response.body().string(); } catch (NullPointerException npe) { 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); + return new Parcel<>(NetworkResultCodes.NETWORK_ERROR, null); } catch (IOException e) { Timber.e(e, "Error getting response body string"); - return new Parcel<>(Parcel.ResultCode.NETWORK_ERROR, null); + return new Parcel<>(NetworkResultCodes.NETWORK_ERROR, null); } try { T data = performTask(Jsoup.parse(responseBodyString)); @@ -53,10 +54,10 @@ public abstract class NetworkTask extends ExternalAsyncTask return new Parcel<>(resultCode, data); } catch (ParseException pe) { Timber.e(pe); - return new Parcel<>(Parcel.ResultCode.PARSE_ERROR, null); + return new Parcel<>(NetworkResultCodes.PARSE_ERROR, null); } catch (Exception e) { Timber.e(e); - return new Parcel<>(Parcel.ResultCode.PERFORM_TASK_ERROR, null); + return new Parcel<>(NetworkResultCodes.PERFORM_TASK_ERROR, null); } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/Parcel.java b/app/src/main/java/gr/thmmy/mthmmy/utils/Parcel.java new file mode 100644 index 00000000..83e65285 --- /dev/null +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/Parcel.java @@ -0,0 +1,20 @@ +package gr.thmmy.mthmmy.utils; + +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; + } +} 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 c9f6ea2e..51749f2a 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 @@ -2,6 +2,8 @@ package gr.thmmy.mthmmy.utils.parsing; import org.jsoup.nodes.Document; +import gr.thmmy.mthmmy.utils.NetworkTask; + public abstract class NewParseTask extends NetworkTask { public NewParseTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskCancelledListener onParseTaskCancelledListener, 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 deleted file mode 100644 index 6e1e0f7f..00000000 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/Parcel.java +++ /dev/null @@ -1,51 +0,0 @@ -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; - } -}