Cotton code
121 lines · 4.8 KB · SwiftRaw
// 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`).import CottonFacesimport CottonKitimport CottonValuesimport SwiftUI#if os(iOS)import AVFoundationimport MediaPlayerimport 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.struct DeckRadioPlaying: View {    let card: DeckCard    let episode: [String: RecordValue]    let drawn: (FaceTrackPlay) -> AnyView    @Environment(\.cottonCore) private var core    @Environment(ToastCenter.self) private var toasts: ToastCenter?    @State private var volume = SystemVolume.shared    private var playback: PodcastPlayback { .shared }    /// Where the player stands with this show: holding one of its episodes,    /// or none.    private var play: TrackPlay {        let now = playback.now        guard let show = card.record, now.holds(show: show) else { return .still }        return TrackPlay(held: true, playing: now.playing, loading: now.loading, elapsed: now.elapsed, duration: now.duration)    }    var body: some View {        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.    private func toggle() {        if let show = card.record, playback.now.holds(show: show) { return playback.toggle() }        guard let 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 @Observablefinal class SystemVolume {    static let shared = SystemVolume()    /// The volume, nought to one.    private(set) var level: Double    @ObservationIgnored private var watching: NSKeyValueObservation?    @ObservationIgnored weak var slider: UISlider?    private init() {        let session = AVAudioSession.sharedInstance()        level = Double(session.outputVolume)        watching = session.observe(\.outputVolume, options: [.new]) { [weak self] _, change in            guard let value = change.newValue else { return }            Task { @MainActor in self?.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) {        guard let slider else { return }        let to = min(1, max(0, share))        slider.value = Float(to)        slider.sendActions(for: .valueChanged)        level = to        Task { @MainActor [weak self] in            try? await Task.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.private struct SystemVolumeSlider: UIViewRepresentable {    let volume: SystemVolume    func 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 }.first        return view    }    func updateUIView(_ view: MPVolumeView, context: Context) {        if volume.slider == nil { volume.slider = view.subviews.compactMap { $0 as? UISlider }.first }    }}#endif