From 0da84f295887c9ce8c33fa2a5aab6692b20069b2 Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 25 Sep 2017 21:04:09 +0200 Subject: [PATCH] change the way directories are grouped and sorted --- .../asynctasks/GetDirectoriesAsynctask.kt | 100 +++++++++++------- .../gallery/extensions/arrayList.kt | 5 + .../gallery/helpers/Config.kt | 8 +- 3 files changed, 69 insertions(+), 44 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/gallery/extensions/arrayList.kt diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt index b68d69d09..9f609bb23 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt @@ -6,10 +6,12 @@ import com.simplemobiletools.commons.extensions.getFilenameFromPath import com.simplemobiletools.commons.extensions.hasWriteStoragePermission import com.simplemobiletools.commons.extensions.internalStoragePath import com.simplemobiletools.commons.extensions.sdCardPath +import com.simplemobiletools.commons.helpers.SORT_DESCENDING import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.extensions.config import com.simplemobiletools.gallery.extensions.containsNoMedia import com.simplemobiletools.gallery.extensions.getFilesFrom +import com.simplemobiletools.gallery.extensions.sumByLong import com.simplemobiletools.gallery.models.Directory import com.simplemobiletools.gallery.models.Medium import java.io.File @@ -19,7 +21,7 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va val callback: (dirs: ArrayList) -> Unit) : AsyncTask>() { var config = context.config var shouldStop = false - val showHidden = config.shouldShowHidden + private val showHidden = config.shouldShowHidden override fun doInBackground(vararg params: Void): ArrayList { if (!context.hasWriteStoragePermission()) @@ -28,53 +30,71 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va val media = context.getFilesFrom("", isPickImage, isPickVideo) val excludedPaths = config.excludedFolders val includedPaths = config.includedFolders + val hidden = context.resources.getString(R.string.hidden) val directories = groupDirectories(media) - val dirs = ArrayList(directories.values - .filter { File(it.path).exists() }) - .filter { shouldFolderBeVisible(it.path, excludedPaths, includedPaths) } as ArrayList + + val removePaths = ArrayList() + directories.keys.forEach { + if (!File(it).exists() || !shouldFolderBeVisible(it, excludedPaths, includedPaths)) { + removePaths.add(it) + } + } + + removePaths.forEach { + directories.remove(it) + } + + val dirs = ArrayList() + 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 { + if (it.path == parentDir && File(it.tmb).exists()) { + thumbnail = it.tmb + } + } + + var dirName = when (parentDir) { + 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): Map { - val albumCovers = config.parseAlbumCovers() - val hidden = context.resources.getString(R.string.hidden) - val directories = LinkedHashMap() - for ((name, path, isVideo, dateModified, dateTaken, size) in media) { - if (shouldStop) + private fun groupDirectories(media: ArrayList): HashMap> { + val directories = LinkedHashMap>() + for (medium in media) { + if (shouldStop) { cancel(true) + break + } - val parentDir = File(path).parent ?: continue - if (directories.containsKey(parentDir.toLowerCase())) { - val directory = directories[parentDir.toLowerCase()]!! - val newImageCnt = directory.mediaCnt + 1 - directory.mediaCnt = newImageCnt - directory.addSize(size) + val parentDir = File(medium.path).parent?.toLowerCase() ?: continue + if (directories.containsKey(parentDir)) { + directories[parentDir]!!.add(medium) } 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()) { - dirName += " $hidden" - - if (!showHidden) - continue - } - - var thumbnail = path - albumCovers.forEach { - if (it.path == parentDir && File(it.tmb).exists()) { - thumbnail = it.tmb - } - } - - val directory = Directory(parentDir, thumbnail, dirName, 1, dateModified, dateTaken, size) - directories.put(parentDir.toLowerCase(), directory) + directories.put(parentDir, arrayListOf(medium)) } } return directories @@ -86,7 +106,7 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va true } else if (isThisOrParentExcluded(path, excludedPaths, includedPaths)) { 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("/.") if (!containsNoMediaOrDot) { containsNoMediaOrDot = checkParentHasNoMedia(file.parentFile) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/arrayList.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/arrayList.kt new file mode 100644 index 000000000..8d5d3ad27 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/arrayList.kt @@ -0,0 +1,5 @@ +package com.simplemobiletools.gallery.extensions + +import java.util.* + +fun ArrayList.sumByLong(selector: (E) -> Long) = map { selector(it) }.sum() diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Config.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Config.kt index f17e9eed1..5cd08be16 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Config.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Config.kt @@ -28,17 +28,17 @@ class Config(context: Context) : BaseConfig(context) { if (path.isEmpty()) { fileSorting = value } 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) { - 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 get() = prefs.getBoolean(HIDE_FOLDER_TOOLTIP_SHOWN, false)