Skip to content

Commit

Permalink
refactor: Move heavy tasks to its own classes
Browse files Browse the repository at this point in the history
  • Loading branch information
aikrq committed Jan 2, 2025
1 parent ecd68ab commit fe78d0c
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 146 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dev.aldi.sayuti.editor.manage;

import static dev.aldi.sayuti.editor.manage.LocalLibrariesUtil.createLibraryMap;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
Expand Down Expand Up @@ -210,7 +212,7 @@ public void onTaskCompleted(@NonNull List<String> dependencies) {
var fileContent = FileUtil.readFile(localLibFile);
var enabledLibs = gson.fromJson(fileContent, Helper.TYPE_MAP_LIST);
enabledLibs.addAll(dependencies.stream()
.map(name -> ManageLocalLibraryActivity.createLibraryMap(name, dependencyName))
.map(name -> createLibraryMap(name, dependencyName))
.collect(Collectors.toList()));
FileUtil.writeFile(localLibFile, gson.toJson(enabledLibs));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package dev.aldi.sayuti.editor.manage;

import static pro.sketchware.utility.FileUtil.deleteFile;
import static pro.sketchware.utility.FileUtil.getExternalStorageDir;
import static pro.sketchware.utility.FileUtil.isExistFile;
import static pro.sketchware.utility.FileUtil.listDirAsFile;
import static pro.sketchware.utility.FileUtil.readFile;
import static pro.sketchware.utility.FileUtil.writeFile;

import com.google.gson.Gson;

import mod.hey.studios.util.Helper;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

public class LocalLibrariesUtil {
private static String localLibsPath = getExternalStorageDir().concat("/.sketchware/libs/local_libs/");

public static List<LocalLibrary> getAllLocalLibraries() {
ArrayList<File> localLibraryFiles = new ArrayList<>();
listDirAsFile(localLibsPath, localLibraryFiles);
localLibraryFiles.sort(new LocalLibrariesComparator());

List<LocalLibrary> localLibraries = new LinkedList<>();
for (File libraryFile : localLibraryFiles) {
if (libraryFile.isDirectory()) {
localLibraries.add(LocalLibrary.fromFile(libraryFile));
}
}

return localLibraries;
}

public static ArrayList<HashMap<String, Object>> getLocalLibraries(String scId) {
File localLibFile = getLocalLibFile(scId);
String fileContent;
if (!localLibFile.exists() || (fileContent = readFile(localLibFile.getAbsolutePath())).isEmpty()) {
writeFile(localLibFile.getAbsolutePath(), "[]");
return new ArrayList<>();
}
return new Gson().fromJson(fileContent, Helper.TYPE_MAP_LIST);
}

public static void deleteSelectedLocalLibraries(List<LocalLibrary> localLibraries) {
for (LocalLibrary library : localLibraries) {
if (library.isSelected()) {
deleteFile(localLibsPath.concat(library.getName()));
}
}
}

public static File getLocalLibFile(String scId) {
return new File(getExternalStorageDir().concat("/.sketchware/data/").concat(scId.concat("/local_library")));
}

public static void rewriteLocalLibFile(String scId, String newContent) {
writeFile(getLocalLibFile(scId).getAbsolutePath(), newContent);
}

public static HashMap<String, Object> createLibraryMap(String name, String dependency) {
String configPath = localLibsPath + name + "/config";
String resPath = localLibsPath + name + "/res";
String jarPath = localLibsPath + name + "/classes.jar";
String dexPath = localLibsPath + name + "/classes.dex";
String manifestPath = localLibsPath + name + "/AndroidManifest.xml";
String pgRulesPath = localLibsPath + name + "/proguard.txt";
String assetsPath = localLibsPath + name + "/assets";

HashMap<String, Object> localLibrary = new HashMap<>();
localLibrary.put("name", name);
if (dependency != null) {
localLibrary.put("dependency", dependency);
}
if (isExistFile(configPath)) {
localLibrary.put("packageName", readFile(configPath));
}
if (isExistFile(resPath)) {
localLibrary.put("resPath", resPath);
}
if (isExistFile(jarPath)) {
localLibrary.put("jarPath", jarPath);
}
if (isExistFile(dexPath)) {
localLibrary.put("dexPath", dexPath);
}
if (isExistFile(manifestPath)) {
localLibrary.put("manifestPath", manifestPath);
}
if (isExistFile(pgRulesPath)) {
localLibrary.put("pgRulesPath", pgRulesPath);
}
if (isExistFile(assetsPath)) {
localLibrary.put("assetsPath", assetsPath);
}
return localLibrary;
}
}
37 changes: 37 additions & 0 deletions app/src/main/java/dev/aldi/sayuti/editor/manage/LocalLibrary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package dev.aldi.sayuti.editor.manage;

import static pro.sketchware.utility.FileUtil.formatFileSize;
import static pro.sketchware.utility.FileUtil.getFileSize;

import java.io.File;

public class LocalLibrary {
private final String name;
private final String size;
private boolean isSelected;

private LocalLibrary(String name, String size) {
this.name = name;
this.size = size;
}

public String getName() {
return name;
}

public String getSize() {
return size;
}

public boolean isSelected() {
return isSelected;
}

public void setSelected(boolean isSelected) {
this.isSelected = isSelected;
}

public static LocalLibrary fromFile(File file) {
return new LocalLibrary(file.getName(), formatFileSize(getFileSize(file)));
}
}
Loading

0 comments on commit fe78d0c

Please sign in to comment.