Browse Source

externalize result codes, change project structure

pull/54/head
Thodoris1999 6 years ago
parent
commit
0107f36d59
  1. 8
      app/src/main/java/gr/thmmy/mthmmy/activities/main/forum/ForumFragment.java
  2. 11
      app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java
  3. 10
      app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java
  4. 4
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java
  5. 6
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/DeleteTask.java
  6. 2
      app/src/main/java/gr/thmmy/mthmmy/utils/ExternalAsyncTask.java
  7. 32
      app/src/main/java/gr/thmmy/mthmmy/utils/NetworkResultCodes.java
  8. 13
      app/src/main/java/gr/thmmy/mthmmy/utils/NetworkTask.java
  9. 20
      app/src/main/java/gr/thmmy/mthmmy/utils/Parcel.java
  10. 2
      app/src/main/java/gr/thmmy/mthmmy/utils/parsing/NewParseTask.java
  11. 51
      app/src/main/java/gr/thmmy/mthmmy/utils/parsing/Parcel.java

8
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<Category> 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<Category> data) {
return Parcel.ResultCode.SUCCESSFUL;
return NetworkResultCodes.SUCCESSFUL;
}
//TODO delete and simplify e.g. in prepareRequest possible?

11
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<TopicSummary> 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<TopicSummary> data) {
return Parcel.ResultCode.SUCCESSFUL;
return NetworkResultCodes.SUCCESSFUL;
}
}
}

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

4
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 {

6
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<Void> {
@Override
protected int getResultCode(Response response, Void data) {
return Parcel.ResultCode.SUCCESSFUL;
return NetworkResultCodes.SUCCESSFUL;
}
}

2
app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ExternalAsyncTask.java → 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;

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

13
app/src/main/java/gr/thmmy/mthmmy/utils/parsing/NetworkTask.java → 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<T> extends ExternalAsyncTask<String, Parcel<T>
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<T> extends ExternalAsyncTask<String, Parcel<T>
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);
}
}

20
app/src/main/java/gr/thmmy/mthmmy/utils/Parcel.java

@ -0,0 +1,20 @@
package gr.thmmy.mthmmy.utils;
public class Parcel<T> {
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;
}
}

2
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<T> extends NetworkTask<T> {
public NewParseTask(OnParseTaskStartedListener onParseTaskStartedListener, OnParseTaskCancelledListener onParseTaskCancelledListener,

51
app/src/main/java/gr/thmmy/mthmmy/utils/parsing/Parcel.java

@ -1,51 +0,0 @@
package gr.thmmy.mthmmy.utils.parsing;
public class Parcel<T> {
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;
}
}
Loading…
Cancel
Save