diff --git a/iOS/AppDefaults.swift b/iOS/AppDefaults.swift index 98c59261a..be4de2a08 100644 --- a/iOS/AppDefaults.swift +++ b/iOS/AppDefaults.swift @@ -11,6 +11,7 @@ import UIKit struct AppDefaults { struct Key { + static let lastImageCacheFlushDate = "lastImageCacheFlushDate" static let firstRunDate = "firstRunDate" static let timelineGroupByFeed = "timelineGroupByFeed" static let timelineNumberOfLines = "timelineNumberOfLines" @@ -27,6 +28,15 @@ struct AppDefaults { return true }() + static var lastImageCacheFlushDate: Date? { + get { + return date(for: Key.lastImageCacheFlushDate) + } + set { + setDate(for: Key.lastImageCacheFlushDate, newValue) + } + } + static var refreshInterval: RefreshInterval { get { let rawValue = UserDefaults.standard.integer(forKey: Key.refreshInterval) @@ -74,7 +84,8 @@ struct AppDefaults { } static func registerDefaults() { - let defaults: [String : Any] = [Key.refreshInterval: RefreshInterval.everyHour.rawValue, + let defaults: [String : Any] = [Key.lastImageCacheFlushDate: Date(), + Key.refreshInterval: RefreshInterval.everyHour.rawValue, Key.timelineGroupByFeed: false, Key.timelineNumberOfLines: 3, Key.timelineSortDirection: ComparisonResult.orderedDescending.rawValue] diff --git a/iOS/AppDelegate.swift b/iOS/AppDelegate.swift index d74176d9d..912675902 100644 --- a/iOS/AppDelegate.swift +++ b/iOS/AppDelegate.swift @@ -178,12 +178,31 @@ private extension AppDelegate { private func initializeDownloaders() { let tempDir = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first! let faviconsFolderURL = tempDir.appendingPathComponent("Favicons") + let imagesFolderURL = tempDir.appendingPathComponent("Images") + let homePageToIconURL = tempDir.appendingPathComponent("HomePageToIconURLCache.plist") + + // If the image disk cache hasn't been flushed for 3 days and the network is available, delete it + if let flushDate = AppDefaults.lastImageCacheFlushDate, flushDate.addingTimeInterval(3600*24*3) < Date() { + if let reachability = try? Reachability(hostname: "apple.com") { + if reachability.connection != .unavailable { + for tempItem in [faviconsFolderURL, imagesFolderURL, homePageToIconURL] { + 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.lastImageCacheFlushDate = Date() + } + } + } + try! FileManager.default.createDirectory(at: faviconsFolderURL, withIntermediateDirectories: true, attributes: nil) let faviconsFolder = faviconsFolderURL.absoluteString let faviconsFolderPath = faviconsFolder.suffix(from: faviconsFolder.index(faviconsFolder.startIndex, offsetBy: 7)) faviconDownloader = FaviconDownloader(folder: String(faviconsFolderPath)) - let imagesFolderURL = tempDir.appendingPathComponent("Images") let imagesFolder = imagesFolderURL.absoluteString let imagesFolderPath = imagesFolder.suffix(from: imagesFolder.index(imagesFolder.startIndex, offsetBy: 7)) try! FileManager.default.createDirectory(at: imagesFolderURL, withIntermediateDirectories: true, attributes: nil)