From 9b3e4ff1ba16740960e6c38346599be83eb49b19 Mon Sep 17 00:00:00 2001 From: SlaVcE <102253539+SlaVcE14@users.noreply.github.com> Date: Tue, 24 Oct 2023 23:26:53 +0200 Subject: [PATCH 01/10] move Java methodes to a separate package --- .../java/com/sjapps/adapters/ListAdapter.java | 2 +- .../com/sjapps/jsonlist/MainActivity.java | 187 +++--------------- .../jsonlist/java/ExceptionCallback.java | 5 + .../com/sjapps/jsonlist/java/JsonData.java | 45 +++++ .../sjapps/jsonlist/java/JsonFunctions.java | 135 +++++++++++++ .../sjapps/jsonlist/{ => java}/ListItem.java | 2 +- 6 files changed, 214 insertions(+), 162 deletions(-) create mode 100644 app/src/main/java/com/sjapps/jsonlist/java/ExceptionCallback.java create mode 100644 app/src/main/java/com/sjapps/jsonlist/java/JsonData.java create mode 100644 app/src/main/java/com/sjapps/jsonlist/java/JsonFunctions.java rename app/src/main/java/com/sjapps/jsonlist/{ => java}/ListItem.java (98%) diff --git a/app/src/main/java/com/sjapps/adapters/ListAdapter.java b/app/src/main/java/com/sjapps/adapters/ListAdapter.java index 0c6ca21..5c60e98 100644 --- a/app/src/main/java/com/sjapps/adapters/ListAdapter.java +++ b/app/src/main/java/com/sjapps/adapters/ListAdapter.java @@ -10,7 +10,7 @@ import android.widget.TextView; import android.widget.Toast; -import com.sjapps.jsonlist.ListItem; +import com.sjapps.jsonlist.java.ListItem; import com.sjapps.jsonlist.MainActivity; import com.sjapps.jsonlist.R; diff --git a/app/src/main/java/com/sjapps/jsonlist/MainActivity.java b/app/src/main/java/com/sjapps/jsonlist/MainActivity.java index 7615b60..d4aa552 100644 --- a/app/src/main/java/com/sjapps/jsonlist/MainActivity.java +++ b/app/src/main/java/com/sjapps/jsonlist/MainActivity.java @@ -1,5 +1,7 @@ package com.sjapps.jsonlist; +import static com.sjapps.jsonlist.java.JsonFunctions.*; + import androidx.activity.result.ActivityResult; import androidx.activity.result.ActivityResultCallback; import androidx.activity.result.ActivityResultLauncher; @@ -23,17 +25,18 @@ import android.widget.TextView; import android.widget.Toast; - import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.sjapps.about.AboutActivity; import com.sjapps.adapters.ListAdapter; +import com.sjapps.jsonlist.java.ExceptionCallback; +import com.sjapps.jsonlist.java.JsonData; +import com.sjapps.jsonlist.java.ListItem; import com.sjapps.library.customdialog.BasicDialog; import java.util.ArrayList; -import java.util.Set; public class MainActivity extends AppCompatActivity { @@ -43,12 +46,11 @@ public class MainActivity extends AppCompatActivity { Button openFileBtn; TextView titleTxt, emptyListTxt; ListView list; - String path = ""; + JsonData data = new JsonData(); ProgressBar progressBar; TextView loadingFileTxt; boolean isMenuOpen; ListAdapter adapter; - ArrayList rootList = new ArrayList<>(); View menu, dim_bg; ViewGroup viewGroup; AutoTransition autoTransition = new AutoTransition(); @@ -118,7 +120,7 @@ public void onBackPressed() { return; } - if (path.equals("")){ + if (data.isEmptyPath()){ BasicDialog dialog = new BasicDialog(); dialog.Builder(this, true) .setTitle("Exit?") @@ -132,28 +134,22 @@ public void onBackPressed() { TransitionManager.beginDelayedTransition(viewGroup, autoTransition); - String[] pathStrings = path.split("///"); - path = ""; + String[] pathStrings = data.splitPath(); + data.clearPath(); String Title = ""; for (int i = 0; i < pathStrings.length-1; i++) { - path = path.concat((path.equals("") ? "" : "///") + pathStrings[i]); - + data.setPath(data.getPath().concat((data.isEmptyPath()?"":"///") + pathStrings[i])); } if (pathStrings.length > 1) { - Title = getName(pathStrings[pathStrings.length-2]); + Title = JsonData.getName(pathStrings[pathStrings.length-2]); } - open(Title, path); - if (path.equals("")) { + open(Title, data.getPath()); + if (data.isEmptyPath()) { backBtn.setVisibility(View.GONE); } } - String getName(String str){ - if (str.startsWith("{") && str.contains("}") && str.substring(1, str.indexOf("}")).matches("^[0-9]+")) - return str.substring(str.indexOf("}")+1); - return str; - } private void initialize() { backBtn = findViewById(R.id.backBtn); @@ -194,7 +190,7 @@ private void LoadData(String Data) { progressBar.setVisibility(View.VISIBLE); new Thread(() -> { - ArrayList temp = rootList; + ArrayList temp = data.getRootList(); JsonElement element; try { element = JsonParser.parseString(Data); @@ -219,27 +215,27 @@ private void LoadData(String Data) { Log.d(TAG, "run: Json object"); JsonObject object = FileSystem.loadDataToJsonObj(element); Log.d(TAG, "LoadData: " + object); - rootList = getJsonObject(object); + data.setRootList(getJsonObject(object,onReadJsonException)); } if (element instanceof JsonArray) { Log.d(TAG, "run: Json array"); JsonArray array = FileSystem.loadDataToJsonArray(element); Log.d(TAG, "LoadData: " + array); - rootList = getJsonArrayRoot(array); + data.setRootList(getJsonArrayRoot(array,onReadJsonException)); } - if (rootList != null) { + if (!data.isRootListNull()) { handler.post(() -> { TransitionManager.beginDelayedTransition(viewGroup, autoTransition); - adapter = new ListAdapter(rootList, MainActivity.this, ""); + adapter = new ListAdapter(data.getRootList(), MainActivity.this, ""); list.setAdapter(adapter); openFileBtn.setVisibility(View.GONE); backBtn.setVisibility(View.GONE); titleTxt.setText(""); - path = ""; + data.clearPath(); }); - } else rootList = temp; + } else data.setRootList(temp); handler.post(() -> progressBar.setVisibility(View.GONE)); @@ -248,140 +244,6 @@ private void LoadData(String Data) { } - ArrayList getJsonArrayRoot(JsonArray array) { - ArrayList Mainlist = new ArrayList<>(); - ListItem item = new ListItem(); - item.setName("Json Array"); - item.setIsArrayOfObjects(true); - item.setListObjects(getJsonArray(array)); - Mainlist.add(item); - - return Mainlist; - } - - ArrayList> getJsonArray(JsonArray array) { - ArrayList> ArrList = new ArrayList<>(); - for (int i = 0; i < array.size(); i++) { - if (array.get(i) instanceof JsonObject) { - ArrayList ListOfItems = getJsonObject((JsonObject) array.get(i)); - ArrList.add(ListOfItems); - } - } - - return ArrList; - - } - - boolean isArrayHasObjects(JsonArray array) { - for (int i = 0; i < array.size(); i++) { - if (!(array.get(i) instanceof JsonObject)) { - return false; - } - } - - return true; - } - - ArrayList getJsonObject(JsonObject obj) { - - ArrayList Mainlist = new ArrayList<>(); - Set keys = obj.keySet(); - - Object[] keysArray = keys.toArray(); - - try { - - - for (Object o : keysArray) { - ListItem item = new ListItem(); - item.setName(o.toString()); - - if (obj.get(o.toString()) instanceof JsonObject) { - item.setIsObject(true); - ArrayList objList = getJsonObject((JsonObject) obj.get(o.toString())); - item.setObjects(objList); - - } else if (obj.get(o.toString()) instanceof JsonArray) { - JsonArray array = (JsonArray) obj.get(o.toString()); - Log.d(TAG, "isArrayHasObjects: " + isArrayHasObjects(array)); - if (isArrayHasObjects(array)) { - item.setIsArrayOfObjects(true); - ArrayList> ArrList = getJsonArray(array); - item.setListObjects(ArrList); - } else { - item.setIsArray(true); - item.setValue(array.toString()); - } - - } else { - - item.setValue(obj.get(o.toString()).toString()); - - } - Mainlist.add(item); - } - } catch (Exception e) { - Log.e(TAG, "getJsonObject: Failed to load data"); - handler.post(() -> Toast.makeText(MainActivity.this, "Failed to load data!", Toast.LENGTH_SHORT).show()); - return null; - } - return Mainlist; - - } - - ArrayList getArrayList(ArrayList> list) { - ArrayList newList = new ArrayList<>(); - for (ArrayList lists : list) { - setId(lists, list.indexOf(lists)); - newList.addAll(lists); - newList.add(new ListItem().Space()); - } - return newList; - } - - private void setId(ArrayList lists, int id) { - - for (ListItem listItem : lists) { - listItem.setId(id); - } - } - - ArrayList getListFromPath() { - - - String[] pathStrings = path.split("///"); - - ArrayList list = rootList; - - for (String pathString : pathStrings) { - - int id = -1; - - if (pathString.startsWith("{") && pathString.contains("}") && pathString.substring(1, pathString.indexOf("}")).matches("^[0-9]+")) { - id = Integer.parseInt(pathString.substring(1, pathString.indexOf("}"))); - } - - for (ListItem item : list) { - if (item.getName() == null || !item.getName().equals(id != -1 ? pathString.substring(pathString.indexOf("}") + 1) : pathString)) - continue; - - if (id != -1 && item.getId() != id) - continue; - - if (item.isArrayOfObjects()) { - list = getArrayList(item.getListObjects()); - break; - } - list = list.get(list.indexOf(item)).getObjects(); - if (list == null) - list = new ArrayList<>(); - break; - } - } - return list; - - } - public void open(String Title, String path) { TransitionManager.beginDelayedTransition(viewGroup, autoTransition); @@ -391,9 +253,9 @@ public void open(String Title, String path) { if (emptyListTxt.getVisibility() == View.VISIBLE) emptyListTxt.setVisibility(View.GONE); - this.path = path; + data.setPath(path); titleTxt.setText(Title); - ArrayList arrayList = getListFromPath(); + ArrayList arrayList = getListFromPath(path,data.getRootList()); adapter = new ListAdapter(arrayList, this, path); list.setAdapter(adapter); if (arrayList.size() == 0) { @@ -450,4 +312,9 @@ public void onActivityResult(ActivityResult result) { } }); + + ExceptionCallback onReadJsonException = e -> { + Log.e(TAG, "getJsonObject: Failed to load data"); + handler.post(() -> Toast.makeText(MainActivity.this, "Failed to load data!", Toast.LENGTH_SHORT).show()); + }; } \ No newline at end of file diff --git a/app/src/main/java/com/sjapps/jsonlist/java/ExceptionCallback.java b/app/src/main/java/com/sjapps/jsonlist/java/ExceptionCallback.java new file mode 100644 index 0000000..454fe58 --- /dev/null +++ b/app/src/main/java/com/sjapps/jsonlist/java/ExceptionCallback.java @@ -0,0 +1,5 @@ +package com.sjapps.jsonlist.java; + +public interface ExceptionCallback { + void onException(Exception e); +} diff --git a/app/src/main/java/com/sjapps/jsonlist/java/JsonData.java b/app/src/main/java/com/sjapps/jsonlist/java/JsonData.java new file mode 100644 index 0000000..e122231 --- /dev/null +++ b/app/src/main/java/com/sjapps/jsonlist/java/JsonData.java @@ -0,0 +1,45 @@ +package com.sjapps.jsonlist.java; + +import java.util.ArrayList; + +public class JsonData { + String path = ""; + ArrayList rootList = new ArrayList<>(); + + public String getPath() { + return path; + } + + public void setPath(String path) { + this.path = path; + } + + public ArrayList getRootList() { + return rootList; + } + + public void setRootList(ArrayList rootList) { + this.rootList = rootList; + } + + public boolean isEmptyPath(){ + return path.equals(""); + } + public void clearPath(){ + path = ""; + } + public String[] splitPath(){ + return path.split("///"); + } + + public boolean isRootListNull(){ + return rootList == null; + } + + + public static String getName(String str){ + if (str.startsWith("{") && str.contains("}") && str.substring(1, str.indexOf("}")).matches("^[0-9]+")) + return str.substring(str.indexOf("}")+1); + return str; + } +} diff --git a/app/src/main/java/com/sjapps/jsonlist/java/JsonFunctions.java b/app/src/main/java/com/sjapps/jsonlist/java/JsonFunctions.java new file mode 100644 index 0000000..fd46660 --- /dev/null +++ b/app/src/main/java/com/sjapps/jsonlist/java/JsonFunctions.java @@ -0,0 +1,135 @@ +package com.sjapps.jsonlist.java; + +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; + +import java.util.ArrayList; +import java.util.Set; + +public class JsonFunctions { + + public static ArrayList getJsonArrayRoot(JsonArray array, ExceptionCallback callback) { + ArrayList mainList = new ArrayList<>(); + ListItem item = new ListItem(); + item.setName("Json Array"); + item.setIsArrayOfObjects(true); + item.setListObjects(getJsonArray(array,callback)); + mainList.add(item); + + return mainList; + } + + public static ArrayList> getJsonArray(JsonArray array, ExceptionCallback callback) { + ArrayList> ArrList = new ArrayList<>(); + for (int i = 0; i < array.size(); i++) { + if (array.get(i) instanceof JsonObject) { + ArrayList ListOfItems = getJsonObject((JsonObject) array.get(i),callback); + ArrList.add(ListOfItems); + } + } + return ArrList; + } + + static boolean isArrayHasObjects(JsonArray array) { + for (int i = 0; i < array.size(); i++) { + if (!(array.get(i) instanceof JsonObject)) { + return false; + } + } + return true; + } + + public static ArrayList getJsonObject(JsonObject obj, ExceptionCallback callback) { + ArrayList mainList = new ArrayList<>(); + Set keys = obj.keySet(); + Object[] keysArray = keys.toArray(); + + try { + for (Object o : keysArray) { + ListItem item = new ListItem(); + item.setName(o.toString()); + setItem(obj,o,item,callback); + mainList.add(item); + } + } catch (Exception e) { + callback.onException(e); + return null; + } + return mainList; + } + + private static void setItem(JsonObject obj, Object o, ListItem item, ExceptionCallback callback){ + if (obj.get(o.toString()) instanceof JsonObject) { + item.setIsObject(true); + ArrayList objList = getJsonObject((JsonObject) obj.get(o.toString()), callback); + item.setObjects(objList); + return; + } + if (obj.get(o.toString()) instanceof JsonArray) { + JsonArray array = (JsonArray) obj.get(o.toString()); + if (isArrayHasObjects(array)) { + item.setIsArrayOfObjects(true); + ArrayList> ArrList = getJsonArray(array,callback); + item.setListObjects(ArrList); + return; + } + item.setIsArray(true); + item.setValue(array.toString()); + return; + } + item.setValue(obj.get(o.toString()).toString()); + } + + static ArrayList getArrayList(ArrayList> list) { + ArrayList newList = new ArrayList<>(); + for (ArrayList lists : list) { + setId(lists, list.indexOf(lists)); + newList.addAll(lists); + newList.add(new ListItem().Space()); + } + return newList; + } + + private static void setId(ArrayList lists, int id) { + + for (ListItem listItem : lists) { + listItem.setId(id); + } + } + + public static ArrayList getListFromPath(String path, ArrayList rootList) { + + + String[] pathStrings = path.split("///"); + + ArrayList list = rootList; + + for (String pathString : pathStrings) { + + int id = -1; + + if (pathString.startsWith("{") && pathString.contains("}") && pathString.substring(1, pathString.indexOf("}")).matches("^[0-9]+")) { + id = Integer.parseInt(pathString.substring(1, pathString.indexOf("}"))); + } + + for (ListItem item : list) { + if (item.getName() == null || !item.getName().equals(id != -1 ? pathString.substring(pathString.indexOf("}") + 1) : pathString)) + continue; + + if (id != -1 && item.getId() != id) + continue; + + if (item.isArrayOfObjects()) { + list = getArrayList(item.getListObjects()); + break; + } + list = list.get(list.indexOf(item)).getObjects(); + if (list == null) + list = new ArrayList<>(); + break; + } + } + return list; + + } +} diff --git a/app/src/main/java/com/sjapps/jsonlist/ListItem.java b/app/src/main/java/com/sjapps/jsonlist/java/ListItem.java similarity index 98% rename from app/src/main/java/com/sjapps/jsonlist/ListItem.java rename to app/src/main/java/com/sjapps/jsonlist/java/ListItem.java index 2fc7324..29f2d10 100644 --- a/app/src/main/java/com/sjapps/jsonlist/ListItem.java +++ b/app/src/main/java/com/sjapps/jsonlist/java/ListItem.java @@ -1,4 +1,4 @@ -package com.sjapps.jsonlist; +package com.sjapps.jsonlist.java; import java.util.ArrayList; From c6f4e0747e9722c5fa83a77a845e0fe5a13dfb58 Mon Sep 17 00:00:00 2001 From: SlaVcE <102253539+SlaVcE14@users.noreply.github.com> Date: Wed, 25 Oct 2023 20:53:11 +0200 Subject: [PATCH 02/10] replace onBackPressed to OnBackPressedDispatcher --- .../com/sjapps/jsonlist/MainActivity.java | 75 ++++++++++--------- 1 file changed, 39 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/com/sjapps/jsonlist/MainActivity.java b/app/src/main/java/com/sjapps/jsonlist/MainActivity.java index d4aa552..d086dab 100644 --- a/app/src/main/java/com/sjapps/jsonlist/MainActivity.java +++ b/app/src/main/java/com/sjapps/jsonlist/MainActivity.java @@ -2,6 +2,7 @@ import static com.sjapps.jsonlist.java.JsonFunctions.*; +import androidx.activity.OnBackPressedCallback; import androidx.activity.result.ActivityResult; import androidx.activity.result.ActivityResultCallback; import androidx.activity.result.ActivityResultLauncher; @@ -70,7 +71,7 @@ protected void onCreate(Bundle savedInstanceState) { autoTransition.setDuration(150); menuBtn.setOnClickListener(view -> open_closeMenu()); - backBtn.setOnClickListener(view -> onBackPressed()); + backBtn.setOnClickListener(view -> getOnBackPressedDispatcher().onBackPressed()); openFileBtn.setOnClickListener(view -> ImportFromFile()); menu.findViewById(R.id.openFileBtn2).setOnClickListener(view -> { @@ -106,52 +107,54 @@ private void OpenAbout() { startActivity(new Intent(MainActivity.this, AboutActivity.class)); } - @Override - public void onBackPressed() { - - if (isMenuOpen) { - open_closeMenu(); - return; - } + OnBackPressedCallback backPressedCallback = new OnBackPressedCallback(true) { + @Override + public void handleOnBackPressed() { + if (isMenuOpen) { + open_closeMenu(); + return; + } - if (adapter!= null && adapter.selectedItem != -1){ - adapter.selectedItem = -1; - adapter.notifyDataSetChanged(); - return; - } + if (adapter!= null && adapter.selectedItem != -1){ + adapter.selectedItem = -1; + adapter.notifyDataSetChanged(); + return; + } - if (data.isEmptyPath()){ - BasicDialog dialog = new BasicDialog(); - dialog.Builder(this, true) - .setTitle("Exit?") - .setRightButtonText("Yes") - .onButtonClick(this::finish) - .show(); - return; - } + if (data.isEmptyPath()){ + BasicDialog dialog = new BasicDialog(); + dialog.Builder(MainActivity.this, true) + .setTitle("Exit?") + .setRightButtonText("Yes") + .onButtonClick(MainActivity.this::finish) + .show(); + return; + } - TransitionManager.beginDelayedTransition(viewGroup, autoTransition); + TransitionManager.beginDelayedTransition(viewGroup, autoTransition); - String[] pathStrings = data.splitPath(); - data.clearPath(); + String[] pathStrings = data.splitPath(); + data.clearPath(); - String Title = ""; - for (int i = 0; i < pathStrings.length-1; i++) { - data.setPath(data.getPath().concat((data.isEmptyPath()?"":"///") + pathStrings[i])); - } - if (pathStrings.length > 1) { - Title = JsonData.getName(pathStrings[pathStrings.length-2]); - } + String Title = ""; + for (int i = 0; i < pathStrings.length-1; i++) { + data.setPath(data.getPath().concat((data.isEmptyPath()?"":"///") + pathStrings[i])); + } + if (pathStrings.length > 1) { + Title = JsonData.getName(pathStrings[pathStrings.length-2]); + } - open(Title, data.getPath()); - if (data.isEmptyPath()) { - backBtn.setVisibility(View.GONE); + open(Title, data.getPath()); + if (data.isEmptyPath()) { + backBtn.setVisibility(View.GONE); + } } - } + }; private void initialize() { + getOnBackPressedDispatcher().addCallback(this, backPressedCallback); backBtn = findViewById(R.id.backBtn); menuBtn = findViewById(R.id.menuBtn); titleTxt = findViewById(R.id.titleTxt); From 5d595e5bfe667d859136e9e146a59a8626a9fa37 Mon Sep 17 00:00:00 2001 From: SlaVcE <102253539+SlaVcE14@users.noreply.github.com> Date: Wed, 25 Oct 2023 20:57:19 +0200 Subject: [PATCH 03/10] rename method --- app/src/main/java/com/sjapps/jsonlist/java/JsonFunctions.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/sjapps/jsonlist/java/JsonFunctions.java b/app/src/main/java/com/sjapps/jsonlist/java/JsonFunctions.java index fd46660..32e5d80 100644 --- a/app/src/main/java/com/sjapps/jsonlist/java/JsonFunctions.java +++ b/app/src/main/java/com/sjapps/jsonlist/java/JsonFunctions.java @@ -30,7 +30,7 @@ public static ArrayList> getJsonArray(JsonArray array, Excep return ArrList; } - static boolean isArrayHasObjects(JsonArray array) { + static boolean isArrayOfObjects(JsonArray array) { for (int i = 0; i < array.size(); i++) { if (!(array.get(i) instanceof JsonObject)) { return false; @@ -67,7 +67,7 @@ private static void setItem(JsonObject obj, Object o, ListItem item, ExceptionCa } if (obj.get(o.toString()) instanceof JsonArray) { JsonArray array = (JsonArray) obj.get(o.toString()); - if (isArrayHasObjects(array)) { + if (isArrayOfObjects(array)) { item.setIsArrayOfObjects(true); ArrayList> ArrList = getJsonArray(array,callback); item.setListObjects(ArrList); From abc230d7b600407531bf81216c9a261e5a2fdd64 Mon Sep 17 00:00:00 2001 From: SlaVcE <102253539+SlaVcE14@users.noreply.github.com> Date: Fri, 27 Oct 2023 20:11:44 +0200 Subject: [PATCH 04/10] change reading and displaying strings --- app/src/main/java/com/sjapps/adapters/ListAdapter.java | 2 +- app/src/main/java/com/sjapps/jsonlist/java/JsonFunctions.java | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/sjapps/adapters/ListAdapter.java b/app/src/main/java/com/sjapps/adapters/ListAdapter.java index 5c60e98..4203e32 100644 --- a/app/src/main/java/com/sjapps/adapters/ListAdapter.java +++ b/app/src/main/java/com/sjapps/adapters/ListAdapter.java @@ -97,7 +97,7 @@ public View getView(int position, View convertView, ViewGroup parent) { TextView valueTxt = view.findViewById(R.id.itemValue); view.setClickable(false); titleTxt.setText(item.getName()); - valueTxt.setText(item.getValue()); + valueTxt.setText(item.getValue().isEmpty() ? "\"\"" : item.getValue()); return view; } diff --git a/app/src/main/java/com/sjapps/jsonlist/java/JsonFunctions.java b/app/src/main/java/com/sjapps/jsonlist/java/JsonFunctions.java index 32e5d80..f9b3083 100644 --- a/app/src/main/java/com/sjapps/jsonlist/java/JsonFunctions.java +++ b/app/src/main/java/com/sjapps/jsonlist/java/JsonFunctions.java @@ -77,7 +77,8 @@ private static void setItem(JsonObject obj, Object o, ListItem item, ExceptionCa item.setValue(array.toString()); return; } - item.setValue(obj.get(o.toString()).toString()); + String value = obj.get(o.toString()).toString(); + item.setValue(value.startsWith("\"") && value.endsWith("\"") ? value.substring(1,value.length()-1) : value); } static ArrayList getArrayList(ArrayList> list) { From 619efe03e05150dd6a3c2b90ac2c72da0ee42e38 Mon Sep 17 00:00:00 2001 From: SlaVcE <102253539+SlaVcE14@users.noreply.github.com> Date: Fri, 27 Oct 2023 20:12:06 +0200 Subject: [PATCH 05/10] add toString in ListItem --- .../main/java/com/sjapps/jsonlist/java/ListItem.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/app/src/main/java/com/sjapps/jsonlist/java/ListItem.java b/app/src/main/java/com/sjapps/jsonlist/java/ListItem.java index 29f2d10..450ff57 100644 --- a/app/src/main/java/com/sjapps/jsonlist/java/ListItem.java +++ b/app/src/main/java/com/sjapps/jsonlist/java/ListItem.java @@ -1,6 +1,7 @@ package com.sjapps.jsonlist.java; import java.util.ArrayList; +import java.util.Objects; public class ListItem { @@ -110,5 +111,16 @@ public ListItem Space() { return this; } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof ListItem)) return false; + ListItem item = (ListItem) o; + return isArrayOfObjects() == item.isArrayOfObjects() && isArray() == item.isArray() && isObject() == item.isObject() && isSpace() == item.isSpace() && java.util.Objects.equals(getName(), item.getName()) && java.util.Objects.equals(getValue(), item.getValue()) && java.util.Objects.equals(getObjects(), item.getObjects()) && java.util.Objects.equals(getListObjects(), item.getListObjects()); + } + @Override + public int hashCode() { + return java.util.Objects.hash(getName(), getValue(), isArrayOfObjects(), isArray(), isObject(), isSpace(), getObjects(), getListObjects()); + } } From 97fbda9348b163e349bdc1dede489de56b0a4301 Mon Sep 17 00:00:00 2001 From: SlaVcE <102253539+SlaVcE14@users.noreply.github.com> Date: Fri, 27 Oct 2023 20:12:59 +0200 Subject: [PATCH 06/10] fix window leak --- app/src/main/java/com/sjapps/jsonlist/MainActivity.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/sjapps/jsonlist/MainActivity.java b/app/src/main/java/com/sjapps/jsonlist/MainActivity.java index d086dab..ee66999 100644 --- a/app/src/main/java/com/sjapps/jsonlist/MainActivity.java +++ b/app/src/main/java/com/sjapps/jsonlist/MainActivity.java @@ -126,7 +126,10 @@ public void handleOnBackPressed() { dialog.Builder(MainActivity.this, true) .setTitle("Exit?") .setRightButtonText("Yes") - .onButtonClick(MainActivity.this::finish) + .onButtonClick(() ->{ + dialog.dismiss(); + MainActivity.this.finish(); + }) .show(); return; } From 66e2efb9727c5bc619b0f833f85d2c2b895b462c Mon Sep 17 00:00:00 2001 From: SlaVcE <102253539+SlaVcE14@users.noreply.github.com> Date: Sat, 28 Oct 2023 22:22:37 +0200 Subject: [PATCH 07/10] update titleTxt --- app/src/main/res/layout/activity_main.xml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index e78542e..aea940e 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -28,16 +28,16 @@ Date: Sat, 28 Oct 2023 23:34:53 +0200 Subject: [PATCH 08/10] Update JsonData.java - goBack() - splitPath(String path) --- .../com/sjapps/jsonlist/java/JsonData.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/app/src/main/java/com/sjapps/jsonlist/java/JsonData.java b/app/src/main/java/com/sjapps/jsonlist/java/JsonData.java index e122231..ceb9ec9 100644 --- a/app/src/main/java/com/sjapps/jsonlist/java/JsonData.java +++ b/app/src/main/java/com/sjapps/jsonlist/java/JsonData.java @@ -31,11 +31,35 @@ public void clearPath(){ public String[] splitPath(){ return path.split("///"); } + public static String[] splitPath(String path){ + return path.split("///"); + } public boolean isRootListNull(){ return rootList == null; } + public void goBack(){ + + String[] pathStrings = splitPath(); + clearPath(); + for (int i = 0; i < pathStrings.length-1; i++) { + setPath(path.concat((isEmptyPath()?"":"///") + pathStrings[i])); + } + + } + + public static String getPathFormat(String path){ + String[] pathStrings = splitPath(path); + StringBuilder builder = new StringBuilder(); + builder.append(pathStrings.length > 3 ? "..." : pathStrings[0]); + + for (int i = pathStrings.length > 3? pathStrings.length-3 : 1; i < pathStrings.length; i++) { + builder.append("/").append(getName(pathStrings[i])); + } + + return builder.toString(); + } public static String getName(String str){ if (str.startsWith("{") && str.contains("}") && str.substring(1, str.indexOf("}")).matches("^[0-9]+")) From 343ef77b1258ca520639e695fb374b26c4f7da63 Mon Sep 17 00:00:00 2001 From: SlaVcE <102253539+SlaVcE14@users.noreply.github.com> Date: Sat, 28 Oct 2023 23:48:34 +0200 Subject: [PATCH 09/10] Update MainActivity.java --- .../main/java/com/sjapps/jsonlist/MainActivity.java | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/sjapps/jsonlist/MainActivity.java b/app/src/main/java/com/sjapps/jsonlist/MainActivity.java index ee66999..ba9e5c9 100644 --- a/app/src/main/java/com/sjapps/jsonlist/MainActivity.java +++ b/app/src/main/java/com/sjapps/jsonlist/MainActivity.java @@ -138,18 +138,11 @@ public void handleOnBackPressed() { TransitionManager.beginDelayedTransition(viewGroup, autoTransition); - String[] pathStrings = data.splitPath(); - data.clearPath(); - String Title = ""; - for (int i = 0; i < pathStrings.length-1; i++) { - data.setPath(data.getPath().concat((data.isEmptyPath()?"":"///") + pathStrings[i])); - } - if (pathStrings.length > 1) { - Title = JsonData.getName(pathStrings[pathStrings.length-2]); - } + data.goBack(); - open(Title, data.getPath()); + String[] pathString = data.splitPath(); + open(JsonData.getName(pathString[pathString.length-1]), data.getPath()); if (data.isEmptyPath()) { backBtn.setVisibility(View.GONE); } From f116a8ca836ec336eac44a1b862ec78777e81e9d Mon Sep 17 00:00:00 2001 From: SlaVcE <102253539+SlaVcE14@users.noreply.github.com> Date: Sat, 28 Oct 2023 23:49:22 +0200 Subject: [PATCH 10/10] fix visibility on emptyListTxt --- app/src/main/java/com/sjapps/jsonlist/MainActivity.java | 1 + 1 file changed, 1 insertion(+) diff --git a/app/src/main/java/com/sjapps/jsonlist/MainActivity.java b/app/src/main/java/com/sjapps/jsonlist/MainActivity.java index ba9e5c9..9002641 100644 --- a/app/src/main/java/com/sjapps/jsonlist/MainActivity.java +++ b/app/src/main/java/com/sjapps/jsonlist/MainActivity.java @@ -187,6 +187,7 @@ private void open_closeMenu() { private void LoadData(String Data) { progressBar.setVisibility(View.VISIBLE); + emptyListTxt.setVisibility(View.GONE); new Thread(() -> { ArrayList temp = data.getRootList();