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

View File

@ -5,6 +5,8 @@ import android.os.AsyncTask;
import java.util.ArrayList; 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.CoreModel;
import ru.volgorobot.vrcatalog.model.FirstLevelModel; import ru.volgorobot.vrcatalog.model.FirstLevelModel;
@ -14,46 +16,53 @@ public class MainPresenter implements MainContract.Presenter {
public MainPresenter(MainContract.MainActivityView mainActivityView, Context context) { public MainPresenter(MainContract.MainActivityView mainActivityView, Context context) {
this.mView = mainActivityView; this.mView = mainActivityView;
try { this.coreModel = new CoreModel(context);
this.coreModel = new CoreModel(context);
} catch (IllegalStateException e) {
mView.onFailureAnswer(3);
}
coreModel.errorSignal.connect(this::failureAnswerSlot);
} }
@Override @Override
public void getFirstLevel() { 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 @Override
protected ArrayList<FirstLevelModel> doInBackground(Void... voids) { protected ResultWithErrorCode<ArrayList<FirstLevelModel>> doInBackground(Void... voids) {
ArrayList<FirstLevelModel> firstLevelModels = coreModel.getFirstLevel(); ArrayList<FirstLevelModel> firstLevelModels = null;
return firstLevelModels; 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 @Override
protected void onPostExecute(ArrayList<FirstLevelModel> firstLevelModels) { protected void onPostExecute(ResultWithErrorCode<ArrayList<FirstLevelModel>> result) {
mView.buildFirstLevelTree(firstLevelModels); 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(); }.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 retrofit2.Response;
import ru.volgorobot.vrcatalog.MainContract; import ru.volgorobot.vrcatalog.MainContract;
import ru.volgorobot.vrcatalog.additional.NetworkErrorException;
import ru.volgorobot.vrcatalog.api.Controller; import ru.volgorobot.vrcatalog.api.Controller;
import ru.volgorobot.vrcatalog.api.VRApi; import ru.volgorobot.vrcatalog.api.VRApi;
public class CoreModel implements MainContract.MainModel { public class CoreModel implements MainContract.MainModel {
private Context context; private Context context;
private VRApi vrApi; private VRApi vrApi;
private String baseURL;
public final Signal1<Integer> errorSignal = new Signal1<>(); public final Signal1<Integer> errorSignal = new Signal1<>();
public CoreModel(Context context) throws IllegalStateException { public CoreModel(Context context) {
this.context = context; this.context = context;
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(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 { try {
vrApi = Controller.getApi(baseURL); vrApi = Controller.getApi(baseURL);
} catch(IllegalStateException e) { } catch(IllegalStateException e) {
@ -31,50 +101,11 @@ public class CoreModel implements MainContract.MainModel {
} }
@Override @Override
public ArrayList<FirstLevelModel> getFirstLevel() { public boolean getApiState() {
final ArrayList<FirstLevelModel> firstLevelModels = new ArrayList<>(); if(vrApi != null) {
try { return true;
Response<List<FirstLevelModel>> response = vrApi.getFirstLevel().execute(); } else {
if(response.isSuccessful()) { return false;
firstLevelModels.addAll(response.body());
} else {
errorSignal.emit(2);
}
} catch (IOException e) {
errorSignal.emit(1);
} }
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 me.texy.treeview.base.BaseNodeViewBinder;
import ru.volgorobot.vrcatalog.MainContract; import ru.volgorobot.vrcatalog.MainContract;
import ru.volgorobot.vrcatalog.R; 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.CoreModel;
import ru.volgorobot.vrcatalog.model.FirstLevelModel; import ru.volgorobot.vrcatalog.model.FirstLevelModel;
import ru.volgorobot.vrcatalog.model.SecondLevelModel; import ru.volgorobot.vrcatalog.model.SecondLevelModel;
@ -45,7 +47,7 @@ public class FirstLevelNodeViewBinder extends BaseNodeViewBinder {
public void onNodeToggled(TreeNode treeNode, boolean expand) { public void onNodeToggled(TreeNode treeNode, boolean expand) {
if(expand) { if(expand) {
FirstLevelModel firstLevelModel = (FirstLevelModel) treeNode.getValue(); FirstLevelModel firstLevelModel = (FirstLevelModel) treeNode.getValue();
new AsyncTask<Void, Void, ArrayList<SecondLevelModel>>() { new AsyncTask<Void, Void, ResultWithErrorCode<ArrayList<SecondLevelModel>>>() {
@Override @Override
protected void onPreExecute() { protected void onPreExecute() {
super.onPreExecute(); super.onPreExecute();
@ -53,24 +55,49 @@ public class FirstLevelNodeViewBinder extends BaseNodeViewBinder {
} }
@Override @Override
protected ArrayList<SecondLevelModel> doInBackground(Void... voids) { protected ResultWithErrorCode<ArrayList<SecondLevelModel>> doInBackground(Void... voids) {
ArrayList<SecondLevelModel> secondLevelModels = coreModel.getSecondLevelByParentID(firstLevelModel.getID()); ArrayList<SecondLevelModel> secondLevelModels = null;
return secondLevelModels; 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 @Override
protected void onPostExecute(ArrayList<SecondLevelModel> secondLevelModels) { protected void onPostExecute(ResultWithErrorCode<ArrayList<SecondLevelModel>> result) {
super.onPostExecute(secondLevelModels); super.onPostExecute(result);
ArrayList<TreeNode> treeNodes = new ArrayList<>(); switch (result.getErrorCode()) {
for (int i = 0; i < secondLevelModels.size(); i++) { case 0: {
TreeNode treeNode1 = new TreeNode(secondLevelModels.get(i)); ArrayList<TreeNode> treeNodes = new ArrayList<>();
treeNode1.setLevel(1); for (int i = 0; i < result.getData().size(); i++) {
treeNodes.add(treeNode1); 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); mView.swipeLayoutSetRefreshing(false);
imageView.animate().rotation(90).setDuration(200).start();
} }
}.execute(); }.execute();
} else { } else {

View File

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

View File

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