diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MainActivity.kt index 3a06fc215..12dd663d0 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MainActivity.kt @@ -958,46 +958,44 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { private fun getDirsToShow(dirs: ArrayList): ArrayList { return if (config.groupDirectSubfolders) { dirs.forEach { - it.subfoldersCount = 1 + it.subfoldersCount = 0 it.subfoldersMediaCount = it.mediaCnt } val dirFolders = dirs.map { it.path }.sorted().toMutableSet() as HashSet val foldersToShow = getDirectParentSubfolders(dirFolders) - val newDirs = dirs.filter { foldersToShow.contains(it.path) } as ArrayList - - // update the directory media counts, add all subfolder media counts to it - for (dir in dirs) { - var longestSharedPath = "" - for (newDir in newDirs) { - if (newDir.path == dir.path) { - longestSharedPath = dir.path - continue - } - - if (dir.path.startsWith(newDir.path, true) && newDir.path.length > longestSharedPath.length) { - longestSharedPath = newDir.path - } - } - - val parentFolder = newDirs.firstOrNull { it.path == longestSharedPath } - parentFolder?.apply { - subfoldersCount++ - } - - val mainFolder = newDirs.firstOrNull { it.path == longestSharedPath } - if (mainFolder != null) { - mainFolder.subfoldersMediaCount += dir.mediaCnt - } - } - - newDirs + val parentDirs = dirs.filter { foldersToShow.contains(it.path) } as ArrayList + updateSubfolderCounts(dirs, parentDirs) + parentDirs } else { dirs.forEach { it.subfoldersMediaCount = it.mediaCnt } dirs } } + private fun updateSubfolderCounts(children: ArrayList, parentDirs: ArrayList) { + for (child in children) { + var longestSharedPath = "" + for (parentDir in parentDirs) { + if (parentDir.path == child.path) { + longestSharedPath = child.path + continue + } + + if (child.path.startsWith(parentDir.path, true) && parentDir.path.length > longestSharedPath.length) { + longestSharedPath = parentDir.path + } + } + + parentDirs.firstOrNull { it.path == longestSharedPath }?.apply { + subfoldersCount++ + if (path != child.path) { + subfoldersMediaCount += child.mediaCnt + } + } + } + } + private fun createDirectoryFromMedia(path: String, curMedia: ArrayList, albumCovers: ArrayList, hiddenString: String, includedFolders: MutableSet, isSortingAscending: Boolean): Directory { var thumbnail = curMedia.firstOrNull { getDoesFilePathExist(it.path) }?.path ?: ""