Made getting subcategories.

This commit is contained in:
ChronosX88 2019-01-03 16:27:45 +04:00
parent c09373514b
commit 3b46a96b0d
No known key found for this signature in database
GPG Key ID: 8F92E090A87804AA
2 changed files with 75 additions and 33 deletions

View File

@ -1,7 +1,9 @@
package ru.volgorobot.vrcatalog;
import android.content.AsyncQueryHandler;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.design.widget.NavigationView;
@ -17,6 +19,7 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@ -29,6 +32,7 @@ import ru.volgorobot.vrcatalog.additional.NodeViewFactory;
import ru.volgorobot.vrcatalog.api.Controller;
import ru.volgorobot.vrcatalog.api.VRApi;
import ru.volgorobot.vrcatalog.model.FirstLevelModel;
import ru.volgorobot.vrcatalog.model.SecondLevelModel;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, SwipeRefreshLayout.OnRefreshListener {
@ -37,7 +41,9 @@ public class MainActivity extends AppCompatActivity
private VRApi vrApi;
private List<FirstLevelModel> firstLevelObjects = new ArrayList<>();
private List<TreeNode> firstLevelNodes = new ArrayList<>();
private List<SecondLevelModel> secondLevelObjects = new ArrayList<>();
private TreeNode rootNode = TreeNode.root();
private SwipeRefreshLayout mSwipeRefreshLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -56,13 +62,11 @@ public class MainActivity extends AppCompatActivity
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
SwipeRefreshLayout mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
mSwipeRefreshLayout.setOnRefreshListener(MainActivity.this);
mSwipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary);
try{
try {
getTreeData(false);
} catch (IllegalStateException e) {
//
@ -114,14 +118,29 @@ public class MainActivity extends AppCompatActivity
for (int i = 0; i < firstLevelObjects.size(); i++) {
firstLevelNodes.add(new TreeNode(firstLevelObjects.get(i).getName()));
firstLevelNodes.get(i).setLevel(0);
firstLevelNodes.get(i).setChildren(getSecondLevelNodesByParentID(firstLevelObjects.get(i).getID()));
}
rootNode.setChildren(firstLevelNodes);
}
private ArrayList<TreeNode> getSecondLevelNodesByParentID(int parentTypeID) {
ArrayList<SecondLevelModel> objects = new ArrayList<>();
for (int i = 0; i < secondLevelObjects.size(); i++) {
if(secondLevelObjects.get(i).getDetailTypeId() == parentTypeID) {
objects.add(secondLevelObjects.get(i));
}
}
ArrayList<TreeNode> nodes = new ArrayList<>();
for (int i = 0; i < objects.size(); i++) {
nodes.add(new TreeNode(objects.get(i).getName()));
nodes.get(i).setLevel(1);
}
return nodes;
}
@Override
public void onRefresh() {
final SwipeRefreshLayout mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
mSwipeRefreshLayout.setRefreshing(true);
try {
@ -131,9 +150,7 @@ public class MainActivity extends AppCompatActivity
}
}
private void getTreeData(boolean clean) throws IllegalStateException {
final SwipeRefreshLayout mSwipeRefreshLayout = findViewById(R.id.swipeRefreshLayout);
private void getTreeData(final boolean clean) throws IllegalStateException {
mSwipeRefreshLayout.setRefreshing(true);
if(clean) {
for (int i = 0; i < firstLevelNodes.size(); i++) {
@ -142,6 +159,9 @@ public class MainActivity extends AppCompatActivity
for (int i = 0; i < firstLevelObjects.size(); i++) {
firstLevelObjects.remove(i);
}
for (int i = 0; i < secondLevelObjects.size(); i++) {
secondLevelObjects.remove(i);
}
}
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
@ -152,39 +172,56 @@ public class MainActivity extends AppCompatActivity
} catch(IllegalStateException e) {
throw new IllegalStateException();
}
vrApi.getFirstLevel().enqueue(new Callback<List<FirstLevelModel>>() {
class GetTreeData extends AsyncTask<Void, Void, Void> {
@Override
public void onResponse(Call<List<FirstLevelModel>> call, Response<List<FirstLevelModel>> response) {
if(response != null) {
protected Void doInBackground(Void... voids) {
try {
firstLevelObjects.addAll(response.body());
mSwipeRefreshLayout.post(new Runnable() {
Response<List<FirstLevelModel>> firstLevelResponse = vrApi.getFirstLevel().execute();
if(firstLevelResponse.isSuccessful()) {
firstLevelObjects.addAll(firstLevelResponse.body());
for (int i = 0; i < firstLevelObjects.size(); i++) {
Response<List<SecondLevelModel>> secondLevelResponse = vrApi.getSecondLevel(firstLevelObjects.get(i).getID()).execute();
if(secondLevelResponse.isSuccessful()) {
secondLevelObjects.addAll(secondLevelResponse.body());
} else {
onFailureAnswer(1);
}
}
}
} catch (Exception e) {
onFailureAnswer(0);
}
return null;
}
@Override
public void run() {
mSwipeRefreshLayout.setRefreshing(false);
}
});
} catch(Exception e) {
Toast.makeText(MainActivity.this, "Ответ от сервера неверен! Перепроверьте данные подключения!", Toast.LENGTH_LONG).show();
Log.e("VRCatalog", "Answer of server is wrong! Re-check your connection credentials! Error: " + response.message());
mSwipeRefreshLayout.setRefreshing(false);
}
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
fillRootNode();
treeView.refreshTreeView();
} else {
Log.e("VRCatalog", "Что-то пошло не так...");
mSwipeRefreshLayout.setRefreshing(false);
}
}
new GetTreeData().execute();
}
@Override
public void onFailure(Call<List<FirstLevelModel>> call, Throwable t) {
private void onFailureAnswer(int errorCode) {
switch (errorCode) {
case 0: {
Toast.makeText(MainActivity.this, "Ошибка сети. Проверьте подключение к сети или данные подключения к API!", Toast.LENGTH_LONG).show();
Log.e("VRCatalog", "Network Error! Re-check your connection credentials or network settings! Technical information: " + t.toString());
Log.e("VRCatalog", "Network Error! Re-check your connection credentials or network settings!");
mSwipeRefreshLayout.setRefreshing(false);
break;
}
});
case 1: {
Toast.makeText(MainActivity.this, "Ответ от сервера неверен! Перепроверьте данные подключения!", Toast.LENGTH_LONG).show();
Log.e("VRCatalog", "Answer of server is wrong! Re-check your connection credentials!");
mSwipeRefreshLayout.setRefreshing(false);
break;
}
}
}
}

View File

@ -4,10 +4,15 @@ import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;
import ru.volgorobot.vrcatalog.model.FirstLevelModel;
import ru.volgorobot.vrcatalog.model.SecondLevelModel;
public interface VRApi {
@GET("/API/Api.php?action=getFirstLevel")
Call<List<FirstLevelModel>> getFirstLevel();
@GET("/API/Api.php?action=getSecondLevel")
Call<List<SecondLevelModel>> getSecondLevel(@Query("parentTypeID") int parentTypeID);
}