Cache Account display name

This commit is contained in:
Thomas Ricouard 2024-01-24 16:22:44 +01:00
parent 20f4eb9c71
commit c7bd5a1d94
2 changed files with 64 additions and 1 deletions

View file

@ -44,6 +44,7 @@ public final class Account: Codable, Identifiable, Hashable, Sendable, Equatable
public let id: String
public let username: String
public let displayName: String?
public let cachedDisplayName: HTMLString
public let avatar: URL
public let header: URL
public let acct: String
@ -89,6 +90,62 @@ public final class Account: Codable, Identifiable, Hashable, Sendable, Equatable
self.source = source
self.bot = bot
self.discoverable = discoverable
if let displayName, !displayName.isEmpty {
self.cachedDisplayName = .init(stringValue: displayName)
} else {
self.cachedDisplayName = .init(stringValue: "@\(username)")
}
}
public enum CodingKeys: CodingKey {
case id
case username
case displayName
case avatar
case header
case acct
case note
case createdAt
case followersCount
case followingCount
case statusesCount
case lastStatusAt
case fields
case locked
case emojis
case url
case source
case bot
case discoverable
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.id = try container.decode(String.self, forKey: .id)
self.username = try container.decode(String.self, forKey: .username)
self.displayName = try container.decodeIfPresent(String.self, forKey: .displayName)
self.avatar = try container.decode(URL.self, forKey: .avatar)
self.header = try container.decode(URL.self, forKey: .header)
self.acct = try container.decode(String.self, forKey: .acct)
self.note = try container.decode(HTMLString.self, forKey: .note)
self.createdAt = try container.decode(ServerDate.self, forKey: .createdAt)
self.followersCount = try container.decodeIfPresent(Int.self, forKey: .followersCount)
self.followingCount = try container.decodeIfPresent(Int.self, forKey: .followingCount)
self.statusesCount = try container.decodeIfPresent(Int.self, forKey: .statusesCount)
self.lastStatusAt = try container.decodeIfPresent(String.self, forKey: .lastStatusAt)
self.fields = try container.decode([Account.Field].self, forKey: .fields)
self.locked = try container.decode(Bool.self, forKey: .locked)
self.emojis = try container.decode([Emoji].self, forKey: .emojis)
self.url = try container.decodeIfPresent(URL.self, forKey: .url)
self.source = try container.decodeIfPresent(Account.Source.self, forKey: .source)
self.bot = try container.decode(Bool.self, forKey: .bot)
self.discoverable = try container.decodeIfPresent(Bool.self, forKey: .discoverable)
if let displayName, !displayName.isEmpty {
self.cachedDisplayName = .init(stringValue: displayName)
} else {
self.cachedDisplayName = .init(stringValue: "@\(username)")
}
}
public static func placeholder() -> Account {

View file

@ -38,12 +38,14 @@ struct StatusRowHeaderView: View {
HStack(alignment: .center) {
if theme.avatarPosition == .top {
AvatarView(viewModel.finalStatus.account.avatar)
#if targetEnvironment(macCatalyst)
.accountPopover(viewModel.finalStatus.account)
#endif
}
VStack(alignment: .leading, spacing: 2) {
HStack(alignment: .firstTextBaseline, spacing: 2) {
Group {
EmojiTextApp(.init(stringValue: viewModel.finalStatus.account.safeDisplayName),
EmojiTextApp(viewModel.finalStatus.account.cachedDisplayName,
emojis: viewModel.finalStatus.account.emojis)
.font(.scaledSubheadline)
.foregroundColor(theme.labelColor)
@ -51,7 +53,9 @@ struct StatusRowHeaderView: View {
.emojiBaselineOffset(Font.scaledSubheadlineFont.emojiBaselineOffset)
.fontWeight(.semibold)
.lineLimit(1)
#if targetEnvironment(macCatalyst)
.accountPopover(viewModel.finalStatus.account)
#endif
if !redactionReasons.contains(.placeholder) {
accountBadgeView
@ -67,7 +71,9 @@ struct StatusRowHeaderView: View {
.font(.scaledFootnote)
.foregroundStyle(.secondary)
.lineLimit(1)
#if targetEnvironment(macCatalyst)
.accountPopover(viewModel.finalStatus.account)
#endif
}
}
}