diff --git a/app/src/main/java/com/hinnka/mycamera/data/CustomImportManager.kt b/app/src/main/java/com/hinnka/mycamera/data/CustomImportManager.kt index 43df2996..2fe23a0d 100644 --- a/app/src/main/java/com/hinnka/mycamera/data/CustomImportManager.kt +++ b/app/src/main/java/com/hinnka/mycamera/data/CustomImportManager.kt @@ -99,6 +99,57 @@ class CustomImportManager(private val context: Context) { } } + /** + * 复制 LUT + * + * @param lut 要复制的 LUT 信息 + * @param copyName 复制后的显示名称 + * @return 复制成功的 LUT ID,失败返回 null + */ + fun copyLut(lut: LutInfo, copyName: String): String? { + return try { + val lutId = "custom_${UUID.randomUUID()}" + val plutFileName = "$lutId.plut" + val plutFile = File(customLutDir, plutFileName) + + if (lut.isBuiltIn) { + // 如果是内置 LUT,从 assets 复制到 custom 目录 + context.assets.open(lut.fileName).use { inputStream -> + FileOutputStream(plutFile).use { outputStream -> + inputStream.copyTo(outputStream) + } + } + } else { + // 如果是自定义 LUT,直接从文件复制 + val originalFile = File(lut.fileName) + if (originalFile.exists()) { + originalFile.inputStream().use { inputStream -> + FileOutputStream(plutFile).use { outputStream -> + inputStream.copyTo(outputStream) + } + } + } else { + PLog.e(TAG, "Copy failed: original file not found: ${lut.fileName}") + return null + } + } + + // 保存到配置文件 + saveLutToConfig(lutId, copyName, plutFileName) + + // 如果原 LUT 有分类,也同步分类 + if (lut.category.isNotEmpty()) { + updateLutCategory(lutId, lut.category) + } + + PLog.d(TAG, "LUT copied successfully: $lutId ($copyName)") + lutId + } catch (e: Exception) { + PLog.e(TAG, "Failed to copy LUT", e) + null + } + } + /** * 导入边框样式文件 * diff --git a/app/src/main/java/com/hinnka/mycamera/ui/settings/FilterManagementScreen.kt b/app/src/main/java/com/hinnka/mycamera/ui/settings/FilterManagementScreen.kt index 8f3e0370..ef1986cf 100644 --- a/app/src/main/java/com/hinnka/mycamera/ui/settings/FilterManagementScreen.kt +++ b/app/src/main/java/com/hinnka/mycamera/ui/settings/FilterManagementScreen.kt @@ -23,6 +23,7 @@ import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.graphics.Color +import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.style.TextOverflow @@ -55,19 +56,42 @@ fun FilterManagementScreen( val availableLuts = viewModel.availableLutList val customImportManager = viewModel.getCustomImportManager() val scope = rememberCoroutineScope() + val context = LocalContext.current // 本地可变列表用于拖拽排序 var localLutList by remember { mutableStateOf(availableLuts) } - // 当 availableLuts 更新时同步本地列表(保留现有顺序,将新项目添加到末尾) + // 当 availableLuts 更新时同步本地列表 LaunchedEffect(availableLuts) { val existingIds = localLutList.map { it.id }.toSet() val newItems = availableLuts.filter { it.id !in existingIds } - val updatedExisting = localLutList.mapNotNull { local -> - availableLuts.find { it.id == local.id } + + if (newItems.isEmpty()) { + localLutList = localLutList.mapNotNull { local -> + availableLuts.find { it.id == local.id } + } + } else { + // 如果新项是一个(如复制)或多个(如批量导入),按 availableLuts 的顺序插入到 localLutList 的合适位置 + val updatedList = localLutList.mapNotNull { local -> + availableLuts.find { it.id == local.id } + }.toMutableList() + + newItems.forEach { newItem -> + val idxInAvailable = availableLuts.indexOfFirst { it.id == newItem.id } + if (idxInAvailable > 0) { + val prevId = availableLuts[idxInAvailable - 1].id + val insertPos = updatedList.indexOfFirst { it.id == prevId } + if (insertPos != -1) { + updatedList.add(insertPos + 1, newItem) + } else { + updatedList.add(0, newItem) + } + } else { + updatedList.add(0, newItem) + } + } + localLutList = updatedList } - // 修正:将新项目添加到末尾,符合注释描述 - localLutList = updatedExisting + newItems } // 重命名对话框状态 @@ -75,6 +99,11 @@ fun FilterManagementScreen( var renamingLut by remember { mutableStateOf(null) } var renameText by remember { mutableStateOf("") } + // 复制对话框状态 + var showCopyDialog by remember { mutableStateOf(false) } + var copyingLut by remember { mutableStateOf(null) } + var copyText by remember { mutableStateOf("") } + // 删除确认对话框状态 var showDeleteDialog by remember { mutableStateOf(false) } var deletingLut by remember { mutableStateOf(null) } @@ -498,6 +527,13 @@ fun FilterManagementScreen( showRenameDialog = true } } else null, + onCopy = if (!isSelectionMode) { + { + copyingLut = lutInfo + copyText = lutInfo.getName() + context.getString(R.string.copy_suffix) + showCopyDialog = true + } + } else null, onEditColorRecipe = if (!isSelectionMode) { { editingLutId = lutInfo.id @@ -565,6 +601,41 @@ fun FilterManagementScreen( ) } + // 复制对话框 + if (showCopyDialog && copyingLut != null) { + AlertDialog( + onDismissRequest = { showCopyDialog = false }, + title = { + Text(stringResource(R.string.copy_lut_dialog_title)) + }, + text = { + OutlinedTextField( + value = copyText, + onValueChange = { copyText = it }, + label = { Text(stringResource(R.string.name)) }, + singleLine = true, + modifier = Modifier.fillMaxWidth() + ) + }, + confirmButton = { + TextButton( + onClick = { + viewModel.copyLut(copyingLut!!, copyText) + showCopyDialog = false + copyingLut = null + } + ) { + Text(stringResource(R.string.confirm)) + } + }, + dismissButton = { + TextButton(onClick = { showCopyDialog = false }) { + Text(stringResource(R.string.cancel)) + } + } + ) + } + // 删除确认对话框 if (showDeleteDialog && deletingLut != null) { AlertDialog( @@ -852,6 +923,7 @@ private fun FilterManagementItem( onSetDefault: () -> Unit, onToggleSelection: () -> Unit, onRename: (() -> Unit)?, + onCopy: (() -> Unit)? = null, onEditColorRecipe: (() -> Unit)?, onDelete: (() -> Unit)?, onEditCategory: (() -> Unit)? = null, @@ -1036,6 +1108,16 @@ private fun FilterManagementItem( ) } + // 复制 (所有滤镜可用) + DropdownMenuItem( + text = { Text(stringResource(R.string.copy), color = Color.White) }, + leadingIcon = { Icon(Icons.Default.ContentCopy, null, tint = Color.White.copy(alpha = 0.7f)) }, + onClick = { + showMenu = false + onCopy?.invoke() + } + ) + // 删除 (仅自定义) if (onDelete != null) { DropdownMenuItem( diff --git a/app/src/main/java/com/hinnka/mycamera/ui/settings/FrameManagementScreen.kt b/app/src/main/java/com/hinnka/mycamera/ui/settings/FrameManagementScreen.kt index 0a5f9b67..4c314e0c 100644 --- a/app/src/main/java/com/hinnka/mycamera/ui/settings/FrameManagementScreen.kt +++ b/app/src/main/java/com/hinnka/mycamera/ui/settings/FrameManagementScreen.kt @@ -67,8 +67,7 @@ fun FrameManagementScreen( val updatedExisting = localFrameList.mapNotNull { local -> availableFrames.find { it.id == local.id } } - // 修正:将新项目添加到末尾,符合注释描述 - localFrameList = updatedExisting + newItems + localFrameList = newItems + updatedExisting } // 重命名对话框状态 diff --git a/app/src/main/java/com/hinnka/mycamera/viewmodel/CameraViewModel.kt b/app/src/main/java/com/hinnka/mycamera/viewmodel/CameraViewModel.kt index 32bf568c..d4a471e9 100644 --- a/app/src/main/java/com/hinnka/mycamera/viewmodel/CameraViewModel.kt +++ b/app/src/main/java/com/hinnka/mycamera/viewmodel/CameraViewModel.kt @@ -32,6 +32,7 @@ import com.hinnka.mycamera.utils.ShutterSoundPlayer import com.hinnka.mycamera.utils.VibrationHelper import kotlinx.coroutines.* import kotlinx.coroutines.flow.* +import kotlinx.coroutines.flow.first import kotlin.math.abs /** @@ -489,6 +490,49 @@ class CameraViewModel(application: Application) : AndroidViewModel(application) } } + /** + * 复制 LUT + */ + fun copyLut(lut: LutInfo, copyName: String) { + viewModelScope.launch { + val newLutId = withContext(Dispatchers.IO) { + contentRepository.getCustomImportManager().copyLut(lut, copyName) + } + if (newLutId != null) { + withContext(Dispatchers.IO) { + // 同时复制色彩配方 + val params = contentRepository.lutManager.loadColorRecipeParams(lut.id) + contentRepository.lutManager.saveColorRecipeParams(newLutId, params) + + // 更新排序顺序:放在原版下面 + val currentOrder = userPreferencesRepository.userPreferences.first().filterOrder.toMutableList() + if (currentOrder.isEmpty()) { + // 如果当前没有排序,则从当前列表初始化并插入 + val allIds = availableLutList.map { it.id }.toMutableList() + val index = allIds.indexOf(lut.id) + if (index != -1) { + allIds.add(index + 1, newLutId) + } else { + allIds.add(newLutId) + } + userPreferencesRepository.saveFilterOrder(allIds) + } else { + val index = currentOrder.indexOf(lut.id) + if (index != -1) { + currentOrder.add(index + 1, newLutId) + } else { + currentOrder.add(newLutId) + } + userPreferencesRepository.saveFilterOrder(currentOrder) + } + + // 刷新列表 + contentRepository.refreshCustomContent() + } + } + } + } + /** * 获取滤镜排序顺序 */ diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xml index db68b1a2..4d58bf13 100644 --- a/app/src/main/res/values-zh/strings.xml +++ b/app/src/main/res/values-zh/strings.xml @@ -226,6 +226,9 @@ 删除滤镜 "%s"? 删除边框 "%s"? 重命名 + 复制 + 复制滤镜 + (副本) 编辑分类 批量编辑分类 分类排序 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f36c0241..32247014 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -227,6 +227,9 @@ Delete filter \"%s\"? Delete frame \"%s\"? Rename + Copy + Copy LUT + (Copy) Edit Category Batch Edit Category Sort Categories