fix storing item Favorite state

This commit is contained in:
tibbi 2018-06-20 11:56:38 +02:00
parent 7336fc263d
commit 368ffd4b1d
3 changed files with 34 additions and 8 deletions

View file

@ -72,6 +72,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
private var mStoredReplaceZoomableImages = false private var mStoredReplaceZoomableImages = false
private var mStoredBottomActions = true private var mStoredBottomActions = true
private var mMediaFiles = ArrayList<Medium>() private var mMediaFiles = ArrayList<Medium>()
private var mFavoritePaths = ArrayList<String>()
companion object { companion object {
var screenWidth = 0 var screenWidth = 0
@ -95,6 +96,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
storeStateVariables() storeStateVariables()
initBottomActions() initBottomActions()
initFavorites()
} }
@TargetApi(Build.VERSION_CODES.LOLLIPOP) @TargetApi(Build.VERSION_CODES.LOLLIPOP)
@ -259,6 +261,12 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
initBottomActionButtons() initBottomActionButtons()
} }
private fun initFavorites() {
Thread {
mFavoritePaths = galleryDB.MediumDao().getFavorites() as ArrayList<String>
}.start()
}
private fun setupRotation() { private fun setupRotation() {
if (mIsOrientationLocked) { if (mIsOrientationLocked) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { 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 { override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_viewpager, menu) menuInflater.inflate(R.menu.menu_viewpager, menu)
val currentMedium = getCurrentMedium() ?: return true val currentMedium = getCurrentMedium() ?: return true
currentMedium.isFavorite = mFavoritePaths.contains(currentMedium.path)
menu.apply { menu.apply {
findItem(R.id.menu_delete).isVisible = !config.bottomActions findItem(R.id.menu_delete).isVisible = !config.bottomActions
@ -775,8 +784,17 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
} }
private fun toggleFavorite() { private fun toggleFavorite() {
getCurrentMedium()!!.isFavorite = !getCurrentMedium()!!.isFavorite val medium = getCurrentMedium() ?: return
invalidateOptionsMenu() 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?) { override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {

View file

@ -6,10 +6,7 @@ import android.net.Uri
import android.provider.MediaStore import android.provider.MediaStore
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.* import com.simplemobiletools.commons.helpers.*
import com.simplemobiletools.gallery.extensions.config import com.simplemobiletools.gallery.extensions.*
import com.simplemobiletools.gallery.extensions.getDistinctPath
import com.simplemobiletools.gallery.extensions.getOTGFolderChildren
import com.simplemobiletools.gallery.extensions.shouldFolderBeVisible
import com.simplemobiletools.gallery.models.Medium import com.simplemobiletools.gallery.models.Medium
import java.io.File import java.io.File
@ -162,6 +159,7 @@ class MediaFetcher(val context: Context) {
val doExtraCheck = context.config.doExtraCheck val doExtraCheck = context.config.doExtraCheck
val showHidden = context.config.shouldShowHidden val showHidden = context.config.shouldShowHidden
val dateTakens = if (getProperDateTaken) getFolderDateTakens(folder) else HashMap() val dateTakens = if (getProperDateTaken) getFolderDateTakens(folder) else HashMap()
val favoritePaths = context.galleryDB.MediumDao().getFavorites()
for (file in files) { for (file in files) {
if (shouldStop) { if (shouldStop) {
@ -210,7 +208,9 @@ class MediaFetcher(val context: Context) {
else -> TYPE_RAWS 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) media.add(medium)
} }
return media return media
@ -221,6 +221,7 @@ class MediaFetcher(val context: Context) {
val files = context.getDocumentFile(folder)?.listFiles() ?: return media val files = context.getDocumentFile(folder)?.listFiles() ?: return media
val doExtraCheck = context.config.doExtraCheck val doExtraCheck = context.config.doExtraCheck
val showHidden = context.config.shouldShowHidden val showHidden = context.config.shouldShowHidden
val favoritePaths = context.galleryDB.MediumDao().getFavorites()
for (file in files) { for (file in files) {
if (shouldStop) { 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 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) media.add(medium)
} }

View file

@ -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") @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<Medium> fun getMediaFromPath(path: String): List<Medium>
@Query("SELECT full_path FROM media WHERE is_favorite = 1")
fun getFavorites(): List<String>
@Insert(onConflict = REPLACE) @Insert(onConflict = REPLACE)
fun insert(medium: Medium) 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") @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) 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)
} }