From 35a510e8ed31251d9d0d0003bbb42771f5cefe6f Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Wed, 13 May 2020 18:58:11 -0400 Subject: [PATCH] Add cache reset button to Advanced Preferences --- Tusker/Controllers/MastodonController.swift | 4 ++++ .../ConversationTableViewController.swift | 3 ++- .../Preferences/AdvancedPrefsView.swift | 22 +++++++++++++++++++ .../Profile/ProfileTableViewController.swift | 3 +-- ...ActionAccountListTableViewController.swift | 3 +-- .../TimelineTableViewController.swift | 3 ++- 6 files changed, 32 insertions(+), 6 deletions(-) diff --git a/Tusker/Controllers/MastodonController.swift b/Tusker/Controllers/MastodonController.swift index a75a0ecc..31dd97b8 100644 --- a/Tusker/Controllers/MastodonController.swift +++ b/Tusker/Controllers/MastodonController.swift @@ -30,6 +30,10 @@ class MastodonController { } } + static func resetAll() { + all = [:] + } + private let transient: Bool private(set) lazy var persistentContainer = MastodonCachePersistentStore(for: accountInfo, transient: transient) diff --git a/Tusker/Screens/Conversation/ConversationTableViewController.swift b/Tusker/Screens/Conversation/ConversationTableViewController.swift index 85ab426f..1712423a 100644 --- a/Tusker/Screens/Conversation/ConversationTableViewController.swift +++ b/Tusker/Screens/Conversation/ConversationTableViewController.swift @@ -43,8 +43,9 @@ class ConversationTableViewController: EnhancedTableViewController { } deinit { + guard let persistentContainer = mastodonController?.persistentContainer else { return } for (id, _) in statuses { - mastodonController.persistentContainer.status(for: id)?.decrementReferenceCount() + persistentContainer.status(for: id)?.decrementReferenceCount() } } diff --git a/Tusker/Screens/Preferences/AdvancedPrefsView.swift b/Tusker/Screens/Preferences/AdvancedPrefsView.swift index fc82c27b..5ab1d6c1 100644 --- a/Tusker/Screens/Preferences/AdvancedPrefsView.swift +++ b/Tusker/Screens/Preferences/AdvancedPrefsView.swift @@ -15,6 +15,7 @@ struct AdvancedPrefsView : View { List { formattingSection automationSection + cachingSection }.listStyle(GroupedListStyle()) .navigationBarTitle(Text("Advanced")) } @@ -41,6 +42,27 @@ struct AdvancedPrefsView : View { } } } + + var cachingSection: some View { + Section(header: Text("CACHING")) { + Button(action: clearCache) { + Text("Clear Cache") + }.foregroundColor(.red) + } + } + + func clearCache() { + for account in LocalData.shared.accounts { + let controller = MastodonController.getForAccount(account) + let coordinator = controller.persistentContainer.persistentStoreCoordinator + for store in coordinator.persistentStores { + try! coordinator.destroyPersistentStore(at: store.url!, ofType: store.type, options: store.options) + } + } + MastodonController.resetAll() + let mostRecent = LocalData.shared.getMostRecentAccount()! + NotificationCenter.default.post(name: .activateAccount, object: nil, userInfo: ["account": mostRecent]) + } } extension StatusContentType { diff --git a/Tusker/Screens/Profile/ProfileTableViewController.swift b/Tusker/Screens/Profile/ProfileTableViewController.swift index b5e76b13..85890d16 100644 --- a/Tusker/Screens/Profile/ProfileTableViewController.swift +++ b/Tusker/Screens/Profile/ProfileTableViewController.swift @@ -62,8 +62,7 @@ class ProfileTableViewController: EnhancedTableViewController { } deinit { - if let id = accountID { - let container = mastodonController.persistentContainer + if let id = accountID, let container = mastodonController?.persistentContainer { container.backgroundContext.perform { container.account(for: id, in: container.backgroundContext)?.decrementReferenceCount() } diff --git a/Tusker/Screens/Status Action Account List/StatusActionAccountListTableViewController.swift b/Tusker/Screens/Status Action Account List/StatusActionAccountListTableViewController.swift index b7595a92..b4f2ff74 100644 --- a/Tusker/Screens/Status Action Account List/StatusActionAccountListTableViewController.swift +++ b/Tusker/Screens/Status Action Account List/StatusActionAccountListTableViewController.swift @@ -59,8 +59,7 @@ class StatusActionAccountListTableViewController: EnhancedTableViewController { } deinit { - if let accountIDs = self.accountIDs { - let container = self.mastodonController.persistentContainer + if let accountIDs = self.accountIDs, let container = mastodonController?.persistentContainer { container.backgroundContext.perform { for id in accountIDs { container.account(for: id, in: container.backgroundContext)?.decrementReferenceCount() diff --git a/Tusker/Screens/Timeline/TimelineTableViewController.swift b/Tusker/Screens/Timeline/TimelineTableViewController.swift index ddc1b908..4a9a8d98 100644 --- a/Tusker/Screens/Timeline/TimelineTableViewController.swift +++ b/Tusker/Screens/Timeline/TimelineTableViewController.swift @@ -39,12 +39,13 @@ class TimelineTableViewController: EnhancedTableViewController { } deinit { + guard let persistentContainer = mastodonController?.persistentContainer else { return } // decrement reference counts of any statuses we still have // if the app is currently being quit, this will not affect the persisted data because // the persistent container would already have been saved in SceneDelegate.sceneDidEnterBackground(_:) for segment in timelineSegments { for (id, _) in segment { - mastodonController.persistentContainer.status(for: id)?.decrementReferenceCount() + persistentContainer.status(for: id)?.decrementReferenceCount() } } }