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

157 lines
5.8 KiB
Swift
Executable File

// PlaylistRow — the shared playlist/album list row: pure navigation. Playback
// choices live on the detail screen, which can back-fill v2's ~5-track partial
// hydration BEFORE any bulk action (see SCPlaylist.isPartiallyLoaded) — a row
// that played directly would ship 5-track "playlists".
//
// Hair separators are the list's job, not the row's (see LikesView).
//
// The `accessory` slot exists because a row's management actions cannot live
// inside the NavigationLink: a Menu (or any button) placed in a link's label is
// swallowed by the link, and a second `.contextMenu` wrapped around the row
// loses to the link's own hit area. So the link covers the CONTENT and the
// accessory sits beside it as a real sibling control. The Flutter row put
// Rename/Delete behind a trailing ⋮ bottom sheet; a context menu alone would
// leave iOS with long-press as the only way in.
import CrossSoundKit
import Inject
import RedWhiteUI
import SwiftUI
public struct PlaylistRow<Accessory: View>: View {
// The active RedWhiteUI palette (set once by AppRoot's .rwTheme()).
@Environment(\.rw) private var p
private let playlist: SCPlaylist
/// Caller-supplied subtitle line (SearchView passes the Dart search rows'
/// "{username} · Album" owner line); nil keeps the track-count default.
private let subtitleOverride: String?
/// Trailing control, OUTSIDE the navigation link (see the header).
private let accessory: Accessory
public init(
playlist: SCPlaylist,
subtitle: String? = nil,
@ViewBuilder accessory: () -> Accessory
) {
self.playlist = playlist
self.subtitleOverride = subtitle
self.accessory = accessory()
}
@ObserveInjection var inject
public var body: some View {
HStack(spacing: 0) {
// value-based link → AppShell's single .navigationDestination switch.
NavigationLink(value: AppDestination.playlist(playlist)) {
HStack(spacing: RwSizes.gap) {
artwork(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(playlist.title)
.rw(.bodyLarge)
.lineLimit(1)
Text(subtitle)
.rw(.labelMedium)
.lineLimit(1)
}
Spacer(minLength: RwSizes.gap)
Image(systemName: "chevron.right")
.font(.system(size: 12, weight: .semibold))
// Icon chrome takes iconInactive (pastel red), not a text token.
.foregroundStyle(p.iconInactive)
}
.padding(.leading, RwSizes.pagePadding)
// No trailing page padding when an accessory follows: the accessory
// owns that edge, and doubling the inset pushed the chevron into the
// middle of the row.
.padding(
.trailing, Accessory.self == EmptyView.self ? RwSizes.pagePadding : RwSizes.gap)
.padding(.vertical, 8)
.contentShape(Rectangle())
}
.buttonStyle(.bareRow())
accessory
}
.enableInjection()
}
/// "Album · 12 tracks" / "3 tracks", unless the caller overrode the line.
/// trackCount comes first because it counts the stub entries partial
/// hydration drops from `tracks` — the raw tracks.count would under-report
/// nearly every v2 playlist.
private var subtitle: String {
if let subtitleOverride { return subtitleOverride }
let count = playlist.trackCount ?? playlist.tracks.count
let tracks = count == 1 ? "1 track" : "\(count) tracks"
return playlist.isAlbum ? "Album · \(tracks)" : tracks
}
private func artwork(_ p: RwPalette) -> some View {
// The model stores t500x500 (decode already ran the 3-level artwork
// fallback); rewrite to 'large' (100x100) for the list-row size, same as
// SCTrack.artworkUrlSmall does for tracks.
AsyncImage(
url: rewriteArtwork(playlist.artworkUrl, to: "large").flatMap(URL.init(string:))
) { image in
image.resizable().scaledToFill()
} placeholder: {
ZStack {
p.bg2.color
Image(systemName: "music.note.list")
.font(.system(size: 16))
// An icon, so iconInactive — textMuted reads gray on glyphs.
.foregroundStyle(p.iconInactive)
}
}
.frame(width: 44, height: 44)
// Square, like everything else. The clip itself stays: scaledToFill
// overflows the 44pt frame on non-square artwork.
.clipShape(Rectangle())
.rwBorder(.accent, width: 2)
}
}
// MARK: - Accessory-less rows
/// Every existing call site (Likes, Search, Artist, Discover, My Albums, My
/// Reposts) keeps compiling unchanged — the RwCard / RwSectionHeader pattern.
extension PlaylistRow where Accessory == EmptyView {
public init(playlist: SCPlaylist, subtitle: String? = nil) {
self.init(playlist: playlist, subtitle: subtitle) { EmptyView() }
}
}
// MARK: - PlaylistRowMenu
/// The trailing ⋮ control rows pass as their accessory: a real button (so it
/// wins the tap over the row's navigation link) that opens the caller's menu
/// items. Not @MainActor, to match PlaylistRow — the row is constructed from
/// plain (non-isolated) view bodies across the app.
public struct PlaylistRowMenu: View {
// The active RedWhiteUI palette (set once by AppRoot's .rwTheme()).
@Environment(\.rw) private var p
private let items: [RwMenuItem]
public init(_ items: [RwMenuItem]) {
self.items = items
}
@ObserveInjection var inject
public var body: some View {
// RwMenu, not Menu: the system menu is rendered out of process and
// arrives as translucent glass on iOS 26+, which no modifier can turn off.
RwMenu(items) {
Image(systemName: "ellipsis")
.font(.system(size: 13, weight: .semibold))
}
.padding(.trailing, RwSizes.pagePadding - 8)
.accessibilityLabel("Playlist actions")
.enableInjection()
}
}