Glimpse: Update favorite button on contract result

Change-Id: I4746412b910da62cc2c10af6d93b02cbc5cba82b
This commit is contained in:
Sebastiano Barezzi 2023-08-08 15:17:05 +02:00
parent d68ca22081
commit 6b81f8e0f8
No known key found for this signature in database
GPG Key ID: 763BD3AE91A7A13F
1 changed files with 8 additions and 5 deletions

View File

@ -155,8 +155,12 @@ class MediaViewerFragment : Fragment(
Snackbar.LENGTH_LONG,
).setAnchorView(bottomSheetLinearLayout).show()
}
private val noopContract =
registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) {}
private val favoriteContract =
registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) {
mediaViewerAdapter.getMediaFromMediaStore(viewPager.currentItem)?.let {
favoriteButton.isSelected = it.isFavorite
}
}
private val onPageChangeCallback = object : OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
@ -261,16 +265,15 @@ class MediaViewerFragment : Fragment(
favoriteButton.setOnClickListener {
mediaViewerAdapter.getMediaFromMediaStore(viewPager.currentItem)?.let {
favoriteButton.isSelected = !it.isFavorite
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
noopContract.launch(
favoriteContract.launch(
requireContext().contentResolver.createFavoriteRequest(
!it.isFavorite, it.externalContentUri
)
)
} else {
it.favorite(requireContext().contentResolver, !it.isFavorite)
favoriteButton.isSelected = !it.isFavorite
}
}
}