From dc01b7960b8bdc9e3fd2bba5ad0618ddb70314c6 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Thu, 3 Jan 2019 21:25:34 +0400 Subject: [PATCH] Added third level loading. --- .../ru/volgorobot/vrcatalog/MainActivity.java | 70 +++++++++++++++---- .../ru/volgorobot/vrcatalog/api/VRApi.java | 4 ++ .../vrcatalog/model/FirstLevelModel.java | 28 +------- .../vrcatalog/model/SecondLevelModel.java | 15 +--- .../vrcatalog/model/ThirdLevelModel.java | 62 ++++++++++++++++ 5 files changed, 123 insertions(+), 56 deletions(-) create mode 100644 app/src/main/java/ru/volgorobot/vrcatalog/model/ThirdLevelModel.java diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java b/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java index c43d822..a287510 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/MainActivity.java @@ -33,6 +33,7 @@ import ru.volgorobot.vrcatalog.api.Controller; import ru.volgorobot.vrcatalog.api.VRApi; import ru.volgorobot.vrcatalog.model.FirstLevelModel; import ru.volgorobot.vrcatalog.model.SecondLevelModel; +import ru.volgorobot.vrcatalog.model.ThirdLevelModel; public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, SwipeRefreshLayout.OnRefreshListener { @@ -42,6 +43,8 @@ public class MainActivity extends AppCompatActivity private List firstLevelObjects = new ArrayList<>(); private List firstLevelNodes = new ArrayList<>(); private List secondLevelObjects = new ArrayList<>(); + private List thirdLevelObjects = new ArrayList<>(); + private TreeNode rootNode = TreeNode.root(); private SwipeRefreshLayout mSwipeRefreshLayout; @@ -119,6 +122,9 @@ public class MainActivity extends AppCompatActivity firstLevelNodes.add(new TreeNode(firstLevelObjects.get(i).getName())); firstLevelNodes.get(i).setLevel(0); firstLevelNodes.get(i).setChildren(getSecondLevelNodesByParentID(firstLevelObjects.get(i).getID())); + for (int j = 0; j < firstLevelNodes.get(i).getChildren().size(); j++) { + firstLevelNodes.get(i).getChildren().get(j).setChildren(getThirdLevelNodesByParentName((String) firstLevelNodes.get(i).getChildren().get(j).getValue())); + } } rootNode.setChildren(firstLevelNodes); @@ -139,6 +145,21 @@ public class MainActivity extends AppCompatActivity return nodes; } + private ArrayList getThirdLevelNodesByParentName(String parentName) { + ArrayList objects = new ArrayList<>(); + for (int i = 0; i < thirdLevelObjects.size(); i++) { + if(thirdLevelObjects.get(i).getName().equals(parentName)) { + objects.add(thirdLevelObjects.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(2); + } + return nodes; + } + @Override public void onRefresh() { mSwipeRefreshLayout.setRefreshing(true); @@ -162,8 +183,10 @@ public class MainActivity extends AppCompatActivity for (int i = 0; i < secondLevelObjects.size(); i++) { secondLevelObjects.remove(i); } + for (int i = 0; i < thirdLevelObjects.size(); i++) { + thirdLevelObjects.remove(i); + } } - SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); String baseURL = sharedPreferences.getString("addressOfServer", ""); @@ -172,9 +195,9 @@ public class MainActivity extends AppCompatActivity } catch(IllegalStateException e) { throw new IllegalStateException(); } - class GetTreeData extends AsyncTask { + class GetTreeData extends AsyncTask { @Override - protected Void doInBackground(Void... voids) { + protected Integer doInBackground(Void... voids) { try { Response> firstLevelResponse = vrApi.getFirstLevel().execute(); if(firstLevelResponse.isSuccessful()) { @@ -184,22 +207,39 @@ public class MainActivity extends AppCompatActivity if(secondLevelResponse.isSuccessful()) { secondLevelObjects.addAll(secondLevelResponse.body()); } else { - onFailureAnswer(1); + return 2; } } + for (int i = 0; i < secondLevelObjects.size(); i++) { + Response> thirdLevelResponse = vrApi.getDetails(secondLevelObjects.get(i).getID()).execute(); + if(thirdLevelResponse.isSuccessful()) { + thirdLevelObjects.addAll(thirdLevelResponse.body()); + } else { + return 2; + } + } + } else { + return 2; } } catch (Exception e) { - onFailureAnswer(0); + return 1; } - return null; + return 0; } @Override - protected void onPostExecute(Void aVoid) { - super.onPostExecute(aVoid); - fillRootNode(); - treeView.refreshTreeView(); - mSwipeRefreshLayout.setRefreshing(false); + protected void onPostExecute(Integer integer) { + super.onPostExecute(integer); + if(integer == 0) { + fillRootNode(); + treeView.refreshTreeView(); + mSwipeRefreshLayout.setRefreshing(false); + } else if(integer == 1) { + onFailureAnswer(1); + } else if(integer == 2) { + onFailureAnswer(2); + } + } } new GetTreeData().execute(); @@ -207,15 +247,15 @@ public class MainActivity extends AppCompatActivity private void onFailureAnswer(int errorCode) { switch (errorCode) { - case 0: { + case 1: { Toast.makeText(MainActivity.this, "Ошибка сети. Проверьте подключение к сети или данные подключения к API!", Toast.LENGTH_LONG).show(); - Log.e("VRCatalog", "Network Error! Re-check your connection credentials or network settings!"); + Log.e("VRCatalog", "Network Error! Re-check your connection credentials or network settings! Technical info: null"); mSwipeRefreshLayout.setRefreshing(false); break; } - case 1: { + case 2: { Toast.makeText(MainActivity.this, "Ответ от сервера неверен! Перепроверьте данные подключения!", Toast.LENGTH_LONG).show(); - Log.e("VRCatalog", "Answer of server is wrong! Re-check your connection credentials!"); + Log.e("VRCatalog", "Answer of server is wrong! Re-check your connection credentials! Technical info: null"); 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 5a6543a..8d6f164 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/api/VRApi.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/api/VRApi.java @@ -7,6 +7,7 @@ import retrofit2.http.GET; import retrofit2.http.Query; import ru.volgorobot.vrcatalog.model.FirstLevelModel; import ru.volgorobot.vrcatalog.model.SecondLevelModel; +import ru.volgorobot.vrcatalog.model.ThirdLevelModel; public interface VRApi { @@ -15,4 +16,7 @@ public interface VRApi { @GET("/API/Api.php?action=getSecondLevel") Call> getSecondLevel(@Query("parentTypeID") int parentTypeID); + + @GET("/API/Api.php?action=getDetailsByParentID") + Call> getDetails(@Query("parentID") int parentID); } diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/model/FirstLevelModel.java b/app/src/main/java/ru/volgorobot/vrcatalog/model/FirstLevelModel.java index 32af26a..b5579b4 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/model/FirstLevelModel.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/model/FirstLevelModel.java @@ -6,10 +6,6 @@ import com.google.gson.annotations.SerializedName; public class FirstLevelModel { - @SerializedName("Description") - @Expose - private Object description; - @SerializedName("Name") @Expose private String name; @@ -18,28 +14,14 @@ public class FirstLevelModel { @Expose private Integer iD; - @SerializedName("Enable") - @Expose - private Boolean enable; public FirstLevelModel() { // Empty constructor } - public FirstLevelModel(Object description, String name, Integer iD, Boolean enable) { - super(); - this.description = description; + public FirstLevelModel(String name, Integer iD) { this.name = name; this.iD = iD; - this.enable = enable; - } - - public Object getDescription() { - return description; - } - - public void setDescription(Object description) { - this.description = description; } public String getName() { @@ -58,12 +40,4 @@ public class FirstLevelModel { this.iD = iD; } - public Boolean getEnable() { - return enable; - } - - public void setEnable(Boolean enable) { - this.enable = enable; - } - } \ No newline at end of file diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/model/SecondLevelModel.java b/app/src/main/java/ru/volgorobot/vrcatalog/model/SecondLevelModel.java index eb42937..c002540 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/model/SecondLevelModel.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/model/SecondLevelModel.java @@ -13,10 +13,6 @@ public class SecondLevelModel { @Expose private Integer detailTypeId; - @SerializedName("Description") - @Expose - private Object description; - @SerializedName("Name") @Expose private String name; @@ -25,10 +21,9 @@ public class SecondLevelModel { // Empty constructor } - public SecondLevelModel(Integer iD, Integer detailTypeId, Object description, String name) { + public SecondLevelModel(Integer iD, Integer detailTypeId, String name) { this.iD = iD; this.detailTypeId = detailTypeId; - this.description = description; this.name = name; } @@ -48,14 +43,6 @@ public class SecondLevelModel { this.detailTypeId = detailTypeId; } - public Object getDescription() { - return description; - } - - public void setDescription(Object description) { - this.description = description; - } - public String getName() { return name; } diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/model/ThirdLevelModel.java b/app/src/main/java/ru/volgorobot/vrcatalog/model/ThirdLevelModel.java new file mode 100644 index 0000000..11c2c7d --- /dev/null +++ b/app/src/main/java/ru/volgorobot/vrcatalog/model/ThirdLevelModel.java @@ -0,0 +1,62 @@ +package ru.volgorobot.vrcatalog.model; + +import com.google.gson.annotations.Expose; +import com.google.gson.annotations.SerializedName; + +public class ThirdLevelModel { + + @SerializedName("ID") + @Expose + private Integer iD; + @SerializedName("DetailSubTypeId") + @Expose + private Integer detailSubTypeId; + @SerializedName("Name") + @Expose + private String name; + + /** + * No args constructor for use in serialization + * + */ + public ThirdLevelModel() { + } + + /** + * + * @param detailSubTypeId Parent category + * @param name Name of detail + * @param iD ID of detail + */ + public ThirdLevelModel(Integer iD, Integer detailSubTypeId, String name) { + super(); + this.iD = iD; + this.detailSubTypeId = detailSubTypeId; + this.name = name; + } + + public Integer getID() { + return iD; + } + + public void setID(Integer iD) { + this.iD = iD; + } + + public Integer getDetailSubTypeId() { + return detailSubTypeId; + } + + public void setDetailSubTypeId(Integer detailSubTypeId) { + this.detailSubTypeId = detailSubTypeId; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + +} \ No newline at end of file