Merge pull request #2924 from esensar/fix/1886-subfolder-grouping-revert
Revert "Make subfolder grouping follow file structure"
This commit is contained in:
commit
29e6a0eef0
1 changed files with 76 additions and 70 deletions
|
@ -197,88 +197,89 @@ fun Context.getDirsToShow(dirs: ArrayList<Directory>, allDirs: ArrayList<Directo
|
||||||
|
|
||||||
fun Context.getDirectParentSubfolders(dirs: ArrayList<Directory>, currentPathPrefix: String): ArrayList<Directory> {
|
fun Context.getDirectParentSubfolders(dirs: ArrayList<Directory>, currentPathPrefix: String): ArrayList<Directory> {
|
||||||
val folders = dirs.map { it.path }.sorted().toMutableSet() as HashSet<String>
|
val folders = dirs.map { it.path }.sorted().toMutableSet() as HashSet<String>
|
||||||
// Sort by path length, to ensure that parents get processed first
|
val currentPaths = LinkedHashSet<String>()
|
||||||
val foldersByPathLength = dirs.filter {
|
val foldersWithoutMediaFiles = ArrayList<String>()
|
||||||
currentPathPrefix.isEmpty() ||
|
|
||||||
(it.path.startsWith(currentPathPrefix, true) && it.path != currentPathPrefix)
|
|
||||||
}.sortedBy {
|
|
||||||
it.path.length
|
|
||||||
}
|
|
||||||
|
|
||||||
var newDirId = 1000L
|
var newDirId = 1000L
|
||||||
val groups = mutableMapOf<String, MutableList<Directory>>()
|
|
||||||
|
|
||||||
for (folder in foldersByPathLength) {
|
for (path in folders) {
|
||||||
val parent = groups.keys.firstOrNull { folder.path.startsWith(it, true) }
|
if (path == RECYCLE_BIN || path == FAVORITES) {
|
||||||
if (parent != null) {
|
continue
|
||||||
// If we have parent in top level groups
|
}
|
||||||
// Add this folder to that group,
|
|
||||||
// but also add all folders in between which may not have media files
|
|
||||||
groups.getOrPut(parent, ::mutableListOf).apply {
|
|
||||||
var midParent = File(folder.path).parent
|
|
||||||
while (midParent != null && none { it.path.equals(midParent, true) }) {
|
|
||||||
val isSortingAscending = config.sorting.isSortingAscending()
|
|
||||||
val subDirs = dirs.filter { File(it.path).parent.equals(midParent, true) } as ArrayList<Directory>
|
|
||||||
if (subDirs.isNotEmpty()) {
|
|
||||||
val lastModified = if (isSortingAscending) {
|
|
||||||
subDirs.minByOrNull { it.modified }?.modified
|
|
||||||
} else {
|
|
||||||
subDirs.maxByOrNull { it.modified }?.modified
|
|
||||||
} ?: 0
|
|
||||||
|
|
||||||
val dateTaken = if (isSortingAscending) {
|
if (currentPathPrefix.isNotEmpty()) {
|
||||||
subDirs.minByOrNull { it.taken }?.taken
|
if (!path.startsWith(currentPathPrefix, true)) {
|
||||||
} else {
|
continue
|
||||||
subDirs.maxByOrNull { it.taken }?.taken
|
}
|
||||||
} ?: 0
|
|
||||||
|
|
||||||
var mediaTypes = 0
|
if (!File(path).parent.equals(currentPathPrefix, true)) {
|
||||||
subDirs.forEach {
|
continue
|
||||||
mediaTypes = mediaTypes or it.types
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val directory = Directory(
|
if (currentPathPrefix.isNotEmpty() && path.equals(currentPathPrefix, true) || File(path).parent.equals(currentPathPrefix, true)) {
|
||||||
newDirId++,
|
currentPaths.add(path)
|
||||||
midParent,
|
} else if (folders.any { !it.equals(path, true) && (File(path).parent.equals(it, true) || File(it).parent.equals(File(path).parent, true)) }) {
|
||||||
subDirs.first().tmb,
|
// if we have folders like
|
||||||
getFolderNameFromPath(midParent),
|
// /storage/emulated/0/Pictures/Images and
|
||||||
subDirs.sumBy { it.mediaCnt },
|
// /storage/emulated/0/Pictures/Screenshots,
|
||||||
lastModified,
|
// but /storage/emulated/0/Pictures is empty, still Pictures with the first folders thumbnails and proper other info
|
||||||
dateTaken,
|
val parent = File(path).parent
|
||||||
subDirs.sumByLong { it.size },
|
if (parent != null && !folders.contains(parent) && dirs.none { it.path.equals(parent, true) }) {
|
||||||
getPathLocation(midParent),
|
currentPaths.add(parent)
|
||||||
mediaTypes,
|
val isSortingAscending = config.sorting.isSortingAscending()
|
||||||
""
|
val subDirs = dirs.filter { File(it.path).parent.equals(File(path).parent, true) } as ArrayList<Directory>
|
||||||
)
|
if (subDirs.isNotEmpty()) {
|
||||||
|
val lastModified = if (isSortingAscending) {
|
||||||
|
subDirs.minByOrNull { it.modified }?.modified
|
||||||
|
} else {
|
||||||
|
subDirs.maxByOrNull { it.modified }?.modified
|
||||||
|
} ?: 0
|
||||||
|
|
||||||
directory.containsMediaFilesDirectly = false
|
val dateTaken = if (isSortingAscending) {
|
||||||
dirs.add(directory)
|
subDirs.minByOrNull { it.taken }?.taken
|
||||||
add(directory)
|
} else {
|
||||||
|
subDirs.maxByOrNull { it.taken }?.taken
|
||||||
|
} ?: 0
|
||||||
|
|
||||||
|
var mediaTypes = 0
|
||||||
|
subDirs.forEach {
|
||||||
|
mediaTypes = mediaTypes or it.types
|
||||||
}
|
}
|
||||||
midParent = File(midParent).parent
|
|
||||||
|
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)
|
||||||
|
foldersWithoutMediaFiles.add(parent)
|
||||||
}
|
}
|
||||||
add(folder)
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// If we have don't have parent in top level groups
|
currentPaths.add(path)
|
||||||
// Set this folder as top level group if it is direct child
|
|
||||||
if (currentPathPrefix.isEmpty() || File(folder.path).parent.equals(currentPathPrefix, true)) {
|
|
||||||
groups.getOrPut(folder.path, ::mutableListOf).add(folder)
|
|
||||||
} else {
|
|
||||||
// Otherwise find its parent which is a direct child of current path prefix
|
|
||||||
// And create a group for it
|
|
||||||
var firstVisibleParent = File(folder.path).parent
|
|
||||||
while (firstVisibleParent != null && !File(firstVisibleParent).parent.equals(currentPathPrefix, true)) {
|
|
||||||
firstVisibleParent = File(firstVisibleParent).parent
|
|
||||||
}
|
|
||||||
if (firstVisibleParent != null) {
|
|
||||||
groups.getOrPut(firstVisibleParent, ::mutableListOf).add(folder)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val currentPaths = groups.keys.toMutableList()
|
var areDirectSubfoldersAvailable = false
|
||||||
|
currentPaths.forEach {
|
||||||
|
val path = it
|
||||||
|
currentPaths.forEach {
|
||||||
|
if (!foldersWithoutMediaFiles.contains(it) && !it.equals(path, true) && File(it).parent?.equals(path, true) == true) {
|
||||||
|
areDirectSubfoldersAvailable = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (currentPathPrefix.isEmpty() && folders.contains(RECYCLE_BIN)) {
|
if (currentPathPrefix.isEmpty() && folders.contains(RECYCLE_BIN)) {
|
||||||
currentPaths.add(RECYCLE_BIN)
|
currentPaths.add(RECYCLE_BIN)
|
||||||
|
@ -295,7 +296,12 @@ fun Context.getDirectParentSubfolders(dirs: ArrayList<Directory>, currentPathPre
|
||||||
folders.clear()
|
folders.clear()
|
||||||
folders.addAll(currentPaths)
|
folders.addAll(currentPaths)
|
||||||
|
|
||||||
return dirs.filter { folders.contains(it.path) } as ArrayList<Directory>
|
val dirsToShow = dirs.filter { folders.contains(it.path) } as ArrayList<Directory>
|
||||||
|
return if (areDirectSubfoldersAvailable) {
|
||||||
|
getDirectParentSubfolders(dirsToShow, currentPathPrefix)
|
||||||
|
} else {
|
||||||
|
dirsToShow
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Context.updateSubfolderCounts(children: ArrayList<Directory>, parentDirs: ArrayList<Directory>) {
|
fun Context.updateSubfolderCounts(children: ArrayList<Directory>, parentDirs: ArrayList<Directory>) {
|
||||||
|
|
Loading…
Reference in a new issue