Glimpse: Add trash action

Change-Id: Iaf4d5a13cbe0e56d7b6be62fe3dfa9be839aa461
This commit is contained in:
Sebastiano Barezzi 2023-08-06 13:49:41 +02:00
parent 1537848b01
commit e048906a31
5 changed files with 104 additions and 0 deletions

View File

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

View File

@ -123,6 +123,36 @@ class MediaViewerFragment : Fragment(
Snackbar.LENGTH_LONG,
).show()
}
private val trashUriContract =
registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) {
Snackbar.make(
bottomSheetLinearLayout,
resources.getQuantityString(
if (it.resultCode == Activity.RESULT_CANCELED) {
R.plurals.file_trashing_unsuccessful
} else {
R.plurals.file_trashing_successful
},
1, 1
),
Snackbar.LENGTH_LONG,
).show()
}
private val restoreUriFromTrashContract =
registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) {
Snackbar.make(
bottomSheetLinearLayout,
resources.getQuantityString(
if (it.resultCode == Activity.RESULT_CANCELED) {
R.plurals.file_restoring_from_trash_unsuccessful
} else {
R.plurals.file_restoring_from_trash_successful
},
1, 1
),
Snackbar.LENGTH_LONG,
).show()
}
private val noopContract =
registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) {}
@ -145,6 +175,12 @@ class MediaViewerFragment : Fragment(
dateTextView.text = dateFormatter.format(media.dateAdded)
timeTextView.text = timeFormatter.format(media.dateAdded)
favoriteButton.isSelected = media.isFavorite
deleteButton.setImageResource(
when (media.isTrashed) {
true -> R.drawable.ic_restore_from_trash
false -> R.drawable.ic_delete
}
)
if (media.mediaType == MediaType.VIDEO) {
exoPlayer.setMediaItem(MediaItem.fromUri(media.externalContentUri))
@ -173,6 +209,23 @@ class MediaViewerFragment : Fragment(
}
deleteButton.setOnClickListener {
mediaViewerAdapter.getMediaFromMediaStore(viewPager.currentItem)?.let {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val contract = when (it.isTrashed) {
true -> restoreUriFromTrashContract
false -> trashUriContract
}
contract.launch(
requireContext().contentResolver.createTrashRequest(
!it.isTrashed, it.externalContentUri
)
)
} else {
it.trash(requireContext().contentResolver, !it.isTrashed)
}
}
}
deleteButton.setOnLongClickListener {
mediaViewerAdapter.getMediaFromMediaStore(viewPager.currentItem)?.let {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
deleteUriContract.launch(
@ -181,7 +234,11 @@ class MediaViewerFragment : Fragment(
} else {
it.delete(requireContext().contentResolver)
}
true
}
false
}
favoriteButton.setOnClickListener {

View File

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

View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
SPDX-FileCopyrightText: Material Design Authors / Google LLC
SPDX-License-Identifier: Apache-2.0
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="#000000"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M15.5,4l-1,-1h-5l-1,1L5,4v2h14L19,4zM6,19c0,1.1 0.9,2 2,2h8c1.1,0 2,-0.9 2,-2L18,7L6,7v12zM8,14L8,9h8v10L8,19v-5zM10,18h4v-4h2l-4,-4 -4,4h2z" />
</vector>

View File

@ -34,4 +34,24 @@
<item quantity="one">File couldn\'t be deleted</item>
<item quantity="many">%d files couldn\'t be deleted</item>
</plurals>
<!-- File trashing -->
<plurals name="file_trashing_successful">
<item quantity="one">File trashed</item>
<item quantity="many">%d files trashed</item>
</plurals>
<plurals name="file_trashing_unsuccessful">
<item quantity="one">File couldn\'t be trashed</item>
<item quantity="many">%d files couldn\'t be trashed</item>
</plurals>
<!-- File restoring from trash -->
<plurals name="file_restoring_from_trash_successful">
<item quantity="one">File restored from trash</item>
<item quantity="many">%d files restored from trash</item>
</plurals>
<plurals name="file_restoring_from_trash_unsuccessful">
<item quantity="one">File couldn\'t be restored from trash</item>
<item quantity="many">%d files couldn\'t be restored from trash</item>
</plurals>
</resources>