perf: eliminate full rescans with local caches; add queue tracking, notifications, SyncView redesign
Cached indexes: - NASManifestCache: actor-based disk cache for the NAS manifest (kisani_manifest_cache.json) Avoids NAS network round-trip when cache < 5 min old. Updated after every manifest download. - LocalPhotoIndex: actor-based disk cache of phone asset metadata (kisani_local_index.json) Built once on first launch, then updated incrementally via PHPhotoLibraryChangeObserver. O(1) countSafe via in-memory dict (vs PHFetchResult batch enumeration + CoreData calls). Fast path reconcile: When both caches are warm, reconcile() skips NAS connection + PHFetchResult enumeration entirely and returns in microseconds. Falls through to full NAS path only when stale. AutoBackupCoordinator.onActive(): Changed force: true → force: false so the 30-second debounce prevents expensive rescans on rapid app foreground/background cycles. Dashboard appears instantly from cached snapshot. BackupQueueItem + queue tracking: BackupEngine publishes queueItems: [BackupQueueItem] with per-file status, progress, speed, error details. Queue is persisted to disk and reloaded on next launch so SyncView always has data to show even after restart. NotificationService: Centralised local notification sender replacing the inline UNMutableNotificationContent blocks. Sends for: backup started, completed, failed, paused, NAS offline. Uses category identifiers so same-type notifications replace each other. SyncView redesign: - Removed pill/chip around NAS IP — replaced with plain "Online/Offline/Checking" dot+text - No live NAS directory listing (was expensive NAS connection on every tab open) - Shows engine.queueItems grouped into Active / Failed / Completed sections - Per-file rows with inline progress bar + bytes/speed for uploading items, error text for failed - Idle state shows archive count from statusService snapshot GalleryViewModel: NAS tab and All tab now use NASManifestCache as fallback when statusService.lastManifest is nil (offline launch). Gallery shows NAS items even when NAS is unreachable. AppDelegate: Bootstraps LocalPhotoIndex.loadOrBuild() in background on launch (non-blocking). Co-Authored-By: Kutesir <kutesir@provoc.ug> Co-Authored-By: Sentry <sentry@provoc.ug>
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import Foundation
|
||||
import UserNotifications
|
||||
import Photos
|
||||
import os.log
|
||||
|
||||
@@ -11,12 +10,18 @@ final class BackupEngine: ObservableObject {
|
||||
static let shared = BackupEngine()
|
||||
|
||||
@Published private(set) var job: BackupJob = BackupJob()
|
||||
@Published private(set) var queueItems: [BackupQueueItem] = []
|
||||
|
||||
private let photoService: PhotoLibraryProtocol
|
||||
private let transferFactory: (NASProtocol) -> any NASTransferProtocol
|
||||
private var activeTransfer: (any NASTransferProtocol)?
|
||||
private var isCancelled = false
|
||||
|
||||
private static let queueCacheURL: URL = {
|
||||
let caches = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0]
|
||||
return caches.appendingPathComponent("kisani_last_queue.json")
|
||||
}()
|
||||
|
||||
private init(
|
||||
photos: PhotoLibraryProtocol = PhotoLibraryService(),
|
||||
transferFactory: @escaping (NASProtocol) -> any NASTransferProtocol = { proto in
|
||||
@@ -28,6 +33,11 @@ final class BackupEngine: ObservableObject {
|
||||
) {
|
||||
self.photoService = photos
|
||||
self.transferFactory = transferFactory
|
||||
// Load last session's queue so SyncView has data to show on next launch.
|
||||
if let data = try? Data(contentsOf: Self.queueCacheURL),
|
||||
let items = try? JSONDecoder.kisani.decode([BackupQueueItem].self, from: data) {
|
||||
self.queueItems = items
|
||||
}
|
||||
}
|
||||
|
||||
static func testInstance(
|
||||
@@ -121,10 +131,19 @@ final class BackupEngine: ObservableObject {
|
||||
}
|
||||
|
||||
// ── 5. Upload loop ─────────────────────────────────────────────────
|
||||
// The main actor is released at every `await`. Progress UI is throttled
|
||||
// to 8 Hz — without throttling, one Task { @MainActor } is fired per
|
||||
// progress callback which causes continuous SwiftUI full-tree re-renders.
|
||||
job.start(totalFiles: pendingAssets.count, totalBytes: 0)
|
||||
NotificationService.notifyBackupStarted(count: pendingAssets.count)
|
||||
|
||||
// Build initial queue (all queued) for SyncView
|
||||
queueItems = pendingAssets.map { asset in
|
||||
BackupQueueItem(
|
||||
id: asset.localIdentifier,
|
||||
filename: asset.filename,
|
||||
nasPath: "\(connection.remotePath)/\(asset.filename)",
|
||||
status: .queued, bytesUploaded: 0, totalBytes: 0, speedBytesPerSec: 0,
|
||||
error: nil, retryCount: 0, queuedAt: Date(), completedAt: nil
|
||||
)
|
||||
}
|
||||
|
||||
var uploaded = 0
|
||||
var skipped = 0
|
||||
@@ -137,7 +156,7 @@ final class BackupEngine: ObservableObject {
|
||||
|
||||
let spUpload = signposter.beginInterval("UploadLoop", "\(pendingAssets.count) files")
|
||||
|
||||
for asset in pendingAssets {
|
||||
for (assetIdx, asset) in pendingAssets.enumerated() {
|
||||
if isCancelled { break }
|
||||
while case .paused = job.status {
|
||||
try await Task.sleep(nanoseconds: 500_000_000)
|
||||
@@ -145,9 +164,18 @@ final class BackupEngine: ObservableObject {
|
||||
|
||||
let remotePath = "\(connection.remotePath)/\(asset.filename)"
|
||||
|
||||
// Mark as uploading
|
||||
if assetIdx < queueItems.count {
|
||||
queueItems[assetIdx].status = .uploading
|
||||
}
|
||||
|
||||
if (try? await transfer.fileExists(at: remotePath)) == true {
|
||||
job.fileCompleted(skipped: true)
|
||||
skipped += 1
|
||||
if assetIdx < queueItems.count {
|
||||
queueItems[assetIdx].status = .skipped
|
||||
queueItems[assetIdx].completedAt = Date()
|
||||
}
|
||||
logger.debug("Skipped (NAS exists): \(asset.filename, privacy: .public)")
|
||||
continue
|
||||
}
|
||||
@@ -160,20 +188,28 @@ final class BackupEngine: ObservableObject {
|
||||
|
||||
let fileSize = (try? localURL.resourceValues(forKeys: [.fileSizeKey]).fileSize)
|
||||
.flatMap { Int64($0) } ?? 0
|
||||
// Capture totalBytes by value so the progress closure doesn't capture
|
||||
// the mutable var — avoids a data race warning and is semantically correct.
|
||||
let bytesSoFar = totalBytes
|
||||
let capturedIdx = assetIdx
|
||||
|
||||
if capturedIdx < queueItems.count {
|
||||
queueItems[capturedIdx].totalBytes = fileSize
|
||||
}
|
||||
|
||||
try await transfer.upload(localURL: localURL, remotePath: remotePath) { sent, _ in
|
||||
let speed = speedTracker.update(bytesSent: sent)
|
||||
guard throttle.shouldUpdate() else { return }
|
||||
Task { @MainActor [weak self] in
|
||||
self?.job.updateProgress(
|
||||
guard let self else { return }
|
||||
self.job.updateProgress(
|
||||
fileName: asset.filename,
|
||||
fileSize: fileSize,
|
||||
bytesTransferred: bytesSoFar + sent,
|
||||
speed: speed
|
||||
)
|
||||
if capturedIdx < self.queueItems.count {
|
||||
self.queueItems[capturedIdx].bytesUploaded = sent
|
||||
self.queueItems[capturedIdx].speedBytesPerSec = speed
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -182,6 +218,12 @@ final class BackupEngine: ObservableObject {
|
||||
job.fileCompleted(skipped: false)
|
||||
uploaded += 1
|
||||
|
||||
if assetIdx < queueItems.count {
|
||||
queueItems[assetIdx].status = .uploaded
|
||||
queueItems[assetIdx].bytesUploaded = fileSize
|
||||
queueItems[assetIdx].completedAt = Date()
|
||||
}
|
||||
|
||||
manifestEntries.append(ManifestEntry(
|
||||
localIdentifier: asset.localIdentifier,
|
||||
filename: asset.filename,
|
||||
@@ -194,6 +236,15 @@ final class BackupEngine: ObservableObject {
|
||||
logger.error("Upload failed for \(asset.filename, privacy: .public): \(error.localizedDescription, privacy: .public)")
|
||||
job.fileFailed()
|
||||
failed += 1
|
||||
if assetIdx < queueItems.count {
|
||||
queueItems[assetIdx].status = .failed
|
||||
queueItems[assetIdx].completedAt = Date()
|
||||
queueItems[assetIdx].error = BackupQueueItem.UploadError(
|
||||
code: (error as NSError).domain,
|
||||
message: error.localizedDescription,
|
||||
timestamp: Date()
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -214,7 +265,23 @@ final class BackupEngine: ObservableObject {
|
||||
|
||||
let entry = BackupHistoryEntry(result: result, nasHost: host, triggeredByLAN: triggeredByLAN)
|
||||
store.appendHistoryEntry(entry)
|
||||
await sendCompletionNotification(result: result)
|
||||
|
||||
// Notifications
|
||||
if result.failedCount > 0 {
|
||||
NotificationService.notifyBackupFailed(count: result.failedCount)
|
||||
} else {
|
||||
NotificationService.notifyBackupCompleted(
|
||||
uploaded: result.uploadedCount,
|
||||
total: BackupStatusService.shared.snapshot.phoneTotal
|
||||
)
|
||||
}
|
||||
|
||||
// Persist queue so SyncView shows last session on next launch
|
||||
let snapshot = queueItems
|
||||
Task.detached(priority: .utility) { [snapshot, url = Self.queueCacheURL] in
|
||||
guard let data = try? JSONEncoder.kisani.encode(snapshot) else { return }
|
||||
try? data.write(to: url, options: .atomic)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
@@ -235,14 +302,6 @@ final class BackupEngine: ObservableObject {
|
||||
|
||||
func resolveSuccess() { job.resolveSuccess() }
|
||||
|
||||
private func sendCompletionNotification(result: BackupResult) async {
|
||||
let content = UNMutableNotificationContent()
|
||||
content.title = result.hasErrors ? "Backup completed with errors" : "Backup complete"
|
||||
content.body = "\(result.uploadedCount) uploaded · \(result.skippedCount) skipped · \(result.failedCount) errors"
|
||||
content.sound = .default
|
||||
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: nil)
|
||||
try? await UNUserNotificationCenter.current().add(request)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: — SpeedTracker
|
||||
|
||||
Reference in New Issue
Block a user