Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
197 lines
6.9 KiB
Swift
Executable File
197 lines
6.9 KiB
Swift
Executable File
// PlayerModeControls — shuffle / radio / autoplay, the three queue-shaping
|
|
// controls that sit alongside the transport on every player surface.
|
|
//
|
|
// One component rather than a copy in each view: the Now Playing screen
|
|
// (TrackDetailView's live transport) and MiniPlayerBar draw these at different
|
|
// sizes but must agree on what each button MEANS, and radio in particular has
|
|
// real behaviour behind it that is easy to get subtly wrong twice.
|
|
//
|
|
// Shuffle and autoplay are TOGGLES — they read straight off QueueController's
|
|
// published state, so they stay correct when something else flips them (the
|
|
// remote-control hooks in AppRoot and PlaylistDetailView's shuffle-play both
|
|
// do). Radio is an ACTION, not a toggle: startRadio fetches ~50 related
|
|
// tracks, filters them to the seed's vibe and swaps the queue, which takes a
|
|
// network round trip and can come back empty. So it gets a spinner while it
|
|
// runs and says so when it fails, instead of looking like a dead button.
|
|
//
|
|
// keepPlaying: true is the point of the radio button here. The seed IS the
|
|
// current track, and that flag swaps the station in BEHIND the playing track
|
|
// rather than calling playTrack — without it, starting a station restarts the
|
|
// song you are already listening to.
|
|
|
|
import CrossSoundKit
|
|
import Inject
|
|
import RedWhiteUI
|
|
import SwiftUI
|
|
|
|
public struct PlayerModeControls: View {
|
|
// The active RedWhiteUI palette (set once by AppRoot's .rwTheme()).
|
|
@Environment(\.rw) private var p
|
|
|
|
let controller: QueueController
|
|
/// Glyph point size. The bar runs small (14) to fit beside the transport;
|
|
/// the Now Playing screen runs larger (18).
|
|
var iconSize: CGFloat = 14
|
|
/// Tap target width. Height is always RwSizes.controlHeight.
|
|
var buttonWidth: CGFloat = 30
|
|
var spacing: CGFloat = 2
|
|
|
|
/// Radio is async and fallible, so the button has three states rather than
|
|
/// one. `.failed` decays back to `.idle` on its own — a transient "that
|
|
/// found nothing" needs no dismissing.
|
|
private enum RadioState { case idle, running, failed }
|
|
@State private var radio: RadioState = .idle
|
|
|
|
public init(
|
|
controller: QueueController,
|
|
iconSize: CGFloat = 14,
|
|
buttonWidth: CGFloat = 30,
|
|
spacing: CGFloat = 2
|
|
) {
|
|
self.controller = controller
|
|
self.iconSize = iconSize
|
|
self.buttonWidth = buttonWidth
|
|
self.spacing = spacing
|
|
}
|
|
|
|
@ObserveInjection var inject
|
|
|
|
public var body: some View {
|
|
HStack(spacing: spacing) {
|
|
modeButton(
|
|
"shuffle",
|
|
isActive: controller.shuffle,
|
|
label: controller.shuffle ? "Shuffle on" : "Shuffle"
|
|
) {
|
|
controller.toggleShuffle()
|
|
}
|
|
|
|
radioButton
|
|
|
|
// Autoplay keeps the queue going past its last track by pulling related
|
|
// tracks; "infinity" is the endless-queue idiom (SoundCloud's own
|
|
// autoplay uses it) and avoids colliding with repeat, which this player
|
|
// does not have.
|
|
modeButton(
|
|
"infinity",
|
|
isActive: controller.autoplay,
|
|
label: controller.autoplay ? "Autoplay on" : "Autoplay"
|
|
) {
|
|
Task { await controller.toggleAutoplay() }
|
|
}
|
|
}
|
|
.enableInjection()
|
|
}
|
|
|
|
// MARK: - As a menu
|
|
|
|
/// The same three as menu rows, for a bar too narrow for the buttons
|
|
/// (MiniPlayerBar on an iPhone). The toggles say their state in the label;
|
|
/// radio starts the station behind the playing track exactly like the
|
|
/// button (keepPlaying), minus the spinner, which a closed menu has nowhere
|
|
/// to show. No seed → the row is disabled (an RwMenuItem with no action).
|
|
@MainActor
|
|
public static func menuItems(controller: QueueController) -> [RwMenuItem] {
|
|
let radio: (() -> Void)? = controller.currentTrack.map { seed in
|
|
{ Task { _ = await controller.startRadio(seed: seed, keepPlaying: true) } }
|
|
}
|
|
return [
|
|
RwMenuItem(controller.shuffle ? "Shuffle: On" : "Shuffle: Off", systemImage: "shuffle") {
|
|
controller.toggleShuffle()
|
|
},
|
|
RwMenuItem("Start Radio", systemImage: "dot.radiowaves.left.and.right", action: radio),
|
|
RwMenuItem(controller.autoplay ? "Autoplay: On" : "Autoplay: Off", systemImage: "infinity") {
|
|
Task { await controller.toggleAutoplay() }
|
|
},
|
|
]
|
|
}
|
|
|
|
// MARK: - Radio
|
|
|
|
@ViewBuilder
|
|
private var radioButton: some View {
|
|
let hasSeed = controller.currentTrack != nil
|
|
Button {
|
|
startRadio()
|
|
} label: {
|
|
LitPalette { p in
|
|
ZStack {
|
|
switch radio {
|
|
case .running:
|
|
ProgressView()
|
|
// .color: ProgressView only honors the Color overload of .tint.
|
|
.tint(p.red.color)
|
|
.controlSize(.small)
|
|
case .failed:
|
|
Image(systemName: "exclamationmark.triangle")
|
|
.font(.system(size: iconSize, weight: .semibold))
|
|
.foregroundStyle(p.warn)
|
|
case .idle:
|
|
// Primary like every idle control; no seed fades, so "nothing to
|
|
// seed from" still looks different from "ready".
|
|
Image(systemName: "dot.radiowaves.left.and.right")
|
|
.font(.system(size: iconSize, weight: .semibold))
|
|
.foregroundStyle(p.red)
|
|
.opacity(hasSeed ? 1 : 0.4)
|
|
}
|
|
}
|
|
.frame(width: buttonWidth, height: RwSizes.controlHeight)
|
|
.contentShape(Rectangle())
|
|
}
|
|
}
|
|
.buttonStyle(.bareRow())
|
|
.disabled(!hasSeed || radio == .running)
|
|
.help(radioHelp)
|
|
.accessibilityLabel(radioHelp)
|
|
}
|
|
|
|
private var radioHelp: String {
|
|
switch radio {
|
|
case .running: "Starting radio…"
|
|
case .failed: "No radio tracks found"
|
|
case .idle: "Start radio from this track"
|
|
}
|
|
}
|
|
|
|
private func startRadio() {
|
|
guard let seed = controller.currentTrack else { return }
|
|
radio = .running
|
|
Task {
|
|
// keepPlaying: the seed is already playing — swap the station in behind
|
|
// it instead of reloading and restarting the track.
|
|
let ok = await controller.startRadio(seed: seed, keepPlaying: true)
|
|
radio = ok ? .idle : .failed
|
|
guard !ok else { return }
|
|
try? await Task.sleep(nanoseconds: 2_500_000_000)
|
|
// Only clear the warning if nothing has been tried since.
|
|
if radio == .failed { radio = .idle }
|
|
}
|
|
}
|
|
|
|
// MARK: - Toggles
|
|
|
|
private func modeButton(
|
|
_ systemName: String,
|
|
isActive: Bool,
|
|
label: String,
|
|
action: @escaping () -> Void
|
|
) -> some View {
|
|
Button(action: action) {
|
|
LitPalette { p in
|
|
Image(systemName: systemName)
|
|
// On is the toggle's active state: the hover colour, flipping on
|
|
// hover (BareButtons.swift) — the like heart's on/off language. The
|
|
// bolder weight says it too.
|
|
.font(.system(size: iconSize, weight: isActive ? .bold : .semibold))
|
|
.foregroundStyle(p.red)
|
|
.frame(width: buttonWidth, height: RwSizes.controlHeight)
|
|
.contentShape(Rectangle())
|
|
}
|
|
}
|
|
.buttonStyle(.bareRow(active: isActive))
|
|
.help(label)
|
|
.accessibilityLabel(label)
|
|
.accessibilityAddTraits(isActive ? [.isSelected] : [])
|
|
}
|
|
}
|