NetNewsWire/Shared/Timer/AccountRefreshTimer.swift
Stuart Breckenridge 673f0ce718
Handles single and multiple sync failures
If a single sync failure is encountered a sheet is presented which allows the user to update their credentials.

If multiple sync failures are encountered an alert is shown listing the accounts which encountered errors. On iOS, this alert can take the user into Settings, but there is no obvious way to programatically pesent macOS preferences.
2020-07-25 16:40:04 +08:00

81 lines
1.7 KiB
Swift

//
// RefreshTimer.swift
// NetNewsWire
//
// Created by Maurice Parker on 4/23/19.
// Copyright © 2019 Ranchero Software. All rights reserved.
//
import Foundation
import Account
class AccountRefreshTimer {
var shuttingDown = false
private var internalTimer: Timer?
private var lastTimedRefresh: Date?
private let launchTime = Date()
func fireOldTimer() {
if let timer = internalTimer {
if timer.fireDate < Date() {
if AppDefaults.shared.refreshInterval != .manually {
timedRefresh(nil)
}
}
}
}
func invalidate() {
guard let timer = internalTimer else {
return
}
if timer.isValid {
timer.invalidate()
}
internalTimer = nil
}
func update() {
guard !shuttingDown else {
return
}
let refreshInterval = AppDefaults.shared.refreshInterval
if refreshInterval == .manually {
invalidate()
return
}
let lastRefreshDate = lastTimedRefresh ?? launchTime
let secondsToAdd = refreshInterval.inSeconds()
var nextRefreshTime = lastRefreshDate.addingTimeInterval(secondsToAdd)
if nextRefreshTime < Date() {
nextRefreshTime = Date().addingTimeInterval(secondsToAdd)
}
if let currentNextFireDate = internalTimer?.fireDate, currentNextFireDate == nextRefreshTime {
return
}
invalidate()
let timer = Timer(fireAt: nextRefreshTime, interval: 0, target: self, selector: #selector(timedRefresh(_:)), userInfo: nil, repeats: false)
RunLoop.main.add(timer, forMode: .common)
internalTimer = timer
}
@objc func timedRefresh(_ sender: Timer?) {
guard !shuttingDown else {
return
}
lastTimedRefresh = Date()
update()
//AccountManager.shared.refreshAll(errorHandler: ErrorHandler.log)
AccountManager.shared.refreshAll(completion: nil)
}
}