From 4b4be1f1d159edf495d9b8d4bb68b757508bf06d Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Wed, 16 Jan 2019 21:23:59 +0400 Subject: [PATCH] Fixed bug when starting search (work on main thread and fixed bug which related with treeview UI. --- .idea/assetWizardSettings.xml | 10 +- .../vrcatalog/SearchablePresenter.java | 219 +++++++----------- .../vrcatalog/view/SearchableActivity.java | 4 +- app/src/main/res/menu/main.xml | 2 +- 4 files changed, 96 insertions(+), 139 deletions(-) diff --git a/.idea/assetWizardSettings.xml b/.idea/assetWizardSettings.xml index e64e3f9..bea7a4f 100644 --- a/.idea/assetWizardSettings.xml +++ b/.idea/assetWizardSettings.xml @@ -3,6 +3,11 @@ diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/SearchablePresenter.java b/app/src/main/java/ru/volgorobot/vrcatalog/SearchablePresenter.java index 324c74f..fac2f76 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/SearchablePresenter.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/SearchablePresenter.java @@ -1,14 +1,13 @@ package ru.volgorobot.vrcatalog; +import android.app.Activity; import android.content.Context; import android.os.AsyncTask; import java.util.ArrayList; -import java.util.concurrent.ExecutionException; import me.texy.treeview.TreeNode; 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; @@ -17,35 +16,86 @@ import ru.volgorobot.vrcatalog.model.ThirdLevelModel; public class SearchablePresenter implements MainContract.SearchablePresenter { private MainContract.SearchableActivityView mView; private MainContract.MainModel coreModel; + private Activity activity; - public SearchablePresenter(MainContract.SearchableActivityView mView, Context context) { + public SearchablePresenter(MainContract.SearchableActivityView mView, Context context, Activity activity) { this.mView = mView; this.coreModel = new CoreModel(context); + this.activity = activity; } @Override public void fetchDetailsTreeByName(String name) { + mView.swipeLayoutSetRefreshing(true); ArrayList categories = new ArrayList<>(); ArrayList subcategories = new ArrayList<>(); ArrayList details = new ArrayList<>(); - try { - FetchDetailsByNameTask fetchDetailsByNameTask = new FetchDetailsByNameTask(); - FetchCategoryByIDTask fetchCategoryByIDTask = new FetchCategoryByIDTask(); - FetchSubCategoryByIDTask fetchSubCategoryByIDTask = new FetchSubCategoryByIDTask(); - details.addAll(new FetchDetailsByNameTask().execute(name).get().getData()); - for (int i = 0; i < details.size(); i++) { - subcategories.add(new FetchSubCategoryByIDTask().execute(details.get(i).getDetailSubTypeId()).get().getData()); + new AsyncTask() { + @Override + protected Integer doInBackground(Void... voids) { + try { + try { + details.addAll(coreModel.fetchDetailsByName(name)); + } catch (NetworkErrorException networkErrorException) { + return networkErrorException.getErrorCode(); + } catch (NullPointerException nullPointerException) { + return 3; + } + + for (int i = 0; i < details.size(); i++) { + try { + if(isHaveDuplicate(subcategories,details.get(i).getDetailSubTypeId())) + continue; + subcategories.add(coreModel.fetchSubCategoryByID(details.get(i).getDetailSubTypeId()).get(0)); + } catch (NetworkErrorException networkErrorException) { + return networkErrorException.getErrorCode(); + } catch (NullPointerException nullPointerException) { + return 3; + } + } + + for (int i = 0; i < subcategories.size(); i++) { + try { + if(isHaveDuplicate(categories, subcategories.get(i).getDetailTypeId())) + continue; + categories.add(coreModel.fetchCategoryByID(subcategories.get(i).getDetailTypeId()).get(0)); + } catch (NetworkErrorException networkErrorException) { + return networkErrorException.getErrorCode(); + } catch (NullPointerException nullPointerException) { + return 3; + } + } + } catch (Exception e) { + // + } + return 0; } - for (int i = 0; i < subcategories.size(); i++) { - categories.add(new FetchCategoryByIDTask().execute(subcategories.get(i).getDetailTypeId()).get().getData()); + + @Override + protected void onPostExecute(Integer resultCode) { + super.onPostExecute(resultCode); + switch (resultCode) { + case 0: { + // + break; + } + case 1: { + mView.onFailureAnswer(1); + break; + } + case 2: { + mView.onFailureAnswer(2); + break; + } + case 3: { + mView.onFailureAnswer(3); + break; + } + } } - } catch (ExecutionException e) { - // - } catch (InterruptedException e) { - // - } + }.execute(); new AsyncTask>() { @Override @@ -53,15 +103,20 @@ public class SearchablePresenter implements MainContract.SearchablePresenter { ArrayList treeNodes = new ArrayList<>(); for (int i = 0; i < categories.size(); i++) { treeNodes.add(new TreeNode(categories.get(i))); + treeNodes.get(i).setLevel(0); ArrayList secondLevelModels = getSecondModelsByCategoryID(subcategories, categories.get(i).getID()); for (int j = 0; j < secondLevelModels.size(); j++) { - treeNodes.get(i).addChild(new TreeNode(secondLevelModels.get(j))); + TreeNode treeNode = new TreeNode(secondLevelModels.get(j)); + treeNode.setLevel(1); + treeNodes.get(i).addChild(treeNode); } ArrayList subnodes = (ArrayList) treeNodes.get(i).getChildren(); for (int j = 0; j < subnodes.size(); j++) { ArrayList thirdLevelModels = getDetailsByCategoryID(details, ((SecondLevelModel) subnodes.get(j).getValue()).getID()); for (int k = 0; k < thirdLevelModels.size(); k++) { - subnodes.get(j).addChild(new TreeNode(thirdLevelModels.get(k))); + TreeNode treeNode = new TreeNode(thirdLevelModels.get(k)); + treeNode.setLevel(2); + subnodes.get(j).addChild(treeNode); } } } @@ -83,7 +138,7 @@ public class SearchablePresenter implements MainContract.SearchablePresenter { if(secondLevelModels.get(i).getDetailTypeId() == id) secondLevelModelsByCategoryID.add(secondLevelModels.get(i)); } - + return secondLevelModelsByCategoryID; } @@ -97,123 +152,19 @@ public class SearchablePresenter implements MainContract.SearchablePresenter { return thirdLevelModelsBySubcategoryID; } - class FetchDetailsByNameTask extends AsyncTask>> { - @Override - protected void onPreExecute() { - super.onPreExecute(); - mView.swipeLayoutSetRefreshing(true); - } - - @Override - protected ResultWithErrorCode> doInBackground(String... strings) { - ArrayList details = new ArrayList<>(); - try { - details.addAll(coreModel.fetchDetailsByName(strings[0])); - } catch (NetworkErrorException networkErrorException) { - return new ResultWithErrorCode<>(details, networkErrorException.getErrorCode()); - } catch (NullPointerException nullPointerException) { - return new ResultWithErrorCode<>(details, 3); - } - return new ResultWithErrorCode<>(details, 0); - } - - @Override - protected void onPostExecute(ResultWithErrorCode> result) { - super.onPostExecute(result); - switch (result.getErrorCode()) { - case 0: { - // - break; - } - case 1: { - mView.onFailureAnswer(1); - break; - } - case 2: { - mView.onFailureAnswer(2); - break; - } - case 3: { - mView.onFailureAnswer(3); - break; - } - } + private boolean isHaveDuplicate(ArrayList secondLevelModels, int subcatID) { + for (int i = 0; i < secondLevelModels.size(); i++) { + if(secondLevelModels.get(i).getID() == subcatID) + return true; } + return false; } - class FetchCategoryByIDTask extends AsyncTask> { - @Override - protected ResultWithErrorCode doInBackground(Integer... integers) { - FirstLevelModel firstLevelModel; - try { - firstLevelModel = coreModel.fetchCategoryByID(integers[0]).get(0); - } catch (NetworkErrorException networkErrorException) { - return new ResultWithErrorCode<>(null, networkErrorException.getErrorCode()); - } catch (NullPointerException nullPointerException) { - return new ResultWithErrorCode<>(null, 3); - } - return new ResultWithErrorCode<>(firstLevelModel, 0); - } - - @Override - protected void onPostExecute(ResultWithErrorCode result) { - super.onPostExecute(result); - switch (result.getErrorCode()) { - case 0: { - // Nothing to do. - break; - } - case 1: { - mView.onFailureAnswer(1); - break; - } - case 2: { - mView.onFailureAnswer(2); - break; - } - case 3: { - mView.onFailureAnswer(3); - break; - } - } - } - } - - class FetchSubCategoryByIDTask extends AsyncTask> { - @Override - protected ResultWithErrorCode doInBackground(Integer... integers) { - SecondLevelModel secondLevelModel; - try { - secondLevelModel = coreModel.fetchSubCategoryByID(integers[0]).get(0); - } catch (NetworkErrorException networkErrorException) { - return new ResultWithErrorCode<>(null, networkErrorException.getErrorCode()); - } catch (NullPointerException nullPointerException) { - return new ResultWithErrorCode<>(null, 3); - } - return new ResultWithErrorCode<>(secondLevelModel, 0); - } - - @Override - protected void onPostExecute(ResultWithErrorCode result) { - super.onPostExecute(result); - switch (result.getErrorCode()) { - case 0: { - // Nothing to do. - break; - } - case 1: { - mView.onFailureAnswer(1); - break; - } - case 2: { - mView.onFailureAnswer(2); - break; - } - case 3: { - mView.onFailureAnswer(3); - break; - } - } + private boolean isHaveDuplicate(ArrayList firstLevelModels, Integer catID) { + for (int i = 0; i < firstLevelModels.size(); i++) { + if(firstLevelModels.get(i).getID().equals(catID)) + return true; } + return false; } } diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/view/SearchableActivity.java b/app/src/main/java/ru/volgorobot/vrcatalog/view/SearchableActivity.java index 12e4436..141c8fa 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/view/SearchableActivity.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/view/SearchableActivity.java @@ -18,7 +18,6 @@ import me.texy.treeview.TreeView; import ru.volgorobot.vrcatalog.MainContract; import ru.volgorobot.vrcatalog.R; import ru.volgorobot.vrcatalog.SearchablePresenter; -import ru.volgorobot.vrcatalog.additional.NodeViewFactory; import ru.volgorobot.vrcatalog.additional.SearchableNodeViewFactory; import ru.volgorobot.vrcatalog.model.FirstLevelModel; @@ -33,7 +32,7 @@ public class SearchableActivity extends AppCompatActivity implements MainContrac protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_searchable); - searchablePresenter = new SearchablePresenter(this, SearchableActivity.this); + searchablePresenter = new SearchablePresenter(this, SearchableActivity.this, this); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeButtonEnabled(true); @@ -55,6 +54,7 @@ public class SearchableActivity extends AppCompatActivity implements MainContrac @Override public void setTreeViewChildren(ArrayList children) { rootNode.setChildren(children); + treeView.expandAll(); treeView.refreshTreeView(); } diff --git a/app/src/main/res/menu/main.xml b/app/src/main/res/menu/main.xml index e515c31..c4d297b 100644 --- a/app/src/main/res/menu/main.xml +++ b/app/src/main/res/menu/main.xml @@ -3,7 +3,7 @@ xmlns:app="http://schemas.android.com/apk/res-auto">