Refactoring of nodeViewBinders
This commit is contained in:
parent
37fdf104d5
commit
312598bd70
Binary file not shown.
@ -28,7 +28,6 @@ dependencies {
|
|||||||
implementation 'com.android.support:appcompat-v7:27.1.1'
|
implementation 'com.android.support:appcompat-v7:27.1.1'
|
||||||
implementation 'com.android.support:design:27.1.1'
|
implementation 'com.android.support:design:27.1.1'
|
||||||
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
|
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 'me.texy.treeview:treeview_lib:1.0.4'
|
||||||
implementation 'com.google.code.gson:gson:2.8.2'
|
implementation 'com.google.code.gson:gson:2.8.2'
|
||||||
implementation 'com.squareup.retrofit2:retrofit:2.1.0'
|
implementation 'com.squareup.retrofit2:retrofit:2.1.0'
|
||||||
|
@ -4,6 +4,7 @@ import android.content.Intent;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
import me.texy.treeview.TreeNode;
|
||||||
import ru.volgorobot.vrcatalog.additional.NetworkErrorException;
|
import ru.volgorobot.vrcatalog.additional.NetworkErrorException;
|
||||||
import ru.volgorobot.vrcatalog.model.CoreModel;
|
import ru.volgorobot.vrcatalog.model.CoreModel;
|
||||||
import ru.volgorobot.vrcatalog.model.DetailModel;
|
import ru.volgorobot.vrcatalog.model.DetailModel;
|
||||||
@ -25,11 +26,21 @@ public interface MainContract {
|
|||||||
CoreModel getCoreModel();
|
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 {
|
interface MainModel {
|
||||||
ArrayList<FirstLevelModel> getFirstLevel() throws NetworkErrorException, NullPointerException;
|
ArrayList<FirstLevelModel> fetchFirstLevel() throws NetworkErrorException, NullPointerException;
|
||||||
ArrayList<SecondLevelModel> getSecondLevelByParentID(int parentID) throws NetworkErrorException, NullPointerException;
|
ArrayList<SecondLevelModel> fetchSecondLevelByParentID(int parentID) throws NetworkErrorException, NullPointerException;
|
||||||
ArrayList<ThirdLevelModel> getThirdLevelByParentID(int parentID) throws NetworkErrorException, NullPointerException;
|
ArrayList<ThirdLevelModel> fetchThirdLevelByParentID(int parentID) throws NetworkErrorException, NullPointerException;
|
||||||
ArrayList<DetailModel> getDetailByID(int detailID) throws NetworkErrorException, NullPointerException;
|
ArrayList<DetailModel> fetchDetailByID(int detailID) throws NetworkErrorException, NullPointerException;
|
||||||
void reinitializeApi() throws IllegalStateException;
|
void reinitializeApi() throws IllegalStateException;
|
||||||
boolean getApiState();
|
boolean getApiState();
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,7 @@ public class MainPresenter implements MainContract.Presenter {
|
|||||||
protected ResultWithErrorCode<ArrayList<FirstLevelModel>> doInBackground(Void... voids) {
|
protected ResultWithErrorCode<ArrayList<FirstLevelModel>> doInBackground(Void... voids) {
|
||||||
ArrayList<FirstLevelModel> firstLevelModels = null;
|
ArrayList<FirstLevelModel> firstLevelModels = null;
|
||||||
try {
|
try {
|
||||||
firstLevelModels = coreModel.getFirstLevel();
|
firstLevelModels = coreModel.fetchFirstLevel();
|
||||||
} catch (NetworkErrorException networkErrorException) {
|
} catch (NetworkErrorException networkErrorException) {
|
||||||
return new ResultWithErrorCode<>(firstLevelModels, networkErrorException.getErrorCode());
|
return new ResultWithErrorCode<>(firstLevelModels, networkErrorException.getErrorCode());
|
||||||
} catch (NullPointerException nullPointerException) {
|
} catch (NullPointerException nullPointerException) {
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -31,7 +31,7 @@ public class CoreModel implements MainContract.MainModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ArrayList<FirstLevelModel> getFirstLevel() throws NetworkErrorException, NullPointerException {
|
public ArrayList<FirstLevelModel> fetchFirstLevel() throws NetworkErrorException, NullPointerException {
|
||||||
if(vrApi == null) {
|
if(vrApi == null) {
|
||||||
throw new NullPointerException();
|
throw new NullPointerException();
|
||||||
}
|
}
|
||||||
@ -50,7 +50,7 @@ public class CoreModel implements MainContract.MainModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ArrayList<SecondLevelModel> getSecondLevelByParentID(int parentID) throws NetworkErrorException, NullPointerException {
|
public ArrayList<SecondLevelModel> fetchSecondLevelByParentID(int parentID) throws NetworkErrorException, NullPointerException {
|
||||||
if(vrApi == null) {
|
if(vrApi == null) {
|
||||||
throw new NullPointerException();
|
throw new NullPointerException();
|
||||||
}
|
}
|
||||||
@ -69,7 +69,7 @@ public class CoreModel implements MainContract.MainModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ArrayList<ThirdLevelModel> getThirdLevelByParentID(int parentID) throws NetworkErrorException, NullPointerException {
|
public ArrayList<ThirdLevelModel> fetchThirdLevelByParentID(int parentID) throws NetworkErrorException, NullPointerException {
|
||||||
if(vrApi == null) {
|
if(vrApi == null) {
|
||||||
throw new NullPointerException();
|
throw new NullPointerException();
|
||||||
}
|
}
|
||||||
@ -88,7 +88,7 @@ public class CoreModel implements MainContract.MainModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ArrayList<DetailModel> getDetailByID(int detailID) throws NetworkErrorException, NullPointerException {
|
public ArrayList<DetailModel> fetchDetailByID(int detailID) throws NetworkErrorException, NullPointerException {
|
||||||
if(vrApi == null) {
|
if(vrApi == null) {
|
||||||
throw new NullPointerException();
|
throw new NullPointerException();
|
||||||
}
|
}
|
||||||
|
@ -12,24 +12,23 @@ 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.ViewBinderPresenter;
|
||||||
import ru.volgorobot.vrcatalog.additional.NetworkErrorException;
|
import ru.volgorobot.vrcatalog.additional.NetworkErrorException;
|
||||||
import ru.volgorobot.vrcatalog.additional.ResultWithErrorCode;
|
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;
|
||||||
|
|
||||||
public class FirstLevelNodeViewBinder extends BaseNodeViewBinder {
|
public class FirstLevelNodeViewBinder extends BaseNodeViewBinder implements MainContract.ViewBinder {
|
||||||
TextView textView;
|
TextView textView;
|
||||||
ImageView imageView;
|
ImageView imageView;
|
||||||
CoreModel coreModel;
|
ViewBinderPresenter viewBinderPresenter;
|
||||||
MainContract.MainActivityView mView;
|
|
||||||
|
|
||||||
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);
|
super(itemView);
|
||||||
textView = (TextView) itemView.findViewById(R.id.node_name_view);
|
textView = (TextView) itemView.findViewById(R.id.node_name_view);
|
||||||
imageView = (ImageView) itemView.findViewById(R.id.arrow_img);
|
imageView = (ImageView) itemView.findViewById(R.id.arrow_img);
|
||||||
this.coreModel = coreModel;
|
viewBinderPresenter = new ViewBinderPresenter(mView, coreModel, this);
|
||||||
this.mView = mView;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -46,61 +45,14 @@ 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, ResultWithErrorCode<ArrayList<SecondLevelModel>>>() {
|
viewBinderPresenter.fetchSecondLevel(firstLevelModel, treeNode);
|
||||||
@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();
|
|
||||||
} else {
|
} else {
|
||||||
imageView.animate().rotation(0).setDuration(200).start();
|
imageView.animate().rotation(0).setDuration(200).start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void rotateImageView() {
|
||||||
|
imageView.animate().rotation(90).setDuration(200).start();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,25 +12,24 @@ 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.ViewBinderPresenter;
|
||||||
import ru.volgorobot.vrcatalog.additional.NetworkErrorException;
|
import ru.volgorobot.vrcatalog.additional.NetworkErrorException;
|
||||||
import ru.volgorobot.vrcatalog.additional.ResultWithErrorCode;
|
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;
|
||||||
|
|
||||||
public class SecondLevelNodeViewBinder extends BaseNodeViewBinder {
|
public class SecondLevelNodeViewBinder extends BaseNodeViewBinder implements MainContract.ViewBinder {
|
||||||
TextView textView;
|
TextView textView;
|
||||||
ImageView imageView;
|
ImageView imageView;
|
||||||
MainContract.MainModel coreModel;
|
MainContract.ViewBinderPresenter viewBinderPresenter;
|
||||||
MainContract.MainActivityView mView;
|
|
||||||
|
|
||||||
|
|
||||||
public SecondLevelNodeViewBinder(View itemView, Context context, MainContract.MainActivityView mView, CoreModel coreModel) {
|
public SecondLevelNodeViewBinder(View itemView, Context context, MainContract.MainActivityView mView, CoreModel coreModel) {
|
||||||
super(itemView);
|
super(itemView);
|
||||||
textView = (TextView) itemView.findViewById(R.id.node_name_view);
|
textView = (TextView) itemView.findViewById(R.id.node_name_view);
|
||||||
imageView = (ImageView) itemView.findViewById(R.id.arrow_img);
|
imageView = (ImageView) itemView.findViewById(R.id.arrow_img);
|
||||||
this.coreModel = coreModel;
|
viewBinderPresenter = new ViewBinderPresenter(mView, coreModel, this);
|
||||||
this.mView = mView;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -48,60 +47,14 @@ 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, ResultWithErrorCode<ArrayList<ThirdLevelModel>>>() {
|
viewBinderPresenter.fetchThirdLevel(secondLevelModel, treeNode);
|
||||||
@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();
|
|
||||||
} else {
|
} else {
|
||||||
imageView.animate().rotation(0).setDuration(200).start();
|
imageView.animate().rotation(0).setDuration(200).start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void rotateImageView() {
|
||||||
|
imageView.animate().rotation(90).setDuration(200).start();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,7 @@ 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.ViewBinderPresenter;
|
||||||
import ru.volgorobot.vrcatalog.additional.NetworkErrorException;
|
import ru.volgorobot.vrcatalog.additional.NetworkErrorException;
|
||||||
import ru.volgorobot.vrcatalog.additional.ResultWithErrorCode;
|
import ru.volgorobot.vrcatalog.additional.ResultWithErrorCode;
|
||||||
import ru.volgorobot.vrcatalog.model.CoreModel;
|
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.model.ThirdLevelModel;
|
||||||
import ru.volgorobot.vrcatalog.view.DetailActivity;
|
import ru.volgorobot.vrcatalog.view.DetailActivity;
|
||||||
|
|
||||||
public class ThirdLevelNodeViewBinder extends BaseNodeViewBinder {
|
public class ThirdLevelNodeViewBinder extends BaseNodeViewBinder implements MainContract.ViewBinder {
|
||||||
TextView textView;
|
TextView textView;
|
||||||
Context context;
|
MainContract.ViewBinderPresenter viewBinderPresenter;
|
||||||
MainContract.MainModel coreModel;
|
|
||||||
MainContract.MainActivityView mView;
|
|
||||||
|
|
||||||
public ThirdLevelNodeViewBinder(View itemView, Context context, MainContract.MainActivityView mView, CoreModel coreModel) {
|
public ThirdLevelNodeViewBinder(View itemView, Context context, MainContract.MainActivityView mView, CoreModel coreModel) {
|
||||||
super(itemView);
|
super(itemView);
|
||||||
this.context = context;
|
viewBinderPresenter = new ViewBinderPresenter(mView, coreModel, this, context);
|
||||||
this.coreModel = coreModel;
|
|
||||||
this.mView = mView;
|
|
||||||
textView = (TextView) itemView.findViewById(R.id.node_name_view);
|
textView = (TextView) itemView.findViewById(R.id.node_name_view);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -46,57 +43,12 @@ public class ThirdLevelNodeViewBinder extends BaseNodeViewBinder {
|
|||||||
@Override
|
@Override
|
||||||
public void onNodeToggled(TreeNode treeNode, boolean expand) {
|
public void onNodeToggled(TreeNode treeNode, boolean expand) {
|
||||||
super.onNodeToggled(treeNode, expand);
|
super.onNodeToggled(treeNode, expand);
|
||||||
new AsyncTask<Void, Void, ResultWithErrorCode<DetailModel>>() {
|
ThirdLevelModel thirdLevelModel = (ThirdLevelModel) treeNode.getValue();
|
||||||
@Override
|
viewBinderPresenter.fetchDetail(thirdLevelModel);
|
||||||
protected void onPreExecute() {
|
}
|
||||||
super.onPreExecute();
|
|
||||||
mView.swipeLayoutSetRefreshing(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected ResultWithErrorCode<DetailModel> doInBackground(Void... voids) {
|
public void rotateImageView() {
|
||||||
ArrayList<DetailModel> detail = null;
|
// This is unnecessary.
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user