Skip to content

Commit

Permalink
feat: Redesign Library Downloader Dialog UI
Browse files Browse the repository at this point in the history
  • Loading branch information
aikrq committed Dec 29, 2024
1 parent ea122d5 commit 5a4fdcb
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public void onBindViewHolder(ViewHolder holder, final int position) {
var binding = holder.binding;

final File libraryFile = libraryFiles.get(position);
final String librarySize = FileUtil.formatFileSize(libraryFile.length());
final String librarySize = FileUtil.formatFileSize(FileUtil.getFileSize(libraryFile));
binding.libraryName.setText(libraryFile.getName());
binding.librarySize.setText(librarySize);
binding.libraryName.setSelected(true);
Expand Down Expand Up @@ -316,7 +316,7 @@ public void onBindViewHolder(ViewHolder holder, final int position) {
var binding = holder.binding;

final File libraryFile = filteredLibraryFiles.get(position);
final String librarySize = FileUtil.formatFileSize(libraryFile.length());
final String librarySize = FileUtil.formatFileSize(FileUtil.getFileSize(libraryFile));
binding.libraryName.setText(libraryFile.getName());
binding.librarySize.setText(librarySize);
binding.libraryName.setSelected(true);
Expand Down
66 changes: 44 additions & 22 deletions app/src/main/java/pro/sketchware/utility/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,34 +54,56 @@

@SuppressWarnings("unused")
public class FileUtil {
public static long getFileSize(File file) {
if (file == null || !file.exists()) {
return 0;
}

if (!file.isDirectory()) {
return file.length();
}

List<File> dirs = new LinkedList<>();
dirs.add(file);

long result = 0;
while (!dirs.isEmpty()) {
File dir = dirs.remove(0);
if (!dir.exists())
continue;
File[] listFiles = dir.listFiles();
if (listFiles == null || listFiles.length == 0)
continue;
for (File child : listFiles) {
if (child.isDirectory()) {
dirs.add(child);
} else {
result += child.length();
}
}
}

return result;
}

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);
}
String[] units = {"B", "KiB", "MiB", "GiB"};
float value = size;
int unitIndex = 0;

while (value >= 1024 && unitIndex < units.length - 1) {
value /= 1024;
unitIndex++;
}

if (removeZero && (value - (int) value) * 10 == 0) {
return String.format("%d %s", (int) value, units[unitIndex]);
} 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);
}
return String.format("%.1f %s", value, units[unitIndex]);
}
}

Expand Down
4 changes: 3 additions & 1 deletion app/src/main/res/layout/manage_locallibraries.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/local_library_manager_no_libraries_title"
android:textColor="?attr/colorOnSurface"
android:textSize="20sp" />
Expand All @@ -76,14 +77,15 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:gravity="center"
android:text="@string/local_library_manager_no_libraries_body"
android:textColor="?attr/colorOnSurfaceVariant"
android:textSize="14sp" />

</LinearLayout>

<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="@+id/downloadLibraryButton"
android:id="@+id/download_library_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
Expand Down

0 comments on commit 5a4fdcb

Please sign in to comment.