From 9fc86b219be38f62684ac359a81f66341fe57d15 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Sat, 5 Jan 2019 15:08:40 +0400 Subject: [PATCH] Added an advanced error response system. --- .idea/caches/build_file_checksums.ser | Bin 537 -> 537 bytes .../ru/volgorobot/vrcatalog/MainContract.java | 15 +-- .../volgorobot/vrcatalog/MainPresenter.java | 71 +++++----- .../additional/NetworkErrorException.java | 13 ++ .../additional/ResultWithErrorCode.java | 28 ++++ .../volgorobot/vrcatalog/model/CoreModel.java | 123 +++++++++++------- .../FirstLevelNodeViewBinder.java | 55 ++++++-- .../SecondLevelNodeViewBinder.java | 55 +++++--- build.gradle | 2 + 9 files changed, 247 insertions(+), 115 deletions(-) create mode 100644 app/src/main/java/ru/volgorobot/vrcatalog/additional/NetworkErrorException.java create mode 100644 app/src/main/java/ru/volgorobot/vrcatalog/additional/ResultWithErrorCode.java diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser index 33a95c05574a131d0f93a7b0ee0ed30a2f7bd9cf..1b56c5d76906aca8b9bd67fc9759fefd1ac50436 100644 GIT binary patch delta 35 tcmV+;0NnqX1epYom;@bxE>)47SM( diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/MainContract.java b/app/src/main/java/ru/volgorobot/vrcatalog/MainContract.java index cfabe87..2e7232f 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/MainContract.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/MainContract.java @@ -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 errorSignal = new Signal1<>(); - ArrayList getFirstLevel(); - ArrayList getSecondLevelByParentID(int parentID); - ArrayList getThirdLevelByParentID(int parentID); + ArrayList getFirstLevel() throws NetworkErrorException, NullPointerException; + ArrayList getSecondLevelByParentID(int parentID) throws NetworkErrorException, NullPointerException; + ArrayList getThirdLevelByParentID(int parentID) throws NetworkErrorException, NullPointerException; + void reinitializeApi() throws IllegalStateException; + boolean getApiState(); } } diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/MainPresenter.java b/app/src/main/java/ru/volgorobot/vrcatalog/MainPresenter.java index eba079a..c707bef 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/MainPresenter.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/MainPresenter.java @@ -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>() { + try { + coreModel.reinitializeApi(); + } catch (IllegalStateException e) { + mView.onFailureAnswer(3); + return; + } + new AsyncTask>>() { @Override - protected ArrayList doInBackground(Void... voids) { - ArrayList firstLevelModels = coreModel.getFirstLevel(); - return firstLevelModels; + protected ResultWithErrorCode> doInBackground(Void... voids) { + ArrayList 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 firstLevelModels) { - mView.buildFirstLevelTree(firstLevelModels); + protected void onPostExecute(ResultWithErrorCode> 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; - } - } } } diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/additional/NetworkErrorException.java b/app/src/main/java/ru/volgorobot/vrcatalog/additional/NetworkErrorException.java new file mode 100644 index 0000000..ea9bf16 --- /dev/null +++ b/app/src/main/java/ru/volgorobot/vrcatalog/additional/NetworkErrorException.java @@ -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; + } +} diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/additional/ResultWithErrorCode.java b/app/src/main/java/ru/volgorobot/vrcatalog/additional/ResultWithErrorCode.java new file mode 100644 index 0000000..6d20600 --- /dev/null +++ b/app/src/main/java/ru/volgorobot/vrcatalog/additional/ResultWithErrorCode.java @@ -0,0 +1,28 @@ +package ru.volgorobot.vrcatalog.additional; + + +public class ResultWithErrorCode { + 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; + } +} diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/model/CoreModel.java b/app/src/main/java/ru/volgorobot/vrcatalog/model/CoreModel.java index 8f24604..90e2498 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/model/CoreModel.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/model/CoreModel.java @@ -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 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 getFirstLevel() throws NetworkErrorException, NullPointerException { + if(vrApi == null) { + throw new NullPointerException(); + } + final ArrayList firstLevelModels = new ArrayList<>(); + try { + Response> 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 getSecondLevelByParentID(int parentID) throws NetworkErrorException, NullPointerException { + if(vrApi == null) { + throw new NullPointerException(); + } + final ArrayList secondLevelModels = new ArrayList<>(); + try { + Response> 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 getThirdLevelByParentID(int parentID) throws NetworkErrorException, NullPointerException { + if(vrApi == null) { + throw new NullPointerException(); + } + final ArrayList thirdLevelModels = new ArrayList<>(); + try { + Response> 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 getFirstLevel() { - final ArrayList firstLevelModels = new ArrayList<>(); - try { - Response> 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 getSecondLevelByParentID(int parentID) { - final ArrayList secondLevelModels = new ArrayList<>(); - try { - Response> 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 getThirdLevelByParentID(int parentID) { - final ArrayList thirdLevelModels = new ArrayList<>(); - try { - Response> response = vrApi.getDetails(parentID).execute(); - if(response.isSuccessful()) { - thirdLevelModels.addAll(response.body()); - } else { - errorSignal.emit(2); - } - } catch (IOException e) { - errorSignal.emit(1); - } - return thirdLevelModels; } } diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/FirstLevelNodeViewBinder.java b/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/FirstLevelNodeViewBinder.java index c08768f..52865a2 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/FirstLevelNodeViewBinder.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/FirstLevelNodeViewBinder.java @@ -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>() { + new AsyncTask>>() { @Override protected void onPreExecute() { super.onPreExecute(); @@ -53,24 +55,49 @@ public class FirstLevelNodeViewBinder extends BaseNodeViewBinder { } @Override - protected ArrayList doInBackground(Void... voids) { - ArrayList secondLevelModels = coreModel.getSecondLevelByParentID(firstLevelModel.getID()); - return secondLevelModels; + protected ResultWithErrorCode> doInBackground(Void... voids) { + ArrayList 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 secondLevelModels) { - super.onPostExecute(secondLevelModels); - ArrayList 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> result) { + super.onPostExecute(result); + switch (result.getErrorCode()) { + case 0: { + ArrayList 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 { diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/SecondLevelNodeViewBinder.java b/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/SecondLevelNodeViewBinder.java index cfd07c7..670fcb1 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/SecondLevelNodeViewBinder.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/SecondLevelNodeViewBinder.java @@ -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>() { + new AsyncTask>>() { @Override protected void onPreExecute() { super.onPreExecute(); @@ -55,24 +56,48 @@ public class SecondLevelNodeViewBinder extends BaseNodeViewBinder { } @Override - protected ArrayList doInBackground(Void... voids) { - ArrayList thirdLevelModels = coreModel.getThirdLevelByParentID(secondLevelModel.getID()); - return thirdLevelModels; + protected ResultWithErrorCode> doInBackground(Void... voids) { + ArrayList 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 thirdLevelModels) { - super.onPostExecute(thirdLevelModels); - ArrayList 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> result) { + super.onPostExecute(result); + switch (result.getErrorCode()) { + case 0: { + ArrayList 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 { diff --git a/build.gradle b/build.gradle index 8d3ef8e..03de3f4 100644 --- a/build.gradle +++ b/build.gradle @@ -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() } }