Glimpse: Dismiss trashed snackbar on fragment's view detach
The trash/untrash contracts are tied to the fragment but
the snackbar can outlive the fragment itself.
This is problematic if the user exits the media viewer
and only then taps the Undo action as the handler
is now destroyed.
Change-Id: I5318349235f2146c0cc90db2acdfcefe2e8ef36b
diff --git a/app/src/main/java/org/lineageos/glimpse/fragments/MediaViewerFragment.kt b/app/src/main/java/org/lineageos/glimpse/fragments/MediaViewerFragment.kt
index 380d93f..d4ccac0 100644
--- a/app/src/main/java/org/lineageos/glimpse/fragments/MediaViewerFragment.kt
+++ b/app/src/main/java/org/lineageos/glimpse/fragments/MediaViewerFragment.kt
@@ -77,7 +77,8 @@
private val topSheetConstraintLayout by getViewProperty<ConstraintLayout>(R.id.topSheetConstraintLayout)
private val viewPager by getViewProperty<ViewPager2>(R.id.viewPager)
- private var restoreLastTrashedMediaFromTrash: (() -> Unit)? = null
+ private var lastTrashedMedia: Media? = null
+ private var undoTrashSnackbar: Snackbar? = null
// Permissions
private val permissionsGatedCallback = PermissionsGatedCallback(this) {
@@ -179,13 +180,17 @@
1, 1
),
Snackbar.LENGTH_LONG,
- ).setAnchorView(bottomSheetLinearLayout).also {
- restoreLastTrashedMediaFromTrash?.takeIf { succeeded }?.let { unit ->
- it.setAction(R.string.file_trashing_undo) { unit() }
+ ).apply {
+ setAnchorView(bottomSheetLinearLayout)
+ lastTrashedMedia?.takeIf { succeeded }?.let { trashedMedia ->
+ setAction(R.string.file_trashing_undo) {
+ trashMedia(trashedMedia, false)
+ }
}
+ undoTrashSnackbar = this
}.show()
- restoreLastTrashedMediaFromTrash = null
+ lastTrashedMedia = null
}
private val restoreUriFromTrashContract =
registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) {
@@ -411,6 +416,9 @@
exoPlayer?.stop()
+ // Dismiss any snackbar open
+ undoTrashSnackbar?.dismiss()
+
super.onDestroyView()
}
@@ -466,7 +474,7 @@
private fun trashMedia(media: Media, trash: Boolean = !media.isTrashed) {
if (trash) {
- restoreLastTrashedMediaFromTrash = { trashMedia(media, false) }
+ lastTrashedMedia = media
}
val contract = when (trash) {