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 5b33873f0..1ed1579c2 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt @@ -72,6 +72,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View private var mStoredReplaceZoomableImages = false private var mStoredBottomActions = true private var mMediaFiles = ArrayList() + private var mFavoritePaths = ArrayList() companion object { var screenWidth = 0 @@ -95,6 +96,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View storeStateVariables() initBottomActions() + initFavorites() } @TargetApi(Build.VERSION_CODES.LOLLIPOP) @@ -259,6 +261,12 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View initBottomActionButtons() } + private fun initFavorites() { + Thread { + mFavoritePaths = galleryDB.MediumDao().getFavorites() as ArrayList + }.start() + } + private fun setupRotation() { if (mIsOrientationLocked) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { @@ -274,6 +282,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View override fun onCreateOptionsMenu(menu: Menu): Boolean { menuInflater.inflate(R.menu.menu_viewpager, menu) val currentMedium = getCurrentMedium() ?: return true + currentMedium.isFavorite = mFavoritePaths.contains(currentMedium.path) menu.apply { findItem(R.id.menu_delete).isVisible = !config.bottomActions @@ -775,8 +784,17 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View } private fun toggleFavorite() { - getCurrentMedium()!!.isFavorite = !getCurrentMedium()!!.isFavorite - invalidateOptionsMenu() + val medium = getCurrentMedium() ?: return + medium.isFavorite = !medium.isFavorite + Thread { + galleryDB.MediumDao().updateFavorite(medium.path, medium.isFavorite) + if (medium.isFavorite) { + mFavoritePaths.add(medium.path) + } else { + mFavoritePaths.remove(medium.path) + } + invalidateOptionsMenu() + }.start() } override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt index cf79c7532..14ca604c1 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt @@ -6,10 +6,7 @@ import android.net.Uri import android.provider.MediaStore import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.helpers.* -import com.simplemobiletools.gallery.extensions.config -import com.simplemobiletools.gallery.extensions.getDistinctPath -import com.simplemobiletools.gallery.extensions.getOTGFolderChildren -import com.simplemobiletools.gallery.extensions.shouldFolderBeVisible +import com.simplemobiletools.gallery.extensions.* import com.simplemobiletools.gallery.models.Medium import java.io.File @@ -162,6 +159,7 @@ class MediaFetcher(val context: Context) { val doExtraCheck = context.config.doExtraCheck val showHidden = context.config.shouldShowHidden val dateTakens = if (getProperDateTaken) getFolderDateTakens(folder) else HashMap() + val favoritePaths = context.galleryDB.MediumDao().getFavorites() for (file in files) { if (shouldStop) { @@ -210,7 +208,9 @@ class MediaFetcher(val context: Context) { else -> TYPE_RAWS } - val medium = Medium(null, filename, file.absolutePath, folder, lastModified, dateTaken, size, type, false) + val path = file.absolutePath + val isFavorite = favoritePaths.contains(path) + val medium = Medium(null, filename, path, folder, lastModified, dateTaken, size, type, isFavorite) media.add(medium) } return media @@ -221,6 +221,7 @@ class MediaFetcher(val context: Context) { val files = context.getDocumentFile(folder)?.listFiles() ?: return media val doExtraCheck = context.config.doExtraCheck val showHidden = context.config.shouldShowHidden + val favoritePaths = context.galleryDB.MediumDao().getFavorites() for (file in files) { if (shouldStop) { @@ -266,7 +267,8 @@ class MediaFetcher(val context: Context) { } val path = Uri.decode(file.uri.toString().replaceFirst("${context.config.OTGTreeUri}/document/${context.config.OTGPartition}%3A", OTG_PATH)) - val medium = Medium(null, filename, path, folder, dateModified, dateTaken, size, type, false) + val isFavorite = favoritePaths.contains(path) + val medium = Medium(null, filename, path, folder, dateModified, dateTaken, size, type, isFavorite) media.add(medium) } 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 7d47aef32..f3c88755b 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 full_path FROM media WHERE is_favorite = 1") + fun getFavorites(): List + @Insert(onConflict = REPLACE) fun insert(medium: Medium) @@ -22,4 +25,7 @@ interface MediumDao { @Query("UPDATE OR REPLACE media SET filename = :newFilename, full_path = :newFullPath, parent_path = :newParentPath WHERE full_path = :oldPath") fun updateMedium(oldPath: String, newParentPath: String, newFilename: String, newFullPath: String) + + @Query("UPDATE media SET is_favorite = :isFavorite WHERE full_path = :path") + fun updateFavorite(path: String, isFavorite: Boolean) }