Skip to content

Commit

Permalink
fix: Load local libraries in the background thread & add loading indi…
Browse files Browse the repository at this point in the history
…cator
  • Loading branch information
aikrq committed Jan 2, 2025
1 parent ef1b046 commit dcd0f7b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.annotation.SuppressLint;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
Expand All @@ -13,15 +14,16 @@

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.besome.sketch.lib.base.BaseAppCompatActivity;
import com.google.gson.Gson;

import java.io.File;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
Expand All @@ -40,9 +42,10 @@
import pro.sketchware.utility.SketchwareUtil;
import pro.sketchware.utility.UI;

import a.a.a.MA;
import a.a.a.mB;

public class ManageLocalLibraryActivity extends AppCompatActivity {
public class ManageLocalLibraryActivity extends BaseAppCompatActivity {

private ArrayList<HashMap<String, Object>> lookupList = new ArrayList<>();
private ArrayList<HashMap<String, Object>> projectUsedLibs = new ArrayList<>();
Expand All @@ -61,7 +64,7 @@ public class ManageLocalLibraryActivity extends AppCompatActivity {
}

@Override
protected void onCreate(Bundle savedInstanceState) {
public void onCreate(Bundle savedInstanceState) {
EdgeToEdge.enable(this);
super.onCreate(savedInstanceState);
binding = ManageLocallibrariesBinding.inflate(getLayoutInflater());
Expand Down Expand Up @@ -97,7 +100,7 @@ protected void onCreate(Bundle savedInstanceState) {

LibraryDownloaderDialogFragment fragment = new LibraryDownloaderDialogFragment();
fragment.setArguments(bundle);
fragment.setOnLibraryDownloadedTask(this::loadLibraries);
fragment.setOnLibraryDownloadedTask(this::runLoadLocalLibrariesTask);
fragment.show(getSupportFragmentManager(), "library_downloader_dialog");
});

Expand All @@ -114,13 +117,17 @@ public void afterTextChanged(Editable s) {

@Override
public void onTextChanged(CharSequence newText, int start, int before, int count) {
if (newText.toString().isEmpty()) {
loadLibraries();
}
}
});

loadLibraries();
runLoadLocalLibrariesTask();
}

private void runLoadLocalLibrariesTask() {
k();
new Handler().postDelayed(() -> {
new LoadLocalLibrariesTask(this).execute();
}, 500L);
}

@Override
Expand Down Expand Up @@ -155,8 +162,10 @@ private void loadLibraries() {
}
}

adapter.setLibraryFiles(libraryFiles);
binding.noContentLayout.setVisibility(libraryFiles.isEmpty() ? View.VISIBLE : View.GONE);
runOnUiThread(() -> {
adapter.setLibraryFiles(libraryFiles);
binding.noContentLayout.setVisibility(libraryFiles.isEmpty() ? View.VISIBLE : View.GONE);
});
}

public static HashMap<String, Object> createLibraryMap(String name, String dependency) {
Expand Down Expand Up @@ -197,6 +206,35 @@ public static HashMap<String, Object> createLibraryMap(String name, String depen
return localLibrary;
}

private static class LoadLocalLibrariesTask extends MA {
private final WeakReference<ManageLocalLibraryActivity> activity;

public LoadLocalLibrariesTask(ManageLocalLibraryActivity activity) {
super(activity);
this.activity = new WeakReference<>(activity);
activity.addTask(this);
}

@Override
public void a() {
activity.get().h();
}

@Override
public void a(String idk) {
activity.get().h();
}

@Override
public void b() {
try {
activity.get().loadLibraries();
} catch (Exception e) {
e.printStackTrace();
}
}
}

public class LibraryAdapter extends RecyclerView.Adapter<LibraryAdapter.ViewHolder> {
private final List<File> libraryFiles = new ArrayList<>();

Expand Down Expand Up @@ -237,7 +275,7 @@ public void onBindViewHolder(ViewHolder holder, final int position) {
popupMenu.setOnMenuItemClickListener(menuItem -> {
FileUtil.deleteFile(localLibsPath.concat(binding.libraryName.getText().toString()));
SketchwareUtil.toast("Deleted successfully");
loadLibraries();
runLoadLocalLibrariesTask();
return true;
});
popupMenu.show();
Expand Down Expand Up @@ -343,7 +381,7 @@ public void onBindViewHolder(ViewHolder holder, final int position) {
popupMenu.setOnMenuItemClickListener(menuItem -> {
FileUtil.deleteFile(localLibsPath.concat(binding.libraryName.getText().toString()));
SketchwareUtil.toast("Deleted successfully");
loadLibraries();
runLoadLocalLibrariesTask();
return true;
});
popupMenu.show();
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:8.7.2'
classpath 'com.android.tools.build:gradle:8.2.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:2.0.0"
classpath "com.google.firebase:firebase-crashlytics-gradle:3.0.2"
classpath "com.google.gms:google-services:4.4.2"
Expand Down

0 comments on commit dcd0f7b

Please sign in to comment.