mirror of
https://github.com/Ranchero-Software/NetNewsWire.git
synced 2025-02-10 00:50:55 +01:00
Cache home pages with no icon between launches
This commit is contained in:
parent
1e7b71a482
commit
5bcb5a982f
@ -32,7 +32,14 @@ public final class FeedIconDownloader {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private var homePagesWithNoIconURL = Set<String>()
|
private var homePagesWithNoIconURLCache = Set<String>()
|
||||||
|
private var homePagesWithNoIconURLCachePath: String
|
||||||
|
private var homePagesWithNoIconURLCacheDirty = false {
|
||||||
|
didSet {
|
||||||
|
queueHomePagesWithNoIconURLCacheIfNeeded()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private var urlsInProgress = Set<String>()
|
private var urlsInProgress = Set<String>()
|
||||||
private var cache = [Feed: RSImage]()
|
private var cache = [Feed: RSImage]()
|
||||||
private var waitingForFeedURLs = [String: Feed]()
|
private var waitingForFeedURLs = [String: Feed]()
|
||||||
@ -40,7 +47,9 @@ public final class FeedIconDownloader {
|
|||||||
init(imageDownloader: ImageDownloader, folder: String) {
|
init(imageDownloader: ImageDownloader, folder: String) {
|
||||||
self.imageDownloader = imageDownloader
|
self.imageDownloader = imageDownloader
|
||||||
self.homePageToIconURLCachePath = (folder as NSString).appendingPathComponent("HomePageToIconURLCache.plist")
|
self.homePageToIconURLCachePath = (folder as NSString).appendingPathComponent("HomePageToIconURLCache.plist")
|
||||||
|
self.homePagesWithNoIconURLCachePath = (folder as NSString).appendingPathComponent("HomePagesWithNoIconURLCache.plist")
|
||||||
loadHomePageToIconURLCache()
|
loadHomePageToIconURLCache()
|
||||||
|
loadHomePagesWithNoIconURLCache()
|
||||||
NotificationCenter.default.addObserver(self, selector: #selector(imageDidBecomeAvailable(_:)), name: .ImageDidBecomeAvailable, object: imageDownloader)
|
NotificationCenter.default.addObserver(self, selector: #selector(imageDidBecomeAvailable(_:)), name: .ImageDidBecomeAvailable, object: imageDownloader)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -99,13 +108,19 @@ public final class FeedIconDownloader {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@objc func saveHomePagesWithNoIconURLCacheIfNeeded() {
|
||||||
|
if homePagesWithNoIconURLCacheDirty {
|
||||||
|
saveHomePagesWithNoIconURLCache()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private extension FeedIconDownloader {
|
private extension FeedIconDownloader {
|
||||||
|
|
||||||
func icon(forHomePageURL homePageURL: String, feed: Feed, _ imageResultBlock: @escaping (RSImage?) -> Void) {
|
func icon(forHomePageURL homePageURL: String, feed: Feed, _ imageResultBlock: @escaping (RSImage?) -> Void) {
|
||||||
|
|
||||||
if homePagesWithNoIconURL.contains(homePageURL) {
|
if homePagesWithNoIconURLCache.contains(homePageURL) {
|
||||||
imageResultBlock(nil)
|
imageResultBlock(nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -141,7 +156,8 @@ private extension FeedIconDownloader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func cacheIconURL(for homePageURL: String, _ iconURL: String) {
|
func cacheIconURL(for homePageURL: String, _ iconURL: String) {
|
||||||
homePagesWithNoIconURL.remove(homePageURL)
|
homePagesWithNoIconURLCache.remove(homePageURL)
|
||||||
|
homePagesWithNoIconURLCacheDirty = true
|
||||||
homePageToIconURLCache[homePageURL] = iconURL
|
homePageToIconURLCache[homePageURL] = iconURL
|
||||||
homePageToIconURLCacheDirty = true
|
homePageToIconURLCacheDirty = true
|
||||||
}
|
}
|
||||||
@ -172,7 +188,8 @@ private extension FeedIconDownloader {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
homePagesWithNoIconURL.insert(homePageURL)
|
homePagesWithNoIconURLCache.insert(homePageURL)
|
||||||
|
homePagesWithNoIconURLCacheDirty = true
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadHomePageToIconURLCache() {
|
func loadHomePageToIconURLCache() {
|
||||||
@ -184,10 +201,24 @@ private extension FeedIconDownloader {
|
|||||||
homePageToIconURLCache = (try? decoder.decode([String: String].self, from: data)) ?? [String: String]()
|
homePageToIconURLCache = (try? decoder.decode([String: String].self, from: data)) ?? [String: String]()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func loadHomePagesWithNoIconURLCache() {
|
||||||
|
let url = URL(fileURLWithPath: homePagesWithNoIconURLCachePath)
|
||||||
|
guard let data = try? Data(contentsOf: url) else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
let decoder = PropertyListDecoder()
|
||||||
|
let decoded = (try? decoder.decode([String].self, from: data)) ?? [String]()
|
||||||
|
homePagesWithNoIconURLCache = Set(decoded)
|
||||||
|
}
|
||||||
|
|
||||||
func queueSaveHomePageToIconURLCacheIfNeeded() {
|
func queueSaveHomePageToIconURLCacheIfNeeded() {
|
||||||
FeedIconDownloader.saveQueue.add(self, #selector(saveHomePageToIconURLCacheIfNeeded))
|
FeedIconDownloader.saveQueue.add(self, #selector(saveHomePageToIconURLCacheIfNeeded))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func queueHomePagesWithNoIconURLCacheIfNeeded() {
|
||||||
|
FeedIconDownloader.saveQueue.add(self, #selector(saveHomePagesWithNoIconURLCacheIfNeeded))
|
||||||
|
}
|
||||||
|
|
||||||
func saveHomePageToIconURLCache() {
|
func saveHomePageToIconURLCache() {
|
||||||
homePageToIconURLCacheDirty = false
|
homePageToIconURLCacheDirty = false
|
||||||
|
|
||||||
@ -202,4 +233,18 @@ private extension FeedIconDownloader {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func saveHomePagesWithNoIconURLCache() {
|
||||||
|
homePagesWithNoIconURLCacheDirty = false
|
||||||
|
|
||||||
|
let encoder = PropertyListEncoder()
|
||||||
|
encoder.outputFormat = .binary
|
||||||
|
let url = URL(fileURLWithPath: homePagesWithNoIconURLCachePath)
|
||||||
|
do {
|
||||||
|
let data = try encoder.encode(Array(homePagesWithNoIconURLCache))
|
||||||
|
try data.write(to: url)
|
||||||
|
} catch {
|
||||||
|
assertionFailure(error.localizedDescription)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -200,12 +200,13 @@ private extension AppDelegate {
|
|||||||
let faviconsFolderURL = tempDir.appendingPathComponent("Favicons")
|
let faviconsFolderURL = tempDir.appendingPathComponent("Favicons")
|
||||||
let imagesFolderURL = tempDir.appendingPathComponent("Images")
|
let imagesFolderURL = tempDir.appendingPathComponent("Images")
|
||||||
let homePageToIconURL = tempDir.appendingPathComponent("HomePageToIconURLCache.plist")
|
let homePageToIconURL = tempDir.appendingPathComponent("HomePageToIconURLCache.plist")
|
||||||
|
let homePagesWithNoIconURL = tempDir.appendingPathComponent("HomePagesWithNoIconURLCache.plist")
|
||||||
|
|
||||||
// If the image disk cache hasn't been flushed for 3 days and the network is available, delete it
|
// 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 flushDate = AppDefaults.lastImageCacheFlushDate, flushDate.addingTimeInterval(3600*24*3) < Date() {
|
||||||
if let reachability = try? Reachability(hostname: "apple.com") {
|
if let reachability = try? Reachability(hostname: "apple.com") {
|
||||||
if reachability.connection != .unavailable {
|
if reachability.connection != .unavailable {
|
||||||
for tempItem in [faviconsFolderURL, imagesFolderURL, homePageToIconURL] {
|
for tempItem in [faviconsFolderURL, imagesFolderURL, homePageToIconURL, homePagesWithNoIconURL] {
|
||||||
do {
|
do {
|
||||||
os_log(.info, log: self.log, "Removing cache file: %@", tempItem.absoluteString)
|
os_log(.info, log: self.log, "Removing cache file: %@", tempItem.absoluteString)
|
||||||
try FileManager.default.removeItem(at: tempItem)
|
try FileManager.default.removeItem(at: tempItem)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user