Fixed bug when starting search (work on main thread and fixed bug which related with treeview UI.

This commit is contained in:
ChronosX88 2019-01-16 21:23:59 +04:00
parent 4fb99782d4
commit 4b4be1f1d1
No known key found for this signature in database
GPG Key ID: 8F92E090A87804AA
4 changed files with 96 additions and 139 deletions

View File

@ -3,6 +3,11 @@
<component name="WizardSettings">
<option name="children">
<map>
<entry key="imageWizard">
<value>
<PersistentState />
</value>
</entry>
<entry key="vectorWizard">
<value>
<PersistentState>
@ -18,7 +23,7 @@
<PersistentState>
<option name="values">
<map>
<entry key="url" value="jar:file:/home/chronosx/Документы/bin/Android%20Studio/plugins/android/lib/android.jar!/images/material_design_icons/hardware/ic_keyboard_arrow_right_black_24dp.xml" />
<entry key="url" value="jar:file:/home/chronosx/%D0%94%D0%BE%D0%BA%D1%83%D0%BC%D0%B5%D0%BD%D1%82%D1%8B/bin/Android%20Studio/plugins/android/lib/android.jar!/images/material_design_icons/action/ic_search_black_24dp.xml" />
</map>
</option>
</PersistentState>
@ -28,7 +33,8 @@
</option>
<option name="values">
<map>
<entry key="outputName" value="ic_keyboard_arrow_right_black_24dp" />
<entry key="color" value="ffffff" />
<entry key="outputName" value="ic_search_white_24dp" />
<entry key="sourceFile" value="$USER_HOME$" />
</map>
</option>

View File

@ -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<FirstLevelModel> categories = new ArrayList<>();
ArrayList<SecondLevelModel> subcategories = new ArrayList<>();
ArrayList<ThirdLevelModel> 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<Void, Void, Integer>() {
@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<Void, Void, ArrayList<TreeNode>>() {
@Override
@ -53,15 +103,20 @@ public class SearchablePresenter implements MainContract.SearchablePresenter {
ArrayList<TreeNode> treeNodes = new ArrayList<>();
for (int i = 0; i < categories.size(); i++) {
treeNodes.add(new TreeNode(categories.get(i)));
treeNodes.get(i).setLevel(0);
ArrayList<SecondLevelModel> 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<TreeNode> subnodes = (ArrayList<TreeNode>) treeNodes.get(i).getChildren();
for (int j = 0; j < subnodes.size(); j++) {
ArrayList<ThirdLevelModel> 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<String, Void, ResultWithErrorCode<ArrayList<ThirdLevelModel>>> {
@Override
protected void onPreExecute() {
super.onPreExecute();
mView.swipeLayoutSetRefreshing(true);
}
@Override
protected ResultWithErrorCode<ArrayList<ThirdLevelModel>> doInBackground(String... strings) {
ArrayList<ThirdLevelModel> 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<ArrayList<ThirdLevelModel>> 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<SecondLevelModel> 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<Integer, Void, ResultWithErrorCode<FirstLevelModel>> {
@Override
protected ResultWithErrorCode<FirstLevelModel> 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<FirstLevelModel> 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<Integer, Void, ResultWithErrorCode<SecondLevelModel>> {
@Override
protected ResultWithErrorCode<SecondLevelModel> 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<SecondLevelModel> 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<FirstLevelModel> firstLevelModels, Integer catID) {
for (int i = 0; i < firstLevelModels.size(); i++) {
if(firstLevelModels.get(i).getID().equals(catID))
return true;
}
return false;
}
}

View File

@ -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<TreeNode> children) {
rootNode.setChildren(children);
treeView.expandAll();
treeView.refreshTreeView();
}

View File

@ -3,7 +3,7 @@
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_search"
android:icon="@android:drawable/ic_menu_search"
android:icon="@drawable/ic_search_white_24dp"
app:showAsAction="always|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="Поиск"/>