From 2eaff8568f7a99fbce4da0b0c7e8d31d671226a3 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 22 Apr 2018 20:01:48 +0200 Subject: [PATCH] properly filter cached directories by file type shown --- .../gallery/activities/MainActivity.kt | 10 +++----- .../asynctasks/GetDirectoriesAsynctask.kt | 24 ++++++++++++++++++- .../gallery/extensions/Context.kt | 16 +++++++++++-- .../gallery/interfaces/DirectoryDao.kt | 2 +- .../gallery/models/Directory.kt | 3 ++- 5 files changed, 43 insertions(+), 12 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt index 8eeadaa0c..c2f714519 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -276,13 +276,8 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { mIsGettingDirs = true if (!mLoadedInitialPhotos) { getCachedDirectories { - val shouldShowHidden = config.shouldShowHidden - val excludedPaths = config.excludedFolders - val includedPaths = config.includedFolders - val dirs = it.filter { it.path.shouldFolderBeVisible(excludedPaths, includedPaths, shouldShowHidden) } as ArrayList - - if (dirs.isNotEmpty()) { - gotDirectories(dirs, true) + if (it.isNotEmpty()) { + gotDirectories(it, true) } } } @@ -312,6 +307,7 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { private fun showFilterMediaDialog() { FilterMediaDialog(this) { + mLoadedInitialPhotos = false directories_refresh_layout.isRefreshing = true getDirectories() } 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 55b127375..2ae5d3b46 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt @@ -10,7 +10,10 @@ import com.simplemobiletools.commons.helpers.sumByLong import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.extensions.checkAppendingHidden import com.simplemobiletools.gallery.extensions.config +import com.simplemobiletools.gallery.helpers.GIFS +import com.simplemobiletools.gallery.helpers.IMAGES import com.simplemobiletools.gallery.helpers.MediaFetcher +import com.simplemobiletools.gallery.helpers.VIDEOS import com.simplemobiletools.gallery.models.Directory import com.simplemobiletools.gallery.models.Medium import java.io.File @@ -55,17 +58,36 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va } } + val mediaTypes = getDirMediaTypes(curMedia) + val dirName = context.checkAppendingHidden(parentDir, hidden, includedFolders) 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(null, parentDir, thumbnail, dirName, curMedia.size, lastModified, dateTaken, size, context.isPathOnSD(parentDir)) + val directory = Directory(null, parentDir, thumbnail, dirName, curMedia.size, lastModified, dateTaken, size, context.isPathOnSD(parentDir), mediaTypes) directories.add(directory) } return directories } + private fun getDirMediaTypes(media: ArrayList): Int { + var types = 0 + if (media.any { it.isImage() }) { + types += IMAGES + } + + if (media.any { it.isVideo() }) { + types += VIDEOS + } + + if (media.any { it.isGif() }) { + types += GIFS + } + + return types + } + override fun onPostExecute(dirs: ArrayList) { super.onPostExecute(dirs) callback(dirs) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt index bbc9f8129..0a0209975 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt @@ -212,7 +212,7 @@ fun Context.addTempFolderIfNeeded(dirs: ArrayList): ArrayList() val tempFolderPath = config.tempFolderPath if (tempFolderPath.isNotEmpty()) { - val newFolder = Directory(null, tempFolderPath, "", tempFolderPath.getFilenameFromPath(), 0, 0, 0, 0L, isPathOnSD(tempFolderPath)) + val newFolder = Directory(null, tempFolderPath, "", tempFolderPath.getFilenameFromPath(), 0, 0, 0, 0L, isPathOnSD(tempFolderPath), 0) directories.add(newFolder) } directories.addAll(dirs) @@ -249,7 +249,19 @@ fun Context.getCachedDirectories(callback: (ArrayList) -> Unit) { Thread { val directoryDao = galleryDB.DirectoryDao() val directories = directoryDao.getAll() as ArrayList - callback(directories) + val shouldShowHidden = config.shouldShowHidden + val excludedPaths = config.excludedFolders + val includedPaths = config.includedFolders + var filteredDirectories = directories.filter { it.path.shouldFolderBeVisible(excludedPaths, includedPaths, shouldShowHidden) } as ArrayList + val filterMedia = config.filterMedia + filteredDirectories = filteredDirectories.filter { + (filterMedia and IMAGES != 0 && it.types == TYPE_IMAGE) || + (filterMedia and VIDEOS != 0 && it.types == TYPE_VIDEO) || + (filterMedia and GIFS != 0 && it.types == TYPE_GIF) + } as ArrayList + + callback(filteredDirectories) + removeInvalidDirectories(directories, directoryDao) }.start() } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/interfaces/DirectoryDao.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/interfaces/DirectoryDao.kt index 6b6d0580b..a8172a8b5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/interfaces/DirectoryDao.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/interfaces/DirectoryDao.kt @@ -9,7 +9,7 @@ import com.simplemobiletools.gallery.models.Directory @Dao interface DirectoryDao { - @Query("SELECT path, thumbnail, filename, media_count, last_modified, date_taken, size, is_on_sd_card FROM directories") + @Query("SELECT path, thumbnail, filename, media_count, last_modified, date_taken, size, is_on_sd_card, media_types FROM directories") fun getAll(): List @Insert(onConflict = REPLACE) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/models/Directory.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/models/Directory.kt index 02b18d8d8..27c15d62e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/models/Directory.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/models/Directory.kt @@ -19,7 +19,8 @@ data class Directory( @ColumnInfo(name = "last_modified") val modified: Long, @ColumnInfo(name = "date_taken") val taken: Long, @ColumnInfo(name = "size") val size: Long, - @ColumnInfo(name = "is_on_sd_card") val isOnSDCard: Boolean) : Serializable, Comparable { + @ColumnInfo(name = "is_on_sd_card") val isOnSDCard: Boolean, + @ColumnInfo(name = "media_types") val types: Int) : Serializable, Comparable { companion object { private const val serialVersionUID = -6553345863555455L