// How a show's radio (`podcast.radio`, Loom's `RadioFace`) plays. The// episode it plays is the core's (`shell.deck`'s `episode` slot: the one the// player holds when it is the show's, else the newest started, else the// newest — owner, 4 Nov 2026).//// THE PLAYER is the app's one podcast player (`PodcastPlayback`): the radio// draws where it stands from what it publishes (`PodcastNowPlaying`), its// button plays the card's episode there with no player over it — dismissing// the deck keeps it playing, and the lock screen is the player's own — its// keys and TUNE seek, and VOL turns the system's volume (`SystemVolume`).importCottonFacesimportCottonKitimportCottonValuesimportSwiftUI#if os(iOS)import AVFoundationimportMediaPlayerimport UIKit#endif#if os(iOS)/// A show's radio under the app's one podcast player: where it stands with/// this show's episode, its button playing or pausing the card's episode,/// its keys and TUNE seeking, VOL turning the system's volume.structDeckRadioPlaying: View {let card: DeckCardlet episode: [String: RecordValue]let drawn: (FaceTrackPlay) -> AnyView @Environment(\.cottonCore) privatevar core @Environment(ToastCenter.self) privatevar toasts: ToastCenter? @State privatevar volume = SystemVolume.sharedprivatevar playback: PodcastPlayback { .shared }/// Where the player stands with this show: holding one of its episodes,/// or none.privatevar play: TrackPlay {let now = playback.nowguardlet show = card.record, now.holds(show: show) else { return .still }returnTrackPlay(held: true, playing: now.playing, loading: now.loading, elapsed: now.elapsed, duration: now.duration) }var body: someView { drawn(FaceTrackPlay( play: play, toggle: { toggle() }, seek: { playback.seek(to: $0) }, volume: volume.level, turnVolume: { volume.turn(to: $0) })) .background(SystemVolumeSlider(volume: volume).frame(width: 1, height: 1).opacity(0.01).allowsHitTesting(false)) }/// The button: the show's episode held plays or pauses; else the card's/// episode plays from where it was stopped.privatefunc toggle() {iflet show = card.record, playback.now.holds(show: show) { return playback.toggle() }guardlet core, let table = episode["table"]?.text, let id = episode["record"]?.text else { return }let say: (String) -> Void = { [toasts] said in toasts?.show(said, tone: .error) } playback.play(table: table, id: id, core: core, say: say) }}/// The system's output volume, as a radio's VOL knob reads and turns it:/// read off the audio session, turned through the system's own volume/// slider (`MPVolumeView`, the one way an app may set it), which a radio on/// screen carries out of sight (`SystemVolumeSlider`).@MainActor @ObservablefinalclassSystemVolume {staticlet shared = SystemVolume()/// The volume, nought to one.private(set) var level: Double @ObservationIgnored privatevar watching: NSKeyValueObservation? @ObservationIgnored weakvar slider: UISlider?privateinit() {let session = AVAudioSession.sharedInstance() level = Double(session.outputVolume) watching = session.observe(\.outputVolume, options: [.new]) { [weakself] _, change inguardlet value = change.newValue else { return }Task { @MainActor inself?.level = Double(value) } } }/// Turns the system's volume to `share`, through its slider, then reads/// back what the system holds: where the system took no turn (no slider,/// no route — the simulator) the knob goes back to the volume it plays at,/// never one it only shows.func turn(to share: Double) {guardlet slider else { return }let to = min(1, max(0, share)) slider.value = Float(to) slider.sendActions(for: .valueChanged) level = toTask { @MainActor [weakself] intry? awaitTask.sleep(for: .milliseconds(400))self?.level = Double(AVAudioSession.sharedInstance().outputVolume) } }}/// The system's volume slider, carried out of sight so `SystemVolume` can/// turn it — and so the system's volume picture stays away while VOL turns.privatestructSystemVolumeSlider: UIViewRepresentable {let volume: SystemVolumefunc makeUIView(context: Context) -> MPVolumeView {let view = MPVolumeView(frame: CGRect(x: 0, y: 0, width: 100, height: 40)) view.isUserInteractionEnabled = false volume.slider = view.subviews.compactMap { $0 as? UISlider }.firstreturn view }func updateUIView(_ view: MPVolumeView, context: Context) {if volume.slider == nil { volume.slider = view.subviews.compactMap { $0 as? UISlider }.first } }}#endif