forked from shadowfacts/Tusker
Add Clear Image Cache option to Advanced prefs
This commit is contained in:
parent
e21dceb3b3
commit
95b215c6b5
|
@ -47,4 +47,15 @@ enum Cache<T> {
|
|||
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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<Data>
|
||||
private let cache: Cache<Data>
|
||||
|
||||
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
|
||||
|
|
|
@ -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])
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue