diff --git a/Mastodon/Persistence/FileManager+SearchHistory.swift b/Mastodon/Persistence/FileManager+SearchHistory.swift index 5d31ececc..a9da200fd 100644 --- a/Mastodon/Persistence/FileManager+SearchHistory.swift +++ b/Mastodon/Persistence/FileManager+SearchHistory.swift @@ -28,6 +28,10 @@ extension FileManager { var searchItems = (try? searchItems(forUser: newSearchItem.userID)) ?? [] + if let index = searchItems.firstIndex(of: newSearchItem) { + searchItems.remove(at: index) + } + searchItems.append(newSearchItem) let jsonEncoder = JSONEncoder() diff --git a/Mastodon/Persistence/Model/SearchHistory.swift b/Mastodon/Persistence/Model/SearchHistory.swift index fc5e992c1..f477a93ad 100644 --- a/Mastodon/Persistence/Model/SearchHistory.swift +++ b/Mastodon/Persistence/Model/SearchHistory.swift @@ -5,11 +5,21 @@ import MastodonCore import MastodonSDK extension Persistence.SearchHistory { - struct Item: Codable { + struct Item: Codable, Hashable, Equatable { let updatedAt: Date let userID: Mastodon.Entity.Account.ID let account: Mastodon.Entity.Account? let hashtag: Mastodon.Entity.Tag? + + func hash(into hasher: inout Hasher) { + hasher.combine(userID) + hasher.combine(account) + hasher.combine(hashtag) + } + + public static func == (lhs: Persistence.SearchHistory.Item, rhs: Persistence.SearchHistory.Item) -> Bool { + return lhs.account == rhs.account && lhs.hashtag == rhs.hashtag + } } }