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 29, 2024
1 parent a34705e commit 0003b99
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.divider.MaterialDividerItemDecoration;
import com.google.gson.Gson;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
Expand All @@ -41,7 +43,7 @@ public class ManageLocalLibraryActivity extends AppCompatActivity {

private ArrayList<HashMap<String, Object>> lookupList = new ArrayList<>();
private ArrayList<HashMap<String, Object>> projectUsedLibs = new ArrayList<>();
private final ArrayList<String> localLibraryNames = new ArrayList<>();
private final ArrayList<File> localLibraryFiles = new ArrayList<>();

private static String localLibsPath;
private boolean notAssociatedWithProject;
Expand Down Expand Up @@ -73,6 +75,7 @@ protected void onCreate(Bundle savedInstanceState) {

binding.librariesList.setAdapter(adapter);
binding.searchList.setAdapter(searchAdapter);
binding.searchList.addItemDecoration(new MaterialDividerItemDecoration(context, MaterialDividerItemDecoration.VERTICAL));

loadLibraries();

Expand Down Expand Up @@ -100,7 +103,7 @@ public void beforeTextChanged(CharSequence s, int start, int count, int after) {
@Override
public void afterTextChanged(Editable s) {
String value = s.toString().trim();
searchAdapter.filter(adapter.getLibraryNames(), value);
searchAdapter.filter(adapter.getLibraryFiles(), value);
}

@Override
Expand All @@ -125,18 +128,18 @@ private void loadLibraries() {
}
}

FileUtil.listDir(localLibsPath, localLibraryNames);
localLibraryNames.sort(String.CASE_INSENSITIVE_ORDER);
FileUtil.listDir(localLibsPath, localLibraryFiles);
// localLibraryNames.sort(String.CASE_INSENSITIVE_ORDER);

List<String> libraryNames = new LinkedList<>();
for (String libraryName : localLibraryNames) {
if (FileUtil.isDirectory(libraryName)) {
libraryNames.add(Uri.parse(libraryName).getLastPathSegment());
List<File> libraryFiles = new LinkedList<>();
for (String libraryFile : localLibraryFiles) {
if (FileUtil.isDirectory(libraryFile)) {
libraryFiles.add(libraryFile);
}
}

adapter.setLibraryNames(libraryNames);
binding.noContentLayout.setVisibility(libraryNames.isEmpty() ? View.VISIBLE : View.GONE);
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 @@ -178,7 +181,7 @@ public static HashMap<String, Object> createLibraryMap(String name, String depen
}

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

@NonNull
@Override
Expand All @@ -191,11 +194,13 @@ public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
public void onBindViewHolder(ViewHolder holder, final int position) {
var binding = holder.binding;

final String libraryName = libraryNames.get(position);
binding.checkboxContent.setText(libraryName);
final File libraryFile = libraryNames.get(position);
final String librarySize = FileUtil.formatFileSize(libraryFile.length());
binding.libraryName.setText(libraryFile.getName());
binding.librarySize.setText(librarySize);

binding.checkboxContent.setOnClickListener(v -> {
String name = binding.checkboxContent.getText().toString();
String name = binding.libraryName.getText().toString();

HashMap<String, Object> localLibrary;
if (!binding.checkboxContent.isChecked()) {
Expand Down Expand Up @@ -231,7 +236,7 @@ public void onBindViewHolder(ViewHolder holder, final int position) {
if (!notAssociatedWithProject) {
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())) {
if (binding.libraryName.getText().toString().equals(Objects.requireNonNull(localLibrary.get("name")).toString())) {
binding.checkboxContent.setChecked(true);
}
}
Expand All @@ -243,7 +248,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(localLibsPath.concat(binding.checkboxContent.getText().toString()));
FileUtil.deleteFile(localLibsPath.concat(binding.libraryName.getText().toString()));
SketchwareUtil.toast("Deleted successfully");
loadLibraries();
return true;
Expand All @@ -254,17 +259,17 @@ public void onBindViewHolder(ViewHolder holder, final int position) {

@Override
public int getItemCount() {
return libraryNames.isEmpty() ? 0 : libraryNames.size();
return libraryFiles.isEmpty() ? 0 : libraryFiles.size();
}

public void setLibraryNames(List<String> libraryNames) {
this.libraryNames.clear();
this.libraryNames.addAll(libraryNames);
public void setLibraryFiles(List<File> libraryFiles) {
this.libraryFiles.clear();
this.libraryFiles.addAll(libraryFiles);
notifyDataSetChanged();
}

public List<String> getLibraryNames() {
return libraryNames;
public List<File> getLibraryFiles() {
return libraryFiles;
}

static class ViewHolder extends RecyclerView.ViewHolder {
Expand All @@ -278,7 +283,7 @@ public ViewHolder(@NonNull ViewItemLocalLibBinding binding) {
}

public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.ViewHolder> {
private final List<String> filteredLibraryNames = new ArrayList<>();
private final List<File> filteredLibraryFiles = new ArrayList<>();

@NonNull
@Override
Expand All @@ -291,11 +296,13 @@ public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
public void onBindViewHolder(ViewHolder holder, final int position) {
var binding = holder.binding;

final String libraryName = filteredLibraryNames.get(position);
binding.checkboxContent.setText(libraryName);
final File libraryFile = libraryNames.get(position);
final String librarySize = FileUtil.formatFileSize(libraryFile.length());
binding.libraryName.setText(libraryFile.getName());
binding.librarySize.setText(librarySize);

binding.checkboxContent.setOnClickListener(v -> {
String name = binding.checkboxContent.getText().toString();
String name = binding.libraryName.getText().toString();

HashMap<String, Object> localLibrary;
if (!binding.checkboxContent.isChecked()) {
Expand Down Expand Up @@ -327,12 +334,11 @@ public void onBindViewHolder(ViewHolder holder, final int position) {
FileUtil.writeFile(localLibFile, new Gson().toJson(projectUsedLibs));
});


binding.checkboxContent.setChecked(false);
if (!notAssociatedWithProject) {
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())) {
if (binding.libraryName.getText().toString().equals(Objects.requireNonNull(localLibrary.get("name")).toString())) {
binding.checkboxContent.setChecked(true);
}
}
Expand All @@ -344,7 +350,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(localLibsPath.concat(binding.checkboxContent.getText().toString()));
FileUtil.deleteFile(localLibsPath.concat(binding.libraryName.getText().toString()));
SketchwareUtil.toast("Deleted successfully");
loadLibraries();
return true;
Expand All @@ -355,17 +361,17 @@ public void onBindViewHolder(ViewHolder holder, final int position) {

@Override
public int getItemCount() {
return filteredLibraryNames.isEmpty() ? 0 : filteredLibraryNames.size();
return filteredLibraryFiles.isEmpty() ? 0 : filteredLibraryFiles.size();
}

public void filter(List<String> libraryNames, String query) {
filteredLibraryNames.clear();
public void filter(List<File> libraryFiles, String query) {
filteredLibraryFiles.clear();
if (query.isEmpty()) {
filteredLibraryNames.addAll(libraryNames);
filteredLibraryFiles.addAll(libraryFiles);
} else {
for (String item : libraryNames) {
if (item.toLowerCase().contains(query.toLowerCase())) {
filteredLibraryNames.add(item);
for (File file : libraryFiles) {
if (file.getName().toLowerCase().contains(query.toLowerCase())) {
filteredLibraryFiles.add(item);
}
}
}
Expand Down
41 changes: 41 additions & 0 deletions app/src/main/java/pro/sketchware/utility/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,36 @@

@SuppressWarnings("unused")
public class FileUtil {
public static String formatFileSize(long size) {
return formatFileSize(size, false);
}

public static String formatFileSize(long size, boolean removeZero) {
if (size < 1024) {
return String.format("%d B", size);
} else if (size < 1024 * 1024) {
float value = size / 1024.0f;
if (removeZero && (value - (int) value) * 10 == 0) {
return String.format("%d KB", (int) value);
} else {
return String.format("%.1f KB", value);
}
} else if (size < 1024 * 1024 * 1024) {
float value = size / 1024.0f / 1024.0f;
if (removeZero && (value - (int) value) * 10 == 0) {
return String.format("%d MB", (int) value);
} else {
return String.format("%.1f MB", value);
}
} else {
float value = size / 1024.0f / 1024.0f / 1024.0f;
if (removeZero && (value - (int) value) * 10 == 0) {
return String.format("%d GB", (int) value);
} else {
return String.format("%.1f GB", value);
}
}
}

public static boolean renameFile(String str, String str2) {
return new File(str).renameTo(new File(str2));
Expand Down Expand Up @@ -274,6 +304,17 @@ public static void listDir(String path, ArrayList<String> list) {
}
}

public static void listDirAsFile(String path, ArrayList<File> list) {
File[] listFiles;
File dir = new File(path);
if (dir.exists() && !dir.isFile() && (listFiles = dir.listFiles()) != null && listFiles.length > 0 && list != null) {
list.clear();
for (File file : listFiles) {
list.add(file);
}
}
}

/**
* @return List of files that have the filename extension {@code extension}.
*/
Expand Down
51 changes: 37 additions & 14 deletions app/src/main/res/layout/view_item_local_lib.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,64 @@
style="?attr/materialCardViewFilledStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
app:cardBackgroundColor="?attr/colorSurfaceContainer">
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginVertical="4dp"
app:cardBackgroundColor="@android:color/transparent">

<LinearLayout
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:paddingHorizontal="16dp"
android:paddingTop="12dp">

<CheckBox
android:id="@+id/checkbox_content"
<TextView
android:id="@+id/library_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_weight="1"
android:paddingStart="8dp"
android:textAppearance="?attr/textAppearanceBodyLarge"
tools:text="com.sketchware.pro:1.0.0" />
android:textColor="?attr/colorOnSurface"
app:layout_constraintEnd_toEndOf="@id/checkbox"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toTopOf="@id/library_size"
tools:text="appcompat-v1.7.0" />

<TextView
android:id="@+id/library_size"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?attr/textAppearanceBodyMedium"
android:textColor="?attr/colorOnSurfaceVariant"
app:layout_constraintEnd_toEndOf="@id/checkbox"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/library_name"
tools:text="Size: 360 KiB" />

<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/img_delete"
app:layout_constraintTop_toTopOf="parent" />

<ImageView
android:id="@+id/img_delete"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginEnd="8dp"
android:layout_gravity="center"
android:background="?attr/selectableItemBackgroundBorderless"
android:focusable="true"
android:clickable="true"
android:padding="4dp"
android:scaleType="fitCenter"
android:src="@drawable/ic_more_vert_grey600_24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:tint="?attr/colorControlNormal" />

</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

</com.google.android.material.card.MaterialCardView>
</FrameLayout>
Loading

0 comments on commit 0003b99

Please sign in to comment.