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 cfb34cdad..c0bc57dac 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -613,6 +613,10 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { try { for (directory in dirs) { + if (directory.areFavorites()) { + continue + } + val curMedia = mediaFetcher.getFilesFrom(directory.path, getImagesOnly, getVideosOnly, getProperDateTaken, favoritePaths) val newDir = if (curMedia.isEmpty()) { directory @@ -656,6 +660,14 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { foldersToScan.remove(it.path) } + val favoriteMedia = mediumDao.getFavorites() as ArrayList + if (favoriteMedia.isNotEmpty() && dirs.none { it.areFavorites() }) { + val favorites = createDirectoryFromMedia(FAVORITES, favoriteMedia, albumCovers, hiddenString, includedFolders, isSortingAscending) + dirs.add(favorites) + showSortedDirs(dirs) + directoryDao.insert(favorites) + } + // check the remaining folders which were not cached at all yet for (folder in foldersToScan) { val newMedia = mediaFetcher.getFilesFrom(folder, getImagesOnly, getVideosOnly, getProperDateTaken, favoritePaths) @@ -721,7 +733,7 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { } val mediaTypes = curMedia.getDirMediaTypes() - val dirName = checkAppendingHidden(path, hiddenString, includedFolders) + val dirName = if (path == FAVORITES) getString(R.string.favorites) else checkAppendingHidden(path, hiddenString, includedFolders) val firstItem = curMedia.first() val lastItem = curMedia.last() @@ -776,7 +788,7 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { private fun checkInvalidDirectories(dirs: ArrayList, directoryDao: DirectoryDao) { val invalidDirs = ArrayList() - dirs.forEach { + dirs.filter { !it.areFavorites() }.forEach { if (!getDoesFilePathExist(it.path)) { invalidDirs.add(it) } else if (it.path != config.tempFolderPath) { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt index 29c5d956b..b0bca34cf 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt @@ -488,7 +488,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { } private fun isDirEmpty(): Boolean { - return if (mMedia.size <= 0 && config.filterMedia > 0) { + return if (mPath != FAVORITES && mMedia.size <= 0 && config.filterMedia > 0) { deleteDirectoryIfEmpty() deleteDBDirectory() finish() 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 1fb50627e..3a29c2568 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt @@ -356,7 +356,7 @@ fun Context.getCachedMedia(path: String, getVideosOnly: Boolean = false, getImag fun Context.removeInvalidDBDirectories(dirs: ArrayList? = null, directoryDao: DirectoryDao = galleryDB.DirectoryDao()) { val dirsToCheck = dirs ?: directoryDao.getAll() - dirsToCheck.filter { !getDoesFilePathExist(it.path) && it.path != config.tempFolderPath }.forEach { + dirsToCheck.filter { !it.areFavorites() && !getDoesFilePathExist(it.path) && it.path != config.tempFolderPath }.forEach { directoryDao.deleteDirPath(it.path) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/interfaces/MediumDao.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/interfaces/MediumDao.kt index 451f92e27..13c0278e1 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/interfaces/MediumDao.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/interfaces/MediumDao.kt @@ -11,6 +11,9 @@ interface MediumDao { @Query("SELECT filename, full_path, parent_path, last_modified, date_taken, size, type, is_favorite FROM media WHERE parent_path = :path") fun getMediaFromPath(path: String): List + @Query("SELECT filename, full_path, parent_path, last_modified, date_taken, size, type, is_favorite FROM media WHERE is_favorite = 1") + fun getFavorites(): List + @Query("SELECT full_path FROM media WHERE is_favorite = 1") fun getFavoritePaths(): List 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 3c4dd01a0..85835f165 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/models/Directory.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/models/Directory.kt @@ -10,6 +10,7 @@ import com.simplemobiletools.commons.helpers.SORT_BY_DATE_MODIFIED import com.simplemobiletools.commons.helpers.SORT_BY_NAME import com.simplemobiletools.commons.helpers.SORT_BY_PATH import com.simplemobiletools.commons.helpers.SORT_BY_SIZE +import com.simplemobiletools.gallery.helpers.FAVORITES import java.io.Serializable @Entity(tableName = "directories", indices = [Index(value = "path", unique = true)]) @@ -36,4 +37,6 @@ data class Directory( sorting and SORT_BY_DATE_MODIFIED != 0 -> modified.formatDate() else -> taken.formatDate() } + + fun areFavorites() = path == FAVORITES }