feat: create delete request optional
This commit is contained in:
1 parent
8cb4a4e41b
commit
908534611f
6 files changed
+131
-18
No files matched your search
@@ -293,21 +293,45 @@ fun GalleryScreen(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 删除确认对话框
|
||||
if (showDeleteDialog) {
|
||||
var deleteExported by remember { mutableStateOf(false) }
|
||||
|
||||
AlertDialog(
|
||||
onDismissRequest = { showDeleteDialog = false },
|
||||
title = { Text(stringResource(R.string.delete)) },
|
||||
text = {
|
||||
Text(
|
||||
text = stringResource(R.string.delete_multiple_confirm, selectedPhotos.size)
|
||||
)
|
||||
Column {
|
||||
Text(
|
||||
text = stringResource(R.string.delete_multiple_confirm, selectedPhotos.size)
|
||||
)
|
||||
Spacer(modifier = Modifier.height(12.dp))
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
modifier = Modifier.clickable { deleteExported = !deleteExported }
|
||||
) {
|
||||
Checkbox(
|
||||
checked = deleteExported,
|
||||
onCheckedChange = { deleteExported = it },
|
||||
colors = CheckboxDefaults.colors(
|
||||
checkedColor = Color(0xFFFF6B35),
|
||||
uncheckedColor = Color.White.copy(alpha = 0.6f)
|
||||
)
|
||||
)
|
||||
Spacer(modifier = Modifier.width(8.dp))
|
||||
Text(
|
||||
text = stringResource(R.string.delete_exported_photos),
|
||||
color = Color.White.copy(alpha = 0.9f),
|
||||
fontSize = 14.sp
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
confirmButton = {
|
||||
TextButton(
|
||||
onClick = {
|
||||
viewModel.deleteSelectedPhotos()
|
||||
viewModel.deleteSelectedPhotos(deleteExported)
|
||||
showDeleteDialog = false
|
||||
}
|
||||
) {
|
||||
|
||||
@@ -6,6 +6,7 @@ import androidx.activity.result.IntentSenderRequest
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.compose.animation.core.animateFloatAsState
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.gestures.*
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.pager.HorizontalPager
|
||||
@@ -267,17 +268,45 @@ fun PhotoDetailScreen(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 删除确认对话框
|
||||
if (showDeleteDialog) {
|
||||
var deleteExported by remember { mutableStateOf(false) }
|
||||
|
||||
AlertDialog(
|
||||
onDismissRequest = { showDeleteDialog = false },
|
||||
title = { Text(stringResource(R.string.delete)) },
|
||||
text = { Text(stringResource(R.string.delete_confirm)) },
|
||||
text = {
|
||||
Column {
|
||||
Text(stringResource(R.string.delete_confirm))
|
||||
Spacer(modifier = Modifier.height(12.dp))
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
modifier = Modifier.clickable { deleteExported = !deleteExported }
|
||||
) {
|
||||
Checkbox(
|
||||
checked = deleteExported,
|
||||
onCheckedChange = { deleteExported = it },
|
||||
colors = CheckboxDefaults.colors(
|
||||
checkedColor = Color(0xFFFF6B35),
|
||||
uncheckedColor = Color.White.copy(alpha = 0.6f)
|
||||
)
|
||||
)
|
||||
Spacer(modifier = Modifier.width(8.dp))
|
||||
Text(
|
||||
text = stringResource(R.string.delete_exported_photos),
|
||||
color = Color.White.copy(alpha = 0.9f),
|
||||
fontSize = 14.sp
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
confirmButton = {
|
||||
TextButton(
|
||||
onClick = {
|
||||
viewModel.deleteCurrentPhoto()
|
||||
currentPhoto?.let { photo ->
|
||||
viewModel.requestDeletePhoto(photo, deleteExported)
|
||||
}
|
||||
showDeleteDialog = false
|
||||
}
|
||||
) {
|
||||
|
||||
@@ -436,11 +436,18 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
|
||||
* 请求删除照片
|
||||
*
|
||||
* 这个方法会:
|
||||
* 1. 在 Android 11+ 上检查是否有导出的照片需要删除
|
||||
* 2. 如果有,设置 deletePendingIntent 和 pendingDeletePhoto,UI 层需要监听这些状态并启动删除确认对话框
|
||||
* 3. 如果没有导出的照片,或者在 Android 10 及以下,直接删除照片
|
||||
* 1. 如果 deleteExported == false,直接删除应用内部照片,不触发系统删除对话框
|
||||
* 2. 如果 deleteExported == true 且在 Android 11+ 上,检查是否有导出的照片需要删除
|
||||
* 3. 如果有导出的照片,设置 deletePendingIntent 和 pendingDeletePhoto,UI 层需要监听这些状态并启动删除确认对话框
|
||||
* 4. 如果没有导出的照片,或者在 Android 10 及以下,直接删除照片
|
||||
*/
|
||||
fun requestDeletePhoto(photo: PhotoData) {
|
||||
fun requestDeletePhoto(photo: PhotoData, deleteExported: Boolean = true) {
|
||||
if (!deleteExported) {
|
||||
// 不删除系统相册照片,直接删除应用内部照片
|
||||
deletePhotoOnlyInternal(photo)
|
||||
return
|
||||
}
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
||||
// Android 11+: 检查是否有导出的照片
|
||||
val pendingIntent = getDeleteRequest(photo)
|
||||
@@ -452,7 +459,7 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
|
||||
PLog.d(TAG, "Set delete pending intent for photo ${photo.id}")
|
||||
} else {
|
||||
// 没有导出的照片,直接删除应用内照片
|
||||
deletePhotoDirectly(photo)
|
||||
deletePhotoOnlyInternal(photo)
|
||||
}
|
||||
} else {
|
||||
// Android 10 及以下: 直接删除所有照片
|
||||
@@ -460,6 +467,26 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 仅删除应用内部照片(不删除系统相册)
|
||||
*/
|
||||
private fun deletePhotoOnlyInternal(photo: PhotoData) {
|
||||
viewModelScope.launch {
|
||||
val context = getApplication<Application>()
|
||||
val success = PhotoManager.deletePhotoOnly(context, photo.id)
|
||||
if (success) {
|
||||
loadPhotos()
|
||||
// 如果删除的是当前照片,调整索引
|
||||
if (photo == getCurrentPhoto() && currentPhotoIndex >= _photos.value.size) {
|
||||
currentPhotoIndex = (_photos.value.size - 1).coerceAtLeast(0)
|
||||
}
|
||||
PLog.d(TAG, "Photo deleted (app only): ${photo.id}")
|
||||
} else {
|
||||
PLog.e(TAG, "Failed to delete photo: ${photo.id}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除删除请求状态
|
||||
*/
|
||||
@@ -526,13 +553,20 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
|
||||
/**
|
||||
* 批量删除选中的照片
|
||||
*
|
||||
* Android 11+: 会使用 MediaStore.createDeleteRequest 弹出系统确认对话框
|
||||
* Android 10-: 直接删除所有照片
|
||||
* @param deleteExported 是否同时删除系统相册中的导出图片
|
||||
* - true: Android 11+ 会使用 MediaStore.createDeleteRequest 弹出系统确认对话框
|
||||
* - false: 只删除应用内部照片,不删除系统相册
|
||||
*/
|
||||
fun deleteSelectedPhotos() {
|
||||
fun deleteSelectedPhotos(deleteExported: Boolean = true) {
|
||||
val toDelete = selectedPhotos.toList()
|
||||
if (toDelete.isEmpty()) return
|
||||
|
||||
if (!deleteExported) {
|
||||
// 不删除系统相册照片,直接删除应用内部照片
|
||||
deleteBatchPhotosOnlyInternal(toDelete)
|
||||
return
|
||||
}
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
||||
// Android 11+: 收集所有导出的 URIs
|
||||
viewModelScope.launch {
|
||||
@@ -565,11 +599,11 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
|
||||
} catch (e: Exception) {
|
||||
PLog.e(TAG, "Failed to create batch delete request", e)
|
||||
// 创建请求失败,直接删除应用内照片
|
||||
deleteBatchPhotosDirectly(toDelete)
|
||||
deleteBatchPhotosOnlyInternal(toDelete)
|
||||
}
|
||||
} else {
|
||||
// 没有导出的照片,直接删除应用内照片
|
||||
deleteBatchPhotosDirectly(toDelete)
|
||||
deleteBatchPhotosOnlyInternal(toDelete)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -578,6 +612,29 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 仅删除应用内部照片(批量,不删除系统相册)
|
||||
*/
|
||||
private fun deleteBatchPhotosOnlyInternal(photos: List<PhotoData>) {
|
||||
viewModelScope.launch {
|
||||
val context = getApplication<Application>()
|
||||
var deletedCount = 0
|
||||
|
||||
withContext(Dispatchers.IO) {
|
||||
photos.forEach { photo ->
|
||||
val success = PhotoManager.deletePhotoOnly(context, photo.id)
|
||||
if (success) {
|
||||
deletedCount++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
exitSelectionMode()
|
||||
loadPhotos()
|
||||
PLog.d(TAG, "Batch deleted $deletedCount photos (app only)")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 直接批量删除照片(不检查系统相册)
|
||||
*/
|
||||
|
||||
@@ -46,6 +46,7 @@
|
||||
<string name="brightness">亮度</string>
|
||||
<string name="delete_confirm">删除这张照片?</string>
|
||||
<string name="delete_multiple_confirm">删除 %d 张照片?</string>
|
||||
<string name="delete_exported_photos">同时删除系统相册中的导出图</string>
|
||||
<string name="no_photos">还没有照片</string>
|
||||
<string name="photo_info">照片信息</string>
|
||||
<string name="filter">滤镜</string>
|
||||
|
||||
@@ -50,6 +50,7 @@
|
||||
<string name="brightness">Brightness</string>
|
||||
<string name="delete_confirm">Delete this photo?</string>
|
||||
<string name="delete_multiple_confirm">Delete %d photos?</string>
|
||||
<string name="delete_exported_photos">Also delete from system gallery</string>
|
||||
<string name="no_photos">No photos yet</string>
|
||||
<string name="photo_info">Photo Info</string>
|
||||
<string name="filter">Filter</string>
|
||||
|
||||
Reference in new issue
Block a user