feat: implement atomic metadata updates in GalleryManager
This commit is contained in:
1 parent
0e51b2566c
commit
a1d144e49f
3 files changed
+170
-132
No files matched your search
@@ -720,11 +720,11 @@ object GalleryManager {
|
||||
}
|
||||
tempMotionVideoFile.delete()
|
||||
|
||||
val currentMetadata = loadMetadata(context, id) ?: metadata
|
||||
val updatedMetadata = currentMetadata.copy(
|
||||
exportedUris = currentMetadata.exportedUris + uri.toString()
|
||||
)
|
||||
saveMetadata(context, id, updatedMetadata)
|
||||
updateMetadata(context, id) { current ->
|
||||
current.copy(
|
||||
exportedUris = current.exportedUris + uri.toString()
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
@@ -738,11 +738,11 @@ object GalleryManager {
|
||||
}
|
||||
|
||||
// Save exported URI to metadata
|
||||
val currentMetadata = loadMetadata(context, id) ?: metadata
|
||||
val updatedMetadata = currentMetadata.copy(
|
||||
exportedUris = currentMetadata.exportedUris + uri.toString()
|
||||
)
|
||||
saveMetadata(context, id, updatedMetadata)
|
||||
updateMetadata(context, id) { current ->
|
||||
current.copy(
|
||||
exportedUris = current.exportedUris + uri.toString()
|
||||
)
|
||||
}
|
||||
PLog.d(TAG, "Exported URI saved: $uri for photo $id")
|
||||
|
||||
return@withContext true
|
||||
@@ -799,11 +799,11 @@ object GalleryManager {
|
||||
tempExportFile.inputStream().use { input -> input.copyTo(output) }
|
||||
}
|
||||
|
||||
val currentMetadata = loadMetadata(context, id) ?: metadata
|
||||
val updatedMetadata = currentMetadata.copy(
|
||||
exportedUris = currentMetadata.exportedUris + uri.toString()
|
||||
)
|
||||
saveMetadata(context, id, updatedMetadata)
|
||||
updateMetadata(context, id) { current ->
|
||||
current.copy(
|
||||
exportedUris = current.exportedUris + uri.toString()
|
||||
)
|
||||
}
|
||||
PLog.d(TAG, "Exported embedded-gainmap URI saved: $uri for photo $id")
|
||||
return true
|
||||
} finally {
|
||||
@@ -837,11 +837,11 @@ object GalleryManager {
|
||||
}
|
||||
PLog.d(TAG, "DNG exported: $uri")
|
||||
|
||||
val currentMetadata = loadMetadata(context, photoId) ?: metadata
|
||||
val updatedMetadata = currentMetadata.copy(
|
||||
exportedUris = currentMetadata.exportedUris + uri.toString()
|
||||
)
|
||||
saveMetadata(context, photoId, updatedMetadata)
|
||||
updateMetadata(context, photoId) { current ->
|
||||
current.copy(
|
||||
exportedUris = current.exportedUris + uri.toString()
|
||||
)
|
||||
}
|
||||
PLog.d(TAG, "Exported URI saved: $uri")
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
@@ -882,11 +882,11 @@ object GalleryManager {
|
||||
}
|
||||
PLog.d(TAG, "DNG exported from file: $uri")
|
||||
|
||||
val currentMetadata = loadMetadata(context, photoId) ?: metadata
|
||||
val updatedMetadata = currentMetadata.copy(
|
||||
exportedUris = currentMetadata.exportedUris + uri.toString()
|
||||
)
|
||||
saveMetadata(context, photoId, updatedMetadata)
|
||||
updateMetadata(context, photoId) { current ->
|
||||
current.copy(
|
||||
exportedUris = current.exportedUris + uri.toString()
|
||||
)
|
||||
}
|
||||
PLog.d(TAG, "Exported URI saved: $uri")
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
@@ -2251,43 +2251,70 @@ object GalleryManager {
|
||||
*/
|
||||
suspend fun loadMetadata(context: Context, photoId: String): MediaMetadata? {
|
||||
return metadataMutex.withLock {
|
||||
withContext(Dispatchers.IO) {
|
||||
try {
|
||||
val file = getMetadataFile(context, photoId)
|
||||
if (file.exists()) {
|
||||
val content = file.readText()
|
||||
val metadata = MediaMetadata.fromJson(content)
|
||||
if (metadata == null) {
|
||||
PLog.e(TAG, "loadMetadata: fromJson returned null for $photoId, content length=${content.length}")
|
||||
}
|
||||
metadata
|
||||
} else {
|
||||
null
|
||||
loadMetadataInternal(context, photoId)
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun loadMetadataInternal(context: Context, photoId: String): MediaMetadata? {
|
||||
return withContext(Dispatchers.IO) {
|
||||
try {
|
||||
val file = getMetadataFile(context, photoId)
|
||||
if (file.exists()) {
|
||||
val content = file.readText()
|
||||
val metadata = MediaMetadata.fromJson(content)
|
||||
if (metadata == null) {
|
||||
PLog.e(TAG, "loadMetadata: fromJson returned null for $photoId, content length=${content.length}")
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
PLog.e(TAG, "Failed to load metadata for photo: $photoId", e)
|
||||
metadata
|
||||
} else {
|
||||
null
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
PLog.e(TAG, "Failed to load metadata for photo: $photoId", e)
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新元数据
|
||||
* 保存元数据
|
||||
*/
|
||||
suspend fun saveMetadata(context: Context, photoId: String, metadata: MediaMetadata): Boolean {
|
||||
return metadataMutex.withLock {
|
||||
withContext(Dispatchers.IO) {
|
||||
try {
|
||||
val dir = getPhotoDir(context, photoId, true)
|
||||
val file = File(dir, METADATA_FILE)
|
||||
val json = metadata.toJson()
|
||||
file.writeText(json)
|
||||
true
|
||||
} catch (e: Exception) {
|
||||
PLog.e(TAG, "Failed to save metadata for photo: $photoId", e)
|
||||
false
|
||||
}
|
||||
saveMetadataInternal(context, photoId, metadata)
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun saveMetadataInternal(context: Context, photoId: String, metadata: MediaMetadata): Boolean {
|
||||
return withContext(Dispatchers.IO) {
|
||||
try {
|
||||
val dir = getPhotoDir(context, photoId, true)
|
||||
val file = File(dir, METADATA_FILE)
|
||||
val json = metadata.toJson()
|
||||
file.writeText(json)
|
||||
true
|
||||
} catch (e: Exception) {
|
||||
PLog.e(TAG, "Failed to save metadata for photo: $photoId", e)
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 原子地更新元数据
|
||||
*/
|
||||
suspend fun updateMetadata(
|
||||
context: Context,
|
||||
photoId: String,
|
||||
update: (MediaMetadata) -> MediaMetadata
|
||||
): MediaMetadata? {
|
||||
return metadataMutex.withLock {
|
||||
val current = loadMetadataInternal(context, photoId) ?: return@withLock null
|
||||
val updated = update(current)
|
||||
if (saveMetadataInternal(context, photoId, updated)) {
|
||||
updated
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2692,13 +2719,13 @@ object GalleryManager {
|
||||
// --- 常规 JPEG 处理逻辑 ---
|
||||
// 传递元数据确保旋转信息被正确处理
|
||||
tempImportJpeg(uri, context, metadata, photoFile, metadataFile, thumbnailFile)
|
||||
val currentMetadata = loadMetadata(context, photoId) ?: metadata
|
||||
val hasEmbeddedGainmap = detectEmbeddedGainmap(context, photoFile)
|
||||
val updatedMetadata = currentMetadata.copy(
|
||||
hasEmbeddedGainmap = hasEmbeddedGainmap,
|
||||
manualHdrEffectEnabled = hasEmbeddedGainmap
|
||||
)
|
||||
saveMetadata(context, photoId, updatedMetadata)
|
||||
updateMetadata(context, photoId) { current ->
|
||||
current.copy(
|
||||
hasEmbeddedGainmap = hasEmbeddedGainmap,
|
||||
manualHdrEffectEnabled = hasEmbeddedGainmap
|
||||
)
|
||||
}
|
||||
// if (metadata.computationalAperture != null) {
|
||||
// val bitmap = BitmapFactory.decodeFile(photoFile.absolutePath)
|
||||
// if (bitmap != null) {
|
||||
@@ -2713,9 +2740,10 @@ object GalleryManager {
|
||||
val videoFile = File(photoDir, VIDEO_FILE)
|
||||
if (MotionPhotoWriter.extractVideo(photoFile.absolutePath, videoFile.absolutePath)) {
|
||||
PLog.d(TAG, "Extracted video from imported Motion Photo: $photoId")
|
||||
val metadata = loadMetadata(context, photoId) ?: MediaMetadata()
|
||||
val timestampUs = MotionPhotoWriter.getPresentationTimestampUs(photoFile.absolutePath)
|
||||
saveMetadata(context, photoId, metadata.copy(presentationTimestampUs = timestampUs))
|
||||
updateMetadata(context, photoId) { current ->
|
||||
current.copy(presentationTimestampUs = timestampUs)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -385,28 +385,30 @@ class PhantomService(val context: Context) : LifecycleOwner, SavedStateRegistryO
|
||||
GalleryManager.importPhoto(context, uri, lutId, computationalAperture, existingPhotoId) ?: run {
|
||||
return@withContext
|
||||
}
|
||||
val metadata = GalleryManager.loadMetadata(context, photoId) ?: run {
|
||||
return@withContext
|
||||
}
|
||||
val phantomBaselineLutId = preferences?.phantomBaselineLutId
|
||||
val updatedMetadata = if (phantomBaselineLutId != null) {
|
||||
metadata.copy(
|
||||
lutId = lutId,
|
||||
baselineTarget = BaselineColorCorrectionTarget.PHANTOM,
|
||||
baselineLutId = phantomBaselineLutId,
|
||||
baselineColorRecipeParams = ContentRepository.getInstance(context)
|
||||
.lutManager
|
||||
.loadColorRecipeParams(phantomBaselineLutId, BaselineColorCorrectionTarget.PHANTOM)
|
||||
)
|
||||
} else {
|
||||
metadata.copy(
|
||||
lutId = lutId,
|
||||
baselineTarget = null,
|
||||
baselineLutId = null,
|
||||
baselineColorRecipeParams = null,
|
||||
)
|
||||
val baselineTarget = if (phantomBaselineLutId != null) BaselineColorCorrectionTarget.PHANTOM else null
|
||||
val baselineLutId = phantomBaselineLutId
|
||||
val baselineColorRecipeParams = phantomBaselineLutId?.let {
|
||||
ContentRepository.getInstance(context).lutManager.loadColorRecipeParams(it, BaselineColorCorrectionTarget.PHANTOM)
|
||||
}
|
||||
saveMetadata(context, photoId, updatedMetadata)
|
||||
|
||||
val updatedMetadata = GalleryManager.updateMetadata(context, photoId) { current ->
|
||||
if (baselineTarget != null) {
|
||||
current.copy(
|
||||
lutId = lutId,
|
||||
baselineTarget = baselineTarget,
|
||||
baselineLutId = baselineLutId,
|
||||
baselineColorRecipeParams = baselineColorRecipeParams,
|
||||
)
|
||||
} else {
|
||||
current.copy(
|
||||
lutId = lutId,
|
||||
baselineTarget = null,
|
||||
baselineLutId = null,
|
||||
baselineColorRecipeParams = null,
|
||||
)
|
||||
}
|
||||
} ?: return@withContext
|
||||
if (!isActive) return@withContext
|
||||
|
||||
if (uri != processingInfo?.uri) {
|
||||
@@ -437,7 +439,7 @@ class PhantomService(val context: Context) : LifecycleOwner, SavedStateRegistryO
|
||||
}
|
||||
|
||||
ExifWriter.writeExif(
|
||||
tempExportFile, metadata.toCaptureInfo().copy(
|
||||
tempExportFile, updatedMetadata.toCaptureInfo().copy(
|
||||
imageWidth = processedBitmap.width,
|
||||
imageHeight = processedBitmap.height
|
||||
)
|
||||
@@ -448,7 +450,7 @@ class PhantomService(val context: Context) : LifecycleOwner, SavedStateRegistryO
|
||||
|
||||
val writeUri = if (saveAsNew) {
|
||||
val lutName =
|
||||
metadata.lutId?.let { ContentRepository.getInstance(context).lutManager.getLutInfo(it)?.getName() }
|
||||
updatedMetadata.lutId?.let { ContentRepository.getInstance(context).lutManager.getLutInfo(it)?.getName() }
|
||||
var withSuffix = ""
|
||||
lutName?.let {
|
||||
withSuffix += ".$lutName"
|
||||
@@ -489,7 +491,7 @@ class PhantomService(val context: Context) : LifecycleOwner, SavedStateRegistryO
|
||||
} else null
|
||||
|
||||
// 重新从磁盘加载最新元数据,以获取可能刚写回的 presentationTimestampUs
|
||||
val latestMetadata = loadMetadata(context, photoId) ?: metadata
|
||||
val latestMetadata = GalleryManager.loadMetadata(context, photoId) ?: updatedMetadata
|
||||
val success = MotionPhotoWriter.write(
|
||||
tempExportFile.absolutePath,
|
||||
videoFile.absolutePath,
|
||||
@@ -553,11 +555,11 @@ class PhantomService(val context: Context) : LifecycleOwner, SavedStateRegistryO
|
||||
}
|
||||
|
||||
// Save exported URI to metadata
|
||||
val currentMetadata = loadMetadata(context, photoId) ?: metadata
|
||||
val updatedMetadata = currentMetadata.copy(
|
||||
exportedUris = currentMetadata.exportedUris + writeUri.toString()
|
||||
)
|
||||
saveMetadata(context, photoId, updatedMetadata)
|
||||
GalleryManager.updateMetadata(context, photoId) { current ->
|
||||
current.copy(
|
||||
exportedUris = current.exportedUris + writeUri.toString()
|
||||
)
|
||||
}
|
||||
shouldNotifyGallery = true
|
||||
PLog.d(TAG, "Exported URI saved: ${writeUri.lastPathSegment} $newName $newSize for photo $photoId")
|
||||
|
||||
|
||||
@@ -1752,22 +1752,31 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
|
||||
}
|
||||
|
||||
private fun persistRawEditMetadata(mediaData: MediaData) {
|
||||
val metadata = (mediaData.metadata ?: currentMediaMetadata)?.copy(
|
||||
rawDenoiseValue = editRawDenoise.value,
|
||||
rawExposureCompensation = editRawExposureCompensation.value,
|
||||
rawAutoExposure = editRawAutoExposure.value,
|
||||
rawBlackPointCorrection = editRawBlackPointCorrection.value,
|
||||
rawWhitePointCorrection = editRawWhitePointCorrection.value,
|
||||
rawDcpId = editRawDcpId.value,
|
||||
baselineLutId = editRawBaselineLutId.value
|
||||
)
|
||||
if (metadata != null) {
|
||||
currentMediaMetadata = metadata
|
||||
}
|
||||
val denoise = editRawDenoise.value
|
||||
val exposure = editRawExposureCompensation.value
|
||||
val autoExposure = editRawAutoExposure.value
|
||||
val blackPoint = editRawBlackPointCorrection.value
|
||||
val whitePoint = editRawWhitePointCorrection.value
|
||||
val dcpId = editRawDcpId.value
|
||||
val baselineLutId = editRawBaselineLutId.value
|
||||
|
||||
viewModelScope.launch {
|
||||
val context = getApplication<Application>()
|
||||
metadata?.let {
|
||||
GalleryManager.saveMetadata(context, mediaData.id, it)
|
||||
GalleryManager.updateMetadata(context, mediaData.id) { current ->
|
||||
current.copy(
|
||||
rawDenoiseValue = denoise,
|
||||
rawExposureCompensation = exposure,
|
||||
rawAutoExposure = autoExposure,
|
||||
rawBlackPointCorrection = blackPoint,
|
||||
rawWhitePointCorrection = whitePoint,
|
||||
rawDcpId = dcpId,
|
||||
baselineLutId = baselineLutId
|
||||
)
|
||||
}.let { updated ->
|
||||
if (updated != null) {
|
||||
currentMediaMetadata = updated
|
||||
mediaData.metadata = updated
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2303,32 +2312,31 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
|
||||
)
|
||||
}
|
||||
|
||||
val metadata = currentMediaMetadata?.copy(
|
||||
lutId = editLutId.value,
|
||||
frameId = editFrameId.value,
|
||||
colorRecipeParams = editPhotoRecipeParams.value,
|
||||
sharpening = editSharpening.value,
|
||||
noiseReduction = editNoiseReduction.value,
|
||||
chromaNoiseReduction = editChromaNoiseReduction.value,
|
||||
rawDenoiseValue = editRawDenoise.value,
|
||||
rawExposureCompensation = editRawExposureCompensation.value,
|
||||
rawAutoExposure = editRawAutoExposure.value,
|
||||
rawBlackPointCorrection = editRawBlackPointCorrection.value,
|
||||
rawWhitePointCorrection = editRawWhitePointCorrection.value,
|
||||
rawDcpId = editRawDcpId.value,
|
||||
baselineLutId = editRawBaselineLutId.value,
|
||||
computationalAperture = editComputationalAperture.value,
|
||||
focusPointX = editFocusPointX.value,
|
||||
focusPointY = editFocusPointY.value,
|
||||
postCropRegion = finalCropRegion
|
||||
) ?: run {
|
||||
onComplete(false)
|
||||
return@launch
|
||||
val success = GalleryManager.updateMetadata(context, targetPhotoId) { current ->
|
||||
current.copy(
|
||||
lutId = editLutId.value,
|
||||
frameId = editFrameId.value,
|
||||
colorRecipeParams = editPhotoRecipeParams.value,
|
||||
sharpening = editSharpening.value,
|
||||
noiseReduction = editNoiseReduction.value,
|
||||
chromaNoiseReduction = editChromaNoiseReduction.value,
|
||||
rawDenoiseValue = editRawDenoise.value,
|
||||
rawExposureCompensation = editRawExposureCompensation.value,
|
||||
rawAutoExposure = editRawAutoExposure.value,
|
||||
rawBlackPointCorrection = editRawBlackPointCorrection.value,
|
||||
rawWhitePointCorrection = editRawWhitePointCorrection.value,
|
||||
rawDcpId = editRawDcpId.value,
|
||||
baselineLutId = editRawBaselineLutId.value,
|
||||
computationalAperture = editComputationalAperture.value,
|
||||
focusPointX = editFocusPointX.value,
|
||||
focusPointY = editFocusPointY.value,
|
||||
postCropRegion = finalCropRegion
|
||||
)
|
||||
}
|
||||
val success = GalleryManager.saveMetadata(context, targetPhotoId, metadata)
|
||||
|
||||
if (success) {
|
||||
currentMediaMetadata = metadata
|
||||
if (success != null) {
|
||||
currentMediaMetadata = success
|
||||
photo.metadata = success
|
||||
|
||||
// 如果是新导入的,刷相册列表
|
||||
if (selectedTab == GalleryTab.SYSTEM) {
|
||||
@@ -2337,7 +2345,7 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
|
||||
// 更新 photos 列表中对应照片的 metadata,触发 UI 刷新
|
||||
val updatedPhotos = _photos.value.map { p ->
|
||||
if (p.id == targetPhotoId) {
|
||||
p.copy(metadata = metadata)
|
||||
p.copy(metadata = success)
|
||||
} else {
|
||||
p
|
||||
}
|
||||
@@ -2347,7 +2355,7 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
|
||||
|
||||
// 同步更新 latestPhoto
|
||||
if (_latestPhoto.value?.id == targetPhotoId) {
|
||||
_latestPhoto.value = _latestPhoto.value?.copy(metadata = metadata)
|
||||
_latestPhoto.value = _latestPhoto.value?.copy(metadata = success)
|
||||
}
|
||||
|
||||
exitEditMode()
|
||||
@@ -2356,13 +2364,13 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
|
||||
GalleryManager.queueDetailHdrCacheBuild(
|
||||
context = context,
|
||||
photoId = targetPhotoId,
|
||||
metadata = metadata,
|
||||
sharpening = metadata.sharpening ?: 0f,
|
||||
noiseReduction = metadata.noiseReduction ?: 0f,
|
||||
chromaNoiseReduction = metadata.chromaNoiseReduction ?: 0f
|
||||
metadata = success,
|
||||
sharpening = success.sharpening ?: 0f,
|
||||
noiseReduction = success.noiseReduction ?: 0f,
|
||||
chromaNoiseReduction = success.chromaNoiseReduction ?: 0f
|
||||
)
|
||||
}
|
||||
onComplete(success)
|
||||
onComplete(success != null)
|
||||
} catch (e: Exception) {
|
||||
PLog.e(TAG, "Failed to save metadata", e)
|
||||
onComplete(false)
|
||||
|
||||
Reference in new issue
Block a user