From 98061e4fc21b46b413dfcf1b59ea794c86c520fd Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 17 Dec 2018 11:52:11 +0100 Subject: [PATCH] moving some functions related to subfolder grouping in Context extensions --- .../gallery/pro/activities/MainActivity.kt | 107 +---------------- .../gallery/pro/extensions/Context.kt | 109 ++++++++++++++++++ 2 files changed, 110 insertions(+), 106 deletions(-) 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 f6597439e..cf2a94ded 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 @@ -932,117 +932,12 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { } } - private fun getDirectParentSubfolders(folders: HashSet): HashSet { - val internalPath = internalStoragePath - val sdPath = sdCardPath - val currentPaths = LinkedHashSet() - folders.forEach { - val path = it - if (!path.equals(internalPath, true) && !path.equals(sdPath, true) && path != RECYCLE_BIN && path != FAVORITES) { - if (mCurrentPathPrefix.isNotEmpty()) { - if (File(path).parent.equals(mCurrentPathPrefix, true) || path == mCurrentPathPrefix) { - currentPaths.add(path) - } - } else if (folders.any { !it.equals(path, true) && (File(path).parent.equals(it, true) || File(it).parent.equals(File(path).parent, true)) }) { - val parent = File(path).parent - currentPaths.add(parent) - } else { - currentPaths.add(path) - } - } - } - - var areDirectSubfoldersAvailable = false - currentPaths.forEach { - val path = it - currentPaths.forEach { - if (!it.equals(path) && File(it).parent?.equals(path) == true) { - areDirectSubfoldersAvailable = true - } - } - } - - if (mCurrentPathPrefix.isEmpty() && folders.contains(RECYCLE_BIN)) { - currentPaths.add(RECYCLE_BIN) - } - - if (mCurrentPathPrefix.isEmpty() && folders.contains(FAVORITES)) { - currentPaths.add(FAVORITES) - } - - if (folders.size == currentPaths.size) { - return currentPaths - } - - folders.clear() - folders.addAll(currentPaths) - return if (areDirectSubfoldersAvailable) { - getDirectParentSubfolders(folders) - } else { - folders - } - } - private fun checkPlaceholderVisibility(dirs: ArrayList) { directories_empty_text_label.beVisibleIf(dirs.isEmpty() && mLoadedInitialPhotos) directories_empty_text.beVisibleIf(dirs.isEmpty() && mLoadedInitialPhotos) directories_grid.beVisibleIf(directories_empty_text_label.isGone()) } - private fun getDirsToShow(dirs: ArrayList): ArrayList { - return if (config.groupDirectSubfolders) { - dirs.forEach { - it.subfoldersCount = 0 - it.subfoldersMediaCount = it.mediaCnt - } - - val dirFolders = dirs.map { it.path }.sorted().toMutableSet() as HashSet - val foldersToShow = getDirectParentSubfolders(dirFolders) - val parentDirs = dirs.filter { foldersToShow.contains(it.path) } as ArrayList - updateSubfolderCounts(dirs, parentDirs) - - // show the current folder as an available option too, not just subfolders - if (mCurrentPathPrefix.isNotEmpty()) { - val currentFolder = mDirs.firstOrNull { parentDirs.firstOrNull { it.path == mCurrentPathPrefix } == null && it.path == mCurrentPathPrefix } - currentFolder?.apply { - subfoldersCount = 1 - parentDirs.add(this) - } - } - - 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 - } - } - - // make sure we count only the proper direct subfolders, grouped the same way as on the main screen - parentDirs.firstOrNull { it.path == longestSharedPath }?.apply { - if (path.equals(child.path, true) || path.equals(File(child.path).parent, true) || children.any { it.path.equals(File(child.path).parent, true) }) { - 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 ?: "" @@ -1070,7 +965,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { val currAdapter = directories_grid.adapter val distinctDirs = dirs.distinctBy { it.path.getDistinctPath() }.toMutableList() as ArrayList val sortedDirs = getSortedDirectories(distinctDirs) - var dirsToShow = getDirsToShow(sortedDirs).clone() as ArrayList + var dirsToShow = getDirsToShow(sortedDirs, mDirs, mCurrentPathPrefix).clone() as ArrayList if (currAdapter == null) { initZoomListener() 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 7e523220c..8fdb3ee20 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 @@ -35,6 +35,10 @@ import com.simplemobiletools.gallery.pro.svg.SvgSoftwareLayerSetter import com.simplemobiletools.gallery.pro.views.MySquareImageView import pl.droidsonroids.gif.GifDrawable import java.io.File +import java.util.HashSet +import java.util.LinkedHashSet +import kotlin.Comparator +import kotlin.collections.ArrayList val Context.portrait get() = resources.configuration.orientation == Configuration.ORIENTATION_PORTRAIT val Context.audioManager get() = getSystemService(Context.AUDIO_SERVICE) as AudioManager @@ -175,6 +179,111 @@ fun Context.getSortedDirectories(source: ArrayList): ArrayList, allDirs: ArrayList, currentPathPrefix: String): ArrayList { + return if (config.groupDirectSubfolders) { + dirs.forEach { + it.subfoldersCount = 0 + it.subfoldersMediaCount = it.mediaCnt + } + + val dirFolders = dirs.map { it.path }.sorted().toMutableSet() as HashSet + val foldersToShow = getDirectParentSubfolders(dirFolders, currentPathPrefix) + val parentDirs = dirs.filter { foldersToShow.contains(it.path) } as ArrayList + updateSubfolderCounts(dirs, parentDirs) + + // show the current folder as an available option too, not just subfolders + if (currentPathPrefix.isNotEmpty()) { + val currentFolder = allDirs.firstOrNull { parentDirs.firstOrNull { it.path == currentPathPrefix } == null && it.path == currentPathPrefix } + currentFolder?.apply { + subfoldersCount = 1 + parentDirs.add(this) + } + } + + parentDirs + } else { + dirs.forEach { it.subfoldersMediaCount = it.mediaCnt } + dirs + } +} + +fun Context.getDirectParentSubfolders(folders: HashSet, currentPathPrefix: String): HashSet { + val internalPath = internalStoragePath + val sdPath = sdCardPath + val currentPaths = LinkedHashSet() + folders.forEach { + val path = it + if (!path.equals(internalPath, true) && !path.equals(sdPath, true) && path != RECYCLE_BIN && path != FAVORITES) { + if (currentPathPrefix.isNotEmpty()) { + if (File(path).parent.equals(currentPathPrefix, true) || path == currentPathPrefix) { + currentPaths.add(path) + } + } else if (folders.any { !it.equals(path, true) && (File(path).parent.equals(it, true) || File(it).parent.equals(File(path).parent, true)) }) { + val parent = File(path).parent + currentPaths.add(parent) + } else { + currentPaths.add(path) + } + } + } + + var areDirectSubfoldersAvailable = false + currentPaths.forEach { + val path = it + currentPaths.forEach { + if (!it.equals(path) && File(it).parent?.equals(path) == true) { + areDirectSubfoldersAvailable = true + } + } + } + + if (currentPathPrefix.isEmpty() && folders.contains(RECYCLE_BIN)) { + currentPaths.add(RECYCLE_BIN) + } + + if (currentPathPrefix.isEmpty() && folders.contains(FAVORITES)) { + currentPaths.add(FAVORITES) + } + + if (folders.size == currentPaths.size) { + return currentPaths + } + + folders.clear() + folders.addAll(currentPaths) + return if (areDirectSubfoldersAvailable) { + getDirectParentSubfolders(folders, currentPathPrefix) + } else { + folders + } +} + +fun Context.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 + } + } + + // make sure we count only the proper direct subfolders, grouped the same way as on the main screen + parentDirs.firstOrNull { it.path == longestSharedPath }?.apply { + if (path.equals(child.path, true) || path.equals(File(child.path).parent, true) || children.any { it.path.equals(File(child.path).parent, true) }) { + subfoldersCount++ + if (path != child.path) { + subfoldersMediaCount += child.mediaCnt + } + } + } + } +} + fun Context.getNoMediaFolders(callback: (folders: ArrayList) -> Unit) { Thread { val folders = ArrayList()