Clean search-history for one user only (IOS-196)

This commit is contained in:
Nathan Mattes 2023-11-23 15:33:28 +01:00
parent a44d4eed47
commit 361ad357db
2 changed files with 17 additions and 6 deletions

View File

@ -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)
}
}

View File

@ -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 = []
}
}