Files
letro-ios/ElementX/Sources/Other/SwiftUI/Views/RoomInviterLabel.swift
Mauro 77c88a817a Compound - Swift 6.2 and Main actor isolation (#5109)
* compound is now using 6.2 and main actor isolation + some tweaks to make it build tests and EX

* better handling of the nonisolated context for CompoundUIColors

* improving docs

* fix comment

* remove unused Sendable conformance
2026-02-16 10:19:12 +00:00

81 lines
2.9 KiB
Swift

//
// Copyright 2025 Element Creations Ltd.
// Copyright 2024-2025 New Vector Ltd.
//
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
// Please see LICENSE files in the repository root for full details.
//
import SwiftUI
@MainActor
struct RoomInviterDetails: Equatable {
let id: String
let displayName: String?
let avatarURL: URL?
let attributedInviteText: AttributedString
init(member: RoomMemberProxyProtocol) {
id = member.userID
displayName = member.displayName
avatarURL = member.avatarURL
let nameOrLocalPart = if let displayName = member.displayName {
displayName
} else {
String(member.userID.dropFirst().prefix { $0 != ":" })
}
// Pre-compute the attributed string.
let placeholder = "{displayname}"
var string = AttributedString(L10n.screenInvitesInvitedYou(placeholder, id))
var displayNameString = AttributedString(nameOrLocalPart)
displayNameString.bold()
displayNameString.foregroundColor = .compound.textPrimary
string.replace(placeholder, with: displayNameString)
attributedInviteText = string
}
}
struct RoomInviterLabel: View {
let inviter: RoomInviterDetails
var shouldHideAvatar = false
let mediaProvider: MediaProviderProtocol?
var body: some View {
HStack(alignment: .firstTextBaseline, spacing: 8) {
LoadableAvatarImage(url: shouldHideAvatar ? nil : inviter.avatarURL,
name: inviter.displayName,
contentID: inviter.id,
avatarSize: .custom(16),
mediaProvider: mediaProvider)
.alignmentGuide(.firstTextBaseline) { $0[.bottom] * 0.8 }
.accessibilityHidden(true)
Text(inviter.attributedInviteText)
}
}
}
// MARK: - Previews
struct RoomInviterLabel_Previews: PreviewProvider, TestablePreview {
static var previews: some View {
VStack(spacing: 10) {
RoomInviterLabel(inviter: .init(member: RoomMemberProxyMock.mockAlice),
mediaProvider: MediaProviderMock(configuration: .init()))
RoomInviterLabel(inviter: .init(member: RoomMemberProxyMock.mockDan),
mediaProvider: MediaProviderMock(configuration: .init()))
RoomInviterLabel(inviter: .init(member: RoomMemberProxyMock.mockNoName),
mediaProvider: MediaProviderMock(configuration: .init()))
RoomInviterLabel(inviter: .init(member: RoomMemberProxyMock.mockCharlie),
mediaProvider: MediaProviderMock(configuration: .init()))
.foregroundStyle(.compound.textPrimary)
}
.font(.compound.bodyMD)
.foregroundStyle(.compound.textSecondary)
}
}