From 312598bd70750a6c080ce834eea80a465c2afd52 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Wed, 9 Jan 2019 18:49:49 +0400 Subject: [PATCH] Refactoring of nodeViewBinders --- .idea/caches/build_file_checksums.ser | Bin 537 -> 537 bytes app/build.gradle | 1 - .../ru/volgorobot/vrcatalog/MainContract.java | 19 +- .../volgorobot/vrcatalog/MainPresenter.java | 2 +- .../vrcatalog/ViewBinderPresenter.java | 205 ++++++++++++++++++ .../volgorobot/vrcatalog/model/CoreModel.java | 8 +- .../FirstLevelNodeViewBinder.java | 70 +----- .../SecondLevelNodeViewBinder.java | 67 +----- .../ThirdLevelNodeViewBinder.java | 68 +----- 9 files changed, 256 insertions(+), 184 deletions(-) create mode 100644 app/src/main/java/ru/volgorobot/vrcatalog/ViewBinderPresenter.java diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser index 4d8407c9fc8e10ed9d50bc92fd154b877dc483e6..9c9d4877679efb0f01b207b4ae0d52e78ccc685f 100644 GIT binary patch delta 33 rcmV++0N($Z1epYom;^HoV7#%MYylBD=;r#8h(4nhX0jA_t<>pw<{b|u delta 33 rcmV++0N($Z1epYom;@dEww1A*YylC4l68)huD getFirstLevel() throws NetworkErrorException, NullPointerException; - ArrayList getSecondLevelByParentID(int parentID) throws NetworkErrorException, NullPointerException; - ArrayList getThirdLevelByParentID(int parentID) throws NetworkErrorException, NullPointerException; - ArrayList getDetailByID(int detailID) throws NetworkErrorException, NullPointerException; + ArrayList fetchFirstLevel() throws NetworkErrorException, NullPointerException; + ArrayList fetchSecondLevelByParentID(int parentID) throws NetworkErrorException, NullPointerException; + ArrayList fetchThirdLevelByParentID(int parentID) throws NetworkErrorException, NullPointerException; + ArrayList fetchDetailByID(int detailID) 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 8bac518..81a6f77 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/MainPresenter.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/MainPresenter.java @@ -33,7 +33,7 @@ public class MainPresenter implements MainContract.Presenter { protected ResultWithErrorCode> doInBackground(Void... voids) { ArrayList firstLevelModels = null; try { - firstLevelModels = coreModel.getFirstLevel(); + firstLevelModels = coreModel.fetchFirstLevel(); } catch (NetworkErrorException networkErrorException) { return new ResultWithErrorCode<>(firstLevelModels, networkErrorException.getErrorCode()); } catch (NullPointerException nullPointerException) { diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/ViewBinderPresenter.java b/app/src/main/java/ru/volgorobot/vrcatalog/ViewBinderPresenter.java new file mode 100644 index 0000000..dc1bdf0 --- /dev/null +++ b/app/src/main/java/ru/volgorobot/vrcatalog/ViewBinderPresenter.java @@ -0,0 +1,205 @@ +package ru.volgorobot.vrcatalog; + +import android.content.Context; +import android.content.Intent; +import android.os.AsyncTask; + +import java.util.ArrayList; + +import me.texy.treeview.TreeNode; +import ru.volgorobot.vrcatalog.additional.NetworkErrorException; +import ru.volgorobot.vrcatalog.additional.ResultWithErrorCode; +import ru.volgorobot.vrcatalog.model.DetailModel; +import ru.volgorobot.vrcatalog.model.FirstLevelModel; +import ru.volgorobot.vrcatalog.model.SecondLevelModel; +import ru.volgorobot.vrcatalog.model.ThirdLevelModel; +import ru.volgorobot.vrcatalog.view.DetailActivity; + +public class ViewBinderPresenter implements MainContract.ViewBinderPresenter { + private MainContract.MainActivityView mView; + private MainContract.MainModel coreModel; + private MainContract.ViewBinder viewBinder; + private Context context; + + public ViewBinderPresenter(MainContract.MainActivityView mView, MainContract.MainModel coreModel, MainContract.ViewBinder viewBinder) { + this.mView = mView; + this.coreModel = coreModel; + this.viewBinder = viewBinder; + } + + public ViewBinderPresenter(MainContract.MainActivityView mView, MainContract.MainModel coreModel, MainContract.ViewBinder viewBinder, Context context) { + this.mView = mView; + this.coreModel = coreModel; + this.viewBinder = viewBinder; + this.context = context; + } + + @Override + public void fetchSecondLevel(FirstLevelModel firstLevelModel, TreeNode treeNode) { + new AsyncTask>>() { + @Override + protected void onPreExecute() { + super.onPreExecute(); + mView.swipeLayoutSetRefreshing(true); + } + + @Override + protected ResultWithErrorCode> doInBackground(FirstLevelModel... firstLevelModel) { + ArrayList secondLevelModels = null; + try { + secondLevelModels = coreModel.fetchSecondLevelByParentID(firstLevelModel[0].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(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(); + viewBinder.rotateImageView(); + break; + } + case 1: { + mView.onFailureAnswer(1); + break; + } + case 2: { + mView.onFailureAnswer(2); + break; + } + case 3: { + mView.onFailureAnswer(3); + break; + } + } + mView.swipeLayoutSetRefreshing(false); + } + }.execute(firstLevelModel); + } + + @Override + public void fetchThirdLevel(SecondLevelModel secondLevelModel, TreeNode treeNode) { + new AsyncTask>>() { + @Override + protected void onPreExecute() { + super.onPreExecute(); + mView.swipeLayoutSetRefreshing(true); + } + + @Override + protected ResultWithErrorCode> doInBackground(SecondLevelModel... secondLevelModel) { + ArrayList thirdLevelModels = null; + try { + thirdLevelModels = coreModel.fetchThirdLevelByParentID(secondLevelModel[0].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(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(); + viewBinder.rotateImageView(); + break; + } + case 1: { + mView.onFailureAnswer(1); + break; + } + case 2: { + mView.onFailureAnswer(2); + break; + } + case 3: { + mView.onFailureAnswer(3); + break; + } + } + mView.swipeLayoutSetRefreshing(false); + } + }.execute(secondLevelModel); + } + + @Override + public void fetchDetail(ThirdLevelModel thirdLevelModel) { + new AsyncTask>() { + @Override + protected void onPreExecute() { + super.onPreExecute(); + mView.swipeLayoutSetRefreshing(true); + } + + @Override + protected ResultWithErrorCode doInBackground(ThirdLevelModel... thirdLevelModel) { + ArrayList detail = null; + try { + detail = coreModel.fetchDetailByID(thirdLevelModel[0].getID()); + } catch (NetworkErrorException networkErrorException) { + return new ResultWithErrorCode<>(detail.get(0), networkErrorException.getErrorCode()); + } catch (NullPointerException nullPointerException) { + return new ResultWithErrorCode<>(detail.get(0), 3); + } + return new ResultWithErrorCode<>(detail.get(0), 0); + } + + @Override + protected void onPostExecute(ResultWithErrorCode result) { + super.onPostExecute(result); + switch (result.getErrorCode()) { + case 0: { + Intent intent = new Intent(context, DetailActivity.class); + intent.putExtra("detailName", result.getData().getName()); + intent.putExtra("detailQuantity", result.getData().getNumber().toString()); + intent.putExtra("detailPrice", result.getData().getPrice()); + intent.putExtra("detailContry", result.getData().getCountry()); + intent.putExtra("detailAnalogue", result.getData().getAnalogue()); + intent.putExtra("detailDatasheet", result.getData().getDocURL()); + intent.putExtra("detailNotes", result.getData().getNotes()); + mView.startActivityByIntent(intent); + break; + } + case 1: { + mView.onFailureAnswer(1); + break; + } + case 2: { + mView.onFailureAnswer(2); + break; + } + case 3: { + mView.onFailureAnswer(3); + break; + } + } + mView.swipeLayoutSetRefreshing(false); + } + }.execute(thirdLevelModel); + } +} 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 349dded..ccbb53a 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/model/CoreModel.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/model/CoreModel.java @@ -31,7 +31,7 @@ public class CoreModel implements MainContract.MainModel { } @Override - public ArrayList getFirstLevel() throws NetworkErrorException, NullPointerException { + public ArrayList fetchFirstLevel() throws NetworkErrorException, NullPointerException { if(vrApi == null) { throw new NullPointerException(); } @@ -50,7 +50,7 @@ public class CoreModel implements MainContract.MainModel { } @Override - public ArrayList getSecondLevelByParentID(int parentID) throws NetworkErrorException, NullPointerException { + public ArrayList fetchSecondLevelByParentID(int parentID) throws NetworkErrorException, NullPointerException { if(vrApi == null) { throw new NullPointerException(); } @@ -69,7 +69,7 @@ public class CoreModel implements MainContract.MainModel { } @Override - public ArrayList getThirdLevelByParentID(int parentID) throws NetworkErrorException, NullPointerException { + public ArrayList fetchThirdLevelByParentID(int parentID) throws NetworkErrorException, NullPointerException { if(vrApi == null) { throw new NullPointerException(); } @@ -88,7 +88,7 @@ public class CoreModel implements MainContract.MainModel { } @Override - public ArrayList getDetailByID(int detailID) throws NetworkErrorException, NullPointerException { + public ArrayList fetchDetailByID(int detailID) throws NetworkErrorException, NullPointerException { if(vrApi == null) { throw new NullPointerException(); } 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 72a2a32..78b6495 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/FirstLevelNodeViewBinder.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/FirstLevelNodeViewBinder.java @@ -12,24 +12,23 @@ 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.ViewBinderPresenter; 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; -public class FirstLevelNodeViewBinder extends BaseNodeViewBinder { +public class FirstLevelNodeViewBinder extends BaseNodeViewBinder implements MainContract.ViewBinder { TextView textView; ImageView imageView; - CoreModel coreModel; - MainContract.MainActivityView mView; + ViewBinderPresenter viewBinderPresenter; - public FirstLevelNodeViewBinder(View itemView, Context context, MainContract.MainActivityView mView, CoreModel coreModel) { + public FirstLevelNodeViewBinder(View itemView, Context context, MainContract.MainActivityView mView, MainContract.MainModel coreModel) { super(itemView); textView = (TextView) itemView.findViewById(R.id.node_name_view); imageView = (ImageView) itemView.findViewById(R.id.arrow_img); - this.coreModel = coreModel; - this.mView = mView; + viewBinderPresenter = new ViewBinderPresenter(mView, coreModel, this); } @Override @@ -46,61 +45,14 @@ public class FirstLevelNodeViewBinder extends BaseNodeViewBinder { public void onNodeToggled(TreeNode treeNode, boolean expand) { if(expand) { FirstLevelModel firstLevelModel = (FirstLevelModel) treeNode.getValue(); - new AsyncTask>>() { - @Override - protected void onPreExecute() { - super.onPreExecute(); - mView.swipeLayoutSetRefreshing(true); - } - - @Override - 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(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; - } - } - mView.swipeLayoutSetRefreshing(false); - } - }.execute(); + viewBinderPresenter.fetchSecondLevel(firstLevelModel, treeNode); } else { imageView.animate().rotation(0).setDuration(200).start(); } } + + @Override + public void rotateImageView() { + imageView.animate().rotation(90).setDuration(200).start(); + } } 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 1494cb2..fb561bb 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/SecondLevelNodeViewBinder.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/SecondLevelNodeViewBinder.java @@ -12,25 +12,24 @@ 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.ViewBinderPresenter; 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; -public class SecondLevelNodeViewBinder extends BaseNodeViewBinder { +public class SecondLevelNodeViewBinder extends BaseNodeViewBinder implements MainContract.ViewBinder { TextView textView; ImageView imageView; - MainContract.MainModel coreModel; - MainContract.MainActivityView mView; + MainContract.ViewBinderPresenter viewBinderPresenter; public SecondLevelNodeViewBinder(View itemView, Context context, MainContract.MainActivityView mView, CoreModel coreModel) { super(itemView); textView = (TextView) itemView.findViewById(R.id.node_name_view); imageView = (ImageView) itemView.findViewById(R.id.arrow_img); - this.coreModel = coreModel; - this.mView = mView; + viewBinderPresenter = new ViewBinderPresenter(mView, coreModel, this); } @Override @@ -48,60 +47,14 @@ public class SecondLevelNodeViewBinder extends BaseNodeViewBinder { public void onNodeToggled(TreeNode treeNode, boolean expand) { if(expand) { SecondLevelModel secondLevelModel = (SecondLevelModel) treeNode.getValue(); - new AsyncTask>>() { - @Override - protected void onPreExecute() { - super.onPreExecute(); - mView.swipeLayoutSetRefreshing(true); - } - - @Override - 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(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; - } - } - mView.swipeLayoutSetRefreshing(false); - } - }.execute(); + viewBinderPresenter.fetchThirdLevel(secondLevelModel, treeNode); } else { imageView.animate().rotation(0).setDuration(200).start(); } } + + @Override + public void rotateImageView() { + imageView.animate().rotation(90).setDuration(200).start(); + } } diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/ThirdLevelNodeViewBinder.java b/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/ThirdLevelNodeViewBinder.java index c3365b9..1545d46 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/ThirdLevelNodeViewBinder.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/nodeViewBinders/ThirdLevelNodeViewBinder.java @@ -12,6 +12,7 @@ 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.ViewBinderPresenter; import ru.volgorobot.vrcatalog.additional.NetworkErrorException; import ru.volgorobot.vrcatalog.additional.ResultWithErrorCode; import ru.volgorobot.vrcatalog.model.CoreModel; @@ -19,17 +20,13 @@ import ru.volgorobot.vrcatalog.model.DetailModel; import ru.volgorobot.vrcatalog.model.ThirdLevelModel; import ru.volgorobot.vrcatalog.view.DetailActivity; -public class ThirdLevelNodeViewBinder extends BaseNodeViewBinder { +public class ThirdLevelNodeViewBinder extends BaseNodeViewBinder implements MainContract.ViewBinder { TextView textView; - Context context; - MainContract.MainModel coreModel; - MainContract.MainActivityView mView; + MainContract.ViewBinderPresenter viewBinderPresenter; public ThirdLevelNodeViewBinder(View itemView, Context context, MainContract.MainActivityView mView, CoreModel coreModel) { super(itemView); - this.context = context; - this.coreModel = coreModel; - this.mView = mView; + viewBinderPresenter = new ViewBinderPresenter(mView, coreModel, this, context); textView = (TextView) itemView.findViewById(R.id.node_name_view); } @@ -46,57 +43,12 @@ public class ThirdLevelNodeViewBinder extends BaseNodeViewBinder { @Override public void onNodeToggled(TreeNode treeNode, boolean expand) { super.onNodeToggled(treeNode, expand); - new AsyncTask>() { - @Override - protected void onPreExecute() { - super.onPreExecute(); - mView.swipeLayoutSetRefreshing(true); - } + ThirdLevelModel thirdLevelModel = (ThirdLevelModel) treeNode.getValue(); + viewBinderPresenter.fetchDetail(thirdLevelModel); + } - @Override - protected ResultWithErrorCode doInBackground(Void... voids) { - ArrayList detail = null; - try { - detail = coreModel.getDetailByID(((ThirdLevelModel)treeNode.getValue()).getID()); - } catch (NetworkErrorException networkErrorException) { - return new ResultWithErrorCode<>(detail.get(0), networkErrorException.getErrorCode()); - } catch (NullPointerException nullPointerException) { - return new ResultWithErrorCode<>(detail.get(0), 3); - } - return new ResultWithErrorCode<>(detail.get(0), 0); - } - - @Override - protected void onPostExecute(ResultWithErrorCode result) { - super.onPostExecute(result); - switch (result.getErrorCode()) { - case 0: { - Intent intent = new Intent(context, DetailActivity.class); - intent.putExtra("detailName", result.getData().getName()); - intent.putExtra("detailQuantity", result.getData().getNumber().toString()); - intent.putExtra("detailPrice", result.getData().getPrice()); - intent.putExtra("detailContry", result.getData().getCountry()); - intent.putExtra("detailAnalogue", result.getData().getAnalogue()); - intent.putExtra("detailDatasheet", result.getData().getDocURL()); - intent.putExtra("detailNotes", result.getData().getNotes()); - mView.startActivityByIntent(intent); - break; - } - case 1: { - mView.onFailureAnswer(1); - break; - } - case 2: { - mView.onFailureAnswer(2); - break; - } - case 3: { - mView.onFailureAnswer(3); - break; - } - } - mView.swipeLayoutSetRefreshing(false); - } - }.execute(); + @Override + public void rotateImageView() { + // This is unnecessary. } }