Files
RedLintu16andClaude Opus 5.5 64ec524ba0 CrossSound for Apple platforms
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
2026-10-14 13:28:34 -07:00

95 lines
3.0 KiB
Swift
Executable File

// UserRow — the shared artist/user list row: pure navigation to the artist
// page. Round avatar distinguishes people from square track/playlist artwork
// at a glance (same convention as the Flutter app).
//
// Hair separators are the list's job, not the row's (see LikesView).
import CrossSoundKit
import Inject
import RedWhiteUI
import SwiftUI
public struct UserRow: View {
// The active RedWhiteUI palette (set once by AppRoot's .rwTheme()).
@Environment(\.rw) private var p
private let user: SCUser
public init(user: SCUser) {
self.user = user
}
@ObserveInjection var inject
public var body: some View {
// value-based link → AppShell's single .navigationDestination switch.
NavigationLink(value: AppDestination.artist(user)) {
HStack(spacing: RwSizes.gap) {
avatar(p)
VStack(alignment: .leading, spacing: 2) {
// No color override: the role supplies it — primary red for the
// title, the secondary red (textMuted) for the line under it.
Text(user.username)
.rw(.bodyLarge)
.lineLimit(1)
if let followers = followersText {
Text(followers)
.rw(.labelMedium)
.lineLimit(1)
}
}
Spacer(minLength: RwSizes.gap)
// Passive chrome icon → iconInactive (the text* tokens are for text).
Image(systemName: "chevron.right")
.font(.system(size: 12, weight: .semibold))
.foregroundStyle(p.iconInactive)
}
.padding(.horizontal, RwSizes.pagePadding)
.padding(.vertical, 8)
.contentShape(Rectangle())
}
.buttonStyle(.bareRow())
.enableInjection()
}
private func avatar(_ p: RwPalette) -> some View {
// Stored avatar is t500x500; 'large' (100x100) is plenty for a 44pt row.
AsyncImage(
url: rewriteArtwork(user.avatarUrl, to: "large").flatMap(URL.init(string:))
) { image in
image.resizable().scaledToFill()
} placeholder: {
ZStack {
p.bg2.color
Image(systemName: "person.fill")
.font(.system(size: 16))
.foregroundStyle(p.iconInactive)
}
}
.frame(width: 44, height: 44)
.clipShape(Circle())
.avatarBorder()
}
/// "1.2M followers" — hidden entirely when the API omitted the count (v1
/// shapes and suggested-user stubs often do), so rows never show "0
/// followers" for someone who simply wasn't hydrated.
private var followersText: String? {
guard let n = user.followersCount else { return nil }
return "\(Self.compact(n)) \(n == 1 ? "follower" : "followers")"
}
/// 999 → "999", 12_400 → "12.4K", 1_200_000 → "1.2M" (trailing .0 trimmed).
private static func compact(_ n: Int) -> String {
func trim(_ v: Double) -> String {
let s = String(format: "%.1f", v)
return s.hasSuffix(".0") ? String(s.dropLast(2)) : s
}
switch n {
case ..<1000: return "\(n)"
case ..<1_000_000: return "\(trim(Double(n) / 1000))K"
default: return "\(trim(Double(n) / 1_000_000))M"
}
}
}