metatext/ViewModels/Sources/ViewModels/View Models/TagViewModel.swift

51 lines
1.2 KiB
Swift
Raw Normal View History

2021-01-24 03:12:30 +00:00
// Copyright © 2021 Metabolist. All rights reserved.
import Combine
import Foundation
import Mastodon
2021-02-04 22:24:27 +00:00
public struct TagViewModel {
2021-02-02 03:39:32 +00:00
public let identityContext: IdentityContext
2021-01-24 03:12:30 +00:00
private let tag: Tag
2021-02-02 03:39:32 +00:00
init(tag: Tag, identityContext: IdentityContext) {
2021-01-24 03:12:30 +00:00
self.tag = tag
2021-02-02 03:39:32 +00:00
self.identityContext = identityContext
2021-01-24 03:12:30 +00:00
}
}
public extension TagViewModel {
var name: String { "#".appending(tag.name) }
var accounts: Int? {
guard let history = tag.history,
let accountsString = history.first?.accounts,
var accounts = Int(accountsString)
else { return nil }
if history.count > 1, let secondDayAccounts = Int(history[1].accounts) {
accounts += secondDayAccounts
}
return accounts
}
var uses: Int? {
guard let history = tag.history,
let usesString = history.first?.uses,
var uses = Int(usesString)
else { return nil }
if history.count > 1, let secondDayUses = Int(history[1].uses) {
uses += secondDayUses
}
return uses
}
var usageHistory: [Int] {
tag.history?.compactMap { Int($0.uses) } ?? []
}
}