import SwiftUI struct NowPlayingView: View { @EnvironmentObject var theme: Theme @EnvironmentObject var player: Player @EnvironmentObject var ui: UIState @EnvironmentObject var stats: PlayStats @EnvironmentObject var prefs: Prefs @Environment(\.dismiss) var dismiss @State private var showLyrics = false @State private var showQueue = false @State private var showSleep = false @State private var showEQ = false @State private var showCovers = false @State private var showLyricsEditor = false @State private var dragPos: Double? @State private var dragY: CGFloat = 0 var body: some View { let p = theme.palette GeometryReader { geo in VStack(spacing: 0) { topBar ZStack { if showLyrics { LyricsPanel() .transition(.opacity) } else { CookieView(image: player.art, playing: player.isPlaying) .padding(.horizontal, 12) .gesture(DragGesture(minimumDistance: 30).onEnded { g in if g.translation.width < -60 { player.next() } else if g.translation.width > 60 { player.prev() } }) .onTapGesture(count: 2) { player.toggle() } .transition(.opacity) } } .frame(maxHeight: .infinity) info seek controls bottomRow } .padding(.bottom, max(8, geo.safeAreaInsets.bottom > 0 ? 0 : 8)) } .background(background.ignoresSafeArea()) .offset(y: dragY) .gesture(DragGesture().onChanged { g in dragY = max(0, g.translation.height) } .onEnded { g in if g.translation.height > 140 { dismiss() } withAnimation(.spring()) { dragY = 0 } }) .sheet(isPresented: $showQueue) { QueueSheet().environmentObjects() } .sheet(isPresented: $showSleep) { SleepSheet().environmentObjects() } .sheet(isPresented: $showEQ) { EqualizerSheet().environmentObjects() } .sheet(isPresented: $showCovers) { if let s = player.current { CoverPicker(song: s).environmentObjects() } } .sheet(isPresented: $showLyricsEditor) { if let s = player.current { LyricsEditor(song: s).environmentObjects() } } .sheet(item: $ui.menu) { m in MenuSheetView(menu: m).environmentObjects() } .sheet(isPresented: Binding(get: { ui.addToPlaylist != nil }, set: { if !$0 { ui.addToPlaylist = nil } })) { AddToPlaylistSheet(songs: ui.addToPlaylist ?? []).environmentObjects() } .sheet(isPresented: Binding(get: { ui.editTags != nil }, set: { if !$0 { ui.editTags = nil } })) { TagEditorView(songs: ui.editTags ?? []).environmentObjects() } .sheet(item: $ui.details) { s in DetailsSheet(song: s).environmentObjects() } .preferredColorScheme(theme.palette.dark ? .dark : .light) .onChange(of: player.current == nil) { if $0 { dismiss() } } } private var background: some View { let p = theme.palette return LinearGradient(colors: [Color(rgb: Palette.mix(p.primaryContainer, p.surface, 0.25)), Color(rgb: Palette.mix(p.primaryContainer, p.surface, 0.75)), p.cSurface], startPoint: .top, endPoint: .bottom) } private var topBar: some View { let p = theme.palette return HStack { IconButton(icon: "chevron.down", tint: p.cOnSurface) { dismiss() } Spacer() VStack(spacing: 4) { Text("NOW PLAYING").font(.system(size: 11, weight: .medium)).tracking(1.2).foregroundColor(p.cOnSurfaceVar) Text("Mulberry") .font(.system(size: 11, weight: .medium)) .padding(.horizontal, 10).padding(.vertical, 3) .foregroundColor(p.cOnPrimaryC) .background(Capsule().fill(p.cPrimaryC)) } Spacer() Button { npMenu() } label: { Image(systemName: "ellipsis").rotationEffect(.degrees(90)) .font(.system(size: 18, weight: .semibold)).foregroundColor(p.cOnSurface) .frame(width: 44, height: 44) } } .padding(.horizontal, 8) } private var info: some View { let p = theme.palette let s = player.current return HStack(alignment: .bottom) { VStack(alignment: .leading, spacing: 3) { Text(s?.displayTitle ?? "Nothing playing") .font(.system(size: 28, weight: .bold)) .foregroundColor(p.cOnSurface) .lineLimit(2) .minimumScaleFactor(0.7) Text(s?.displayArtist ?? "") .font(.system(size: 17)) .foregroundColor(p.cOnSurface.opacity(0.9)) .lineLimit(1) .onTapGesture { if let s { goArtist(s) } } if let a = s?.album, !a.isEmpty { Text(a).font(.system(size: 16)).foregroundColor(p.cPrimary).lineLimit(1) .onTapGesture { if let s { goAlbum(s) } } } } Spacer() if let s { let fav = stats.fav(s) Button { UIImpactFeedbackGenerator(style: .light).impactOccurred() stats.setFav(s, !fav) } label: { Image(systemName: fav ? "heart.fill" : "heart") .font(.system(size: 24)) .foregroundColor(fav ? p.cPrimary : p.cOnSurface) .frame(width: 48, height: 48) .scaleEffect(fav ? 1.08 : 1) .animation(.spring(response: 0.25, dampingFraction: 0.5), value: fav) } } } .padding(.horizontal, 20) } private var seek: some View { let p = theme.palette return TimelineView(.periodic(from: .now, by: 0.5)) { _ in VStack(spacing: 0) { WavySeekBar(position: player.position, duration: player.duration, playing: player.isPlaying, onDrag: { dragPos = $0 }) { player.seek($0) } HStack { Text(TimeFmt.string(dragPos ?? player.position)) Spacer() Text(TimeFmt.string(player.duration)) } .font(.system(size: 13).monospacedDigit()) .foregroundColor(p.cOnSurfaceVar) .padding(.horizontal, 20) } .padding(.horizontal, 10) .padding(.top, 10) } } private var controls: some View { let p = theme.palette return HStack(spacing: 14) { IconButton(icon: "shuffle", size: 52, tint: prefs.shuffle ? p.cOnPrimaryC : p.cOnSecondaryC, filled: prefs.shuffle ? p.cPrimaryC : p.cSecondaryC.opacity(0.7)) { player.setShuffle(!prefs.shuffle) } IconButton(icon: "backward.end.fill", size: 52, tint: p.cOnSecondaryC, filled: p.cSecondaryC.opacity(0.7)) { player.prev() } PlayButton(playing: player.isPlaying) { player.toggle() } .frame(width: 96, height: 72) IconButton(icon: "forward.end.fill", size: 52, tint: p.cOnSecondaryC, filled: p.cSecondaryC.opacity(0.7)) { player.next() } IconButton(icon: prefs.repeatMode == 2 ? "repeat.1" : "repeat", size: 52, tint: prefs.repeatMode > 0 ? p.cOnPrimaryC : p.cOnSecondaryC, filled: prefs.repeatMode > 0 ? p.cPrimaryC : p.cSecondaryC.opacity(0.7)) { player.cycleRepeat() } } .padding(.vertical, 14) } private var bottomRow: some View { let p = theme.palette return HStack { IconButton(icon: showLyrics ? "quote.bubble.fill" : "quote.bubble", tint: showLyrics ? p.cPrimary : p.cOnSurfaceVar) { withAnimation(.easeInOut(duration: 0.25)) { showLyrics.toggle() } } Spacer() IconButton(icon: "list.bullet") { showQueue = true } Spacer() IconButton(icon: player.sleepAt != nil || player.sleepAtTrackEnd ? "moon.zzz.fill" : "moon.zzz", tint: player.sleepAt != nil || player.sleepAtTrackEnd ? p.cPrimary : p.cOnSurfaceVar) { showSleep = true } Spacer() IconButton(icon: "pencil") { if let s = player.current, !s.isStream { ui.editTags = [s] } } } .padding(.horizontal, 40) .padding(.bottom, 6) } private func goAlbum(_ s: Song) { if let g = Library.shared.albums.first(where: { $0.id == "album:" + s.albumKey }) { dismiss(); UIState.shared.go(.group(g)) } } private func goArtist(_ s: Song) { if let g = Library.shared.artists.first(where: { $0.title == s.groupArtist }) { dismiss(); UIState.shared.go(.group(g)) } } private func npMenu() { guard let s = player.current else { return } var items: [MenuSheet.Item] = [ .init("Add to playlist…", "music.note.list") { ui.addToPlaylist = [s] }, .init("Go to album", "square.stack") { goAlbum(s) }, .init("Go to artist", "person") { goArtist(s) }, .init(player.stopAfterCurrent ? "Keep playing after this song" : "Stop after this song", "stop.circle") { player.stopAfterCurrent.toggle() ui.show(player.stopAfterCurrent ? "Stops after this song" : "Keeps playing") }, .init("Equalizer", "slider.vertical.3") { showEQ = true }, .init("Search lyrics again", "arrow.clockwise") { player.reloadLyrics(online: true); showLyrics = true }, .init("Edit / save lyrics…", "text.quote") { showLyricsEditor = true }, .init("Choose cover…", "photo") { showCovers = true }, ] if !s.isStream { items.append(.init("Details", "info.circle") { ui.details = s }) items.append(.init("Share", "square.and.arrow.up") { share([s.url]) }) } if Scrobbler.shared.lfmOn { items.append(.init("Love on Last.fm", "heart.circle") { Scrobbler.shared.love(s, true) }) } ui.menu = MenuSheet(title: s.displayTitle, subtitle: s.subtitle, song: s, items: items) } } /// Synced lyrics, auto-scrolling, tap a line to seek (Android LyricsView). struct LyricsPanel: View { @EnvironmentObject var theme: Theme @EnvironmentObject var player: Player @State private var userScrolled = Date.distantPast var body: some View { let p = theme.palette Group { if let lr = player.lyrics, !lr.lines.isEmpty { TimelineView(.periodic(from: .now, by: 0.2)) { _ in let cur = lr.synced ? Lyrics.lineAt(lr.lines, player.position) : -1 ScrollViewReader { proxy in ScrollView(showsIndicators: false) { VStack(alignment: .leading, spacing: 18) { Color.clear.frame(height: 100) ForEach(Array(lr.lines.enumerated()), id: \.offset) { i, l in Text(l.text.isEmpty ? "♪" : l.text) .font(.system(size: lr.synced ? 24 : 19, weight: lr.synced ? .bold : .medium)) .foregroundColor(i == cur ? p.cOnSurface : p.cOnSurfaceVar.opacity(lr.synced ? (i < cur ? 0.45 : 0.6) : 0.9)) .scaleEffect(i == cur ? 1.0 : 0.96, anchor: .leading) .animation(.easeOut(duration: 0.25), value: cur) .id(i) .onTapGesture { if l.t >= 0 { player.seek(l.t) } } } if !lr.source.isEmpty { Text("Lyrics from \(lr.source)").font(.system(size: 12)).foregroundColor(p.cOnSurfaceVar.opacity(0.6)) } Color.clear.frame(height: 200) } .frame(maxWidth: .infinity, alignment: .leading) .padding(.horizontal, 28) } .simultaneousGesture(DragGesture().onChanged { _ in userScrolled = Date() }) .onChange(of: cur) { c in guard c >= 0, Date().timeIntervalSince(userScrolled) > 3 else { return } withAnimation(.easeInOut(duration: 0.4)) { proxy.scrollTo(c, anchor: UnitPoint(x: 0, y: 0.35)) } } .onAppear { if cur >= 0 { proxy.scrollTo(cur, anchor: UnitPoint(x: 0, y: 0.35)) } } } } .mask(LinearGradient(stops: [.init(color: .clear, location: 0), .init(color: .black, location: 0.12), .init(color: .black, location: 0.85), .init(color: .clear, location: 1)], startPoint: .top, endPoint: .bottom)) } else { VStack(spacing: 14) { Image(systemName: "text.quote").font(.system(size: 36)).foregroundColor(p.cPrimary) Text(player.lyrics?.message.isEmpty == false ? player.lyrics!.message : (player.lyrics == nil ? "Looking for lyrics…" : "No lyrics")) .multilineTextAlignment(.center).foregroundColor(p.cOnSurfaceVar) Pill(icon: "arrow.clockwise", title: "Search again") { player.reloadLyrics(online: true) } } .padding(32) } } } } /// Up-next queue with reorder and swipe to remove. struct QueueSheet: View { @EnvironmentObject var theme: Theme @EnvironmentObject var player: Player @EnvironmentObject var ui: UIState @State private var saveName = "" @State private var askSave = false var body: some View { let p = theme.palette NavigationStack { ScrollViewReader { proxy in List { ForEach(Array(player.queue.enumerated()), id: \.offset) { i, s in SongRow(song: s, current: i == player.index) .listRowInsets(EdgeInsets()) .listRowBackground(i == player.index ? p.cSecondaryC.opacity(0.5) : Color.clear) .onTapGesture { player.jump(i) } .id(i) } .onDelete { idx in idx.sorted(by: >).forEach { player.removeAt($0) } } .onMove { from, to in if let f = from.first { player.move(f, to) } } } .listStyle(.plain) .scrollContentBackground(.hidden) .environment(\.editMode, .constant(.active)) .onAppear { proxy.scrollTo(max(0, player.index - 2), anchor: .top) } } .navigationTitle("Queue · \(player.queue.count)") .navigationBarTitleDisplayMode(.inline) .toolbar { ToolbarItem(placement: .topBarLeading) { Button("Save") { saveName = "Queue \(Date().formatted(date: .abbreviated, time: .omitted))"; askSave = true } } ToolbarItem(placement: .topBarTrailing) { Button("Clear", role: .destructive) { player.clearQueue() } } } .alert("Save queue as playlist", isPresented: $askSave) { TextField("Name", text: $saveName) Button("Save") { Playlists.shared.create(saveName, player.queue); ui.show("Saved") } Button("Cancel", role: .cancel) {} } } .presentationDetents([.large]) .presentationBackground(p.cSurfaceLow) } } struct SleepSheet: View { @EnvironmentObject var theme: Theme @EnvironmentObject var player: Player @Environment(\.dismiss) var dismiss var body: some View { let p = theme.palette VStack(alignment: .leading, spacing: 0) { Text("Sleep timer").font(.system(size: 22, weight: .semibold)).foregroundColor(p.cOnSurface).padding(24) if let at = player.sleepAt { Text("Stops at \(at.formatted(date: .omitted, time: .shortened))").foregroundColor(p.cPrimary).padding(.horizontal, 24) } else if player.sleepAtTrackEnd { Text("Stops after this song").foregroundColor(p.cPrimary).padding(.horizontal, 24) } ForEach([5, 15, 30, 45, 60, 90, 120], id: \.self) { m in row("\(m) minutes") { player.sleep(minutes: m) } } row("End of this song") { player.sleepAtEndOfTrack() } row("Turn off") { player.cancelSleep() } } .frame(maxWidth: .infinity, alignment: .leading) .presentationDetents([.medium, .large]) .presentationBackground(p.cSurfaceLow) } private func row(_ t: String, _ a: @escaping () -> Void) -> some View { Button { a(); dismiss() } label: { Text(t).font(.system(size: 17)).foregroundColor(theme.palette.cOnSurface) .frame(maxWidth: .infinity, alignment: .leading) .padding(.horizontal, 24).padding(.vertical, 13) .contentShape(Rectangle()) } .buttonStyle(.plain) } } struct EqualizerSheet: View { @EnvironmentObject var theme: Theme @EnvironmentObject var prefs: Prefs static let presets: [(String, [Double])] = [ ("Flat", [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), ("Bass boost", [6, 5, 4, 2, 0, 0, 0, 0, 0, 0]), ("Treble boost", [0, 0, 0, 0, 0, 1, 2, 4, 5, 6]), ("Rock", [4, 3, 2, 0, -1, -1, 1, 2, 3, 4]), ("Pop", [-1, 1, 3, 4, 3, 0, -1, -1, 1, 2]), ("Vocal", [-2, -2, -1, 1, 3, 4, 3, 1, 0, -1]), ("Loudness", [5, 4, 1, 0, -1, 0, 0, 1, 4, 5]), ] var body: some View { let p = theme.palette VStack(spacing: 18) { HStack { Text("Equalizer").font(.system(size: 22, weight: .semibold)).foregroundColor(p.cOnSurface) Spacer() Toggle("", isOn: $prefs.eqOn).labelsHidden().tint(p.cPrimary) } ScrollView(.horizontal, showsIndicators: false) { HStack { ForEach(Self.presets, id: \.0) { pr in Chip(title: pr.0, selected: prefs.eqBands == pr.1) { prefs.eqBands = pr.1; prefs.eqOn = true; Player.shared.applyEQ() } } } } HStack(alignment: .bottom, spacing: 0) { ForEach(0..<10, id: \.self) { i in VStack(spacing: 6) { Text(String(format: "%+.0f", prefs.eqBands[i])).font(.system(size: 11).monospacedDigit()).foregroundColor(p.cOnSurfaceVar) Slider(value: Binding(get: { prefs.eqBands[i] }, set: { prefs.eqBands[i] = $0; Player.shared.applyEQ() }), in: -12...12, step: 1) .rotationEffect(.degrees(-90)) .frame(width: 180, height: 30) .frame(width: 30, height: 180) let f = Player.eqFreqs[i] Text(f >= 1000 ? "\(Int(f / 1000))k" : "\(Int(f))").font(.system(size: 11)).foregroundColor(p.cOnSurfaceVar) } .frame(maxWidth: .infinity) } } .disabled(!prefs.eqOn) .opacity(prefs.eqOn ? 1 : 0.5) Spacer() } .padding(24) .tint(p.cPrimary) .onChange(of: prefs.eqOn) { _ in Player.shared.applyEQ() } .presentationDetents([.medium]) .presentationBackground(p.cSurfaceLow) } }