Skip to content

Commit

Permalink
feat: Redesign Local Libraries UI
Browse files Browse the repository at this point in the history
  • Loading branch information
aikrq committed Dec 28, 2024
1 parent 371284d commit 388680f
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public class LibraryDownloaderDialogFragment extends DialogFragment {
private String local_lib_file = "";
private LibraryDownloaderListener listener;


@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,30 @@
import pro.sketchware.utility.FileUtil;
import pro.sketchware.utility.SketchwareUtil;

public class ManageLocalLibraryActivity extends AppCompatActivity implements View.OnClickListener {
private static String local_libs_path = "";
private boolean notAssociatedWithProject = false;
private String local_lib_file = "";
private ArrayList<HashMap<String, Object>> lookup_list = new ArrayList<>();
private ArrayList<HashMap<String, Object>> project_used_libs = new ArrayList<>();
public class ManageLocalLibraryActivity extends AppCompatActivity {

private static String localLibsPath;
private boolean notAssociatedWithProject;
private String localLibFile;
private ArrayList<HashMap<String, Object>> lookupList = new ArrayList<>();
private ArrayList<HashMap<String, Object>> projectUsedLibs = new ArrayList<>();
private BuildSettings buildSettings;
private final ArrayList<String> arrayList = new ArrayList<>();
private ManageLocallibrariesBinding binding;
private LibraryAdapter adapter;

static {
localLibsPath = FileUtil.getExternalStorageDir().concat("/.sketchware/libs/local_libs/");
}

public static HashMap<String, Object> createLibraryMap(String name, String dependency) {
String configPath = local_libs_path + name + "/config";
String resPath = local_libs_path + name + "/res";
String jarPath = local_libs_path + name + "/classes.jar";
String dexPath = local_libs_path + name + "/classes.dex";
String manifestPath = local_libs_path + name + "/AndroidManifest.xml";
String pgRulesPath = local_libs_path + name + "/proguard.txt";
String assetsPath = local_libs_path + name + "/assets";
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);
Expand Down Expand Up @@ -93,41 +99,35 @@ protected void onCreate(Bundle savedInstanceState) {

ViewCompat.setOnApplyWindowInsetsListener(binding.downloadLibraryButton, new AddMarginOnApplyWindowInsetsListener(WindowInsetsCompat.Type.navigationBars(), WindowInsetsCompat.CONSUMED));

initButtons();
if (getIntent().hasExtra("sc_id")) {
String sc_id = Objects.requireNonNull(getIntent().getStringExtra("sc_id"));
buildSettings = new BuildSettings(sc_id);
String scId = Objects.requireNonNull(getIntent().getStringExtra("sc_id"));
buildSettings = new BuildSettings(scId);
notAssociatedWithProject = sc_id.equals("system");
local_lib_file = FileUtil.getExternalStorageDir().concat("/.sketchware/data/").concat(sc_id.concat("/local_library"));
localLibFile = FileUtil.getExternalStorageDir().concat("/.sketchware/data/").concat(sc_id.concat("/local_library"));
}
local_libs_path = FileUtil.getExternalStorageDir().concat("/.sketchware/libs/local_libs/");
loadFiles();
setUpSearchView();
}

private void initButtons() {
binding.topAppBar.setNavigationOnClickListener(Helper.getBackPressedClickListener(this));
binding.downloadLibraryButton.setOnClickListener(this);
}
loadLibraries();
setupSearchBar();

@Override
@SuppressLint("SetTextI18n")
public void onClick(View v) {

LibraryDownloaderDialogFragment dialogFragment = new LibraryDownloaderDialogFragment();
Bundle bundle = new Bundle();
bundle.putBoolean("notAssociatedWithProject", notAssociatedWithProject);
bundle.putSerializable("buildSettings", buildSettings);
bundle.putString("local_lib_file", local_lib_file);
dialogFragment.setArguments(bundle);
if (getSupportFragmentManager().findFragmentByTag("library_downloader_dialog") != null)
return;
dialogFragment.setListener(this::loadFiles);
dialogFragment.show(getSupportFragmentManager(), "library_downloader_dialog");
binding.downloadLibraryButton.setOnClickListener(v ->
if (getSupportFragmentManager().findFragmentByTag("library_downloader_dialog") != null) {
return;
}

Bundle bundle = new Bundle();
bundle.putBoolean("notAssociatedWithProject", notAssociatedWithProject);
bundle.putSerializable("buildSettings", buildSettings);
bundle.putString("localLibFile", localLibFile);

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

private void setUpSearchView() {
binding.searchInput.addTextChangedListener(new TextWatcher() {
private void setupSearchBar() {
binding.searchBar.getEditText().addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
Expand All @@ -147,19 +147,19 @@ public void onTextChanged(CharSequence newText, int start, int before, int count
});
}

private void loadFiles() {
project_used_libs.clear();
lookup_list.clear();
private void loadLibraries() {
projectUsedLibs.clear();
lookupList.clear();
if (!notAssociatedWithProject) {
String fileContent;
if (!FileUtil.isExistFile(local_lib_file) || (fileContent = FileUtil.readFile(local_lib_file)).isEmpty()) {
FileUtil.writeFile(local_lib_file, "[]");
if (!FileUtil.isExistFile(localLibFile) || (fileContent = FileUtil.readFile(localLibFile)).isEmpty()) {
FileUtil.writeFile(localLibFile, "[]");
} else {
project_used_libs = new Gson().fromJson(fileContent, Helper.TYPE_MAP_LIST);
projectUsedLibs = new Gson().fromJson(fileContent, Helper.TYPE_MAP_LIST);
}
}

FileUtil.listDir(local_libs_path, arrayList);
FileUtil.listDir(localLibsPath, arrayList);
arrayList.sort(String.CASE_INSENSITIVE_ORDER);

List<String> localLibraryNames = new LinkedList<>();
Expand All @@ -168,13 +168,10 @@ private void loadFiles() {
localLibraryNames.add(Uri.parse(filename).getLastPathSegment());
}
}
if (localLibraryNames.isEmpty()) {
binding.noContentLayout.setVisibility(View.VISIBLE);
} else {
binding.noContentLayout.setVisibility(View.GONE);
}
binding.librariesList.setLayoutManager(new LinearLayoutManager(this));

adapter = new LibraryAdapter(localLibraryNames);
binding.noContentLayout.setVisibility(localLibraryNames.isEmpty() ? View.VISIBLE : View.GONE);
binding.librariesList.setLayoutManager(new LinearLayoutManager(this));
binding.librariesList.setAdapter(adapter);
}

Expand Down Expand Up @@ -211,37 +208,37 @@ public void onBindViewHolder(ViewHolder holder, final int position) {
if (!binding.checkboxContent.isChecked()) {
// Remove the library from the list
int indexToRemove = -1;
for (int i = 0; i < project_used_libs.size(); i++) {
HashMap<String, Object> lib = project_used_libs.get(i);
for (int i = 0; i < projectUsedLibs.size(); i++) {
HashMap<String, Object> lib = projectUsedLibs.get(i);
if (name.equals(lib.get("name"))) {
indexToRemove = i;
break;
}
}
if (indexToRemove != -1) {
project_used_libs.remove(indexToRemove);
projectUsedLibs.remove(indexToRemove);
}
} else {
// Add the library to the list
// Here, we need to find the dependency string if it exists
String dependency = null;
for (HashMap<String, Object> lib : lookup_list) {
for (HashMap<String, Object> lib : lookupList) {
if (name.equals(lib.get("name"))) {
dependency = (String) lib.get("dependency");
break;
}
}
localLibrary = createLibraryMap(name, dependency);
project_used_libs.add(localLibrary);
projectUsedLibs.add(localLibrary);
}
FileUtil.writeFile(local_lib_file, new Gson().toJson(project_used_libs));
FileUtil.writeFile(localLibFile, new Gson().toJson(projectUsedLibs));
});


binding.checkboxContent.setChecked(false);
if (!notAssociatedWithProject) {
lookup_list = new Gson().fromJson(FileUtil.readFile(local_lib_file), Helper.TYPE_MAP_LIST);
for (HashMap<String, Object> localLibrary : lookup_list) {
lookupList = new Gson().fromJson(FileUtil.readFile(localLibFile), Helper.TYPE_MAP_LIST);
for (HashMap<String, Object> localLibrary : lookupList) {
if (binding.checkboxContent.getText().toString().equals(Objects.requireNonNull(localLibrary.get("name")).toString())) {
binding.checkboxContent.setChecked(true);
}
Expand All @@ -254,7 +251,7 @@ public void onBindViewHolder(ViewHolder holder, final int position) {
PopupMenu popupMenu = new PopupMenu(ManageLocalLibraryActivity.this, v);
popupMenu.getMenu().add(Menu.NONE, Menu.NONE, Menu.NONE, "Delete");
popupMenu.setOnMenuItemClickListener(menuItem -> {
FileUtil.deleteFile(local_libs_path.concat(binding.checkboxContent.getText().toString()));
FileUtil.deleteFile(localLibsPath.concat(binding.checkboxContent.getText().toString()));
SketchwareUtil.toast("Deleted successfully");
loadFiles();
return true;
Expand Down
74 changes: 16 additions & 58 deletions app/src/main/res/layout/manage_locallibraries.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,60 +6,22 @@
android:fitsSystemWindows="true">

<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
app:elevation="0dp"
app:liftOnScroll="false"
app:elevation="0dp">
app:liftOnScrollColor="@android:color/transparent"
app:navigationIcon="?attr/homeAsUpIndicator"
app:statusBarForeground="?attr/colorSurface">

<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/collapsingToolbar"
android:layout_width="match_parent"
android:layout_height="?attr/collapsingToolbarLayoutLargeSize"
android:fitsSystemWindows="true"
app:layout_scrollFlags="scroll|exitUntilCollapsed">

<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/topAppBar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:navigationIcon="?attr/homeAsUpIndicator"
app:title="@string/local_library_manager" />
</com.google.android.material.appbar.CollapsingToolbarLayout>

<LinearLayout
<com.google.android.material.search.SearchBar
android:id="@+id/search_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="4dp"
android:gravity="center_vertical"
android:orientation="horizontal"
android:visibility="visible">

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/searchInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Search for libraries"
android:importantForAutofill="noExcludeDescendants"
app:boxCornerRadiusBottomEnd="50dp"
app:boxCornerRadiusBottomStart="50dp"
app:boxCornerRadiusTopEnd="50dp"
app:boxCornerRadiusTopStart="50dp"
app:startIconDrawable="@drawable/search_icon_grey">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/searchInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:inputType="text" />
</com.google.android.material.textfield.TextInputLayout>
android:hint="Search for libraries" />

</LinearLayout>
</com.google.android.material.appbar.AppBarLayout>

<LinearLayout
Expand All @@ -78,41 +40,36 @@
android:paddingBottom="120dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:listitem="@layout/view_item_local_lib" />

</LinearLayout>

<LinearLayout
android:id="@+id/noContentLayout"
android:id="@+id/no_content_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:colorBackground"
android:fitsSystemWindows="false"
android:background="?attr/colorSurface"
android:gravity="center"
android:orientation="vertical"
android:paddingHorizontal="16dp"
android:paddingBottom="100dp"
android:visibility="gone"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<ImageView
android:layout_width="wrap_content"
android:layout_height="150dp"
android:src="@drawable/img_nocontent" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center"
android:text="@string/local_library_manager_no_libraries_title"
android:textColor="?attr/colorOnSurface"
android:textSize="20sp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center"
android:layout_marginTop="4dp"
android:text="@string/local_library_manager_no_libraries_body"
android:textColor="?attr/colorOnSurfaceVariant"
android:textSize="14sp" />

</LinearLayout>

<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
Expand All @@ -123,4 +80,5 @@
android:layout_marginBottom="20dp"
android:text="@string/local_library_manager_download"
app:icon="@drawable/ic_mtrl_download" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

0 comments on commit 388680f

Please sign in to comment.