IceCubesApp/Packages/Models/Sources/Models/Tag.swift

59 lines
1.5 KiB
Swift
Raw Normal View History

import Foundation
2023-01-05 13:11:55 +00:00
public struct Tag: Codable, Identifiable, Equatable, Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(name)
}
2023-01-17 10:36:01 +00:00
2023-01-05 13:11:55 +00:00
public static func == (lhs: Tag, rhs: Tag) -> Bool {
2024-01-05 20:28:46 +00:00
lhs.name == rhs.name &&
2024-02-14 11:48:14 +00:00
lhs.following == rhs.following
2023-01-05 13:11:55 +00:00
}
2023-01-17 10:36:01 +00:00
public var id: String {
name
}
2023-01-17 10:36:01 +00:00
public let name: String
public let url: String
public let following: Bool
public let history: [History]
2023-01-17 10:36:01 +00:00
public var totalUses: Int {
2023-01-17 10:36:01 +00:00
history.compactMap { Int($0.uses) }.reduce(0, +)
}
2023-01-17 10:36:01 +00:00
public var totalAccounts: Int {
2023-01-17 10:36:01 +00:00
history.compactMap { Int($0.accounts) }.reduce(0, +)
}
}
2022-12-21 19:26:38 +00:00
public struct FeaturedTag: Codable, Identifiable {
public let id: String
public let name: String
public let url: URL
public let statusesCount: String
public var statusesCountInt: Int {
Int(statusesCount) ?? 0
}
2023-01-17 10:36:01 +00:00
2023-01-15 13:21:08 +00:00
private enum CodingKeys: String, CodingKey {
case id, name, url, statusesCount
}
2023-01-17 10:36:01 +00:00
2023-01-15 13:21:08 +00:00
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
id = try container.decode(String.self, forKey: .id)
name = try container.decode(String.self, forKey: .name)
url = try container.decode(URL.self, forKey: .url)
do {
statusesCount = try container.decode(String.self, forKey: .statusesCount)
} catch DecodingError.typeMismatch {
statusesCount = try String(container.decode(Int.self, forKey: .statusesCount))
}
}
2022-12-21 19:26:38 +00:00
}
extension Tag: Sendable {}
extension FeaturedTag: Sendable {}