Glimpse: Hook up favorite button

Change-Id: Ib578f87581b58c9f27f815747d1529a6c9255703
This commit is contained in:
LuK1337 2023-08-05 17:35:53 +02:00
parent 3f6a1d81dd
commit 824b8856c9
3 changed files with 36 additions and 0 deletions

View File

@ -16,3 +16,9 @@ import androidx.annotation.RequiresApi
fun ContentResolver.createDeleteRequest(vararg uris: Uri) = IntentSenderRequest.Builder(
MediaStore.createDeleteRequest(this, uris.toCollection(ArrayList()))
).build()
@RequiresApi(Build.VERSION_CODES.R)
fun ContentResolver.createFavoriteRequest(value: Boolean, vararg uris: Uri) =
IntentSenderRequest.Builder(
MediaStore.createFavoriteRequest(this, uris.toCollection(ArrayList()), value)
).build()

View File

@ -119,6 +119,8 @@ class MediaViewerFragment : Fragment(
Snackbar.LENGTH_LONG,
).show()
}
private val noopContract =
registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) {}
private val onPageChangeCallback = object : OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
@ -176,6 +178,28 @@ class MediaViewerFragment : Fragment(
}
}
favoriteButton.setOnClickListener {
mediaViewerAdapter.getMediaFromMediaStore(viewPager.currentItem)?.let {
favoriteButton.setImageResource(
if (!it.isFavorite) {
R.drawable.ic_star
} else {
R.drawable.ic_star_border
}
)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
noopContract.launch(
requireContext().contentResolver.createFavoriteRequest(
!it.isFavorite, it.externalContentUri
)
)
} else {
it.favorite(requireContext().contentResolver, !it.isFavorite)
}
}
}
ViewCompat.setOnApplyWindowInsetsListener(view) { _, windowInsets ->
val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())

View File

@ -45,6 +45,12 @@ data class Media(
contentResolver.delete(externalContentUri, null, null)
}
fun favorite(contentResolver: ContentResolver, value: Boolean) {
contentResolver.update(externalContentUri, ContentValues().apply {
put(MediaStore.MediaColumns.IS_FAVORITE, value)
}, null, null)
}
companion object CREATOR : Parcelable.Creator<Media> {
override fun createFromParcel(parcel: Parcel) = Media(parcel)