Added third level loading.

This commit is contained in:
ChronosX88 2019-01-03 21:25:34 +04:00
parent d1cc2cc3dc
commit dc01b7960b
No known key found for this signature in database
GPG Key ID: 8F92E090A87804AA
5 changed files with 123 additions and 56 deletions

View File

@ -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<FirstLevelModel> firstLevelObjects = new ArrayList<>();
private List<TreeNode> firstLevelNodes = new ArrayList<>();
private List<SecondLevelModel> secondLevelObjects = new ArrayList<>();
private List<ThirdLevelModel> 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<TreeNode> getThirdLevelNodesByParentName(String parentName) {
ArrayList<ThirdLevelModel> objects = new ArrayList<>();
for (int i = 0; i < thirdLevelObjects.size(); i++) {
if(thirdLevelObjects.get(i).getName().equals(parentName)) {
objects.add(thirdLevelObjects.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(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<Void, Void, Void> {
class GetTreeData extends AsyncTask<Void, Void, Integer> {
@Override
protected Void doInBackground(Void... voids) {
protected Integer doInBackground(Void... voids) {
try {
Response<List<FirstLevelModel>> 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<List<ThirdLevelModel>> 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;
}

View File

@ -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<List<SecondLevelModel>> getSecondLevel(@Query("parentTypeID") int parentTypeID);
@GET("/API/Api.php?action=getDetailsByParentID")
Call<List<ThirdLevelModel>> getDetails(@Query("parentID") int parentID);
}

View File

@ -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;
}
}

View File

@ -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;
}

View File

@ -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;
}
}