Skip to content

Commit

Permalink
feat: save formatted json instead
Browse files Browse the repository at this point in the history
  • Loading branch information
trindadedev13 committed Jan 3, 2025
1 parent 183776e commit 6599b05
Show file tree
Hide file tree
Showing 17 changed files with 143 additions and 119 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 1,7 @@
package mod.hilal.saif.activities.android_manifest;

import static pro.sketchware.utility.SketchwareUtil.getDip;
import static pro.sketchware.utility.GsonUtils.getGson;

import android.annotation.SuppressLint;
import android.content.Intent;
Expand All @@ -22,7 23,6 @@
import com.besome.sketch.lib.base.BaseAppCompatActivity;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.textfield.TextInputEditText;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -83,7 83,7 @@ public void onResume() {
private void checkAttrs() {
String path = FileUtil.getExternalStorageDir().concat("/.sketchware/data/").concat(sc_id).concat("/Injection/androidmanifest/attributes.json");
if (FileUtil.isExistFile(path)) {
ArrayList<HashMap<String, Object>> data = new Gson().fromJson(FileUtil.readFile(path),
ArrayList<HashMap<String, Object>> data = getGson().fromJson(FileUtil.readFile(path),
Helper.TYPE_MAP_LIST);
for (int i = 0; i < data.size(); i ) {
String str = (String) data.get(i).get("name");
Expand All @@ -99,7 99,7 @@ private void checkAttrs() {
_item.put("name", "_application_attrs");
_item.put("value", "android:theme=\"@style/AppTheme\"");
data.add(_item);
FileUtil.writeFile(path, new Gson().toJson(data));
FileUtil.writeFile(path, getGson().toJson(data));
}
}

Expand Down Expand Up @@ -245,7 245,7 @@ private void addNewActivity(String componentName) {
String path = FileUtil.getExternalStorageDir().concat("/.sketchware/data/").concat(sc_id).concat("/Injection/androidmanifest/attributes.json");
ArrayList<HashMap<String, Object>> data = new ArrayList<>();
if (FileUtil.isExistFile(path)) {
data = new Gson().fromJson(FileUtil.readFile(path), Helper.TYPE_MAP_LIST);
data = getGson().fromJson(FileUtil.readFile(path), Helper.TYPE_MAP_LIST);
}
{
HashMap<String, Object> _item = new HashMap<>();
Expand Down Expand Up @@ -292,7 292,7 @@ private void addNewActivity(String componentName) {
}


FileUtil.writeFile(path, new Gson().toJson(data));
FileUtil.writeFile(path, getGson().toJson(data));
refreshList();

}
Expand All @@ -303,7 303,7 @@ private void refreshList() {
ArrayList<String> temp = new ArrayList<>();
ArrayList<HashMap<String, Object>> data;
if (FileUtil.isExistFile(path)) {
data = new Gson().fromJson(FileUtil.readFile(path), Helper.TYPE_MAP_LIST);
data = getGson().fromJson(FileUtil.readFile(path), Helper.TYPE_MAP_LIST);
for (int i = 0; i < data.size(); i ) {
if (!temp.contains(Objects.requireNonNull(data.get(i).get("name")).toString())) {
if (!Objects.requireNonNull(data.get(i).get("name")).equals("_application_attrs") && !Objects.requireNonNull(data.get(i).get("name")).equals("_apply_for_all_activities") && !Objects.requireNonNull(data.get(i).get("name")).equals("_application_permissions")) {
Expand All @@ -325,14 325,14 @@ private void deleteActivity(int pos) {
String activity_name = (String) list_map.get(pos).get("act_name");
String path = FileUtil.getExternalStorageDir().concat("/.sketchware/data/").concat(sc_id).concat("/Injection/androidmanifest/attributes.json");
ArrayList<HashMap<String, Object>> data;
data = new Gson().fromJson(FileUtil.readFile(path), Helper.TYPE_MAP_LIST);
data = getGson().fromJson(FileUtil.readFile(path), Helper.TYPE_MAP_LIST);
for (int i = data.size() - 1; i > -1; i--) {
String temp = (String) data.get(i).get("name");
if (Objects.requireNonNull(temp).equals(activity_name)) {
data.remove(i);
}
}
FileUtil.writeFile(path, new Gson().toJson(data));
FileUtil.writeFile(path, getGson().toJson(data));
refreshList();
removeComponents(activity_name);
SketchwareUtil.toast("Activity removed");
Expand All @@ -342,15 342,15 @@ private void removeComponents(String str) {
String path = FileUtil.getExternalStorageDir().concat("/.sketchware/data/").concat(sc_id).concat("/Injection/androidmanifest/activities_components.json");
ArrayList<HashMap<String, Object>> data;
if (FileUtil.isExistFile(path)) {
data = new Gson().fromJson(FileUtil.readFile(path), Helper.TYPE_MAP_LIST);
data = getGson().fromJson(FileUtil.readFile(path), Helper.TYPE_MAP_LIST);
for (int i = data.size() - 1; i > -1; i--) {
String name = (String) data.get(i).get("name");
if (Objects.requireNonNull(name).equals(str)) {
data.remove(i);
break;
}
}
FileUtil.writeFile(path, new Gson().toJson(data));
FileUtil.writeFile(path, getGson().toJson(data));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 1,7 @@
package mod.hilal.saif.activities.android_manifest;

import static pro.sketchware.utility.SketchwareUtil.getDip;
import static pro.sketchware.utility.GsonUtils.getGson;

import android.os.Bundle;
import android.text.SpannableString;
Expand All @@ -12,7 13,6 @@
import android.widget.TextView;

import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.gson.Gson;

import pro.sketchware.utility.ThemeUtils;
import pro.sketchware.R;
Expand Down Expand Up @@ -86,7 86,7 @@ private void refreshList() {
listMap.clear();
ArrayList<HashMap<String, Object>> data;
if (FileUtil.isExistFile(ATTRIBUTES_FILE_PATH)) {
data = new Gson().fromJson(FileUtil.readFile(ATTRIBUTES_FILE_PATH), Helper.TYPE_MAP_LIST);
data = getGson().fromJson(FileUtil.readFile(ATTRIBUTES_FILE_PATH), Helper.TYPE_MAP_LIST);
for (HashMap<String, Object> item : data) {
String str = (String) item.get("name");
if (str.equals(constant)) {
Expand Down Expand Up @@ -168,7 168,7 @@ private void showAddDial() {
private void applyChange() {
ArrayList<HashMap<String, Object>> data;
if (FileUtil.isExistFile(ATTRIBUTES_FILE_PATH)) {
data = new Gson().fromJson(FileUtil.readFile(ATTRIBUTES_FILE_PATH), Helper.TYPE_MAP_LIST);
data = getGson().fromJson(FileUtil.readFile(ATTRIBUTES_FILE_PATH), Helper.TYPE_MAP_LIST);
for (int i = data.size() - 1; i > -1; i--) {
String str = (String) data.get(i).get("name");
if (str.equals(constant)) {
Expand All @@ -179,7 179,7 @@ private void applyChange() {
} else {
data = new ArrayList<>(listMap);
}
FileUtil.writeFile(ATTRIBUTES_FILE_PATH, new Gson().toJson(data));
FileUtil.writeFile(ATTRIBUTES_FILE_PATH, getGson().toJson(data));
refreshList();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 1,7 @@
package mod.hilal.saif.activities.tools;

import static pro.sketchware.utility.GsonUtils.getGson;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
Expand Down Expand Up @@ -30,7 32,6 @@

import com.besome.sketch.lib.base.BaseAppCompatActivity;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
Expand Down Expand Up @@ -143,8 144,8 @@ public boolean isLongPressDragEnabled() {
@Override
public void clearView(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
viewHolder.itemView.setAlpha(1f);
FileUtil.writeFile(blocks_dir, new Gson().toJson(all_blocks_list));
FileUtil.writeFile(pallet_dir, new Gson().toJson(pallet_listmap));
FileUtil.writeFile(blocks_dir, getGson().toJson(all_blocks_list));
FileUtil.writeFile(pallet_dir, getGson().toJson(pallet_listmap));
}

@Override
Expand Down Expand Up @@ -236,8 237,8 @@ private void showMoveToBinDialog(int position) {
draggedView = null;
moveRelatedBlocksToRecycleBin(position 9);
removeRelatedBlocks(position 9);
FileUtil.writeFile(blocks_dir, new Gson().toJson(all_blocks_list));
FileUtil.writeFile(pallet_dir, new Gson().toJson(pallet_listmap));
FileUtil.writeFile(blocks_dir, getGson().toJson(all_blocks_list));
FileUtil.writeFile(pallet_dir, getGson().toJson(pallet_listmap));
refreshCount();
dialog.dismiss();
});
Expand All @@ -253,7 254,7 @@ private void readSettings() {

if (FileUtil.isExistFile(blocks_dir) && isValidJson(FileUtil.readFile(blocks_dir))) {
try {
all_blocks_list = new Gson().fromJson(FileUtil.readFile(blocks_dir), Helper.TYPE_MAP_LIST);
all_blocks_list = getGson().fromJson(FileUtil.readFile(blocks_dir), Helper.TYPE_MAP_LIST);

if (all_blocks_list != null) {
return;
Expand Down Expand Up @@ -282,7 283,7 @@ private void refresh_list() {
String paletteJsonContent;
if (FileUtil.isExistFile(pallet_dir) && !(paletteJsonContent = FileUtil.readFile(pallet_dir)).isEmpty()) {
try {
pallet_listmap = new Gson().fromJson(paletteJsonContent, Helper.TYPE_MAP_LIST);
pallet_listmap = getGson().fromJson(paletteJsonContent, Helper.TYPE_MAP_LIST);

if (pallet_listmap != null) {
break parsePaletteJson;
Expand Down Expand Up @@ -355,7 356,7 @@ private void removeRelatedBlocks(final double _p) {
}
}
}
FileUtil.writeFile(blocks_dir, new Gson().toJson(newBlocks));
FileUtil.writeFile(blocks_dir, getGson().toJson(newBlocks));
readSettings();
}

Expand Down Expand Up @@ -391,7 392,7 @@ private void insertBlocksAt(final double _p) {
all_blocks_list.get(i).put("palette", String.valueOf((long) (Double.parseDouble(Objects.requireNonNull(all_blocks_list.get(i).get("palette")).toString()) 1)));
}
}
FileUtil.writeFile(blocks_dir, new Gson().toJson(all_blocks_list));
FileUtil.writeFile(blocks_dir, getGson().toJson(all_blocks_list));
readSettings();
refresh_list();
}
Expand All @@ -402,7 403,7 @@ private void moveRelatedBlocksToRecycleBin(final double _p) {
all_blocks_list.get(i).put("palette", "-1");
}
}
FileUtil.writeFile(blocks_dir, new Gson().toJson(all_blocks_list));
FileUtil.writeFile(blocks_dir, getGson().toJson(all_blocks_list));
readSettings();
}

Expand All @@ -413,7 414,7 @@ private void emptyRecyclebin() {
newBlocks.add(all_blocks_list.get(i));
}
}
FileUtil.writeFile(blocks_dir, new Gson().toJson(newBlocks));
FileUtil.writeFile(blocks_dir, getGson().toJson(newBlocks));
readSettings();
refresh_list();
}
Expand Down Expand Up @@ -473,20 474,20 @@ public void a(String var1, int var2) {

if (insertAtPosition == null) {
pallet_listmap.add(map);
FileUtil.writeFile(pallet_dir, new Gson().toJson(pallet_listmap));
FileUtil.writeFile(pallet_dir, getGson().toJson(pallet_listmap));
Objects.requireNonNull(binding.paletteRecycler.getAdapter()).notifyItemInserted(pallet_listmap.size() - 1);
readSettings();
}else{
pallet_listmap.add(insertAtPosition, map);
FileUtil.writeFile(pallet_dir, new Gson().toJson(pallet_listmap));
FileUtil.writeFile(pallet_dir, getGson().toJson(pallet_listmap));
readSettings();
Objects.requireNonNull(binding.paletteRecycler.getAdapter()).notifyItemInserted(insertAtPosition);
insertBlocksAt(insertAtPosition 9);
}
}else{
pallet_listmap.get(oldPosition).put("name", nameInput);
pallet_listmap.get(oldPosition).put("color", colorInput);
FileUtil.writeFile(pallet_dir, new Gson().toJson(pallet_listmap));
FileUtil.writeFile(pallet_dir, getGson().toJson(pallet_listmap));
readSettings();
refresh_list();
}
Expand Down Expand Up @@ -585,7 586,7 @@ public void onBindViewHolder(@NonNull PaletteAdapter.ViewHolder holder, int posi
.setPositiveButton("Remove permanently", (dialog, which) -> {
palettes.remove(pos);
notifyItemRemoved(pos);
FileUtil.writeFile(pallet_dir, new Gson().toJson(pallet_listmap));
FileUtil.writeFile(pallet_dir, getGson().toJson(pallet_listmap));
removeRelatedBlocks(pos 9);
readSettings();
refreshCount();
Expand All @@ -595,7 596,7 @@ public void onBindViewHolder(@NonNull PaletteAdapter.ViewHolder holder, int posi
moveRelatedBlocksToRecycleBin(position 9);
palettes.remove(pos);
notifyItemRemoved(pos);
FileUtil.writeFile(pallet_dir, new Gson().toJson(pallet_listmap));
FileUtil.writeFile(pallet_dir, getGson().toJson(pallet_listmap));
removeRelatedBlocks(pos 9);
readSettings();
refreshCount();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 1,7 @@
package mod.hilal.saif.activities.tools;

import static pro.sketchware.utility.GsonUtils.getGson;

import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
Expand All @@ -22,7 24,6 @@
import com.besome.sketch.lib.base.BaseAppCompatActivity;
import com.google.android.material.color.MaterialColors;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.gson.Gson;
import com.google.gson.JsonParseException;

import mod.hey.studios.util.Helper;
Expand Down Expand Up @@ -396,7 397,7 @@ private void fillUpInputs(int position) {

private void getBlockList() {
try {
blocksList = new Gson().fromJson(FileUtil.readFile(path), Helper.TYPE_MAP_LIST);
blocksList = getGson().fromJson(FileUtil.readFile(path), Helper.TYPE_MAP_LIST);

if (blocksList != null) {
for (int i = 0, blocksListSize = blocksList.size(); i < blocksListSize; i ) {
Expand Down Expand Up @@ -462,7 463,7 @@ private void addBlock() {
tempMap.put("code", binding.code.getText().toString());
tempMap.put("palette", String.valueOf(blockPosition));
blocksList.add(tempMap);
FileUtil.writeFile(path, new Gson().toJson(blocksList));
FileUtil.writeFile(path, getGson().toJson(blocksList));
SketchwareUtil.toast("Saved");
finish();
}
Expand All @@ -487,7 488,7 @@ private void insertBlockAt(int position) {
tempMap.put("code", binding.code.getText().toString());
tempMap.put("palette", blocksList.get(position).get("palette"));
blocksList.add(position, tempMap);
FileUtil.writeFile(path, new Gson().toJson(blocksList));
FileUtil.writeFile(path, getGson().toJson(blocksList));
SketchwareUtil.toast("Saved");
finish();
}
Expand All @@ -508,7 509,7 @@ private void editBlock(int position) {
}
tempMap.put("imports", binding.customImport.getText().toString());
tempMap.put("code", binding.code.getText().toString());
FileUtil.writeFile(path, new Gson().toJson(blocksList));
FileUtil.writeFile(path, getGson().toJson(blocksList));
SketchwareUtil.toast("Saved");
finish();
}
Expand Down
Loading

0 comments on commit 6599b05

Please sign in to comment.