diff --git a/Account/Sources/Account/AccountManager.swift b/Account/Sources/Account/AccountManager.swift index 1ec8b3841..21b19b5de 100644 --- a/Account/Sources/Account/AccountManager.swift +++ b/Account/Sources/Account/AccountManager.swift @@ -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 } diff --git a/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift b/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift index 67a6dcd36..b0c0531d5 100644 --- a/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift +++ b/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift @@ -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 } diff --git a/Shared/Extensions/CacheCleaner.swift b/Shared/Extensions/CacheCleaner.swift index 47b609883..483b4358e 100644 --- a/Shared/Extensions/CacheCleaner.swift +++ b/Shared/Extensions/CacheCleaner.swift @@ -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() } } - } - }