moving some functions related to subfolder grouping in Context extensions

This commit is contained in:
tibbi 2018-12-17 11:52:11 +01:00
parent 7ba0be43a9
commit 98061e4fc2
2 changed files with 110 additions and 106 deletions

View file

@ -932,117 +932,12 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener {
} }
} }
private fun getDirectParentSubfolders(folders: HashSet<String>): HashSet<String> {
val internalPath = internalStoragePath
val sdPath = sdCardPath
val currentPaths = LinkedHashSet<String>()
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<Directory>) { private fun checkPlaceholderVisibility(dirs: ArrayList<Directory>) {
directories_empty_text_label.beVisibleIf(dirs.isEmpty() && mLoadedInitialPhotos) directories_empty_text_label.beVisibleIf(dirs.isEmpty() && mLoadedInitialPhotos)
directories_empty_text.beVisibleIf(dirs.isEmpty() && mLoadedInitialPhotos) directories_empty_text.beVisibleIf(dirs.isEmpty() && mLoadedInitialPhotos)
directories_grid.beVisibleIf(directories_empty_text_label.isGone()) directories_grid.beVisibleIf(directories_empty_text_label.isGone())
} }
private fun getDirsToShow(dirs: ArrayList<Directory>): ArrayList<Directory> {
return if (config.groupDirectSubfolders) {
dirs.forEach {
it.subfoldersCount = 0
it.subfoldersMediaCount = it.mediaCnt
}
val dirFolders = dirs.map { it.path }.sorted().toMutableSet() as HashSet<String>
val foldersToShow = getDirectParentSubfolders(dirFolders)
val parentDirs = dirs.filter { foldersToShow.contains(it.path) } as ArrayList<Directory>
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<Directory>, parentDirs: ArrayList<Directory>) {
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<Medium>, albumCovers: ArrayList<AlbumCover>, hiddenString: String, private fun createDirectoryFromMedia(path: String, curMedia: ArrayList<Medium>, albumCovers: ArrayList<AlbumCover>, hiddenString: String,
includedFolders: MutableSet<String>, isSortingAscending: Boolean): Directory { includedFolders: MutableSet<String>, isSortingAscending: Boolean): Directory {
var thumbnail = curMedia.firstOrNull { getDoesFilePathExist(it.path) }?.path ?: "" var thumbnail = curMedia.firstOrNull { getDoesFilePathExist(it.path) }?.path ?: ""
@ -1070,7 +965,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener {
val currAdapter = directories_grid.adapter val currAdapter = directories_grid.adapter
val distinctDirs = dirs.distinctBy { it.path.getDistinctPath() }.toMutableList() as ArrayList<Directory> val distinctDirs = dirs.distinctBy { it.path.getDistinctPath() }.toMutableList() as ArrayList<Directory>
val sortedDirs = getSortedDirectories(distinctDirs) val sortedDirs = getSortedDirectories(distinctDirs)
var dirsToShow = getDirsToShow(sortedDirs).clone() as ArrayList<Directory> var dirsToShow = getDirsToShow(sortedDirs, mDirs, mCurrentPathPrefix).clone() as ArrayList<Directory>
if (currAdapter == null) { if (currAdapter == null) {
initZoomListener() initZoomListener()

View file

@ -35,6 +35,10 @@ import com.simplemobiletools.gallery.pro.svg.SvgSoftwareLayerSetter
import com.simplemobiletools.gallery.pro.views.MySquareImageView import com.simplemobiletools.gallery.pro.views.MySquareImageView
import pl.droidsonroids.gif.GifDrawable import pl.droidsonroids.gif.GifDrawable
import java.io.File 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.portrait get() = resources.configuration.orientation == Configuration.ORIENTATION_PORTRAIT
val Context.audioManager get() = getSystemService(Context.AUDIO_SERVICE) as AudioManager val Context.audioManager get() = getSystemService(Context.AUDIO_SERVICE) as AudioManager
@ -175,6 +179,111 @@ fun Context.getSortedDirectories(source: ArrayList<Directory>): ArrayList<Direct
return movePinnedDirectoriesToFront(dirs) return movePinnedDirectoriesToFront(dirs)
} }
fun Context.getDirsToShow(dirs: ArrayList<Directory>, allDirs: ArrayList<Directory>, currentPathPrefix: String): ArrayList<Directory> {
return if (config.groupDirectSubfolders) {
dirs.forEach {
it.subfoldersCount = 0
it.subfoldersMediaCount = it.mediaCnt
}
val dirFolders = dirs.map { it.path }.sorted().toMutableSet() as HashSet<String>
val foldersToShow = getDirectParentSubfolders(dirFolders, currentPathPrefix)
val parentDirs = dirs.filter { foldersToShow.contains(it.path) } as ArrayList<Directory>
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<String>, currentPathPrefix: String): HashSet<String> {
val internalPath = internalStoragePath
val sdPath = sdCardPath
val currentPaths = LinkedHashSet<String>()
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<Directory>, parentDirs: ArrayList<Directory>) {
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<String>) -> Unit) { fun Context.getNoMediaFolders(callback: (folders: ArrayList<String>) -> Unit) {
Thread { Thread {
val folders = ArrayList<String>() val folders = ArrayList<String>()