Files
Kisani/App/NASBackupApp.swift
Robin Kutesa 712e110f57 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>
2026-05-17 12:51:03 +03:00

52 lines
1.8 KiB
Swift

import SwiftUI
import Photos
@main
struct NASBackupApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
@StateObject private var store = ConnectionStore.shared
@StateObject private var engine = BackupEngine.shared
@StateObject private var lanMonitor = LANMonitor.shared
@StateObject private var statusService = BackupStatusService.shared
var body: some Scene {
WindowGroup {
RootView()
.environmentObject(store)
.environmentObject(engine)
.environmentObject(lanMonitor)
.environmentObject(statusService)
.preferredColorScheme(store.appearanceMode.resolvedScheme)
}
}
}
struct RootView: View {
@EnvironmentObject var store: ConnectionStore
@EnvironmentObject var lanMonitor: LANMonitor
var body: some View {
Group {
if store.savedConnection == nil {
ConnectView()
} else if !store.isSessionActive {
LoginView()
} else if store.savedConnection?.remotePath == "/" {
FolderSetupView()
} else if !store.onboardingPhotoShown {
PhotoPermissionView()
} else {
MainTabView()
}
}
.animation(.spring(response: 0.4, dampingFraction: 0.85), value: store.isSessionActive)
.animation(.spring(response: 0.4, dampingFraction: 0.85), value: store.savedConnection?.remotePath)
.animation(.spring(response: 0.4, dampingFraction: 0.85), value: store.onboardingPhotoShown)
.task(id: store.isSessionActive) {
guard store.isSessionActive, let conn = store.savedConnection else { return }
await lanMonitor.checkNASReachability(host: conn.host, port: conn.port)
}
}
}