From 430596b146b2230baa6fe858abbe672dc5d2ef05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ensar=20Saraj=C4=8Di=C4=87?= Date: Mon, 31 Jul 2023 17:16:24 +0200 Subject: [PATCH] Pre-fill parent folders when grouping subfolders This ensures that folders without media files which have direct child folders with media don't get different treatment during grouping. This should make subfolder grouping a bit more predictable. --- .../gallery/pro/extensions/Context.kt | 108 +++++++++++------- 1 file changed, 68 insertions(+), 40 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt index 075a0e4eb..f3ceeecb6 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt @@ -42,6 +42,7 @@ import java.io.FileInputStream import java.nio.ByteBuffer import java.nio.channels.FileChannel import kotlin.collections.set +import kotlin.math.max val Context.audioManager get() = getSystemService(Context.AUDIO_SERVICE) as AudioManager @@ -175,8 +176,9 @@ fun Context.getDirsToShow(dirs: ArrayList, allDirs: ArrayList, allDirs: ArrayList, path: String): Boolean { + val isSortingAscending = config.sorting.isSortingAscending() + val subDirs = into.filter { File(it.path).parent.equals(path, true) } as ArrayList + val newDirId = max(1000L, into.maxOf { it.id ?: 0L }) + if (subDirs.isNotEmpty()) { + val lastModified = if (isSortingAscending) { + subDirs.minByOrNull { it.modified }?.modified + } else { + subDirs.maxByOrNull { it.modified }?.modified + } ?: 0 + + val dateTaken = if (isSortingAscending) { + subDirs.minByOrNull { it.taken }?.taken + } else { + subDirs.maxByOrNull { it.taken }?.taken + } ?: 0 + + var mediaTypes = 0 + subDirs.forEach { + mediaTypes = mediaTypes or it.types + } + + val directory = Directory( + newDirId + 1, + path, + subDirs.first().tmb, + getFolderNameFromPath(path), + subDirs.sumBy { it.mediaCnt }, + lastModified, + dateTaken, + subDirs.sumByLong { it.size }, + getPathLocation(path), + mediaTypes, + "" + ) + + directory.containsMediaFilesDirectly = false + into.add(directory) + return true + } + return false +} + +fun Context.fillWithSharedDirectParents(dirs: ArrayList): ArrayList { + val allDirs = ArrayList(dirs) + val childCounts = mutableMapOf() + for (dir in dirs) { + File(dir.path).parent?.let { + val current = childCounts[it] ?: 0 + childCounts.put(it, current + 1) + } + } + + childCounts + .filter { dir -> dir.value > 1 && dirs.none { it.path.equals(dir.key, true) } } + .toList() + .sortedByDescending { it.first.length } + .forEach { (parent, _) -> + addParentWithoutMediaFiles(allDirs, parent) + } + return allDirs +} + fun Context.getDirectParentSubfolders(dirs: ArrayList, currentPathPrefix: String): ArrayList { val folders = dirs.map { it.path }.sorted().toMutableSet() as HashSet val currentPaths = LinkedHashSet() val foldersWithoutMediaFiles = ArrayList() - var newDirId = 1000L for (path in folders) { if (path == RECYCLE_BIN || path == FAVORITES) { @@ -226,43 +290,7 @@ fun Context.getDirectParentSubfolders(dirs: ArrayList, currentPathPre val parent = File(path).parent if (parent != null && !folders.contains(parent) && dirs.none { it.path.equals(parent, true) }) { currentPaths.add(parent) - val isSortingAscending = config.sorting.isSortingAscending() - val subDirs = dirs.filter { File(it.path).parent.equals(File(path).parent, true) } as ArrayList - if (subDirs.isNotEmpty()) { - val lastModified = if (isSortingAscending) { - subDirs.minByOrNull { it.modified }?.modified - } else { - subDirs.maxByOrNull { it.modified }?.modified - } ?: 0 - - val dateTaken = if (isSortingAscending) { - subDirs.minByOrNull { it.taken }?.taken - } else { - subDirs.maxByOrNull { it.taken }?.taken - } ?: 0 - - var mediaTypes = 0 - subDirs.forEach { - mediaTypes = mediaTypes or it.types - } - - val directory = Directory( - newDirId++, - parent, - subDirs.first().tmb, - getFolderNameFromPath(parent), - subDirs.sumBy { it.mediaCnt }, - lastModified, - dateTaken, - subDirs.sumByLong { it.size }, - getPathLocation(parent), - mediaTypes, - "" - ) - - directory.containsMediaFilesDirectly = false - dirs.add(directory) - currentPaths.add(parent) + if (addParentWithoutMediaFiles(dirs, parent)) { foldersWithoutMediaFiles.add(parent) } }