diff --git a/app/src/main/java/com/sjapps/adapters/ListAdapter.java b/app/src/main/java/com/sjapps/adapters/ListAdapter.java index 0c6ca21..4203e32 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; @@ -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/MainActivity.java b/app/src/main/java/com/sjapps/jsonlist/MainActivity.java index 7615b60..9002641 100644 --- a/app/src/main/java/com/sjapps/jsonlist/MainActivity.java +++ b/app/src/main/java/com/sjapps/jsonlist/MainActivity.java @@ -1,5 +1,8 @@ package com.sjapps.jsonlist; +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; @@ -23,17 +26,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 +47,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(); @@ -68,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 -> { @@ -104,58 +107,50 @@ private void OpenAbout() { startActivity(new Intent(MainActivity.this, AboutActivity.class)); } - @Override - public void onBackPressed() { - - if (isMenuOpen) { - open_closeMenu(); - return; - } - - if (adapter!= null && adapter.selectedItem != -1){ - adapter.selectedItem = -1; - adapter.notifyDataSetChanged(); - return; - } + OnBackPressedCallback backPressedCallback = new OnBackPressedCallback(true) { + @Override + public void handleOnBackPressed() { + if (isMenuOpen) { + open_closeMenu(); + return; + } - if (path.equals("")){ - BasicDialog dialog = new BasicDialog(); - dialog.Builder(this, true) - .setTitle("Exit?") - .setRightButtonText("Yes") - .onButtonClick(this::finish) - .show(); - return; - } + if (adapter!= null && adapter.selectedItem != -1){ + adapter.selectedItem = -1; + adapter.notifyDataSetChanged(); + return; + } + if (data.isEmptyPath()){ + BasicDialog dialog = new BasicDialog(); + dialog.Builder(MainActivity.this, true) + .setTitle("Exit?") + .setRightButtonText("Yes") + .onButtonClick(() ->{ + dialog.dismiss(); + MainActivity.this.finish(); + }) + .show(); + return; + } - TransitionManager.beginDelayedTransition(viewGroup, autoTransition); + TransitionManager.beginDelayedTransition(viewGroup, autoTransition); - String[] pathStrings = path.split("///"); - path = ""; - String Title = ""; - for (int i = 0; i < pathStrings.length-1; i++) { - path = path.concat((path.equals("") ? "" : "///") + pathStrings[i]); - } - if (pathStrings.length > 1) { - Title = getName(pathStrings[pathStrings.length-2]); - } + data.goBack(); - open(Title, path); - if (path.equals("")) { - backBtn.setVisibility(View.GONE); + String[] pathString = data.splitPath(); + open(JsonData.getName(pathString[pathString.length-1]), 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() { + getOnBackPressedDispatcher().addCallback(this, backPressedCallback); backBtn = findViewById(R.id.backBtn); menuBtn = findViewById(R.id.menuBtn); titleTxt = findViewById(R.id.titleTxt); @@ -192,9 +187,10 @@ private void open_closeMenu() { private void LoadData(String Data) { progressBar.setVisibility(View.VISIBLE); + emptyListTxt.setVisibility(View.GONE); 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..ceb9ec9 --- /dev/null +++ b/app/src/main/java/com/sjapps/jsonlist/java/JsonData.java @@ -0,0 +1,69 @@ +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 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]+")) + 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..f9b3083 --- /dev/null +++ b/app/src/main/java/com/sjapps/jsonlist/java/JsonFunctions.java @@ -0,0 +1,136 @@ +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 isArrayOfObjects(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 (isArrayOfObjects(array)) { + item.setIsArrayOfObjects(true); + ArrayList> ArrList = getJsonArray(array,callback); + item.setListObjects(ArrList); + return; + } + item.setIsArray(true); + item.setValue(array.toString()); + return; + } + 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) { + 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 75% 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..450ff57 100644 --- a/app/src/main/java/com/sjapps/jsonlist/ListItem.java +++ b/app/src/main/java/com/sjapps/jsonlist/java/ListItem.java @@ -1,6 +1,7 @@ -package com.sjapps.jsonlist; +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()); + } } 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 @@