From a0d80dbbf4b1a1c47ca7f75292e2d90c9575ec4b Mon Sep 17 00:00:00 2001 From: Hamza417 Date: Sun, 17 Mar 2024 00:40:39 +0530 Subject: [PATCH] v1.37_beta - Better loading status --- .../java/app/simple/peri/utils/FileUtils.kt | 24 ++++++++++++------- .../peri/viewmodels/WallpaperViewModel.kt | 7 ++++-- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/app/simple/peri/utils/FileUtils.kt b/app/src/main/java/app/simple/peri/utils/FileUtils.kt index bf5b1045..30296bb5 100644 --- a/app/src/main/java/app/simple/peri/utils/FileUtils.kt +++ b/app/src/main/java/app/simple/peri/utils/FileUtils.kt @@ -5,6 +5,7 @@ import androidx.documentfile.provider.DocumentFile import app.simple.peri.utils.StringUtils.endsWithAny import java.text.CharacterIterator import java.text.StringCharacterIterator +import java.util.Stack object FileUtils { @@ -39,17 +40,24 @@ object FileUtils { return String.format("%.1f %cB", bytes / 1000.0, ci.current()) } - fun DocumentFile.listCompleteFiles(): List { + fun DocumentFile.listCompleteFiles(func: (String) -> Unit): List { val allFiles = mutableListOf() + val stack = Stack() + stack.push(this) - listFiles().forEach { child -> - when { - child.isDirectory -> { - allFiles.addAll(child.listCompleteFiles()) // Recursive call for subdirectory - } + while (stack.isNotEmpty()) { + val currentFile = stack.pop() + + currentFile.listFiles().forEach { child -> + when { + child.isDirectory -> { + func(child.name!!) + stack.push(child) + } - child.isFile -> { - allFiles.add(child) + child.isFile -> { + allFiles.add(child) + } } } } diff --git a/app/src/main/java/app/simple/peri/viewmodels/WallpaperViewModel.kt b/app/src/main/java/app/simple/peri/viewmodels/WallpaperViewModel.kt index 29c673dd..11e82fea 100644 --- a/app/src/main/java/app/simple/peri/viewmodels/WallpaperViewModel.kt +++ b/app/src/main/java/app/simple/peri/viewmodels/WallpaperViewModel.kt @@ -135,11 +135,14 @@ class WallpaperViewModel(application: Application) : AndroidViewModel(applicatio val uri = MainPreferences.getStorageUri()?.toUri()!! val pickedDirectory = DocumentFile.fromTreeUri(getApplication(), uri) - loadingStatus.postValue("0 : 0%") - val files = pickedDirectory?.listCompleteFiles() + loadingStatus.postValue(getApplication().getString(app.simple.peri.R.string.preparing)) + val files = pickedDirectory?.listCompleteFiles() { + loadingStatus.postValue(it) + } var count = 0 val total = files?.size ?: 0 + loadingStatus.postValue("0 : 0%") files?.parallelStream()?.forEach { file -> try { val wallpaper = Wallpaper()