Use new Reachability.internetIsReachable.

This commit is contained in:
Brent Simmons 2024-04-06 19:02:11 -07:00
parent 46645f700a
commit 3b59ffc446
3 changed files with 18 additions and 38 deletions

View File

@ -221,18 +221,9 @@ import Secrets
}
}
private func internetIsReachable() -> Bool {
guard let reachability = try? Reachability(hostname: "apple.com"), reachability.connection != .unavailable else {
return false
}
return true
}
public func refreshAll(errorHandler: ((Error) -> Void)? = nil) async {
guard internetIsReachable() else {
guard Reachability.internetIsReachable else {
return
}

View File

@ -90,13 +90,7 @@ enum CloudKitAccountDelegateError: LocalizedError {
func refreshAll(for account: Account) async throws {
guard refreshProgress.isComplete else {
return
}
let reachability = SCNetworkReachabilityCreateWithName(nil, "apple.com")
var flags = SCNetworkReachabilityFlags()
guard SCNetworkReachabilityGetFlags(reachability!, &flags), flags.contains(.reachable) else {
guard refreshProgress.isComplete, Reachability.internetIsReachable else {
return
}

View File

@ -20,34 +20,29 @@ struct CacheCleaner {
AppDefaults.shared.lastImageCacheFlushDate = Date()
return
}
// If the image disk cache hasn't been flushed for 3 days and the network is available, delete it
if flushDate.addingTimeInterval(3600 * 24 * 3) < Date() {
if let reachability = try? Reachability(hostname: "apple.com") {
if reachability.connection != .unavailable {
if Reachability.internetIsReachable {
let tempDir = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!
let faviconsFolderURL = tempDir.appendingPathComponent("Favicons")
let imagesFolderURL = tempDir.appendingPathComponent("Images")
let feedURLToIconURL = tempDir.appendingPathComponent("FeedURLToIconURLCache.plist")
let homePageToIconURL = tempDir.appendingPathComponent("HomePageToIconURLCache.plist")
let homePagesWithNoIconURL = tempDir.appendingPathComponent("HomePagesWithNoIconURLCache.plist")
let tempDir = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!
let faviconsFolderURL = tempDir.appendingPathComponent("Favicons")
let imagesFolderURL = tempDir.appendingPathComponent("Images")
let feedURLToIconURL = tempDir.appendingPathComponent("FeedURLToIconURLCache.plist")
let homePageToIconURL = tempDir.appendingPathComponent("HomePageToIconURLCache.plist")
let homePagesWithNoIconURL = tempDir.appendingPathComponent("HomePagesWithNoIconURLCache.plist")
for tempItem in [faviconsFolderURL, imagesFolderURL, feedURLToIconURL, homePageToIconURL, homePagesWithNoIconURL] {
do {
os_log(.info, log: self.log, "Removing cache file: %@", tempItem.absoluteString)
try FileManager.default.removeItem(at: tempItem)
} catch {
os_log(.error, log: self.log, "Could not delete cache file: %@", error.localizedDescription)
}
for tempItem in [faviconsFolderURL, imagesFolderURL, feedURLToIconURL, homePageToIconURL, homePagesWithNoIconURL] {
do {
os_log(.info, log: self.log, "Removing cache file: %@", tempItem.absoluteString)
try FileManager.default.removeItem(at: tempItem)
} catch {
os_log(.error, log: self.log, "Could not delete cache file: %@", error.localizedDescription)
}
AppDefaults.shared.lastImageCacheFlushDate = Date()
}
AppDefaults.shared.lastImageCacheFlushDate = Date()
}
}
}
}