@@ -2,11 +2,13 @@ package com.xayah.feature.main.details
22
33import android.content.Context
44import android.content.pm.PackageManager
5- import android.widget.Toast
65import android.util.Log
6+ import android.widget.Toast
77import androidx.lifecycle.SavedStateHandle
88import androidx.lifecycle.ViewModel
99import androidx.lifecycle.viewModelScope
10+ // ⭐ 修正:保留所需的 Repository 和工具类
11+ import com.xayah.core.data.R // 假设资源 R.string.failed 在此模块
1012import com.xayah.core.data.repository.AppsRepo
1113import com.xayah.core.data.repository.FilesRepo
1214import com.xayah.core.data.repository.LabelsRepo
@@ -19,19 +21,13 @@ import com.xayah.core.model.database.LabelFileCrossRefEntity
1921import com.xayah.core.model.database.MediaEntity
2022import com.xayah.core.model.database.PackageDataStates
2123import com.xayah.core.model.database.PackageEntity
24+ import com.xayah.core.restic.ResticSnapshot
2225import com.xayah.core.rootservice.service.RemoteRootService
2326import com.xayah.core.ui.route.MainRoutes
2427import com.xayah.core.util.decodeURL
2528import com.xayah.core.util.LogUtil
26- import com.xayah.core.util.module.combine
2729import com.xayah.core.util.launchOnDefault
2830import com.xayah.core.util.withMainContext
29- import com.xayah.core.restic.ResticRepository
30- import com.xayah.core.restic.ResticSnapshot
31- import com.xayah.core.datastore.readResticPassword
32- import com.xayah.feature.main.details.DetailsUiState.Error
33- import com.xayah.feature.main.details.DetailsUiState.Loading
34- import com.xayah.feature.main.details.DetailsUiState.Success
3531import dagger.hilt.android.lifecycle.HiltViewModel
3632import dagger.hilt.android.qualifiers.ApplicationContext
3733import kotlinx.coroutines.flow.MutableStateFlow
@@ -51,15 +47,12 @@ class DetailsViewModel @Inject constructor(
5147 private val rootService : RemoteRootService ,
5248 private val appsRepo : AppsRepo ,
5349 private val filesRepo : FilesRepo ,
54- private val labelsRepo : LabelsRepo ,
55- private val resticRepo : ResticRepository ,
50+ private val labelsRepo : LabelsRepo
5651) : ViewModel() {
5752 private val id: Long = savedStateHandle.get<String >(MainRoutes .ARG_ID )?.toLongOrNull() ? : 0L
5853 private val target: Target = Target .valueOf(savedStateHandle.get<String >(MainRoutes .ARG_TARGET )!! .decodeURL().trim())
5954 private val isRefreshing: MutableStateFlow <Boolean > = MutableStateFlow (false )
60- // 添加 isProtecting 状态
6155 private val isProtecting: MutableStateFlow <Boolean > = MutableStateFlow (false )
62- // 添加 protectProgress 状态
6356 private val protectProgress: MutableStateFlow <String ?> = MutableStateFlow (null )
6457
6558 companion object {
@@ -78,7 +71,7 @@ class DetailsViewModel @Inject constructor(
7871 appsRepo.getApp(id),
7972 isRefreshing,
8073 isProtecting,
81- protectProgress, // 添加这个
74+ protectProgress,
8275 labelsRepo.getLabelsFlow(),
8376 labelsRepo.getAppRefsFlow()
8477 ) { app, isRefreshing, isProtecting, protectProgress, labels, refs ->
@@ -87,7 +80,7 @@ class DetailsViewModel @Inject constructor(
8780 uuid = UUID .randomUUID(),
8881 isRefreshing = isRefreshing,
8982 isProtecting = isProtecting,
90- protectProgress = protectProgress, // 添加这个
83+ protectProgress = protectProgress,
9184 labels = labels,
9285 app = app,
9386 refs = refs.filter { ref ->
@@ -108,7 +101,7 @@ class DetailsViewModel @Inject constructor(
108101 filesRepo.getFile(id),
109102 isRefreshing,
110103 isProtecting,
111- protectProgress, // 添加这个
104+ protectProgress,
112105 labelsRepo.getLabelsFlow(),
113106 labelsRepo.getFileRefsFlow()
114107 ) { file, isRefreshing, isProtecting, protectProgress, labels, refs ->
@@ -117,7 +110,7 @@ class DetailsViewModel @Inject constructor(
117110 uuid = UUID .randomUUID(),
118111 isRefreshing = isRefreshing,
119112 isProtecting = isProtecting,
120- protectProgress = protectProgress, // 添加这个
113+ protectProgress = protectProgress,
121114 labels = labels,
122115 file = file,
123116 refs = refs.filter { ref ->
@@ -287,7 +280,7 @@ class DetailsViewModel @Inject constructor(
287280
288281 fun protect () {
289282 viewModelScope.launchOnDefault {
290- log { " protect() started" } // 1. 方法开始
283+ log { " protect() started" }
291284 isProtecting.emit(true )
292285 protectProgress.emit(null )
293286 try {
@@ -312,26 +305,26 @@ class DetailsViewModel @Inject constructor(
312305 else -> {
313306 val state = uiState.value.castTo<Success .File >()
314307 val file = state.file
315- log { " Protecting file: ${file.name} , cloud: ${file.indexInfo.cloud} " } // 3. File 分支开始
316- log { " Calling filesRepo.protectFile with progress callback" } // 4. 调用前
308+ log { " Protecting file: ${file.name} , cloud: ${file.indexInfo.cloud} " }
309+ log { " Calling filesRepo.protectFile with progress callback" }
317310
318311 filesRepo.protectFile(file.indexInfo.cloud, file) { currentPart, totalParts, currentFile, totalFiles ->
319- log { " Progress callback received in ViewModel: Part $currentPart /$totalParts , File $currentFile /$totalFiles " } // 5. 回调被触发
312+ log { " Progress callback received in ViewModel: Part $currentPart /$totalParts , File $currentFile /$totalFiles " }
320313 viewModelScope.launch {
321314 protectProgress.emit(" $currentPart /$totalParts " )
322- log { " protectProgress updated to: $currentPart /$totalParts " } // 6. 状态更新后
315+ log { " protectProgress updated to: $currentPart /$totalParts " }
323316 }
324317 }
325- log { " filesRepo.protectFile completed" } // 7. 调用完成
318+ log { " filesRepo.protectFile completed" }
326319 }
327320 }
328321 } catch (e: Exception ) {
329- log { " protect() failed with exception: ${e.message} " } // 8. 异常捕获
322+ log { " protect() failed with exception: ${e.message} " }
330323 } finally {
331- log { " protect() finally block - cleaning up" } // 9. 清理前
324+ log { " protect() finally block - cleaning up" }
332325 isProtecting.emit(false )
333326 protectProgress.emit(null )
334- log { " protect() completed" } // 10. 方法结束
327+ log { " protect() completed" }
335328 }
336329 }
337330 }
@@ -340,27 +333,25 @@ class DetailsViewModel @Inject constructor(
340333 private val _snapshotDetailState = MutableStateFlow <ResticSnapshot ?>(null )
341334 val snapshotDetailState: StateFlow <ResticSnapshot ?> = _snapshotDetailState .asStateFlow()
342335
343- // 获取快照详情
344- fun loadSnapshotDetail (packageEntity : PackageEntity ) {
336+ // 获取快照详情 - 使用扩展的 Repository 方法
337+ // 修改快照详情加载方法
338+ fun loadSnapshotDetail (entity : Any ) {
345339 viewModelScope.launch {
346- packageEntity.resticSnapshotId?.let { snapshotId ->
347- packageEntity.resticRepoPath?.let { repoPath ->
348- val password = context.readResticPassword()
349- // 检查密码是否为空
350- if (password.isNullOrEmpty()) {
351- Log .e(TAG , " Restic password is null or empty" )
352- return @launch
340+ when (entity) {
341+ is PackageEntity -> {
342+ appsRepo.loadSnapshotDetail(entity)?.let { snapshot ->
343+ _snapshotDetailState .value = snapshot
344+ }
345+ }
346+ is MediaEntity -> {
347+ filesRepo.loadSnapshotDetail(entity)?.let { snapshot ->
348+ _snapshotDetailState .value = snapshot
353349 }
354-
355- val snapshots = resticRepo.listSnapshots(repoPath, password)
356- val snapshot = snapshots.find { it.id == snapshotId }
357- _snapshotDetailState .value = snapshot
358350 }
359351 }
360352 }
361353 }
362354
363- // 修复:将 delete() 方法移到类内部
364355 fun delete () {
365356 viewModelScope.launchOnDefault {
366357 when (uiState.value) {
@@ -400,14 +391,14 @@ sealed interface DetailsUiState {
400391 open val uuid : UUID ,
401392 open val isRefreshing : Boolean ,
402393 open val isProtecting : Boolean ,
403- open val protectProgress : String? , // 添加这个字段
394+ open val protectProgress : String? ,
404395 open val labels : List <LabelEntity >,
405396 ) : DetailsUiState {
406397 data class App (
407398 override val uuid : UUID ,
408399 override val isRefreshing : Boolean ,
409400 override val isProtecting : Boolean ,
410- override val protectProgress : String? , // 添加这个字段
401+ override val protectProgress : String? ,
411402 override val labels : List <LabelEntity >,
412403 val app : PackageEntity ,
413404 val refs : List <LabelAppCrossRefEntity >,
@@ -417,7 +408,7 @@ sealed interface DetailsUiState {
417408 override val uuid : UUID ,
418409 override val isRefreshing : Boolean ,
419410 override val isProtecting : Boolean ,
420- override val protectProgress : String? , // 添加这个字段
411+ override val protectProgress : String? ,
421412 override val labels : List <LabelEntity >,
422413 val file : MediaEntity ,
423414 val refs : List <LabelFileCrossRefEntity >,
0 commit comments