feat: implement atomic preview updates and serialized metadata persistence
This commit is contained in:
1 parent
566ef0d98d
commit
138f61a40a
2 files changed
+64
-28
No files matched your search
@@ -508,11 +508,9 @@ object GalleryManager {
|
||||
chromaNoiseReduction: Float = 0f
|
||||
) {
|
||||
val existingJob = detailHdrBuildJobs[photoId]
|
||||
if (existingJob?.isActive == true) {
|
||||
return
|
||||
}
|
||||
val job = processingScope.launch {
|
||||
try {
|
||||
existingJob?.join()
|
||||
buildDetailHdrCache(
|
||||
context = context,
|
||||
photoId = photoId,
|
||||
@@ -522,7 +520,7 @@ object GalleryManager {
|
||||
chromaNoiseReduction = chromaNoiseReduction
|
||||
)
|
||||
} finally {
|
||||
detailHdrBuildJobs.remove(photoId)
|
||||
detailHdrBuildJobs.remove(photoId, coroutineContext[Job])
|
||||
}
|
||||
}
|
||||
detailHdrBuildJobs[photoId] = job
|
||||
@@ -2752,6 +2750,7 @@ object GalleryManager {
|
||||
try {
|
||||
val photoDir = getPhotoDir(context, photoId, true)
|
||||
val photoFile = File(photoDir, PHOTO_FILE)
|
||||
val tempPhotoFile = File(photoDir, "raw_refresh_temp.jpg")
|
||||
val dngFile = File(photoDir, DNG_FILE)
|
||||
val thumbnailFile = File(photoDir, THUMBNAIL_FILE)
|
||||
|
||||
@@ -2780,10 +2779,18 @@ object GalleryManager {
|
||||
)
|
||||
|
||||
if (processedBitmap != null) {
|
||||
// 保存为 original.jpg
|
||||
FileOutputStream(photoFile).use { out ->
|
||||
// 先写临时文件再替换,避免详情页或 HDR 任务读到半写入的 original.jpg。
|
||||
FileOutputStream(tempPhotoFile).use { out ->
|
||||
processedBitmap.compress(Bitmap.CompressFormat.JPEG, 95, out)
|
||||
}
|
||||
if (photoFile.exists() && !photoFile.delete()) {
|
||||
tempPhotoFile.delete()
|
||||
return@withContext null
|
||||
}
|
||||
if (!tempPhotoFile.renameTo(photoFile)) {
|
||||
tempPhotoFile.delete()
|
||||
return@withContext null
|
||||
}
|
||||
// 刷新 RAW 后,AI 降噪结果已失效,清理之
|
||||
getAiDenoiseFile(context, photoId).takeIf { it.exists() }?.delete()
|
||||
|
||||
@@ -2809,6 +2816,7 @@ object GalleryManager {
|
||||
processedBitmap
|
||||
} catch (e: Exception) {
|
||||
PLog.e(TAG, "Failed to refresh RAW preview", e)
|
||||
File(getPhotoDir(context, photoId, true), "raw_refresh_temp.jpg").takeIf { it.exists() }?.delete()
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,6 +44,7 @@ import kotlinx.coroutines.sync.withLock
|
||||
import kotlinx.coroutines.sync.withPermit
|
||||
import java.io.File
|
||||
import java.io.FileOutputStream
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import java.util.Locale
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
@@ -235,6 +236,7 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
|
||||
|
||||
// 正在刷新的照片 ID 集合
|
||||
val refreshingPhotos = mutableStateListOf<String>()
|
||||
private val rawEditMetadataJobs = ConcurrentHashMap<String, Job>()
|
||||
|
||||
// 预览图 LRU 缓存,按 Bitmap 字节数计算大小
|
||||
private val previewBitmapCache = object : LruCache<String, Bitmap>(
|
||||
@@ -1732,33 +1734,55 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
|
||||
val dcpId = editRawDcpId.value
|
||||
val baselineLutId = editRawBaselineLutId.value
|
||||
|
||||
viewModelScope.launch {
|
||||
val context = getApplication<Application>()
|
||||
GalleryManager.updateMetadata(context, mediaData.id) { current ->
|
||||
current.copy(
|
||||
rawDenoiseValue = denoise,
|
||||
rawExposureCompensation = exposure,
|
||||
rawAutoExposure = autoExposure,
|
||||
rawBlackPointCorrection = blackPoint,
|
||||
rawWhitePointCorrection = whitePoint,
|
||||
rawDROEnabled = droEnabled,
|
||||
rawDcpId = dcpId,
|
||||
baselineLutId = baselineLutId
|
||||
val previousJob = rawEditMetadataJobs[mediaData.id]
|
||||
val job = viewModelScope.launch {
|
||||
try {
|
||||
previousJob?.join()
|
||||
val context = getApplication<Application>()
|
||||
PLog.d(
|
||||
TAG,
|
||||
"persist RAW edit metadata: ${mediaData.id}, dro=$droEnabled, denoise=$denoise"
|
||||
)
|
||||
}.let { updated ->
|
||||
if (updated != null) {
|
||||
currentMediaMetadata = updated
|
||||
mediaData.metadata = updated
|
||||
GalleryManager.updateThumbnail(
|
||||
context = context,
|
||||
photoId = mediaData.id,
|
||||
photoProcessor = contentRepository.photoProcessor,
|
||||
metadata = updated
|
||||
GalleryManager.updateMetadata(context, mediaData.id) { current ->
|
||||
current.copy(
|
||||
rawDenoiseValue = denoise,
|
||||
rawExposureCompensation = exposure,
|
||||
rawAutoExposure = autoExposure,
|
||||
rawBlackPointCorrection = blackPoint,
|
||||
rawWhitePointCorrection = whitePoint,
|
||||
rawDROEnabled = droEnabled,
|
||||
rawDcpId = dcpId,
|
||||
baselineLutId = baselineLutId
|
||||
)
|
||||
photoRefreshKeys[mediaData.id] = System.currentTimeMillis()
|
||||
}.let { updated ->
|
||||
if (updated != null) {
|
||||
withContext(Dispatchers.Main) {
|
||||
if (currentPhotoMetadataId == mediaData.id || currentMediaMetadata != null) {
|
||||
currentMediaMetadata = updated
|
||||
currentPhotoMetadataId = mediaData.id
|
||||
}
|
||||
mediaData.metadata = updated
|
||||
_photos.value = _photos.value.map { photo ->
|
||||
if (photo.id == mediaData.id) photo.copy(metadata = updated) else photo
|
||||
}
|
||||
if (_latestPhoto.value?.id == mediaData.id) {
|
||||
_latestPhoto.value = _latestPhoto.value?.copy(metadata = updated)
|
||||
}
|
||||
photoRefreshKeys[mediaData.id] = System.currentTimeMillis()
|
||||
}
|
||||
GalleryManager.updateThumbnail(
|
||||
context = context,
|
||||
photoId = mediaData.id,
|
||||
photoProcessor = contentRepository.photoProcessor,
|
||||
metadata = updated
|
||||
)
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
rawEditMetadataJobs.remove(mediaData.id, coroutineContext[Job])
|
||||
}
|
||||
}
|
||||
rawEditMetadataJobs[mediaData.id] = job
|
||||
}
|
||||
|
||||
fun saveRawDenoiseValue(mediaData: MediaData, value: Float) {
|
||||
@@ -2380,6 +2404,10 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
|
||||
refreshingPhotos.add(photo.id)
|
||||
try {
|
||||
val context = getApplication<Application>()
|
||||
rawEditMetadataJobs[photo.id]?.let { pendingRawEditJob ->
|
||||
PLog.d(TAG, "refresh RAW preview waiting pending RAW edit metadata: ${photo.id}")
|
||||
pendingRawEditJob.join()
|
||||
}
|
||||
val result = GalleryManager.refreshRawPreview(
|
||||
context,
|
||||
photo.id,
|
||||
|
||||
Reference in new issue
Block a user