Made getting subcategories.
This commit is contained in:
parent
c09373514b
commit
3b46a96b0d
@ -1,7 +1,9 @@
|
|||||||
package ru.volgorobot.vrcatalog;
|
package ru.volgorobot.vrcatalog;
|
||||||
|
|
||||||
|
import android.content.AsyncQueryHandler;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
|
import android.os.AsyncTask;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
import android.support.design.widget.NavigationView;
|
import android.support.design.widget.NavigationView;
|
||||||
@ -17,6 +19,7 @@ import android.view.View;
|
|||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
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.Controller;
|
||||||
import ru.volgorobot.vrcatalog.api.VRApi;
|
import ru.volgorobot.vrcatalog.api.VRApi;
|
||||||
import ru.volgorobot.vrcatalog.model.FirstLevelModel;
|
import ru.volgorobot.vrcatalog.model.FirstLevelModel;
|
||||||
|
import ru.volgorobot.vrcatalog.model.SecondLevelModel;
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity
|
public class MainActivity extends AppCompatActivity
|
||||||
implements NavigationView.OnNavigationItemSelectedListener, SwipeRefreshLayout.OnRefreshListener {
|
implements NavigationView.OnNavigationItemSelectedListener, SwipeRefreshLayout.OnRefreshListener {
|
||||||
@ -37,7 +41,9 @@ public class MainActivity extends AppCompatActivity
|
|||||||
private VRApi vrApi;
|
private VRApi vrApi;
|
||||||
private List<FirstLevelModel> firstLevelObjects = new ArrayList<>();
|
private List<FirstLevelModel> firstLevelObjects = new ArrayList<>();
|
||||||
private List<TreeNode> firstLevelNodes = new ArrayList<>();
|
private List<TreeNode> firstLevelNodes = new ArrayList<>();
|
||||||
|
private List<SecondLevelModel> secondLevelObjects = new ArrayList<>();
|
||||||
private TreeNode rootNode = TreeNode.root();
|
private TreeNode rootNode = TreeNode.root();
|
||||||
|
private SwipeRefreshLayout mSwipeRefreshLayout;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
@ -56,13 +62,11 @@ public class MainActivity extends AppCompatActivity
|
|||||||
NavigationView navigationView = findViewById(R.id.nav_view);
|
NavigationView navigationView = findViewById(R.id.nav_view);
|
||||||
navigationView.setNavigationItemSelectedListener(this);
|
navigationView.setNavigationItemSelectedListener(this);
|
||||||
|
|
||||||
SwipeRefreshLayout mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
|
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
|
||||||
mSwipeRefreshLayout.setOnRefreshListener(MainActivity.this);
|
mSwipeRefreshLayout.setOnRefreshListener(MainActivity.this);
|
||||||
mSwipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary);
|
mSwipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary);
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
try{
|
|
||||||
getTreeData(false);
|
getTreeData(false);
|
||||||
} catch (IllegalStateException e) {
|
} catch (IllegalStateException e) {
|
||||||
//
|
//
|
||||||
@ -114,14 +118,29 @@ public class MainActivity extends AppCompatActivity
|
|||||||
for (int i = 0; i < firstLevelObjects.size(); i++) {
|
for (int i = 0; i < firstLevelObjects.size(); i++) {
|
||||||
firstLevelNodes.add(new TreeNode(firstLevelObjects.get(i).getName()));
|
firstLevelNodes.add(new TreeNode(firstLevelObjects.get(i).getName()));
|
||||||
firstLevelNodes.get(i).setLevel(0);
|
firstLevelNodes.get(i).setLevel(0);
|
||||||
|
firstLevelNodes.get(i).setChildren(getSecondLevelNodesByParentID(firstLevelObjects.get(i).getID()));
|
||||||
}
|
}
|
||||||
|
|
||||||
rootNode.setChildren(firstLevelNodes);
|
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
|
@Override
|
||||||
public void onRefresh() {
|
public void onRefresh() {
|
||||||
final SwipeRefreshLayout mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
|
|
||||||
mSwipeRefreshLayout.setRefreshing(true);
|
mSwipeRefreshLayout.setRefreshing(true);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -131,9 +150,7 @@ public class MainActivity extends AppCompatActivity
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void getTreeData(boolean clean) throws IllegalStateException {
|
private void getTreeData(final boolean clean) throws IllegalStateException {
|
||||||
|
|
||||||
final SwipeRefreshLayout mSwipeRefreshLayout = findViewById(R.id.swipeRefreshLayout);
|
|
||||||
mSwipeRefreshLayout.setRefreshing(true);
|
mSwipeRefreshLayout.setRefreshing(true);
|
||||||
if(clean) {
|
if(clean) {
|
||||||
for (int i = 0; i < firstLevelNodes.size(); i++) {
|
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++) {
|
for (int i = 0; i < firstLevelObjects.size(); i++) {
|
||||||
firstLevelObjects.remove(i);
|
firstLevelObjects.remove(i);
|
||||||
}
|
}
|
||||||
|
for (int i = 0; i < secondLevelObjects.size(); i++) {
|
||||||
|
secondLevelObjects.remove(i);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
|
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
|
||||||
@ -152,39 +172,56 @@ public class MainActivity extends AppCompatActivity
|
|||||||
} catch(IllegalStateException e) {
|
} catch(IllegalStateException e) {
|
||||||
throw new IllegalStateException();
|
throw new IllegalStateException();
|
||||||
}
|
}
|
||||||
|
class GetTreeData extends AsyncTask<Void, Void, Void> {
|
||||||
vrApi.getFirstLevel().enqueue(new Callback<List<FirstLevelModel>>() {
|
|
||||||
@Override
|
@Override
|
||||||
public void onResponse(Call<List<FirstLevelModel>> call, Response<List<FirstLevelModel>> response) {
|
protected Void doInBackground(Void... voids) {
|
||||||
if(response != null) {
|
|
||||||
try {
|
try {
|
||||||
firstLevelObjects.addAll(response.body());
|
Response<List<FirstLevelModel>> firstLevelResponse = vrApi.getFirstLevel().execute();
|
||||||
mSwipeRefreshLayout.post(new Runnable() {
|
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
|
@Override
|
||||||
public void run() {
|
protected void onPostExecute(Void aVoid) {
|
||||||
mSwipeRefreshLayout.setRefreshing(false);
|
super.onPostExecute(aVoid);
|
||||||
}
|
|
||||||
});
|
|
||||||
} 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);
|
|
||||||
}
|
|
||||||
fillRootNode();
|
fillRootNode();
|
||||||
treeView.refreshTreeView();
|
treeView.refreshTreeView();
|
||||||
} else {
|
|
||||||
Log.e("VRCatalog", "Что-то пошло не так...");
|
|
||||||
mSwipeRefreshLayout.setRefreshing(false);
|
mSwipeRefreshLayout.setRefreshing(false);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
new GetTreeData().execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private void onFailureAnswer(int errorCode) {
|
||||||
public void onFailure(Call<List<FirstLevelModel>> call, Throwable t) {
|
switch (errorCode) {
|
||||||
|
case 0: {
|
||||||
Toast.makeText(MainActivity.this, "Ошибка сети. Проверьте подключение к сети или данные подключения к API!", Toast.LENGTH_LONG).show();
|
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);
|
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;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -4,10 +4,15 @@ import java.util.List;
|
|||||||
|
|
||||||
import retrofit2.Call;
|
import retrofit2.Call;
|
||||||
import retrofit2.http.GET;
|
import retrofit2.http.GET;
|
||||||
|
import retrofit2.http.Query;
|
||||||
import ru.volgorobot.vrcatalog.model.FirstLevelModel;
|
import ru.volgorobot.vrcatalog.model.FirstLevelModel;
|
||||||
|
import ru.volgorobot.vrcatalog.model.SecondLevelModel;
|
||||||
|
|
||||||
public interface VRApi {
|
public interface VRApi {
|
||||||
|
|
||||||
@GET("/API/Api.php?action=getFirstLevel")
|
@GET("/API/Api.php?action=getFirstLevel")
|
||||||
Call<List<FirstLevelModel>> getFirstLevel();
|
Call<List<FirstLevelModel>> getFirstLevel();
|
||||||
|
|
||||||
|
@GET("/API/Api.php?action=getSecondLevel")
|
||||||
|
Call<List<SecondLevelModel>> getSecondLevel(@Query("parentTypeID") int parentTypeID);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user