From 95b215c6b509bf2b264bb7c118d7e0da28b93ad3 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sat, 12 Sep 2020 10:49:08 -0400 Subject: [PATCH] Add Clear Image Cache option to Advanced prefs --- Tusker/Caching/Cache.swift | 11 +++++++++ Tusker/Caching/ImageCache.swift | 6 ++++- .../Preferences/AdvancedPrefsView.swift | 24 ++++++++++++++++--- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/Tusker/Caching/Cache.swift b/Tusker/Caching/Cache.swift index 9f0ab5b1f3..efa0c02c70 100644 --- a/Tusker/Caching/Cache.swift +++ b/Tusker/Caching/Cache.swift @@ -47,4 +47,15 @@ enum Cache { try hybrid.setObject(object, forKey: key, expiry: expiry) } } + + func removeAll() throws { + switch self { + case let .memory(memory): + memory.removeAll() + case let .disk(disk): + try disk.removeAll() + case let .hybrid(hybrid): + try hybrid.removeAll() + } + } } diff --git a/Tusker/Caching/ImageCache.swift b/Tusker/Caching/ImageCache.swift index 7681ab6e13..fcd4052c9c 100644 --- a/Tusker/Caching/ImageCache.swift +++ b/Tusker/Caching/ImageCache.swift @@ -16,7 +16,7 @@ class ImageCache { static let attachments = ImageCache(name: "Attachments", memoryExpiry: .seconds(60 * 2)) static let emojis = ImageCache(name: "Emojis", memoryExpiry: .seconds(60 * 5), diskExpiry: .seconds(60 * 60)) - let cache: Cache + private let cache: Cache private var groups = [URL: RequestGroup]() @@ -68,6 +68,10 @@ class ImageCache { groups[url]?.cancelWithoutCallback() } + func reset() throws { + try cache.removeAll() + } + private class RequestGroup { let url: URL private let onFinished: (Data?) -> Void diff --git a/Tusker/Screens/Preferences/AdvancedPrefsView.swift b/Tusker/Screens/Preferences/AdvancedPrefsView.swift index cd9fde5618..78cd39f34a 100644 --- a/Tusker/Screens/Preferences/AdvancedPrefsView.swift +++ b/Tusker/Screens/Preferences/AdvancedPrefsView.swift @@ -46,12 +46,15 @@ struct AdvancedPrefsView : View { var cachingSection: some View { Section(header: Text("Caching")) { Button(action: clearCache) { - Text("Clear Cache") + Text("Clear Mastodon Cache") + }.foregroundColor(.red) + Button(action: clearImageCaches) { + Text("Clear Image Caches") }.foregroundColor(.red) } } - func clearCache() { + private func clearCache() { for account in LocalData.shared.accounts { let controller = MastodonController.getForAccount(account) let coordinator = controller.persistentContainer.persistentStoreCoordinator @@ -59,7 +62,22 @@ struct AdvancedPrefsView : View { try! coordinator.destroyPersistentStore(at: store.url!, ofType: store.type, options: store.options) } } - MastodonController.resetAll() + resetUI() + } + + private func clearImageCaches() { + [ + ImageCache.avatars, + ImageCache.headers, + ImageCache.attachments, + ImageCache.emojis, + ].forEach { + try! $0.reset() + } + resetUI() + } + + private func resetUI() { let mostRecent = LocalData.shared.getMostRecentAccount()! NotificationCenter.default.post(name: .activateAccount, object: nil, userInfo: ["account": mostRecent]) }