Added an advanced error response system.

This commit is contained in:
ChronosX88 2019-01-05 15:08:40 +04:00
parent 259006d354
commit 9fc86b219b
No known key found for this signature in database
GPG Key ID: 8F92E090A87804AA
9 changed files with 247 additions and 115 deletions

View File

@ -1,11 +1,8 @@
package ru.volgorobot.vrcatalog;
import android.widget.ImageView;
import com.github.msteinbeck.sig4j.signal.Signal1;
import java.util.ArrayList;
import ru.volgorobot.vrcatalog.additional.NetworkErrorException;
import ru.volgorobot.vrcatalog.model.FirstLevelModel;
import ru.volgorobot.vrcatalog.model.SecondLevelModel;
import ru.volgorobot.vrcatalog.model.ThirdLevelModel;
@ -20,13 +17,13 @@ public interface MainContract {
interface Presenter {
void getFirstLevel();
void failureAnswerSlot(int errorCode);
}
interface MainModel {
Signal1<Integer> errorSignal = new Signal1<>();
ArrayList<FirstLevelModel> getFirstLevel();
ArrayList<SecondLevelModel> getSecondLevelByParentID(int parentID);
ArrayList<ThirdLevelModel> getThirdLevelByParentID(int parentID);
ArrayList<FirstLevelModel> getFirstLevel() throws NetworkErrorException, NullPointerException;
ArrayList<SecondLevelModel> getSecondLevelByParentID(int parentID) throws NetworkErrorException, NullPointerException;
ArrayList<ThirdLevelModel> getThirdLevelByParentID(int parentID) throws NetworkErrorException, NullPointerException;
void reinitializeApi() throws IllegalStateException;
boolean getApiState();
}
}

View File

