Refactoring of nodeViewBinders

This commit is contained in:
ChronosX88 2019-01-09 18:49:49 +04:00
parent 37fdf104d5
commit 312598bd70
No known key found for this signature in database
GPG Key ID: 8F92E090A87804AA
9 changed files with 256 additions and 184 deletions

View File

@ -28,7 +28,6 @@ dependencies {
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.github.msteinbeck:sig4j:1.0.1'
implementation 'me.texy.treeview:treeview_lib:1.0.4'
implementation 'com.google.code.gson:gson:2.8.2'
implementation 'com.squareup.retrofit2:retrofit:2.1.0'

View File

@ -4,6 +4,7 @@ import android.content.Intent;
import java.util.ArrayList;
import me.texy.treeview.TreeNode;
import ru.volgorobot.vrcatalog.additional.NetworkErrorException;
import ru.volgorobot.vrcatalog.model.CoreModel;
import ru.volgorobot.vrcatalog.model.DetailModel;
@ -25,11 +26,21 @@ public interface MainContract {
CoreModel getCoreModel();
}
interface ViewBinderPresenter {
void fetchSecondLevel(FirstLevelModel firstLevelModel, TreeNode treeNode);
void fetchThirdLevel(SecondLevelModel secondLevelModel, TreeNode treeNode);
void fetchDetail(ThirdLevelModel thirdLevelModel);
}
interface ViewBinder {
void rotateImageView();
}
interface MainModel {
ArrayList<FirstLevelModel> getFirstLevel() throws NetworkErrorException, NullPointerException;
ArrayList<SecondLevelModel> getSecondLevelByParentID(int parentID) throws NetworkErrorException, NullPointerException;
ArrayList<ThirdLevelModel> getThirdLevelByParentID(int parentID) throws NetworkErrorException, NullPointerException;
ArrayList<DetailModel> getDetailByID(int detailID) throws NetworkErrorException, NullPointerException;
ArrayList<FirstLevelModel> fetchFirstLevel() throws NetworkErrorException, NullPointerException;
ArrayList<SecondLevelModel> fetchSecondLevelByParentID(int parentID) throws NetworkErrorException, NullPointerException;
ArrayList<ThirdLevelModel> fetchThirdLevelByParentID(int parentID) throws NetworkErrorException, NullPointerException;
ArrayList<DetailModel> fetchDetailByID(int detailID) throws NetworkErrorException, NullPointerException;
void reinitializeApi() throws IllegalStateException;
boolean getApiState();
}

View File

@ -33,7 +33,7 @@ public class MainPresenter implements MainContract.Presenter {
protected ResultWithErrorCode<ArrayList<FirstLevelModel>> doInBackground(Void... voids) {
ArrayList<FirstLevelModel> firstLevelModels = null;
try {
firstLevelModels = coreModel.getFirstLevel();
firstLevelModels = coreModel.fetchFirstLevel();
} catch (NetworkErrorException networkErrorException) {
return new ResultWithErrorCode<>(firstLevelModels, networkErrorException.getErrorCode());
} catch (NullPointerException nullPointerException) {

View File

@ -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<FirstLevelModel, Void, ResultWithErrorCode<ArrayList<SecondLevelModel>>>() {
@Override
protected void onPreExecute() {
super.onPreExecute();
mView.swipeLayoutSetRefreshing(true);
}
@Override
protected ResultWithErrorCode<ArrayList<SecondLevelModel>> doInBackground(FirstLevelModel... firstLevelModel) {
ArrayList<SecondLevelModel> 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<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();
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<SecondLevelModel, Void, ResultWithErrorCode<ArrayList<ThirdLevelModel>>>() {
@Override
protected void onPreExecute() {
super.onPreExecute();
mView.swipeLayoutSetRefreshing(true);
}
@Override
protected ResultWithErrorCode<ArrayList<ThirdLevelModel>> doInBackground(SecondLevelModel... secondLevelModel) {
ArrayList<ThirdLevelModel> 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<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();
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<ThirdLevelModel, Void, ResultWithErrorCode<DetailModel>>() {
@Override
protected void onPreExecute() {
super.onPreExecute();
mView.swipeLayoutSetRefreshing(true);
}
@Override
protected ResultWithErrorCode<DetailModel> doInBackground(ThirdLevelModel... thirdLevelModel) {
ArrayList<DetailModel> 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<DetailModel> 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);
}
}

View File

@ -31,7 +31,7 @@ public class CoreModel implements MainContract.MainModel {
}
@Override
public ArrayList<FirstLevelModel> getFirstLevel() throws NetworkErrorException, NullPointerException {
public ArrayList<FirstLevelModel> fetchFirstLevel() throws NetworkErrorException, NullPointerException {
if(vrApi == null) {
throw new NullPointerException();
}
@ -50,7 +50,7 @@ public class CoreModel implements MainContract.MainModel {
}
@Override
public ArrayList<SecondLevelModel> getSecondLevelByParentID(int parentID) throws NetworkErrorException, NullPointerException {
public ArrayList<SecondLevelModel> 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<ThirdLevelModel> getThirdLevelByParentID(int parentID) throws NetworkErrorException, NullPointerException {
public ArrayList<ThirdLevelModel> 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<DetailModel> getDetailByID(int detailID) throws NetworkErrorException, NullPointerException {
public ArrayList<DetailModel> fetchDetailByID(int detailID) throws NetworkErrorException, NullPointerException {
if(vrApi == null) {
throw new NullPointerException();
}

View File

@ -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<Void, Void, ResultWithErrorCode<ArrayList<SecondLevelModel>>>() {
@Override
protected void onPreExecute() {
super.onPreExecute();
mView.swipeLayoutSetRefreshing(true);
}
@Override
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(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;
}
}
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();
}
}

View File

@ -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<Void, Void, ResultWithErrorCode<ArrayList<ThirdLevelModel>>>() {
@Override
protected void onPreExecute() {
super.onPreExecute();
mView.swipeLayoutSetRefreshing(true);
}
@Override
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(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;
}
}
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();
}
}

View File

@ -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<Void, Void, ResultWithErrorCode<DetailModel>>() {
@Override
protected void onPreExecute() {
super.onPreExecute();
mView.swipeLayoutSetRefreshing(true);
}
ThirdLevelModel thirdLevelModel = (ThirdLevelModel) treeNode.getValue();
viewBinderPresenter.fetchDetail(thirdLevelModel);
}
@Override
protected ResultWithErrorCode<DetailModel> doInBackground(Void... voids) {
ArrayList<DetailModel> 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<DetailModel> 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.
}
}