mastodon-ios/Mastodon/Diffiable/Item/SearchResultItem.swift

74 lines
2.3 KiB
Swift
Raw Normal View History

//
// SearchResultItem.swift
// Mastodon
//
// Created by sxiaojian on 2021/4/6.
//
2021-04-07 13:49:33 +02:00
import CoreData
import Foundation
import MastodonSDK
enum SearchResultItem {
2021-04-07 15:01:32 +02:00
case hashtag(tag: Mastodon.Entity.Tag)
case account(account: Mastodon.Entity.Account)
2021-04-06 09:25:04 +02:00
2021-04-07 13:49:33 +02:00
case accountObjectID(accountObjectID: NSManagedObjectID)
2021-04-07 15:01:32 +02:00
case hashtagObjectID(hashtagObjectID: NSManagedObjectID)
case status(statusObjectID: NSManagedObjectID, attribute: Item.StatusAttribute)
2021-04-07 13:49:33 +02:00
2021-04-06 09:25:04 +02:00
case bottomLoader
}
extension SearchResultItem: Equatable {
static func == (lhs: SearchResultItem, rhs: SearchResultItem) -> Bool {
switch (lhs, rhs) {
2021-04-07 15:01:32 +02:00
case (.hashtag(let tagLeft), .hashtag(let tagRight)):
return tagLeft == tagRight
2021-04-06 09:25:04 +02:00
case (.account(let accountLeft), .account(let accountRight)):
return accountLeft == accountRight
case (.accountObjectID(let idLeft), .accountObjectID(let idRight)):
2021-04-07 13:49:33 +02:00
return idLeft == idRight
case (.hashtagObjectID(let idLeft), .hashtagObjectID(let idRight)):
2021-04-07 13:49:33 +02:00
return idLeft == idRight
case (.status(let idLeft, _), .status(let idRight, _)):
return idLeft == idRight
case (.bottomLoader, .bottomLoader):
return true
default:
return false
}
}
}
extension SearchResultItem: Hashable {
func hash(into hasher: inout Hasher) {
switch self {
case .account(let account):
hasher.combine(String(describing: SearchResultItem.account.self))
hasher.combine(account.id)
2021-04-07 15:01:32 +02:00
case .hashtag(let tag):
hasher.combine(String(describing: SearchResultItem.hashtag.self))
hasher.combine(tag.name)
2021-04-07 13:49:33 +02:00
case .accountObjectID(let id):
hasher.combine(id)
2021-04-07 15:01:32 +02:00
case .hashtagObjectID(let id):
2021-04-07 13:49:33 +02:00
hasher.combine(id)
case .status(let id, _):
hasher.combine(id)
2021-04-06 09:25:04 +02:00
case .bottomLoader:
hasher.combine(String(describing: SearchResultItem.bottomLoader.self))
}
}
}
extension SearchResultItem {
var sortKey: String? {
switch self {
case .account(let account): return account.displayName.lowercased()
case .hashtag(let hashtag): return hashtag.name.lowercased()
default: return nil
}
}
}