PulseCanvas / app · pulsecanvas.ai / mobile / MobileRadio.swift
| 1 | import SwiftUI |
| 2 | import Combine |
| 3 | import UIKit |
| 4 | import UniformTypeIdentifiers |
| 5 | |
| 6 | struct MobileRadio: View { |
| 7 | @ObservedObject var editor: MobileEditor |
| 8 | @ObservedObject var radio: RadioController |
| 9 | @ObservedObject var audio: AudioController |
| 10 | @ObservedObject private var library = ProjectMPresets.shared |
| 11 | let trailingControlInset: CGFloat |
| 12 | @State private var favorites = false |
| 13 | @State private var musicFolder = false |
| 14 | @State private var folderError: String? |
| 15 | init(editor: MobileEditor, trailingControlInset: CGFloat = 0) { self.editor = editor; radio = editor.radio; audio = editor.audio; self.trailingControlInset = trailingControlInset } |
| 16 | var body: some View { |
| 17 | GeometryReader { geometry in |
| 18 | if usesBoundedLayout(geometry.size) { |
| 19 | landscapeRadio |
| 20 | } else { |
| 21 | ScrollView { |
| 22 | VStack(spacing: 16) { |
| 23 | visualizerControls(inlinePicker: false).padding(.trailing, trailingControlInset) |
| 24 | VisualizerRandomizerPicker(fillsWidth: true).padding(.trailing, trailingControlInset) |
| 25 | VisualizerView(state: radio.visualizer).aspectRatio(16 / 9.0, contentMode: .fit) |
| 26 | .clipShape(RoundedRectangle(cornerRadius: 18)) |
| 27 | .accessibilityElement(children: .ignore).accessibilityLabel("Radio visualization") |
| 28 | .accessibilityValue(radio.sceneTitle).accessibilityIdentifier("mobileRadioPreview") |
| 29 | .modifier(MobileVisualizerSwipe { editor.randomizePreviewVisualizer() }) |
| 30 | .modifier(VisualizerFavoriteGesture(favorite: editor.favoritePreviewVisualizer)) |
| 31 | songHeading() |
| 32 | HStack { |
| 33 | stationPicker |
| 34 | Spacer() |
| 35 | nextSong |
| 36 | }.buttonStyle(MobileButton()) |
| 37 | MobileRadioTransport(radio: radio, audio: audio) |
| 38 | status |
| 39 | volume |
| 40 | }.padding(16) |
| 41 | } |
| 42 | } |
| 43 | }.onAppear { radio.loadSavedCatalogIfNeeded() } |
| 44 | .fileImporter(isPresented: $musicFolder, allowedContentTypes: [.folder]) { result in |
| 45 | switch result { |
| 46 | case .success(let url): radio.importMusicFolder(url) |
| 47 | case .failure(let error): folderError = error.localizedDescription |
| 48 | } |
| 49 | } |
| 50 | .alert("Music folder", isPresented: Binding(get: { folderError != nil }, set: { if !$0 { folderError = nil } })) { |
| 51 | Button("OK") { folderError = nil } |
| 52 | } message: { Text(folderError ?? "") } |
| 53 | .sheet(isPresented: $favorites) { |
| 54 | NavigationStack { |
| 55 | List { |
| 56 | Button("Play favorite songs", systemImage: "play.fill") { radio.playFavoriteStation(); favorites = false }.disabled(radio.favoriteSongs.isEmpty) |
| 57 | ForEach(radio.favoriteSongs) { song in |
| 58 | HStack { |
| 59 | Button { radio.playFavoriteSong(song); favorites = false } label: { VStack(alignment: .leading) { Text(song.track.title); Text(song.track.genre).font(.caption).foregroundStyle(.secondary) }.frame(maxWidth: .infinity, alignment: .leading).frame(minHeight: 44) } |
| 60 | Button { radio.toggleFavoriteSong(song.track) } label: { Image(systemName: "heart.fill").frame(width: 44, height: 44) }.buttonStyle(.borderless).accessibilityLabel("Unfavorite \(song.track.title)") |
| 61 | } |
| 62 | } |
| 63 | if radio.favoriteSongs.isEmpty { Text("Save songs with the heart while listening to Radio.").foregroundStyle(.secondary) } |
| 64 | }.navigationTitle("Favorite songs").toolbar { Button("Done") { favorites = false } } |
| 65 | }.task { await radio.refreshFavoriteSongs() } |
| 66 | } |
| 67 | } |
| 68 | // Choose from the live window size so folding or rotating an iPhone Duo |
| 69 | // gets the same spacious preview as other wide or large windows. |
| 70 | private func usesBoundedLayout(_ size: CGSize) -> Bool { |
| 71 | size.width >= 520 && size.height >= 600 |
| 72 | || size.width >= 650 && size.width > size.height && size.height >= 320 |
| 73 | } |
| 74 | // The preview takes only the space left after the controls; deriving its |
| 75 | // height from the width can push transport controls outside a short window. |
| 76 | private var landscapeRadio: some View { |
| 77 | VStack(spacing: 10) { |
| 78 | visualizerControls(inlinePicker: true).padding(.trailing, trailingControlInset) |
| 79 | GeometryReader { preview in |
| 80 | VisualizerView(state: radio.visualizer) |
| 81 | .frame(width: preview.size.width, height: preview.size.height) |
| 82 | .clipShape(RoundedRectangle(cornerRadius: 18)) |
| 83 | }.frame(minHeight: 0, maxHeight: .infinity) |
| 84 | .accessibilityElement(children: .ignore).accessibilityLabel("Radio visualization") |
| 85 | .accessibilityIdentifier("mobileRadioPreview") |
| 86 | .accessibilityValue(radio.sceneTitle) |
| 87 | .modifier(MobileVisualizerSwipe { editor.randomizePreviewVisualizer() }) |
| 88 | .modifier(VisualizerFavoriteGesture(favorite: editor.favoritePreviewVisualizer)) |
| 89 | HStack(spacing: 12) { |
| 90 | songHeading(includesFavoriteList: false) |
| 91 | HStack(spacing: 8) { |
| 92 | if !radio.isPersonalMusic { favoriteSongs } |
| 93 | stationPicker.frame(maxWidth: 220) |
| 94 | nextSong |
| 95 | }.fixedSize(horizontal: true, vertical: false) |
| 96 | }.buttonStyle(MobileButton()) |
| 97 | HStack(spacing: 20) { |
| 98 | MobileRadioTransport(radio: radio, audio: audio) |
| 99 | volume.frame(width: 170) |
| 100 | } |
| 101 | status |
| 102 | }.padding(12) |
| 103 | .frame(maxWidth: .infinity, maxHeight: .infinity) |
| 104 | .accessibilityElement(children: .contain) |
| 105 | .accessibilityIdentifier("mobileRadioLandscape") |
| 106 | } |
| 107 | private func visualizerControls(inlinePicker: Bool) -> some View { |
| 108 | HStack(spacing: 12) { |
| 109 | if radio.isActive { |
| 110 | Button { library.toggleFavorite(radio.visualizer.scene, appearance: VisualPreset(render: radio.visualizer)) } label: { |
| 111 | Image(systemName: library.isFavorite(radio.visualizer.scene) ? "heart.fill" : "heart").frame(width: 16) |
| 112 | }.accessibilityLabel("Favorite radio visualizer") |
| 113 | .accessibilityValue(library.isFavorite(radio.visualizer.scene) ? "Favorited" : "Not favorited") |
| 114 | } |
| 115 | Text(radio.sceneTitle.isEmpty ? "Radio" : radio.sceneTitle).font(.headline).lineLimit(1) |
| 116 | Spacer(minLength: 0) |
| 117 | if inlinePicker { VisualizerRandomizerPicker(fillsWidth: true).frame(maxWidth: 320) } |
| 118 | Button { radio.randomizeCurrentVisualizer() } label: { Image(systemName: "shuffle") } |
| 119 | .disabled(!radio.canRandomizeVisualizer).accessibilityLabel("Random radio visualizer") |
| 120 | }.buttonStyle(MobileButton()) |
| 121 | } |
| 122 | private func songHeading(includesFavoriteList: Bool = true) -> some View { |
| 123 | HStack { |
| 124 | if let track = radio.currentTrack, radio.canFavoriteCurrentSong { |
| 125 | Button { radio.toggleFavoriteSong(track) } label: { |
| 126 | Image(systemName: radio.isFavoriteSong(track) ? "heart.fill" : "heart").frame(width: 16) |
| 127 | }.accessibilityLabel("Favorite song") |
| 128 | .accessibilityValue(radio.isFavoriteSong(track) ? "Favorited" : "Not favorited") |
| 129 | } |
| 130 | Text(radio.currentTrack?.title ?? "PulseCanvas Radio").font(.headline).lineLimit(1) |
| 131 | .accessibilityIdentifier("radioCurrentSongTitle") |
| 132 | Spacer(minLength: 0) |
| 133 | if includesFavoriteList && !radio.isPersonalMusic { |
| 134 | favoriteSongs |
| 135 | } |
| 136 | }.buttonStyle(MobileButton()) |
| 137 | } |
| 138 | private var favoriteSongs: some View { |
| 139 | Button { favorites = true } label: { Image(systemName: "heart.text.clipboard") } |
| 140 | .accessibilityLabel("Favorite songs") |
| 141 | } |
| 142 | private var stationPicker: some View { |
| 143 | MobileRadioStationPicker(radio: radio, chooseMusicFolder: { musicFolder = true }) |
| 144 | } |
| 145 | |
| 146 | private var nextSong: some View { |
| 147 | Button { radio.next() } label: { Label("Next", systemImage: "forward.end.fill") } |
| 148 | .disabled(!radio.isActive || radio.isLoading).accessibilityLabel("Next song") |
| 149 | } |
| 150 | private var volume: some View { |
| 151 | HStack { |
| 152 | Image(systemName: "speaker.fill") |
| 153 | Slider(value: $radio.volume, in: 0...1).accessibilityLabel("Radio volume") |
| 154 | Image(systemName: "speaker.wave.3.fill") |
| 155 | } |
| 156 | } |
| 157 | private var status: some View { |
| 158 | VStack(spacing: 4) { |
| 159 | if radio.isCatalogLoading || radio.isLoading { ProgressView(radio.status).frame(maxWidth: .infinity) } |
| 160 | else { Text(radio.status).font(.caption).foregroundStyle(.secondary).lineLimit(2) } |
| 161 | if radio.isPersonalMusic, let notice = radio.folderImportNotice { Text(notice).font(.caption).foregroundStyle(.secondary) } |
| 162 | if radio.favoriteSongsOnly && radio.favoriteSongs.isEmpty { |
| 163 | Text("No favorite songs yet. Choose All music, then save songs with the heart.").font(.caption).foregroundStyle(.secondary).lineLimit(2) |
| 164 | } |
| 165 | if radio.catalogError != nil && !radio.isCatalogLoading { |
| 166 | Button("Retry radio") { radio.loadSavedCatalogIfNeeded(refresh: true) }.buttonStyle(MobileButton()).accessibilityIdentifier("retryRadioButton") |
| 167 | } |
| 168 | if let error = radio.visualizerGroupMessage { Text(error).font(.caption).foregroundStyle(.secondary).lineLimit(2) } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | } |
| 173 | |
| 174 | /// Keep station selection available in both the workspace and presentation. |
| 175 | struct MobileRadioStationPicker: View { |
| 176 | @ObservedObject var radio: RadioController |
| 177 | var chooseMusicFolder: (() -> Void)? = nil |
| 178 | var identifier = "radioStationPicker" |
| 179 | var body: some View { |
| 180 | Menu { |
| 181 | Button("Next station", systemImage: "forward.frame") { radio.nextStation() } |
| 182 | .disabled(!radio.canSkipStation) |
| 183 | Divider() |
| 184 | Button("All music") { radio.selectMusicGroup("all-music", startPlayback: true) } |
| 185 | Button("Favorite songs") { radio.playFavoriteStation() } |
| 186 | Divider() |
| 187 | if radio.hasPersonalMusic { Button("My music folder") { radio.usePersonalMusic() } } |
| 188 | if let chooseMusicFolder { Button("Choose music folder…", systemImage: "folder.badge.plus", action: chooseMusicFolder) } |
| 189 | Toggle("Shuffle songs", isOn: $radio.shuffle) |
| 190 | ForEach(radio.stations.filter { $0.id != "all-music" }) { station in |
| 191 | Button(station.title) { radio.selectMusicGroup(station.id, startPlayback: true) } |
| 192 | } |
| 193 | } label: { |
| 194 | Label(radio.favoriteSongsOnly ? "Favorite songs" : radio.stationTitle, systemImage: "antenna.radiowaves.left.and.right") |
| 195 | .lineLimit(1) |
| 196 | }.disabled(radio.isCatalogLoading).accessibilityIdentifier(identifier) |
| 197 | .accessibilityLabel("Radio station") |
| 198 | .accessibilityValue(radio.favoriteSongsOnly ? "Favorite songs" : radio.stationTitle) |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /// Observe only the small transport at playback cadence, not the Metal preview |
| 203 | /// and catalog. Seeking commits on release and preserves play/pause state. |
| 204 | private struct MobileRadioTransport: View { |
| 205 | @ObservedObject var radio: RadioController |
| 206 | @ObservedObject var audio: AudioController |
| 207 | @State private var fraction = 0.0 |
| 208 | @State private var seeking = false |
| 209 | var body: some View { |
| 210 | HStack(spacing: 10) { |
| 211 | Button { if radio.isActive { radio.togglePlayback() } else { radio.start() } } label: { |
| 212 | Image(systemName: radio.isActive && radio.isPlaybackRequested ? "pause.fill" : "play.fill") |
| 213 | .frame(width: 30) |
| 214 | }.buttonStyle(MobileButton(prominent: true)) |
| 215 | .accessibilityLabel(radio.isActive && radio.isPlaybackRequested ? "Pause" : "Play") |
| 216 | .accessibilityIdentifier("radioPlaybackButton") |
| 217 | .disabled(!radio.isActive && !radio.canStart) |
| 218 | Text(MobileTransport.time(radio.canSeek ? (seeking ? fraction * audio.duration : audio.playbackSeconds) : 0)) |
| 219 | .monospacedDigit().font(.caption).frame(minWidth: 34).accessibilityIdentifier("radioElapsedTime") |
| 220 | Slider(value: $fraction, in: 0...1, onEditingChanged: { editing in |
| 221 | seeking = editing |
| 222 | if !editing { radio.seek(fraction: fraction) } |
| 223 | }).disabled(!radio.canSeek).accessibilityLabel("Radio song position") |
| 224 | .accessibilityIdentifier("radioSongPosition") |
| 225 | Text(MobileTransport.time(radio.canSeek ? audio.duration : 0)).monospacedDigit().font(.caption) |
| 226 | }.onAppear { fraction = radio.canSeek ? audio.progress : 0 } |
| 227 | .onReceive(audio.visualizationUpdated.throttle(for: .milliseconds(100), scheduler: RunLoop.main, latest: true)) { _ in |
| 228 | if !seeking { fraction = radio.canSeek ? audio.progress : 0 } |
| 229 | } |
| 230 | } |
| 231 | } |