diff --git a/Tusker/Caching/Cache.swift b/Tusker/Caching/Cache.swift index 9f0ab5b1..efa0c02c 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 7681ab6e..fcd4052c 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 cd9fde56..78cd39f3 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]) }