From c7bd5a1d943da5d936fecb48c426e334630f8a4b Mon Sep 17 00:00:00 2001 From: Thomas Ricouard Date: Wed, 24 Jan 2024 16:22:44 +0100 Subject: [PATCH] Cache Account display name --- Packages/Models/Sources/Models/Account.swift | 57 +++++++++++++++++++ .../Row/Subviews/StatusRowHeaderView.swift | 8 ++- 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/Packages/Models/Sources/Models/Account.swift b/Packages/Models/Sources/Models/Account.swift index 8570c130..3aa29952 100644 --- a/Packages/Models/Sources/Models/Account.swift +++ b/Packages/Models/Sources/Models/Account.swift @@ -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 { diff --git a/Packages/StatusKit/Sources/StatusKit/Row/Subviews/StatusRowHeaderView.swift b/Packages/StatusKit/Sources/StatusKit/Row/Subviews/StatusRowHeaderView.swift index b4226a6b..a6e0987a 100644 --- a/Packages/StatusKit/Sources/StatusKit/Row/Subviews/StatusRowHeaderView.swift +++ b/Packages/StatusKit/Sources/StatusKit/Row/Subviews/StatusRowHeaderView.swift @@ -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 } } }