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 65d981fc3..e780d62c4 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 @@ -472,7 +472,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { private fun showSortingDialog() { ChangeSortingDialog(this, true, false) { directories_grid.adapter = null - if (config.directorySorting and SORT_BY_DATE_MODIFIED > 0 || config.directorySorting and SORT_BY_DATE_TAKEN > 0) { + if (config.directorySorting and SORT_BY_DATE_MODIFIED != 0 || config.directorySorting and SORT_BY_DATE_TAKEN != 0) { getDirectories() } else { ensureBackgroundThread { @@ -893,7 +893,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { val tempFolderPath = config.tempFolderPath val isSortingAscending = config.directorySorting and SORT_DESCENDING == 0 val getProperDateTaken = config.directorySorting and SORT_BY_DATE_TAKEN != 0 - val getProperLastModified = config.directorySorting and SORT_BY_DATE_MODIFIED != 0 + var getProperLastModified = config.directorySorting and SORT_BY_DATE_MODIFIED != 0 val getProperFileSize = config.directorySorting and SORT_BY_SIZE != 0 val favoritePaths = getFavoritePaths() val dirPathsToRemove = ArrayList() @@ -904,6 +904,10 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { return } + if (!getProperLastModified) { + getProperLastModified = config.getFileSorting(directory.path) and SORT_BY_DATE_MODIFIED != 0 + } + val curMedia = mLastMediaFetcher!!.getFilesFrom(directory.path, getImagesOnly, getVideosOnly, getProperDateTaken, getProperLastModified, getProperFileSize, favoritePaths, false) val newDir = if (curMedia.isEmpty()) { if (directory.path != tempFolderPath) { @@ -927,6 +931,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { taken = newDir.taken this@apply.size = newDir.size types = newDir.types + sortValue = getDirectorySortingValue(curMedia) } setupAdapter(dirs) @@ -977,6 +982,10 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { return } + if (!getProperLastModified) { + getProperLastModified = config.getFileSorting(folder) and SORT_BY_DATE_MODIFIED != 0 + } + val newMedia = mLastMediaFetcher!!.getFilesFrom(folder, getImagesOnly, getVideosOnly, getProperDateTaken, getProperLastModified, getProperFileSize, favoritePaths, false) if (newMedia.isEmpty()) { continue diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/asynctasks/GetMediaAsynctask.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/asynctasks/GetMediaAsynctask.kt index 955d55df8..c459a0b30 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/asynctasks/GetMediaAsynctask.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/asynctasks/GetMediaAsynctask.kt @@ -36,7 +36,7 @@ class GetMediaAsynctask(val context: Context, val mPath: String, val isPickImage val foldersToScan = mediaFetcher.getFoldersToScan().filter { it != RECYCLE_BIN && it != FAVORITES && !context.config.isFolderProtected(it) } val media = ArrayList() foldersToScan.forEach { - val newMedia = mediaFetcher.getFilesFrom(it, isPickImage, isPickVideo, getProperDateTaken, getProperLastModified, getProperFileSize, favoritePaths, getVideoDurations, false) + val newMedia = mediaFetcher.getFilesFrom(it, isPickImage, isPickVideo, getProperDateTaken, getProperLastModified, getProperFileSize, favoritePaths, getVideoDurations) media.addAll(newMedia) } 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 363b9cb19..d481041e8 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 @@ -165,25 +165,26 @@ fun Context.getSortedDirectories(source: ArrayList): ArrayList o1 as Directory o2 as Directory + var result = when { sorting and SORT_BY_NAME != 0 -> { if (sorting and SORT_USE_NUMERIC_VALUE != 0) { - AlphanumericComparator().compare(o1.name.toLowerCase(), o2.name.toLowerCase()) + AlphanumericComparator().compare(o1.sortValue.toLowerCase(), o2.sortValue.toLowerCase()) } else { - o1.name.toLowerCase().compareTo(o2.name.toLowerCase()) + o1.sortValue.toLowerCase().compareTo(o2.sortValue.toLowerCase()) } } sorting and SORT_BY_PATH != 0 -> { if (sorting and SORT_USE_NUMERIC_VALUE != 0) { - AlphanumericComparator().compare(o1.path.toLowerCase(), o2.path.toLowerCase()) + AlphanumericComparator().compare(o1.sortValue.toLowerCase(), o2.sortValue.toLowerCase()) } else { - o1.path.toLowerCase().compareTo(o2.path.toLowerCase()) + o1.sortValue.toLowerCase().compareTo(o2.sortValue.toLowerCase()) } } - sorting and SORT_BY_PATH != 0 -> AlphanumericComparator().compare(o1.path.toLowerCase(), o2.path.toLowerCase()) - sorting and SORT_BY_SIZE != 0 -> o1.size.compareTo(o2.size) - sorting and SORT_BY_DATE_MODIFIED != 0 -> o1.modified.compareTo(o2.modified) - else -> o1.taken.compareTo(o2.taken) + sorting and SORT_BY_PATH != 0 -> AlphanumericComparator().compare(o1.sortValue.toLowerCase(), o2.sortValue.toLowerCase()) + sorting and SORT_BY_SIZE != 0 -> (o1.sortValue.toLong()).compareTo(o2.sortValue.toLong()) + sorting and SORT_BY_DATE_MODIFIED != 0 -> (o1.sortValue.toLong()).compareTo(o2.sortValue.toLong()) + else -> (o1.sortValue.toLong()).compareTo(o2.sortValue.toLong()) } if (sorting and SORT_DESCENDING != 0) { @@ -252,7 +253,7 @@ fun Context.getDirectParentSubfolders(dirs: ArrayList, currentPathPre val parent = File(path).parent if (parent != null && !folders.contains(parent) && dirs.none { it.path == parent }) { currentPaths.add(parent) - val isSortingAscending = config.sorting and SORT_DESCENDING == 0 + val isSortingAscending = config.sorting.isSortingAscending() val subDirs = dirs.filter { File(it.path).parent.equals(File(path).parent, true) } as ArrayList if (subDirs.isNotEmpty()) { val lastModified = if (isSortingAscending) { @@ -281,7 +282,8 @@ fun Context.getDirectParentSubfolders(dirs: ArrayList, currentPathPre dateTaken, subDirs.sumByLong { it.size }, getPathLocation(parent), - mediaTypes) + mediaTypes, + "") directory.containsMediaFilesDirectly = false dirs.add(directory) @@ -479,7 +481,7 @@ fun Context.addTempFolderIfNeeded(dirs: ArrayList): ArrayList() - val newFolder = Directory(null, tempFolderPath, "", tempFolderPath.getFilenameFromPath(), 0, 0, 0, 0L, getPathLocation(tempFolderPath), 0) + val newFolder = Directory(null, tempFolderPath, "", tempFolderPath.getFilenameFromPath(), 0, 0, 0, 0L, getPathLocation(tempFolderPath), 0, "") directories.add(newFolder) directories.addAll(dirs) directories @@ -706,7 +708,7 @@ fun Context.updateDBMediaPath(oldPath: String, newPath: String) { fun Context.updateDBDirectory(directory: Directory) { try { - directoryDao.updateDirectory(directory.path, directory.tmb, directory.mediaCnt, directory.modified, directory.taken, directory.size, directory.types) + directoryDao.updateDirectory(directory.path, directory.tmb, directory.mediaCnt, directory.modified, directory.taken, directory.size, directory.types, directory.sortValue) } catch (ignored: Exception) { } } @@ -858,6 +860,7 @@ fun Context.createDirectoryFromMedia(path: String, curMedia: ArrayList, includedFolders: MutableSet, isSortingAscending: Boolean, getProperFileSize: Boolean): Directory { val OTGPath = config.OTGPath var thumbnail = curMedia.firstOrNull { getDoesFilePathExist(it.path, OTGPath) }?.path ?: "" + albumCovers.forEach { if (it.path == path && getDoesFilePathExist(it.tmb, OTGPath)) { thumbnail = it.tmb @@ -876,7 +879,36 @@ fun Context.createDirectoryFromMedia(path: String, curMedia: ArrayList, val dateTaken = if (isSortingAscending) Math.min(firstItem.taken, lastItem.taken) else Math.max(firstItem.taken, lastItem.taken) val size = if (getProperFileSize) curMedia.sumByLong { it.size } else 0L val mediaTypes = curMedia.getDirMediaTypes() - return Directory(null, path, thumbnail, dirName, curMedia.size, lastModified, dateTaken, size, getPathLocation(path), mediaTypes) + return Directory(null, path, thumbnail, dirName, curMedia.size, lastModified, dateTaken, size, getPathLocation(path), mediaTypes, getDirectorySortingValue(curMedia)) +} + +fun Context.getDirectorySortingValue(media: ArrayList): String { + val sorting = config.directorySorting + val sorted = when { + sorting and SORT_BY_NAME != 0 -> media.sortedBy { it.name } + sorting and SORT_BY_PATH != 0 -> media.sortedBy { it.path } + sorting and SORT_BY_SIZE != 0 -> media.sortedBy { it.size } + sorting and SORT_BY_DATE_MODIFIED != 0 -> media.sortedBy { it.modified } + sorting and SORT_BY_DATE_TAKEN != 0 -> media.sortedBy { it.taken } + else -> media + } + + val relevantMedium = if (sorting.isSortingAscending()) { + sorted.first() + } else { + sorted.last() + } + + val result: Any = when { + sorting and SORT_BY_NAME != 0 -> relevantMedium.name + sorting and SORT_BY_PATH != 0 -> relevantMedium.path + sorting and SORT_BY_SIZE != 0 -> relevantMedium.size + sorting and SORT_BY_DATE_MODIFIED != 0 -> relevantMedium.modified + sorting and SORT_BY_DATE_TAKEN != 0 -> relevantMedium.taken + else -> 0 + } + + return result.toString() } fun Context.updateDirectoryPath(path: String) { @@ -886,7 +918,7 @@ fun Context.updateDirectoryPath(path: String) { val hiddenString = getString(R.string.hidden) val albumCovers = config.parseAlbumCovers() val includedFolders = config.includedFolders - val isSortingAscending = config.directorySorting and SORT_DESCENDING == 0 + val isSortingAscending = config.directorySorting.isSortingAscending() val getProperDateTaken = config.directorySorting and SORT_BY_DATE_TAKEN != 0 val getProperLastModified = config.directorySorting and SORT_BY_DATE_MODIFIED != 0 val getProperFileSize = config.directorySorting and SORT_BY_SIZE != 0 @@ -919,5 +951,3 @@ fun Context.getFileDateTaken(path: String): Long { return 0L } - -fun Context.isChromebook() = packageManager.hasSystemFeature("org.chromium.arc.device_management") diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Int.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Int.kt new file mode 100644 index 000000000..7792ef112 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Int.kt @@ -0,0 +1,5 @@ +package com.simplemobiletools.gallery.pro.extensions + +import com.simplemobiletools.commons.helpers.SORT_DESCENDING + +fun Int.isSortingAscending() = this and SORT_DESCENDING == 0 diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt index 37b8207fc..f80f1aece 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt @@ -21,7 +21,7 @@ class MediaFetcher(val context: Context) { var shouldStop = false fun getFilesFrom(curPath: String, isPickImage: Boolean, isPickVideo: Boolean, getProperDateTaken: Boolean, getProperLastModified: Boolean, - getProperFileSize: Boolean, favoritePaths: ArrayList, getVideoDurations: Boolean, sortMedia: Boolean = true): ArrayList { + getProperFileSize: Boolean, favoritePaths: ArrayList, getVideoDurations: Boolean): ArrayList { val filterMedia = context.config.filterMedia if (filterMedia == 0) { return ArrayList() @@ -38,9 +38,7 @@ class MediaFetcher(val context: Context) { curMedia.addAll(newMedia) } - if (sortMedia) { - sortMedia(curMedia, context.config.getFileSorting(curPath)) - } + sortMedia(curMedia, context.config.getFileSorting(curPath)) return curMedia } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/interfaces/DirectoryDao.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/interfaces/DirectoryDao.kt index 57d6807c2..75acd43c4 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/interfaces/DirectoryDao.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/interfaces/DirectoryDao.kt @@ -9,7 +9,7 @@ import com.simplemobiletools.gallery.pro.models.Directory @Dao interface DirectoryDao { - @Query("SELECT path, thumbnail, filename, media_count, last_modified, date_taken, size, location, media_types FROM directories") + @Query("SELECT path, thumbnail, filename, media_count, last_modified, date_taken, size, location, media_types, sort_value FROM directories") fun getAll(): List @Insert(onConflict = REPLACE) @@ -21,8 +21,8 @@ interface DirectoryDao { @Query("DELETE FROM directories WHERE path = :path COLLATE NOCASE") fun deleteDirPath(path: String) - @Query("UPDATE OR REPLACE directories SET thumbnail = :thumbnail, media_count = :mediaCnt, last_modified = :lastModified, date_taken = :dateTaken, size = :size, media_types = :mediaTypes WHERE path = :path COLLATE NOCASE") - fun updateDirectory(path: String, thumbnail: String, mediaCnt: Int, lastModified: Long, dateTaken: Long, size: Long, mediaTypes: Int) + @Query("UPDATE OR REPLACE directories SET thumbnail = :thumbnail, media_count = :mediaCnt, last_modified = :lastModified, date_taken = :dateTaken, size = :size, media_types = :mediaTypes, sort_value = :sortValue WHERE path = :path COLLATE NOCASE") + fun updateDirectory(path: String, thumbnail: String, mediaCnt: Int, lastModified: Long, dateTaken: Long, size: Long, mediaTypes: Int, sortValue: String) @Query("UPDATE directories SET thumbnail = :thumbnail, filename = :name, path = :newPath WHERE path = :oldPath COLLATE NOCASE") fun updateDirectoryAfterRename(thumbnail: String, name: String, newPath: String, oldPath: String) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/models/Directory.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/models/Directory.kt index 3b2ebedc5..f68ae3171 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/models/Directory.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/models/Directory.kt @@ -23,13 +23,14 @@ data class Directory( @ColumnInfo(name = "size") var size: Long, @ColumnInfo(name = "location") var location: Int, @ColumnInfo(name = "media_types") var types: Int, + @ColumnInfo(name = "sort_value") var sortValue: String, // used with "Group direct subfolders" enabled @Ignore var subfoldersCount: Int = 0, @Ignore var subfoldersMediaCount: Int = 0, @Ignore var containsMediaFilesDirectly: Boolean = true) { - constructor() : this(null, "", "", "", 0, 0L, 0L, 0L, 0, 0, 0, 0) + constructor() : this(null, "", "", "", 0, 0L, 0L, 0L, 0, 0, "", 0, 0) fun getBubbleText(sorting: Int, context: Context) = when { sorting and SORT_BY_NAME != 0 -> name