Add live backup status reconciliation with NAS manifest
BackupStatusService (new singleton, ObservableObject, PHPhotoLibraryChangeObserver):
- Loads cached BackupStatusSnapshot instantly from UserDefaults on init
- Full reconcile: fetch phone assets → connect NAS → load/build manifest → compare
→ enforce invariant (alreadySafe ≤ phoneTotal), persist result
- Debounced (30s) for photo library changes; force=true bypasses debounce
- If NAS unreachable: keeps last cached numbers, marks connectionState = .offline
- PHPhotoLibraryChangeObserver triggers refresh on any library change
BackupManifest (new):
- Stored at {remotePath}/.kisani.json on NAS
- Indexed by localIdentifier (PHAsset stable ID); filename fallback for legacy entries
- Built from directory listing if manifest missing (bootstrap for existing backups)
- merged/updated after each backup run via NASTransferProtocol.writeData
BackupStatusSnapshot (new):
- Single source of truth: phoneTotal, alreadySafe, needBackup (derived), nasArchiveTotal,
lastCheckedAt, connectionState
- Invariant enforced in service: alreadySafe = min(safe, phoneTotal)
Protocol / services:
- NASTransferProtocol: adds writeData(_ data: Data, to remotePath: String)
- SMBService: implements writeData via temp file + SMBClient.upload
- SFTPService: implements writeData via SFTP ByteBuffer write
BackupEngine:
- Tracks ManifestEntry per successful upload during backup loop
- After backup: calls BackupStatusService.refreshAfterBackup(entries:connection:)
which applies optimistic UI update then writes manifest + triggers reconcile
BackupView:
- Reads all stats from BackupStatusService.snapshot (not vm/nasFileCount)
- Stats labels: "NAS Archive" / "On iPhone" / "Need Backup" / "Already Safe"
- Live refresh triggers: .task (force), scenePhase.active (force),
nasReachable change (force), remotePath change (force), backup completed (+2s)
- Subtle status row below stats: "Checking…" spinner or "Updated X ago" with
wifi-slash icon when NAS offline; tap refresh button for forced reconcile
- AppMenuView sheet now correctly passes engine EnvironmentObject
BackupViewModel: stripped to auth-only (photosAuthStatus + requestPhotosAccess)
Co-Authored-By: Kutesir <kutesir@provoc.ug>
Co-Authored-By: Sentry <sentry@provoc.ug>
This commit is contained in:
@@ -3,42 +3,9 @@ import Photos
|
||||
|
||||
@MainActor
|
||||
final class BackupViewModel: ObservableObject {
|
||||
@Published var totalPhotoCount: Int = 0
|
||||
@Published var alreadySafeCount: Int = 0
|
||||
@Published var notBackedUpCount: Int = 0
|
||||
@Published var photosAuthStatus: PHAuthorizationStatus = PHPhotoLibrary.authorizationStatus(for: .readWrite)
|
||||
|
||||
private let photoService = PhotoLibraryService()
|
||||
private var fetchedAssets: [PhotoAsset] = []
|
||||
|
||||
func loadPhotoCount(filter: BackupFilter) {
|
||||
let assets = photoService.fetchAssets(filter: filter)
|
||||
fetchedAssets = assets
|
||||
totalPhotoCount = assets.count
|
||||
// Conservative initial state until NAS scan completes
|
||||
alreadySafeCount = 0
|
||||
notBackedUpCount = assets.count
|
||||
}
|
||||
|
||||
// Called after NAS directory listing loads.
|
||||
// Matches by lowercased filename — same heuristic BackupEngine uses for skip checks.
|
||||
// This gives an accurate pre-scan "Already Safe / Not Backed Up" split before
|
||||
// the user ever taps Start Backup.
|
||||
func compareWithNAS(nasItems: [NASItem]) {
|
||||
let nasFilenames = Set(
|
||||
nasItems
|
||||
.filter { !$0.isDirectory }
|
||||
.map { $0.name.lowercased() }
|
||||
)
|
||||
var safe = 0
|
||||
for asset in fetchedAssets {
|
||||
if nasFilenames.contains(asset.filename.lowercased()) {
|
||||
safe += 1
|
||||
}
|
||||
}
|
||||
alreadySafeCount = safe
|
||||
notBackedUpCount = totalPhotoCount - safe
|
||||
}
|
||||
|
||||
func requestPhotosAccess() async {
|
||||
photosAuthStatus = await photoService.requestAuthorization()
|
||||
|
||||
Reference in New Issue
Block a user