@ -5,6 +5,8 @@ import android.os.AsyncTask;
import java.util.ArrayList;
import ru.volgorobot.vrcatalog.additional.NetworkErrorException;
import ru.volgorobot.vrcatalog.additional.ResultWithErrorCode;
import ru.volgorobot.vrcatalog.model.CoreModel;
import ru.volgorobot.vrcatalog.model.FirstLevelModel;
@ -14,46 +16,53 @@ public class MainPresenter implements MainContract.Presenter {
public MainPresenter(MainContract.MainActivityView mainActivityView, Context context) {
this.mView = mainActivityView;
try {
this.coreModel = new CoreModel(context);
} catch (IllegalStateException e) {
mView.onFailureAnswer(3);
}
coreModel.errorSignal.connect(this::failureAnswerSlot);
this.coreModel = new CoreModel(context);
}
@Override
public void getFirstLevel() {
new AsyncTask<Void, Void, ArrayList<FirstLevelModel>>() {
try {
coreModel.reinitializeApi();
} catch (IllegalStateException e) {
mView.onFailureAnswer(3);
return;
}
new AsyncTask<Void, Void, ResultWithErrorCode<ArrayList<FirstLevelModel>>>() {
@Override
protected ArrayList<FirstLevelModel> doInBackground(Void... voids) {
ArrayList<FirstLevelModel> firstLevelModels = coreModel.getFirstLevel();
return firstLevelModels;
protected ResultWithErrorCode<ArrayList<FirstLevelModel>> doInBackground(Void... voids) {
ArrayList<FirstLevelModel> firstLevelModels = null;
try {
firstLevelModels = coreModel.getFirstLevel();
} catch (NetworkErrorException networkErrorException) {
return new ResultWithErrorCode<>(firstLevelModels, networkErrorException.getErrorCode());
} catch (NullPointerException nullPointerException) {
return new ResultWithErrorCode<>(firstLevelModels, 3);
}
return new ResultWithErrorCode<>(firstLevelModels, 0);
}
@Override
protected void onPostExecute(ArrayList<FirstLevelModel> firstLevelModels) {
mView.buildFirstLevelTree(firstLevelModels);
protected void onPostExecute(ResultWithErrorCode<ArrayList<FirstLevelModel>> result) {
switch (result.getErrorCode()) {
case 0: {
mView.buildFirstLevelTree(result.getData());
break;
}
case 1: {
mView.onFailureAnswer(1);
break;
}
case 2: {
mView.onFailureAnswer(2);
break;
}
case 3: {
mView.onFailureAnswer(3);
break;
}
}
mView.swipeLayoutSetRefreshing(false);
}
}.execute();
mView.swipeLayoutSetRefreshing(false);
}
@Override
public void failureAnswerSlot(int errorCode) {
switch (errorCode) {
case 1: {
mView.onFailureAnswer(1);
break;
}
case 2: {
mView.onFailureAnswer(2);
break;
}
case 3: {
mView.onFailureAnswer(3);
break;
}
}
}
}

View File

@ -0,0 +1,13 @@
package ru.volgorobot.vrcatalog.additional;
public class NetworkErrorException extends Exception {
private int errorCode;
public NetworkErrorException(int errorCode) {
this.errorCode = errorCode;
}
public int getErrorCode() {
return errorCode;
}
}

View File

@ -0,0 +1,28 @@
package ru.volgorobot.vrcatalog.additional;
public class ResultWithErrorCode<T> {
private T data;
private int errorCode;
public ResultWithErrorCode(T data, int errorCode) {
this.data = data;
this.errorCode = errorCode;
}
public T getData() {
return data;
}
public int getErrorCode() {
return errorCode;
}
public void setData(T data) {
this.data = data;
}
public void setErrorCode(int errorCode) {
this.errorCode = errorCode;
}
}

View File

@ -11,18 +11,88 @@ import java.util.List;
import retrofit2.Response;
import ru.volgorobot.vrcatalog.MainContract;
import ru.volgorobot.vrcatalog.additional.NetworkErrorException;
import ru.volgorobot.vrcatalog.api.Controller;
import ru.volgorobot.vrcatalog.api.VRApi;
public class CoreModel implements MainContract.MainModel {
private Context context;
private VRApi vrApi;
private String baseURL;
public final Signal1<Integer> errorSignal = new Signal1<>();
public CoreModel(Context context) throws IllegalStateException {
public CoreModel(Context context) {
this.context = context;
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
String baseURL = sharedPreferences.getString("addressOfServer", "");
this.baseURL = sharedPreferences.getString("addressOfServer", "");
try {
vrApi = Controller.getApi(baseURL);
} catch(IllegalStateException e) {
// Nothing
}
}
@Override
public ArrayList<FirstLevelModel> getFirstLevel() throws NetworkErrorException, NullPointerException {
if(vrApi == null) {
throw new NullPointerException();
}
final ArrayList<FirstLevelModel> firstLevelModels = new ArrayList<>();
try {
Response<List<FirstLevelModel>> response = vrApi.getFirstLevel().execute();
if(response.isSuccessful()) {
firstLevelModels.addAll(response.body());
} else {
throw new NetworkErrorException(2);
}
} catch (IOException e) {
throw new NetworkErrorException(1);
}
return firstLevelModels;
}
@Override
public ArrayList<SecondLevelModel> getSecondLevelByParentID(int parentID) throws NetworkErrorException, NullPointerException {
if(vrApi == null) {
throw new NullPointerException();
}
final ArrayList<SecondLevelModel> secondLevelModels = new ArrayList<>();
try {
Response<List<SecondLevelModel>> response = vrApi.getSecondLevel(parentID).execute();
if(response.isSuccessful()) {
secondLevelModels.addAll(response.body());
} else {
throw new NetworkErrorException(2);
}
} catch (IOException e) {
throw new NetworkErrorException(1);
}
return secondLevelModels;
}
@Override
public ArrayList<ThirdLevelModel> getThirdLevelByParentID(int parentID) throws NetworkErrorException, NullPointerException {
if(vrApi == null) {
throw new NullPointerException();
}
final ArrayList<ThirdLevelModel> thirdLevelModels = new ArrayList<>();
try {
Response<List<ThirdLevelModel>> response = vrApi.getDetails(parentID).execute();
if(response.isSuccessful()) {
thirdLevelModels.addAll(response.body());
} else {
throw new NetworkErrorException(2);
}
} catch (IOException e) {
throw new NetworkErrorException(1);
}
return thirdLevelModels;
}
@Override
public void reinitializeApi() throws IllegalStateException {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
this.baseURL = sharedPreferences.getString("addressOfServer", "");
try {
vrApi = Controller.getApi(baseURL);
} catch(IllegalStateException e) {
@ -31,50 +101,11 @@ public class CoreModel implements MainContract.MainModel {
}
@Override
public ArrayList<FirstLevelModel> getFirstLevel() {
final ArrayList<FirstLevelModel> firstLevelModels = new ArrayList<>();
try {
Response<List<FirstLevelModel>> response = vrApi.getFirstLevel().execute();
if(response.isSuccessful()) {
firstLevelModels.addAll(response.body());
} else {
errorSignal.emit(2);
}
} catch (IOException e) {
errorSignal.emit(1);
public boolean getApiState() {
if(vrApi != null) {
return true;
} else {
return false;
}
return firstLevelModels;
}
@Override
public ArrayList<SecondLevelModel> getSecondLevelByParentID(int parentID) {
final ArrayList<SecondLevelModel> secondLevelModels = new ArrayList<>();
try {
Response<List<SecondLevelModel>> response = vrApi.getSecondLevel(parentID).execute();
if(response.isSuccessful()) {
secondLevelModels.addAll(response.body());
} else {
errorSignal.emit(2);
}
} catch (IOException e) {
errorSignal.emit(1);
}
return secondLevelModels;
}
@Override
public ArrayList<ThirdLevelModel> getThirdLevelByParentID(int parentID) {
final ArrayList<ThirdLevelModel> thirdLevelModels = new ArrayList<>();
try {
Response<List<ThirdLevelModel>> response = vrApi.getDetails(parentID).execute();
if(response.isSuccessful()) {
thirdLevelModels.addAll(response.body());
} else {
errorSignal.emit(2);
}
} catch (IOException e) {
errorSignal.emit(1);
}
return thirdLevelModels;
}
}

View File

@ -12,6 +12,8 @@ import me.texy.treeview.TreeNode;
import me.texy.treeview.base.BaseNodeViewBinder;
import ru.volgorobot.vrcatalog.MainContract;
import ru.volgorobot.vrcatalog.R;
import ru.volgorobot.vrcatalog.additional.NetworkErrorException;
import ru.volgorobot.vrcatalog.additional.ResultWithErrorCode;
import ru.volgorobot.vrcatalog.model.CoreModel;
import ru.volgorobot.vrcatalog.model.FirstLevelModel;
import ru.volgorobot.vrcatalog.model.SecondLevelModel;
@ -45,7 +47,7 @@ public class FirstLevelNodeViewBinder extends BaseNodeViewBinder {
public void onNodeToggled(TreeNode treeNode, boolean expand) {
if(expand) {
FirstLevelModel firstLevelModel = (FirstLevelModel) treeNode.getValue();
new AsyncTask<Void, Void, ArrayList<SecondLevelModel>>() {
new AsyncTask<Void, Void, ResultWithErrorCode<ArrayList<SecondLevelModel>>>() {
@Override
protected void onPreExecute() {
super.onPreExecute();
@ -53,24 +55,49 @@ public class FirstLevelNodeViewBinder extends BaseNodeViewBinder {
}
@Override
protected ArrayList<SecondLevelModel> doInBackground(Void... voids) {
ArrayList<SecondLevelModel> secondLevelModels = coreModel.getSecondLevelByParentID(firstLevelModel.getID());
return secondLevelModels;
protected ResultWithErrorCode<ArrayList<SecondLevelModel>> doInBackground(Void... voids) {
ArrayList<SecondLevelModel> secondLevelModels = null;
try {
secondLevelModels = coreModel.getSecondLevelByParentID(firstLevelModel.getID());
} catch (NetworkErrorException networkErrorException) {
return new ResultWithErrorCode<>(secondLevelModels, networkErrorException.getErrorCode());
} catch (NullPointerException nullPointerException) {
return new ResultWithErrorCode<>(secondLevelModels, 3);
}
return new ResultWithErrorCode<>(secondLevelModels, 0);
}
@Override
protected void onPostExecute(ArrayList<SecondLevelModel> secondLevelModels) {
super.onPostExecute(secondLevelModels);
ArrayList<TreeNode> treeNodes = new ArrayList<>();
for (int i = 0; i < secondLevelModels.size(); i++) {
TreeNode treeNode1 = new TreeNode(secondLevelModels.get(i));
treeNode1.setLevel(1);
treeNodes.add(treeNode1);
protected void onPostExecute(ResultWithErrorCode<ArrayList<SecondLevelModel>> result) {
super.onPostExecute(result);
switch (result.getErrorCode()) {
case 0: {
ArrayList<TreeNode> treeNodes = new ArrayList<>();
for (int i = 0; i < result.getData().size(); i++) {
TreeNode treeNode1 = new TreeNode(result.getData().get(i));
treeNode1.setLevel(1);
treeNodes.add(treeNode1);
}
treeNode.setChildren(treeNodes);
mView.refreshTree();
imageView.animate().rotation(90).setDuration(200).start();
break;
}
case 1: {
mView.onFailureAnswer(1);
break;
}
case 2: {
mView.onFailureAnswer(2);
break;
}
case 3: {
mView.onFailureAnswer(3);
break;
}
}
treeNode.setChildren(treeNodes);
mView.refreshTree();
mView.swipeLayoutSetRefreshing(false);
imageView.animate().rotation(90).setDuration(200).start();
}
}.execute();
} else {

View File

@ -12,6 +12,8 @@ import me.texy.treeview.TreeNode;
import me.texy.treeview.base.BaseNodeViewBinder;
import ru.volgorobot.vrcatalog.MainContract;
import ru.volgorobot.vrcatalog.R;
import ru.volgorobot.vrcatalog.additional.NetworkErrorException;
import ru.volgorobot.vrcatalog.additional.ResultWithErrorCode;
import ru.volgorobot.vrcatalog.model.CoreModel;
import ru.volgorobot.vrcatalog.model.SecondLevelModel;
import ru.volgorobot.vrcatalog.model.ThirdLevelModel;
@ -29,7 +31,6 @@ public class SecondLevelNodeViewBinder extends BaseNodeViewBinder {
imageView = (ImageView) itemView.findViewById(R.id.arrow_img);
this.coreModel = new CoreModel(context);
this.mView = mView;
coreModel.errorSignal.connect(mView::onFailureAnswer);
}
@Override
@ -47,7 +48,7 @@ public class SecondLevelNodeViewBinder extends BaseNodeViewBinder {
public void onNodeToggled(TreeNode treeNode, boolean expand) {
if(expand) {
SecondLevelModel secondLevelModel = (SecondLevelModel) treeNode.getValue();
new AsyncTask<Void, Void, ArrayList<ThirdLevelModel>>() {
new AsyncTask<Void, Void, ResultWithErrorCode<ArrayList<ThirdLevelModel>>>() {
@Override
protected void onPreExecute() {
super.onPreExecute();
@ -55,24 +56,48 @@ public class SecondLevelNodeViewBinder extends BaseNodeViewBinder {
}
@Override
protected ArrayList<ThirdLevelModel> doInBackground(Void... voids) {
ArrayList<ThirdLevelModel> thirdLevelModels = coreModel.getThirdLevelByParentID(secondLevelModel.getID());
return thirdLevelModels;
protected ResultWithErrorCode<ArrayList<ThirdLevelModel>> doInBackground(Void... voids) {
ArrayList<ThirdLevelModel> thirdLevelModels = null;
try {
thirdLevelModels = coreModel.getThirdLevelByParentID(secondLevelModel.getID());
} catch (NetworkErrorException networkErrorException) {
return new ResultWithErrorCode<>(thirdLevelModels, networkErrorException.getErrorCode());
} catch (NullPointerException nullPointerException) {
return new ResultWithErrorCode<>(thirdLevelModels, 3);
}
return new ResultWithErrorCode<>(thirdLevelModels, 0);
}
@Override
protected void onPostExecute(ArrayList<ThirdLevelModel> thirdLevelModels) {
super.onPostExecute(thirdLevelModels);
ArrayList<TreeNode> treeNodes = new ArrayList<>();
for (int i = 0; i < thirdLevelModels.size(); i++) {
TreeNode treeNode1 = new TreeNode(thirdLevelModels.get(i));
treeNode1.setLevel(2);
treeNodes.add(treeNode1);
protected void onPostExecute(ResultWithErrorCode<ArrayList<ThirdLevelModel>> result) {
super.onPostExecute(result);
switch (result.getErrorCode()) {
case 0: {
ArrayList<TreeNode> treeNodes = new ArrayList<>();
for (int i = 0; i < result.getData().size(); i++) {
TreeNode treeNode1 = new TreeNode(result.getData().get(i));
treeNode1.setLevel(2);
treeNodes.add(treeNode1);
}
treeNode.setChildren(treeNodes);
mView.refreshTree();
imageView.animate().rotation(90).setDuration(200).start();
break;
}
case 1: {
mView.onFailureAnswer(1);
break;
}
case 2: {
mView.onFailureAnswer(2);
break;
}
case 3: {
mView.onFailureAnswer(3);
break;
}
}
treeNode.setChildren(treeNodes);
mView.refreshTree();
mView.swipeLayoutSetRefreshing(false);
imageView.animate().rotation(90).setDuration(200).start();
}
}.execute();
} else {

View File

@ -5,6 +5,7 @@ buildscript {
repositories {
google()
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
@ -19,6 +20,7 @@ allprojects {
repositories {
google()
jcenter()
mavenCentral()
}
}