change the way directories are grouped and sorted
This commit is contained in:
parent
9d9866c1d2
commit
0da84f2958
3 changed files with 69 additions and 44 deletions
|
@ -6,10 +6,12 @@ import com.simplemobiletools.commons.extensions.getFilenameFromPath
|
||||||
import com.simplemobiletools.commons.extensions.hasWriteStoragePermission
|
import com.simplemobiletools.commons.extensions.hasWriteStoragePermission
|
||||||
import com.simplemobiletools.commons.extensions.internalStoragePath
|
import com.simplemobiletools.commons.extensions.internalStoragePath
|
||||||
import com.simplemobiletools.commons.extensions.sdCardPath
|
import com.simplemobiletools.commons.extensions.sdCardPath
|
||||||
|
import com.simplemobiletools.commons.helpers.SORT_DESCENDING
|
||||||
import com.simplemobiletools.gallery.R
|
import com.simplemobiletools.gallery.R
|
||||||
import com.simplemobiletools.gallery.extensions.config
|
import com.simplemobiletools.gallery.extensions.config
|
||||||
import com.simplemobiletools.gallery.extensions.containsNoMedia
|
import com.simplemobiletools.gallery.extensions.containsNoMedia
|
||||||
import com.simplemobiletools.gallery.extensions.getFilesFrom
|
import com.simplemobiletools.gallery.extensions.getFilesFrom
|
||||||
|
import com.simplemobiletools.gallery.extensions.sumByLong
|
||||||
import com.simplemobiletools.gallery.models.Directory
|
import com.simplemobiletools.gallery.models.Directory
|
||||||
import com.simplemobiletools.gallery.models.Medium
|
import com.simplemobiletools.gallery.models.Medium
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
@ -19,7 +21,7 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va
|
||||||
val callback: (dirs: ArrayList<Directory>) -> Unit) : AsyncTask<Void, Void, ArrayList<Directory>>() {
|
val callback: (dirs: ArrayList<Directory>) -> Unit) : AsyncTask<Void, Void, ArrayList<Directory>>() {
|
||||||
var config = context.config
|
var config = context.config
|
||||||
var shouldStop = false
|
var shouldStop = false
|
||||||
val showHidden = config.shouldShowHidden
|
private val showHidden = config.shouldShowHidden
|
||||||
|
|
||||||
override fun doInBackground(vararg params: Void): ArrayList<Directory> {
|
override fun doInBackground(vararg params: Void): ArrayList<Directory> {
|
||||||
if (!context.hasWriteStoragePermission())
|
if (!context.hasWriteStoragePermission())
|
||||||
|
@ -28,53 +30,71 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va
|
||||||
val media = context.getFilesFrom("", isPickImage, isPickVideo)
|
val media = context.getFilesFrom("", isPickImage, isPickVideo)
|
||||||
val excludedPaths = config.excludedFolders
|
val excludedPaths = config.excludedFolders
|
||||||
val includedPaths = config.includedFolders
|
val includedPaths = config.includedFolders
|
||||||
val directories = groupDirectories(media)
|
|
||||||
val dirs = ArrayList(directories.values
|
|
||||||
.filter { File(it.path).exists() })
|
|
||||||
.filter { shouldFolderBeVisible(it.path, excludedPaths, includedPaths) } as ArrayList<Directory>
|
|
||||||
Directory.sorting = config.directorySorting
|
|
||||||
dirs.sort()
|
|
||||||
return movePinnedToFront(dirs)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun groupDirectories(media: ArrayList<Medium>): Map<String, Directory> {
|
|
||||||
val albumCovers = config.parseAlbumCovers()
|
|
||||||
val hidden = context.resources.getString(R.string.hidden)
|
val hidden = context.resources.getString(R.string.hidden)
|
||||||
val directories = LinkedHashMap<String, Directory>()
|
val directories = groupDirectories(media)
|
||||||
for ((name, path, isVideo, dateModified, dateTaken, size) in media) {
|
|
||||||
if (shouldStop)
|
|
||||||
cancel(true)
|
|
||||||
|
|
||||||
val parentDir = File(path).parent ?: continue
|
val removePaths = ArrayList<String>()
|
||||||
if (directories.containsKey(parentDir.toLowerCase())) {
|
directories.keys.forEach {
|
||||||
val directory = directories[parentDir.toLowerCase()]!!
|
if (!File(it).exists() || !shouldFolderBeVisible(it, excludedPaths, includedPaths)) {
|
||||||
val newImageCnt = directory.mediaCnt + 1
|
removePaths.add(it)
|
||||||
directory.mediaCnt = newImageCnt
|
}
|
||||||
directory.addSize(size)
|
|
||||||
} else {
|
|
||||||
var dirName = parentDir.getFilenameFromPath()
|
|
||||||
if (parentDir == context.internalStoragePath) {
|
|
||||||
dirName = context.getString(R.string.internal)
|
|
||||||
} else if (parentDir == context.sdCardPath) {
|
|
||||||
dirName = context.getString(R.string.sd_card)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (File(parentDir).containsNoMedia()) {
|
removePaths.forEach {
|
||||||
dirName += " $hidden"
|
directories.remove(it)
|
||||||
|
|
||||||
if (!showHidden)
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var thumbnail = path
|
val dirs = ArrayList<Directory>()
|
||||||
|
val albumCovers = config.parseAlbumCovers()
|
||||||
|
for ((path, curMedia) in directories) {
|
||||||
|
Medium.sorting = config.getFileSorting(path)
|
||||||
|
curMedia.sort()
|
||||||
|
|
||||||
|
val firstItem = curMedia.first()
|
||||||
|
val lastItem = curMedia.last()
|
||||||
|
val parentDir = File(firstItem.path).parent
|
||||||
|
var thumbnail = firstItem.path
|
||||||
albumCovers.forEach {
|
albumCovers.forEach {
|
||||||
if (it.path == parentDir && File(it.tmb).exists()) {
|
if (it.path == parentDir && File(it.tmb).exists()) {
|
||||||
thumbnail = it.tmb
|
thumbnail = it.tmb
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val directory = Directory(parentDir, thumbnail, dirName, 1, dateModified, dateTaken, size)
|
var dirName = when (parentDir) {
|
||||||
directories.put(parentDir.toLowerCase(), directory)
|
context.internalStoragePath -> context.getString(R.string.internal)
|
||||||
|
context.sdCardPath -> context.getString(R.string.sd_card)
|
||||||
|
else -> parentDir.getFilenameFromPath()
|
||||||
|
}
|
||||||
|
|
||||||
|
if (File(path).containsNoMedia()) {
|
||||||
|
dirName += " $hidden"
|
||||||
|
}
|
||||||
|
|
||||||
|
val lastModified = if (config.directorySorting and SORT_DESCENDING > 0) Math.max(firstItem.modified, lastItem.modified) else Math.min(firstItem.modified, lastItem.modified)
|
||||||
|
val dateTaken = if (config.directorySorting and SORT_DESCENDING > 0) Math.max(firstItem.taken, lastItem.taken) else Math.min(firstItem.taken, lastItem.taken)
|
||||||
|
val size = curMedia.sumByLong { it.size }
|
||||||
|
val directory = Directory(parentDir, thumbnail, dirName, curMedia.size, lastModified, dateTaken, size)
|
||||||
|
dirs.add(directory)
|
||||||
|
}
|
||||||
|
|
||||||
|
Directory.sorting = config.directorySorting
|
||||||
|
dirs.sort()
|
||||||
|
return movePinnedToFront(dirs)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun groupDirectories(media: ArrayList<Medium>): HashMap<String, ArrayList<Medium>> {
|
||||||
|
val directories = LinkedHashMap<String, ArrayList<Medium>>()
|
||||||
|
for (medium in media) {
|
||||||
|
if (shouldStop) {
|
||||||
|
cancel(true)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
val parentDir = File(medium.path).parent?.toLowerCase() ?: continue
|
||||||
|
if (directories.containsKey(parentDir)) {
|
||||||
|
directories[parentDir]!!.add(medium)
|
||||||
|
} else {
|
||||||
|
directories.put(parentDir, arrayListOf(medium))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return directories
|
return directories
|
||||||
|
@ -86,7 +106,7 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va
|
||||||
true
|
true
|
||||||
} else if (isThisOrParentExcluded(path, excludedPaths, includedPaths)) {
|
} else if (isThisOrParentExcluded(path, excludedPaths, includedPaths)) {
|
||||||
false
|
false
|
||||||
} else if (!config.shouldShowHidden && file.isDirectory && file.canonicalFile == file.absoluteFile) {
|
} else if (!showHidden && file.isDirectory && file.canonicalFile == file.absoluteFile) {
|
||||||
var containsNoMediaOrDot = file.containsNoMedia() || path.contains("/.")
|
var containsNoMediaOrDot = file.containsNoMedia() || path.contains("/.")
|
||||||
if (!containsNoMediaOrDot) {
|
if (!containsNoMediaOrDot) {
|
||||||
containsNoMediaOrDot = checkParentHasNoMedia(file.parentFile)
|
containsNoMediaOrDot = checkParentHasNoMedia(file.parentFile)
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
package com.simplemobiletools.gallery.extensions
|
||||||
|
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
fun <E> ArrayList<E>.sumByLong(selector: (E) -> Long) = map { selector(it) }.sum()
|
|
@ -28,17 +28,17 @@ class Config(context: Context) : BaseConfig(context) {
|
||||||
if (path.isEmpty()) {
|
if (path.isEmpty()) {
|
||||||
fileSorting = value
|
fileSorting = value
|
||||||
} else {
|
} else {
|
||||||
prefs.edit().putInt(SORT_FOLDER_PREFIX + path, value).apply()
|
prefs.edit().putInt(SORT_FOLDER_PREFIX + path.toLowerCase(), value).apply()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getFileSorting(path: String) = prefs.getInt(SORT_FOLDER_PREFIX + path, fileSorting)
|
fun getFileSorting(path: String) = prefs.getInt(SORT_FOLDER_PREFIX + path.toLowerCase(), fileSorting)
|
||||||
|
|
||||||
fun removeFileSorting(path: String) {
|
fun removeFileSorting(path: String) {
|
||||||
prefs.edit().remove(SORT_FOLDER_PREFIX + path).apply()
|
prefs.edit().remove(SORT_FOLDER_PREFIX + path.toLowerCase()).apply()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun hasCustomSorting(path: String) = prefs.contains(SORT_FOLDER_PREFIX + path)
|
fun hasCustomSorting(path: String) = prefs.contains(SORT_FOLDER_PREFIX + path.toLowerCase())
|
||||||
|
|
||||||
var wasHideFolderTooltipShown: Boolean
|
var wasHideFolderTooltipShown: Boolean
|
||||||
get() = prefs.getBoolean(HIDE_FOLDER_TOOLTIP_SHOWN, false)
|
get() = prefs.getBoolean(HIDE_FOLDER_TOOLTIP_SHOWN, false)
|
||||||
|
|
Loading…
Reference in a new issue