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 c0bc57dac..c55b5837a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -661,11 +661,18 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { } val favoriteMedia = mediumDao.getFavorites() as ArrayList - if (favoriteMedia.isNotEmpty() && dirs.none { it.areFavorites() }) { + if (favoriteMedia.isNotEmpty()) { val favorites = createDirectoryFromMedia(FAVORITES, favoriteMedia, albumCovers, hiddenString, includedFolders, isSortingAscending) dirs.add(favorites) showSortedDirs(dirs) directoryDao.insert(favorites) + } else if (dirs.any { it.areFavorites() }) { + val currentFavoriteDir = dirs.firstOrNull { it.areFavorites() } + if (currentFavoriteDir != null) { + dirs.remove(currentFavoriteDir) + showSortedDirs(dirs) + directoryDao.deleteDirPath(FAVORITES) + } } // check the remaining folders which were not cached at all yet 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 b0bca34cf..25f0a2e63 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt @@ -290,6 +290,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { handlePermission(PERMISSION_WRITE_STORAGE) { if (it) { val dirName = when { + mPath == FAVORITES -> getString(R.string.favorites) mPath == OTG_PATH -> getString(R.string.otg) mPath.startsWith(OTG_PATH) -> mPath.trimEnd('/').substringAfterLast('/') else -> getHumanizedFilename(mPath) @@ -488,9 +489,11 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { } private fun isDirEmpty(): Boolean { - return if (mPath != FAVORITES && mMedia.size <= 0 && config.filterMedia > 0) { - deleteDirectoryIfEmpty() - deleteDBDirectory() + return if (mMedia.size <= 0 && config.filterMedia > 0) { + if (mPath != FAVORITES) { + deleteDirectoryIfEmpty() + deleteDBDirectory() + } finish() true } else { @@ -642,6 +645,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { Intent(this, ViewPagerActivity::class.java).apply { putExtra(PATH, path) putExtra(SHOW_ALL, mShowAll) + putExtra(SHOW_FAVORITES, mPath == FAVORITES) startActivity(this) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt index dfd2b21d5..295e15c9b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt @@ -59,6 +59,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View private var mPos = -1 private var mShowAll = false private var mIsSlideshowActive = false + private var mIsShowingFavorites = false private var mRotationDegrees = 0 private var mPrevHashcode = 0 @@ -84,6 +85,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View super.onCreate(savedInstanceState) setContentView(R.layout.activity_medium) mMediaFiles = MediaActivity.mMedia.clone() as ArrayList + mIsShowingFavorites = intent.getBooleanExtra(SHOW_FAVORITES, false) handlePermission(PERMISSION_WRITE_STORAGE) { if (it) { @@ -198,7 +200,9 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View if (!getDoesFilePathExist(mPath)) { Thread { - scanPathRecursively(mPath) + if (!getIsPathDirectory(mPath)) { + scanPathRecursively(mPath) + } }.start() finish() return @@ -216,7 +220,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View showSystemUI() - mDirectory = mPath.getParentPath() + mDirectory = if (mIsShowingFavorites) FAVORITES else mPath.getParentPath() if (mDirectory.startsWith(OTG_PATH.trimEnd('/'))) { mDirectory += "/" } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt index 07b9bb630..fcd89b099 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt @@ -4,7 +4,9 @@ import android.content.Context import android.os.AsyncTask import com.simplemobiletools.commons.helpers.SORT_BY_DATE_TAKEN import com.simplemobiletools.gallery.extensions.config +import com.simplemobiletools.gallery.extensions.galleryDB import com.simplemobiletools.gallery.extensions.getFavoritePaths +import com.simplemobiletools.gallery.helpers.FAVORITES import com.simplemobiletools.gallery.helpers.MediaFetcher import com.simplemobiletools.gallery.models.Medium import java.util.* @@ -28,7 +30,11 @@ class GetMediaAsynctask(val context: Context, val mPath: String, val isPickImage MediaFetcher(context).sortMedia(media, context.config.getFileSorting("")) media } else { - mediaFetcher.getFilesFrom(mPath, isPickImage, isPickVideo, getProperDateTaken, favoritePaths) + if (mPath == FAVORITES) { + context.galleryDB.MediumDao().getFavorites() as ArrayList + } else { + mediaFetcher.getFilesFrom(mPath, isPickImage, isPickVideo, getProperDateTaken, favoritePaths) + } } } 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 3a29c2568..49743ba99 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt @@ -320,6 +320,10 @@ fun Context.getCachedMedia(path: String, getVideosOnly: Boolean = false, getImag val mediumDao = galleryDB.MediumDao() val foldersToScan = if (path == "/") MediaFetcher(this).getFoldersToScan() else arrayListOf(path) var media = ArrayList() + if (path == FAVORITES) { + media.addAll(mediumDao.getFavorites()) + } + val shouldShowHidden = config.shouldShowHidden foldersToScan.forEach { try { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt index 39fdd8a4b..e0f3c7085 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt @@ -64,6 +64,7 @@ const val SLIDESHOW_SCROLL_DURATION = 500L const val NOMEDIA = ".nomedia" const val FAVORITES = "favorites" +const val SHOW_FAVORITES = "show_favorites" const val MAX_COLUMN_COUNT = 20 const val SHOW_TEMP_HIDDEN_DURATION = 300000L const val CLICK_MAX_DURATION = 150