Fix backup loop, NAS Archive regression, and manifest validity check
- AutoBackupCoordinator: add 55s debounce to onActive() so repeated .task and scenePhase triggers (navigation appear, rapid foreground cycles) are no-ops; start a 60s periodic recheck timer on first active call; stop timer and reset debounce on willResignActiveNotification so each new foreground session always gets an immediate fresh status check - BackupView: remove the redundant lanMonitor.nasReachable onChange that was calling statusService.refresh() directly — coordinator already handles this via its Combine $nasReachable subscriber, avoiding a double reconcile that could bypass the autoBackupOnOpen gate with the wrong lastTriggerWasLAN value - BackupStatusService: when NAS connect fails, restore nasArchiveTotal from NASManifestCache before writing the offline snapshot — prevents the count from showing 0 when the NAS is temporarily unreachable but the cache is warm - BackupStatusService: manifest validity check — if the decoded manifest has ≤ 1 entries (corrupt or newly created), scan the NAS directory and use the real file count as the display floor for nasArchiveTotal without adding orphan entries to the manifest (that was the 7421 regression) - BackupManifest: add ManifestIndex.init(manifest:overrideTotalCount:) for the validity check path — keeps localIdentifier matching intact while correcting the displayed archive count - SMBService: add os.log around connect/auth/listDirectory with actual error reason instead of always surfacing authenticationFailed; distinguish network errors (timeout, host unreachable) from auth errors in thrown BackupError Co-Authored-By: Kutesir <kutesir@provoc.ug> Co-Authored-By: Sentry <sentry@provoc.ug>
This commit is contained in:
@@ -30,6 +30,8 @@ final class AutoBackupCoordinator: ObservableObject {
|
||||
@Published private(set) var pendingAtDisconnect: Int = 0
|
||||
|
||||
private var lastTriggerWasLAN = false
|
||||
private var lastAutoCheckAt: Date = .distantPast
|
||||
private var recheckTimer: AnyCancellable?
|
||||
|
||||
private let engine = BackupEngine.shared
|
||||
private let statusService = BackupStatusService.shared
|
||||
@@ -42,6 +44,36 @@ final class AutoBackupCoordinator: ObservableObject {
|
||||
|
||||
private init() {
|
||||
setupObservation()
|
||||
setupAppLifecycle()
|
||||
}
|
||||
|
||||
private func setupAppLifecycle() {
|
||||
NotificationCenter.default.addObserver(
|
||||
self,
|
||||
selector: #selector(appWillResignActive),
|
||||
name: UIApplication.willResignActiveNotification,
|
||||
object: nil
|
||||
)
|
||||
}
|
||||
|
||||
// Stop the timer and reset the debounce window on every background transition.
|
||||
// This ensures the next foreground session always gets an immediate status check.
|
||||
@objc private nonisolated func appWillResignActive() {
|
||||
Task { @MainActor [weak self] in
|
||||
self?.recheckTimer = nil
|
||||
self?.lastAutoCheckAt = .distantPast
|
||||
log.debug("App resigned — recheck timer stopped, debounce reset")
|
||||
}
|
||||
}
|
||||
|
||||
private func startPeriodicRecheckIfNeeded() {
|
||||
guard recheckTimer == nil else { return }
|
||||
recheckTimer = Timer.publish(every: 60, on: .main, in: .common)
|
||||
.autoconnect()
|
||||
.sink { [weak self] _ in
|
||||
log.debug("Periodic 60s recheck fired")
|
||||
self?.onActive()
|
||||
}
|
||||
}
|
||||
|
||||
private func setupObservation() {
|
||||
@@ -69,13 +101,28 @@ final class AutoBackupCoordinator: ObservableObject {
|
||||
// MARK: — External triggers
|
||||
|
||||
/// Call when the app becomes active: on first launch, foreground resume, or session unlock.
|
||||
/// Safe to call repeatedly — guards prevent redundant reconciliation.
|
||||
/// Safe to call repeatedly — debounce prevents redundant reconciliation within 55s.
|
||||
func onActive(lanTriggered: Bool = false) {
|
||||
lastTriggerWasLAN = lanTriggered
|
||||
guard !engine.job.status.isActive else { return }
|
||||
guard store.savedConnection != nil else { return }
|
||||
guard phase == .idle else { return } // Don't interrupt an in-progress check
|
||||
guard phase == .idle else { return }
|
||||
|
||||
// Non-LAN triggers are debounced: .task {} fires on every navigation appear,
|
||||
// and scenePhase fires on every foreground. Only allow one check per 55 seconds
|
||||
// so the user can background+foreground without restarting a new backup job.
|
||||
if !lanTriggered {
|
||||
let elapsed = Date().timeIntervalSince(lastAutoCheckAt)
|
||||
guard elapsed > 55 else {
|
||||
log.debug("onActive debounced — \(Int(elapsed))s since last check (need >55s)")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
lastAutoCheckAt = Date()
|
||||
phase = .checking
|
||||
startPeriodicRecheckIfNeeded()
|
||||
log.info("onActive: trigger=\(lanTriggered ? "LAN" : "appOpen") — status check starting")
|
||||
statusService.refresh(force: false)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user