allow batch favorite toggling by long pressing items

This commit is contained in:
tibbi 2018-06-20 21:09:51 +02:00
parent e508030496
commit 40dc05602c
2 changed files with 42 additions and 0 deletions

View file

@ -80,6 +80,7 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList<Medium>,
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<Medium>,
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<Medium>,
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<String>
listener?.selectedPaths(paths)
@ -177,6 +195,20 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList<Medium>,
}.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])

View file

@ -46,6 +46,16 @@
android:icon="@drawable/ic_select_all"
android:title="@string/select_all"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/cab_add_to_favorites"
android:icon="@drawable/ic_star_off"
android:title="@string/add_to_favorites"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/cab_remove_from_favorites"
android:icon="@drawable/ic_star_on"
android:title="@string/remove_from_favorites"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/cab_open_with"
android:title="@string/open_with"