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; - } -}