From 3b46a96b0d5d15156b167390074a878e3ad1f99e Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Thu, 3 Jan 2019 16:27:45 +0400 Subject: [PATCH] Made getting subcategories. --- .../ru/volgorobot/vrcatalog/MainActivity.java | 103 ++++++++++++------ .../ru/volgorobot/vrcatalog/api/VRApi.java | 5 + 2 files changed, 75 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java b/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java index 1f71f27..c43d822 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java @@ -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 firstLevelObjects = new ArrayList<>(); private List firstLevelNodes = new ArrayList<>(); + private List 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 getSecondLevelNodesByParentID(int parentTypeID) { + ArrayList objects = new ArrayList<>(); + for (int i = 0; i < secondLevelObjects.size(); i++) { + if(secondLevelObjects.get(i).getDetailTypeId() == parentTypeID) { + objects.add(secondLevelObjects.get(i)); + } + } + ArrayList 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>() { + class GetTreeData extends AsyncTask { @Override - public void onResponse(Call> call, Response> response) { - if(response != null) { - try { - firstLevelObjects.addAll(response.body()); - mSwipeRefreshLayout.post(new Runnable() { - @Override - public void run() { - mSwipeRefreshLayout.setRefreshing(false); + protected Void doInBackground(Void... voids) { + try { + Response> firstLevelResponse = vrApi.getFirstLevel().execute(); + if(firstLevelResponse.isSuccessful()) { + firstLevelObjects.addAll(firstLevelResponse.body()); + for (int i = 0; i < firstLevelObjects.size(); i++) { + Response> secondLevelResponse = vrApi.getSecondLevel(firstLevelObjects.get(i).getID()).execute(); + if(secondLevelResponse.isSuccessful()) { + secondLevelObjects.addAll(secondLevelResponse.body()); + } else { + onFailureAnswer(1); } - }); - } 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(); - treeView.refreshTreeView(); - } else { - Log.e("VRCatalog", "Что-то пошло не так..."); - mSwipeRefreshLayout.setRefreshing(false); + } catch (Exception e) { + onFailureAnswer(0); } - + return null; } @Override - public void onFailure(Call> call, Throwable t) { - 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()); + protected void onPostExecute(Void aVoid) { + super.onPostExecute(aVoid); + fillRootNode(); + treeView.refreshTreeView(); mSwipeRefreshLayout.setRefreshing(false); } - }); + } + new GetTreeData().execute(); } + + 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!"); + 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; + } + } + } + } + + diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/api/VRApi.java b/app/src/main/java/ru/volgorobot/vrcatalog/api/VRApi.java index 8c6cafa..5a6543a 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/api/VRApi.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/api/VRApi.java @@ -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> getFirstLevel(); + + @GET("/API/Api.php?action=getSecondLevel") + Call> getSecondLevel(@Query("parentTypeID") int parentTypeID); }