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 f183fdbf5..9de742572 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 @@ -43,6 +43,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 @@ -176,8 +177,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) { @@ -227,43 +291,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) } }