diff --git a/Mastodon/Persistence/FileManager+SearchHistory.swift b/Mastodon/Persistence/FileManager+SearchHistory.swift index 45abe98b2..8c1cabd1d 100644 --- a/Mastodon/Persistence/FileManager+SearchHistory.swift +++ b/Mastodon/Persistence/FileManager+SearchHistory.swift @@ -39,23 +39,32 @@ extension FileManager { searchItems.append(newSearchItem) + storeJSON(searchItems, .searchHistory) + } + + private func storeJSON(_ encodable: Encodable, _ persistence: Persistence) { + guard let documentsDirectory else { return } + let jsonEncoder = JSONEncoder() jsonEncoder.dateEncodingStrategy = .iso8601 do { - let data = try jsonEncoder.encode(searchItems) + let data = try jsonEncoder.encode(encodable) - let searchHistoryPath = Persistence.searchHistory.filepath(baseURL: documentsDirectory) + let searchHistoryPath = persistence.filepath(baseURL: documentsDirectory) try data.write(to: searchHistoryPath) } catch { debugPrint(error.localizedDescription) } + } - func removeSearchHistory() { + func removeSearchHistory(forUser userID: String) { guard let documentsDirectory else { return } - let searchHistoryPath = Persistence.searchHistory.filepath(baseURL: documentsDirectory) - try? removeItem(at: searchHistoryPath) + var searchItems = (try? searchItems()) ?? [] + let newSearchItems = searchItems.filter { $0.userID != userID } + + storeJSON(newSearchItems, .searchHistory) } } diff --git a/Mastodon/Scene/Search/SearchDetail/SearchHistory/SearchHistoryViewController.swift b/Mastodon/Scene/Search/SearchDetail/SearchHistory/SearchHistoryViewController.swift index ca46a7f1a..a2c696c3b 100644 --- a/Mastodon/Scene/Search/SearchDetail/SearchHistory/SearchHistoryViewController.swift +++ b/Mastodon/Scene/Search/SearchDetail/SearchHistory/SearchHistoryViewController.swift @@ -98,7 +98,9 @@ extension SearchHistoryViewController: SearchHistorySectionHeaderCollectionReusa _ searchHistorySectionHeaderCollectionReusableView: SearchHistorySectionHeaderCollectionReusableView, clearButtonDidPressed button: UIButton ) { - FileManager.default.removeSearchHistory() + let userID = authContext.mastodonAuthenticationBox.userID + + FileManager.default.removeSearchHistory(forUser: userID) viewModel.items = [] } }