Glimpse: Hook up favorite button
Change-Id: Ib578f87581b58c9f27f815747d1529a6c9255703
This commit is contained in:
parent
3f6a1d81dd
commit
824b8856c9
|
@ -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()
|
||||
|
|
|
@ -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())
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
Loading…
Reference in New Issue