diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt index 2e2199529..a2e963800 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt @@ -80,6 +80,7 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList, findItem(R.id.cab_confirm_selection).isVisible = isAGetIntent && allowMultiplePicks && selectedPositions.size > 0 checkHideBtnVisibility(this) + checkFavoriteBtnVisibility(this) } } @@ -95,6 +96,8 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList, R.id.cab_edit -> editFile() R.id.cab_hide -> toggleFileVisibility(true) R.id.cab_unhide -> toggleFileVisibility(false) + R.id.cab_add_to_favorites -> toggleFavorites(true) + R.id.cab_remove_from_favorites -> toggleFavorites(false) R.id.cab_share -> shareMedia() R.id.cab_copy_to -> copyMoveTo(true) R.id.cab_move_to -> copyMoveTo(false) @@ -131,6 +134,21 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList, menu.findItem(R.id.cab_unhide).isVisible = hiddenCnt > 0 } + private fun checkFavoriteBtnVisibility(menu: Menu) { + var favoriteCnt = 0 + var nonFavoriteCnt = 0 + selectedPositions.mapNotNull { media.getOrNull(it) }.forEach { + if (it.isFavorite) { + favoriteCnt++ + } else { + nonFavoriteCnt++ + } + } + + menu.findItem(R.id.cab_add_to_favorites).isVisible = nonFavoriteCnt > 0 + menu.findItem(R.id.cab_remove_from_favorites).isVisible = favoriteCnt > 0 + } + private fun confirmSelection() { val paths = getSelectedMedia().map { it.path } as ArrayList listener?.selectedPaths(paths) @@ -177,6 +195,20 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList, }.start() } + private fun toggleFavorites(add: Boolean) { + Thread { + val mediumDao = activity.galleryDB.MediumDao() + getSelectedMedia().forEach { + it.isFavorite = add + mediumDao.updateFavorite(it.path, add) + } + activity.runOnUiThread { + listener?.refreshItems() + finishActMode() + } + }.start() + } + private fun shareMedia() { if (selectedPositions.size == 1 && selectedPositions.first() != -1) { activity.shareMedium(getSelectedMedia()[0]) diff --git a/app/src/main/res/menu/cab_media.xml b/app/src/main/res/menu/cab_media.xml index 8ebb87f08..748d13541 100644 --- a/app/src/main/res/menu/cab_media.xml +++ b/app/src/main/res/menu/cab_media.xml @@ -46,6 +46,16 @@ android:icon="@drawable/ic_select_all" android:title="@string/select_all" app:showAsAction="ifRoom"/> + +