PulseCanvas / app · pulsecanvas.ai / Sources / PulseCanvas / StudioMusicPicker.swift
| 1 | import SwiftUI |
| 2 | |
| 3 | /// A soundtrack picker, separate from Radio's listening and station controls. |
| 4 | @MainActor struct StudioMusicPicker: View { |
| 5 | @StateObject private var catalog: StudioMusicCatalog |
| 6 | let favoriteIDs: Set<String> |
| 7 | let useSong: (URL, String) async throws -> Void |
| 8 | let close: () -> Void |
| 9 | @State private var favoritesOnly = false |
| 10 | @State private var search = "" |
| 11 | @FocusState private var searchFocused: Bool |
| 12 | @State private var adding: String? |
| 13 | @State private var error: String? |
| 14 | @State private var addition: Task<Void, Never>? |
| 15 | |
| 16 | init(source: URL, folderBookmark: Data? = nil, library: RadioLibrary = RadioLibrary(), favoriteIDs: Set<String>, |
| 17 | useSong: @escaping (URL, String) async throws -> Void, close: @escaping () -> Void) { |
| 18 | _catalog = StateObject(wrappedValue: StudioMusicCatalog(source: source, folderBookmark: folderBookmark, library: library)) |
| 19 | self.favoriteIDs = favoriteIDs; self.useSong = useSong; self.close = close |
| 20 | } |
| 21 | |
| 22 | private var songs: [RadioTrack] { catalog.matching(search: search, favoritesOnly: favoritesOnly, favoriteIDs: favoriteIDs) } |
| 23 | var body: some View { |
| 24 | VStack(alignment: .leading, spacing: 16) { |
| 25 | HStack { |
| 26 | Label("Choose music", systemImage: "music.note.list").font(.title2.bold()) |
| 27 | Spacer() |
| 28 | Button("Cancel") { addition?.cancel(); close() }.accessibilityIdentifier("cancelStudioMusic") |
| 29 | } |
| 30 | Text("Add a song as your Studio soundtrack, then create and export your video.").foregroundStyle(.secondary) |
| 31 | Picker("Songs", selection: $favoritesOnly) { |
| 32 | Text("All songs").tag(false) |
| 33 | Label("Favorites", systemImage: "heart.fill").tag(true) |
| 34 | }.pickerStyle(.segmented).accessibilityIdentifier("studioMusicFilter") |
| 35 | HStack(spacing: 10) { |
| 36 | Image(systemName: "magnifyingglass").foregroundStyle(.secondary) |
| 37 | TextField("Search songs or genres", text: $search).textFieldStyle(.plain) |
| 38 | .focused($searchFocused).autocorrectionDisabled() |
| 39 | .onSubmit { searchFocused = false } |
| 40 | .accessibilityIdentifier("studioMusicSearch") |
| 41 | if !search.isEmpty { |
| 42 | Button { searchFocused = false; search = "" } label: { |
| 43 | Image(systemName: "xmark.circle.fill").frame(minWidth: 44, minHeight: 44).contentShape(Rectangle()) |
| 44 | }.buttonStyle(.plain).accessibilityLabel("Clear song search").accessibilityIdentifier("clearStudioMusicSearch") |
| 45 | } |
| 46 | }.padding(.horizontal, 14).frame(minHeight: 48) |
| 47 | .background(.white.opacity(0.07), in: RoundedRectangle(cornerRadius: 12)) |
| 48 | if catalog.loading { |
| 49 | ProgressView("Loading music…").frame(maxWidth: .infinity, maxHeight: .infinity) |
| 50 | } else if let failure = catalog.error { |
| 51 | ContentUnavailableView { |
| 52 | Label("Music couldn’t load", systemImage: "wifi.exclamationmark") |
| 53 | } description: { Text(failure) } actions: { |
| 54 | Button("Try again") { Task { await catalog.load() } } |
| 55 | }.frame(maxWidth: .infinity, maxHeight: .infinity) |
| 56 | } else if songs.isEmpty { |
| 57 | ContentUnavailableView(favoritesOnly ? "No matching favorite songs" : "No matching songs", |
| 58 | systemImage: favoritesOnly ? "heart" : "music.note", |
| 59 | description: Text(favoritesOnly ? "Save songs with the heart in Radio, or choose All songs." : "Try another search.")) |
| 60 | .frame(maxWidth: .infinity, maxHeight: .infinity) |
| 61 | } else { |
| 62 | ScrollView { |
| 63 | LazyVStack(spacing: 8) { |
| 64 | ForEach(songs) { song in |
| 65 | Button { add(song) } label: { |
| 66 | HStack(spacing: 12) { |
| 67 | Image(systemName: favoriteIDs.contains(song.id) ? "heart.fill" : "music.note") |
| 68 | .foregroundStyle(.orange).frame(width: 24) |
| 69 | VStack(alignment: .leading, spacing: 4) { |
| 70 | Text(song.title).font(.headline).lineLimit(1) |
| 71 | Text("\(song.genre) · \(duration(song.duration))").font(.caption).foregroundStyle(.secondary) |
| 72 | } |
| 73 | Spacer(minLength: 8) |
| 74 | if adding == song.id { ProgressView().controlSize(.small) } |
| 75 | else { Image(systemName: "plus.circle.fill").font(.title2).foregroundStyle(.orange) } |
| 76 | }.padding(14).frame(maxWidth: .infinity, minHeight: 64, alignment: .leading) |
| 77 | .contentShape(RoundedRectangle(cornerRadius: 12)) |
| 78 | }.buttonStyle(.plain).background(.white.opacity(0.06), in: RoundedRectangle(cornerRadius: 12)) |
| 79 | .accessibilityLabel("Use \(song.title) in Studio").accessibilityIdentifier("studioMusicSong-\(song.id)") |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | if adding != nil { Label("Downloading and preparing your soundtrack…", systemImage: "arrow.down.circle").font(.callout).foregroundStyle(.secondary) } |
| 85 | if let error { Text(error).font(.callout).foregroundStyle(.red).accessibilityIdentifier("studioMusicError") } |
| 86 | }.padding(24) |
| 87 | .background(Color(red: 0.12, green: 0.10, blue: 0.14)).tint(Color(red: 0.95, green: 0.68, blue: 0.55)) |
| 88 | .task { await catalog.load() } |
| 89 | .onDisappear { addition?.cancel() } |
| 90 | .interactiveDismissDisabled(adding != nil) |
| 91 | } |
| 92 | private func add(_ song: RadioTrack) { |
| 93 | guard adding == nil else { return } |
| 94 | searchFocused = false |
| 95 | adding = song.id; error = nil |
| 96 | addition = Task { |
| 97 | defer { adding = nil } |
| 98 | do { |
| 99 | let url = try await catalog.audio(for: song) |
| 100 | try Task.checkCancellation() |
| 101 | try await useSong(url, song.title) |
| 102 | close() |
| 103 | } catch is CancellationError { } |
| 104 | catch { self.error = error.localizedDescription } |
| 105 | } |
| 106 | } |
| 107 | private func duration(_ seconds: Double) -> String { |
| 108 | String(format: "%d:%02d", Int(seconds) / 60, Int(seconds) % 60) |
| 109 | } |
| 110 | } |