feat(android): показывать ход восстановления оригиналов

Возврат из переноса в копии скачивает оригиналы с сервера, но ход нигде не
отражался: карточка умела показывать только загрузку, а восстановление шло в
области экрана и обрывалось при уходе с него.

Добавлен RestoreCoordinator: выполняет восстановление в области приложения
(уход с экрана больше не прерывает), ведёт ход «альбом -> восстановлено из
всего» и защищает от одновременного запуска по одному альбому. RestoreManager
сообщает ход по каждому элементу и вынесен за интерфейс AlbumRestorer; SyncCheck
ходит через координатора и, как и раньше, дожидается завершения.

Карточка альбома показывает «Копии — восстановление N%» (пульсирует), экран
альбома устройства — строку «Восстановление оригиналов: X из Y» с полосой.

Изменение OpenSpec: android-album-restore-progress.
This commit is contained in:
iu
2026-10-16 20:15:05 +03:00
parent 1eb1177273
commit fcc7ac05c1
21 changed files with 593 additions and 27 deletions
@@ -0,0 +1,56 @@
package com.galo.android.data.sync
import com.galo.android.data.db.entity.DeviceAssetEntity
import com.galo.android.domain.sync.RestoreProgress
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
/**
* Runs album restores in the application scope, so leaving the album screen does
* not interrupt them, and exposes per-album progress for the album card and the
* album screen.
*/
class RestoreCoordinator(
private val scope: CoroutineScope,
private val restorer: AlbumRestorer,
) {
private val _progress = MutableStateFlow<Map<String, RestoreProgress>>(emptyMap())
/** Progress keyed by device album id; an album is present only while restoring. */
val progress: StateFlow<Map<String, RestoreProgress>> = _progress.asStateFlow()
private val gate = Mutex()
private val busy = mutableSetOf<String>()
/** Starts restoring in the application scope; does nothing when there is nothing to do. */
fun start(deviceAlbumId: String, targets: List<DeviceAssetEntity>) {
if (targets.isEmpty()) return
scope.launch { run(deviceAlbumId, targets) }
}
/** Restores and reports progress; the caller waits for completion. */
suspend fun run(deviceAlbumId: String, targets: List<DeviceAssetEntity>) {
if (targets.isEmpty()) return
// One restore per album at a time: a screen action and a background check
// must not fight over the same previews.
gate.withLock {
if (!busy.add(deviceAlbumId)) return
}
_progress.update { it + (deviceAlbumId to RestoreProgress(0, targets.size)) }
try {
restorer.restore(targets) { done, total ->
_progress.update { it + (deviceAlbumId to RestoreProgress(done, total)) }
}
} finally {
gate.withLock { busy.remove(deviceAlbumId) }
_progress.update { it - deviceAlbumId }
}
}
}
@@ -14,6 +14,14 @@ interface OriginalFetcher {
suspend fun fetch(photoId: String): ByteArray?
}
/** Brings originals back onto the device, reporting how far it got. */
interface AlbumRestorer {
suspend fun restore(
assets: List<DeviceAssetEntity>,
onProgress: ((done: Int, total: Int) -> Unit)? = null,
): RestoreReport
}
/** Outcome of restoring originals for a set of offloaded assets. */
data class RestoreReport(
val restored: Int,
@@ -31,7 +39,7 @@ class RestoreManager(
private val context: Context,
private val dao: SyncDao,
private val fetcher: OriginalFetcher,
) {
) : AlbumRestorer {
fun availableBytes(): Long = runCatching { context.cacheDir.usableSpace }.getOrDefault(0L)
@@ -50,28 +58,36 @@ class RestoreManager(
}
}
suspend fun restore(assets: List<DeviceAssetEntity>): RestoreReport = withContext(Dispatchers.IO) {
override suspend fun restore(
assets: List<DeviceAssetEntity>,
onProgress: ((done: Int, total: Int) -> Unit)?,
): RestoreReport = withContext(Dispatchers.IO) {
var restored = 0
var bytes = 0L
var needsConsent = 0
for (asset in assets) {
val photoId = asset.serverPhotoId ?: continue
val data = runCatching { fetcher.fetch(photoId) }.getOrNull() ?: continue
if (data.isEmpty()) continue
if (!canRestore(data.size.toLong(), availableBytes())) {
return@withContext RestoreReport(restored, bytes, needsConsent, outOfSpace = true)
assets.forEachIndexed { index, asset ->
val data = asset.serverPhotoId?.let { photoId ->
runCatching { fetcher.fetch(photoId) }.getOrNull()
}
try {
writeInPlace(Uri.parse(asset.uri), data)
dao.markState(asset.assetKey, LocalAssetState.LOCAL.name)
restored++
bytes += data.size
} catch (e: SecurityException) {
needsConsent++
} catch (e: Exception) {
// Leave the asset as is and continue with the rest.
if (data != null && data.isNotEmpty()) {
if (!canRestore(data.size.toLong(), availableBytes())) {
return@withContext RestoreReport(restored, bytes, needsConsent, outOfSpace = true)
}
try {
writeInPlace(Uri.parse(asset.uri), data)
dao.markState(asset.assetKey, LocalAssetState.LOCAL.name)
restored++
bytes += data.size
} catch (e: SecurityException) {
needsConsent++
} catch (e: Exception) {
// Leave the asset as is and continue with the rest.
}
}
// Progress counts every planned item, including the skipped ones, so the
// bar reaches the total even when some originals cannot be fetched.
onProgress?.invoke(index + 1, assets.size)
}
RestoreReport(restored, bytes, needsConsent, outOfSpace = false)
}
@@ -15,7 +15,7 @@ import com.galo.android.domain.sync.GallerySyncPolicy
class SyncCheck(
private val repository: SyncRepository,
private val recovery: SyncRecovery,
private val restoreManager: RestoreManager,
private val restoreCoordinator: RestoreCoordinator,
private val coordinator: SyncCoordinator,
) {
@@ -35,7 +35,7 @@ class SyncCheck(
hasServerPhoto = entity.serverPhotoId != null,
) == GallerySyncPolicy.LocalFix.RESTORE
}
if (restore.isNotEmpty()) runCatching { restoreManager.restore(restore) }
if (restore.isNotEmpty()) runCatching { restoreCoordinator.run(deviceAlbumId, restore) }
// Upload what is missing and offload what is already on the server (move).
runCatching { coordinator.reconcile(deviceAlbumId) }
@@ -28,6 +28,7 @@ import com.galo.android.data.sync.DeviceLocalAssetProvider
import com.galo.android.data.sync.DeviceMediaObserver
import com.galo.android.data.sync.OffloadManager
import com.galo.android.data.sync.RepositorySyncUploadQueue
import com.galo.android.data.sync.RestoreCoordinator
import com.galo.android.data.sync.RestoreManager
import com.galo.android.data.sync.SyncCheck
import com.galo.android.data.sync.SyncCoordinator
@@ -115,10 +116,13 @@ class AppContainer(context: Context) {
fetcher = ApiOriginalFetcher(api, apiClient),
)
/** Restores album originals in the application scope and reports their progress. */
val restoreCoordinator = RestoreCoordinator(scope, restoreManager)
val syncRecovery = SyncRecovery(appContext, database.syncDao(), deviceSource)
/** Unified per-gallery check and fix (startup + album open). */
val syncCheck = SyncCheck(syncRepository, syncRecovery, restoreManager, syncCoordinator)
val syncCheck = SyncCheck(syncRepository, syncRecovery, restoreCoordinator, syncCoordinator)
fun start() {
scope.launch {
@@ -20,14 +20,20 @@ data class AlbumSyncProgress(
val percent: Int?,
/** Photos whose server copy is gone; the most severe state and shown first. */
val missingCopies: Int = 0,
/** Share of an ongoing restore of the album's originals, when any. */
val restorePercent: Int? = null,
) {
/** Pulsing accompanies a live percentage, never a waiting, failed or lost state. */
val pulsing: Boolean get() = percent != null && missingCopies == 0
/**
* Pulsing accompanies live work: an upload percentage or a running restore,
* never a waiting, failed or lost state.
*/
val pulsing: Boolean get() = (percent != null || restorePercent != null) && missingCopies == 0
/** The third line of the card: the mode label plus the operation state. */
fun cardText(): String = when {
missingCopies > 0 -> "${modeLabel()} — потеряна копия: $missingCopies"
restorePercent != null -> "${modeLabel()} — восстановление $restorePercent%"
running && percent != null -> "${modeLabel()} $percent%"
failed -> "${modeLabel()} — ошибка"
waitingForWifi -> "${modeLabel()} — ждёт Wi-Fi"
@@ -55,6 +61,7 @@ data class AlbumSyncProgress(
uploaded: Int,
total: Int,
missingCopies: Int = 0,
restorePercent: Int? = null,
): AlbumSyncProgress? {
if (mode == SyncMode.DISABLED) return null
@@ -81,6 +88,7 @@ data class AlbumSyncProgress(
failed = failed,
percent = percent,
missingCopies = missingCopies,
restorePercent = restorePercent,
)
}
}
@@ -0,0 +1,8 @@
package com.galo.android.domain.sync
/** How many of an album's originals have been brought back, out of the planned total. */
data class RestoreProgress(val done: Int, val total: Int) {
/** Share of the restore that is finished; zero when there is nothing planned. */
val percent: Int get() = if (total > 0) (done * 100 / total).coerceIn(0, 100) else 0
}
@@ -12,6 +12,7 @@ import com.galo.android.domain.model.DeviceAlbumMeta
import com.galo.android.domain.model.SyncMode
import com.galo.android.domain.model.SyncUpload
import com.galo.android.domain.sync.AlbumSyncProgress
import com.galo.android.domain.sync.RestoreProgress
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
@@ -64,6 +65,7 @@ class AlbumsViewModel(
val uploads: List<SyncUpload>,
val uploadedCounts: Map<String, Int>,
val missingCounts: Map<String, Int>,
val restores: Map<String, RestoreProgress>,
)
private val deviceInput: Flow<DeviceInput> = combine(
@@ -74,9 +76,18 @@ class AlbumsViewModel(
combine(
container.syncRepository.observeUploadedCounts(),
container.syncRepository.observeMissingCopyCounts(),
) { uploaded, missing -> uploaded to missing },
container.restoreCoordinator.progress,
) { uploaded, missing, restores -> Triple(uploaded, missing, restores) },
) { devices, access, meta, uploads, counts ->
DeviceInput(devices, access, meta, uploads, counts.first, counts.second)
DeviceInput(
devices = devices,
access = access,
meta = meta,
uploads = uploads,
uploadedCounts = counts.first,
missingCounts = counts.second,
restores = counts.third,
)
}
val state: StateFlow<UiState> = combine(
@@ -114,6 +125,7 @@ class AlbumsViewModel(
uploads = device.uploads,
uploadedCounts = device.uploadedCounts,
missingCounts = device.missingCounts,
restoreByDevice = device.restores,
),
)
@@ -193,6 +205,7 @@ internal fun albumSyncProgressByDevice(
uploads: List<SyncUpload>,
uploadedCounts: Map<String, Int>,
missingCounts: Map<String, Int> = emptyMap(),
restoreByDevice: Map<String, RestoreProgress> = emptyMap(),
): Map<String, AlbumSyncProgress> {
val uploadsByDevice = uploads.groupBy { it.deviceAlbumId }
return devices.mapNotNull { device ->
@@ -202,6 +215,7 @@ internal fun albumSyncProgressByDevice(
uploaded = uploadedCounts[device.id] ?: 0,
total = device.itemCount,
missingCopies = missingCounts[device.id] ?: 0,
restorePercent = restoreByDevice[device.id]?.percent,
)?.let { device.id to it }
}.toMap()
}
@@ -37,6 +37,7 @@ import androidx.compose.material3.Button
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ModalNavigationDrawer
import androidx.compose.material3.ModalDrawerSheet
@@ -80,6 +81,7 @@ import com.galo.android.domain.model.DeviceMedia
import com.galo.android.domain.model.MediaKind
import com.galo.android.domain.model.Photo
import com.galo.android.domain.model.SyncMode
import com.galo.android.domain.sync.RestoreProgress
import com.galo.android.ui.album.RenameAlbumDialog
import com.galo.android.ui.common.GaloAsyncImage
import com.galo.android.ui.panel.AlbumAttributesPanel
@@ -95,6 +97,7 @@ const val TAG_DEVICE_ITEM_PREFIX = "device_item_"
const val TAG_TRANSFERRED_PREFIX = "transferred_"
const val TAG_ORPHANED_PREFIX = "orphaned_"
const val TAG_DEVICE_LOST_COPY = "device_lost_copy"
const val TAG_DEVICE_RESTORE = "device_restore"
const val TAG_DEVICE_SETTINGS = "device_album_settings"
const val TAG_MODE_PREFIX = "sync_mode_"
const val TAG_TARGET_PREFIX = "sync_target_"
@@ -467,6 +470,7 @@ fun DeviceAlbumContent(
if (state.orphanedMediaIds.isNotEmpty()) {
LostCopyBanner(count = state.orphanedMediaIds.size)
}
state.restore?.let { RestoreRow(progress = it) }
if (state.items.isEmpty()) {
Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
Text("В альбоме нет медиа")
@@ -794,6 +798,27 @@ fun TargetPickerDialog(
)
}
/** Shows how far the restore of this album's originals has got. */
@Composable
private fun RestoreRow(progress: RestoreProgress) {
Column(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 16.dp, vertical = 8.dp)
.testTag(TAG_DEVICE_RESTORE),
) {
Text(
text = "Восстановление оригиналов: ${progress.done} из ${progress.total}",
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
LinearProgressIndicator(
progress = { progress.percent / 100f },
modifier = Modifier.fillMaxWidth().padding(top = 4.dp),
)
}
}
/** Shown when some of the album's photos lost their server copy. */
@Composable
private fun LostCopyBanner(count: Int) {
@@ -16,6 +16,7 @@ import com.galo.android.domain.model.DeviceMedia
import com.galo.android.domain.model.LocalAssetState
import com.galo.android.domain.model.Photo
import com.galo.android.domain.model.SyncMode
import com.galo.android.domain.sync.RestoreProgress
import com.galo.android.data.sync.OffloadResult
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
@@ -43,6 +44,8 @@ class DeviceAlbumViewModel(
val serverPhotoByMediaId: Map<Long, String> = emptyMap(),
val transferredMediaIds: Set<Long> = emptySet(),
val orphanedMediaIds: Set<Long> = emptySet(),
/** Progress of an ongoing restore of this album's originals, when any. */
val restore: RestoreProgress? = null,
)
private data class Local(
@@ -78,13 +81,19 @@ class DeviceAlbumViewModel(
.map { it[deviceAlbumId] }
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), null)
private val assetsAndRestore = combine(
container.syncRepository.observeAssets(deviceAlbumId),
container.restoreCoordinator.progress.map { it[deviceAlbumId] },
) { assets, restore -> assets to restore }
val state: StateFlow<UiState> = combine(
container.albumRepository.observeAlbums(),
container.syncRepository.observeLinks(),
container.syncRepository.observeAssets(deviceAlbumId),
assetsAndRestore,
local,
pendingMode,
) { albums, links, assets, l, pending ->
) { albums, links, assetsState, l, pending ->
val (assets, restore) = assetsState
val link = links.firstOrNull { it.deviceAlbumId == deviceAlbumId }
UiState(
name = l.name,
@@ -97,6 +106,7 @@ class DeviceAlbumViewModel(
serverPhotoByMediaId = serverOriginalsByMediaId(assets),
transferredMediaIds = serverOriginalsByMediaId(assets).keys,
orphanedMediaIds = orphanedMediaIds(assets),
restore = restore,
)
}.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), UiState())
@@ -388,7 +398,9 @@ class DeviceAlbumViewModel(
fun restoreOriginals() {
viewModelScope.launch {
val targets = RestoreManager.restoreTargets(container.syncRepository.assets(deviceAlbumId))
if (targets.isNotEmpty()) container.restoreManager.restore(targets)
// The coordinator runs it in the application scope, so leaving the album
// screen does not interrupt the restore.
container.restoreCoordinator.start(deviceAlbumId, targets)
}
}