Yandex Music client for Apple TV
tvOS 17+ SwiftUI client for the unofficial Yandex Music API: rotor stations with feedback, library, search, artist and album pages, synced lyrics, and system Now Playing integration.
This commit is contained in:
@@ -0,0 +1,159 @@
|
||||
import SwiftUI
|
||||
|
||||
/// Synced lyrics, with a run-in before the first line.
|
||||
struct LyricsPane: View {
|
||||
let lyrics: Lyrics?
|
||||
|
||||
/// Intros longer than this get dots for their whole length; shorter ones
|
||||
/// only for the last few seconds.
|
||||
private let longIntro: Double = 5
|
||||
private let shortLead: Double = 4
|
||||
/// Words appear this far before the dots run out, so the first line is
|
||||
/// already on screen and rolls away instead of appearing from nothing.
|
||||
private let wordsLead: Double = 1
|
||||
|
||||
@ObservedObject private var clock = PlayerController.shared.clock
|
||||
|
||||
init(lyrics: Lyrics?) {
|
||||
self.lyrics = lyrics
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
// Dots and words share an origin, so the first line lands exactly where
|
||||
// the dots were.
|
||||
ZStack(alignment: .topLeading) {
|
||||
if !linesVisible, let range = countdownRange {
|
||||
CountdownDots(start: range.start, end: range.end)
|
||||
.transition(.opacity)
|
||||
}
|
||||
|
||||
if linesVisible {
|
||||
LyricsLines(
|
||||
lyrics: lyrics,
|
||||
activeID: lyrics?.activeIndex(at: clock.currentTime),
|
||||
armed: armed
|
||||
)
|
||||
.equatable()
|
||||
.transition(.opacity)
|
||||
}
|
||||
}
|
||||
.animation(.easeInOut(duration: 0.45), value: linesVisible)
|
||||
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
|
||||
}
|
||||
|
||||
private var firstLineTime: Double? {
|
||||
lyrics?.lines.first(where: { !$0.isBlank })?.time
|
||||
}
|
||||
|
||||
private var countdownStart: Double {
|
||||
guard let first = firstLineTime else { return 0 }
|
||||
return first > longIntro ? 0 : max(first - shortLead, 0)
|
||||
}
|
||||
|
||||
private var countdownRange: (start: Double, end: Double)? {
|
||||
guard let first = firstLineTime else { return nil }
|
||||
let now = clock.currentTime
|
||||
guard now >= countdownStart, now < first else { return nil }
|
||||
return (countdownStart, first)
|
||||
}
|
||||
|
||||
private var linesVisible: Bool {
|
||||
guard let first = firstLineTime else { return true }
|
||||
return clock.currentTime >= first - wordsLead
|
||||
}
|
||||
|
||||
private var armed: Bool {
|
||||
guard firstLineTime != nil else { return true }
|
||||
return clock.currentTime >= countdownStart
|
||||
}
|
||||
}
|
||||
|
||||
/// Three dots filling one after another across the run-in. Each fills all the
|
||||
/// way before the next one starts.
|
||||
private struct CountdownDots: View {
|
||||
let start: Double
|
||||
let end: Double
|
||||
|
||||
@ObservedObject private var clock = PlayerController.shared.clock
|
||||
|
||||
init(start: Double, end: Double) {
|
||||
self.start = start
|
||||
self.end = end
|
||||
}
|
||||
|
||||
private var progress: Double {
|
||||
let span = max(end - start, 0.001)
|
||||
return min(max((clock.currentTime - start) / span, 0), 1)
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
HStack(spacing: 22) {
|
||||
ForEach(0 ..< 3, id: \.self) { index in
|
||||
let fill = min(max(progress * 3 - Double(index), 0), 1)
|
||||
Circle()
|
||||
.fill(.white.opacity(0.18 + 0.77 * fill))
|
||||
.frame(width: 30, height: 30)
|
||||
}
|
||||
}
|
||||
.padding(.top, 26)
|
||||
// The clock ticks five times a second; this rides between the ticks.
|
||||
.animation(.linear(duration: 0.2), value: progress)
|
||||
}
|
||||
}
|
||||
|
||||
/// The line being sung sits at the top, the lines still to come fade away below
|
||||
/// it, and everything already sung is gone.
|
||||
///
|
||||
/// Not a ScrollView: driving a scroll offset from a timer made the highlight
|
||||
/// drift out of view.
|
||||
private struct LyricsLines: View, Equatable {
|
||||
let lyrics: Lyrics?
|
||||
let activeID: Int?
|
||||
let armed: Bool
|
||||
|
||||
private let window = 6
|
||||
|
||||
/// Equatable collapses five ticks a second into one rebuild per line.
|
||||
static func == (lhs: Self, rhs: Self) -> Bool {
|
||||
lhs.activeID == rhs.activeID
|
||||
&& lhs.armed == rhs.armed
|
||||
&& lhs.lyrics == rhs.lyrics
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
rolling
|
||||
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
|
||||
}
|
||||
|
||||
private var visible: [LyricsLine] {
|
||||
guard armed else { return [] }
|
||||
let sung = (lyrics?.lines ?? []).filter { !$0.isBlank }
|
||||
guard let anchor = activeID ?? sung.first?.id else { return [] }
|
||||
return Array(sung.filter { $0.id >= anchor }.prefix(window))
|
||||
}
|
||||
|
||||
private var rolling: some View {
|
||||
VStack(alignment: .leading, spacing: 34) {
|
||||
ForEach(visible) { line in
|
||||
Text(line.text)
|
||||
.font(.system(size: line.id == activeID ? 96 : 76, weight: .bold))
|
||||
.foregroundStyle(.white.opacity(opacity(for: line)))
|
||||
.fixedSize(horizontal: false, vertical: true)
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
.transition(.opacity)
|
||||
}
|
||||
|
||||
Spacer(minLength: 0)
|
||||
}
|
||||
.animation(.easeInOut(duration: 0.35), value: activeID)
|
||||
.clipped()
|
||||
}
|
||||
|
||||
/// The sung line is solid; each line further ahead is dimmer than the last.
|
||||
private func opacity(for line: LyricsLine) -> Double {
|
||||
guard let activeID else { return 0.4 }
|
||||
if line.id == activeID { return 1 }
|
||||
let position = visible.firstIndex(where: { $0.id == line.id }) ?? 0
|
||||
return max(0.08, 0.32 - Double(position) * 0.07)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,210 @@
|
||||
import SwiftUI
|
||||
|
||||
/// The player screen.
|
||||
///
|
||||
/// Every width and height here is a constant. Whether a track has lyrics comes
|
||||
/// from its metadata, which arrives with the track, so the two columns are
|
||||
/// placed once per song and never move while it plays.
|
||||
struct NowPlayingView: View {
|
||||
@EnvironmentObject private var player: PlayerController
|
||||
|
||||
private enum Control: Hashable { case chrome, scrubber, previous, play, next, like }
|
||||
|
||||
private let stageHeight: CGFloat = 620
|
||||
private let detailsWidth: CGFloat = 430
|
||||
private let lyricsWidth: CGFloat = 1150
|
||||
|
||||
@State private var likeBusy = false
|
||||
@FocusState private var focus: Control?
|
||||
|
||||
var body: some View {
|
||||
ZStack {
|
||||
BlurredBackdrop(url: player.current?.coverURL(size: 200))
|
||||
if let track = player.current {
|
||||
content(for: track)
|
||||
} else {
|
||||
idle
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private var idle: some View {
|
||||
VStack(spacing: 16) {
|
||||
Image(systemName: "music.note")
|
||||
.font(.system(size: 80))
|
||||
.foregroundStyle(.secondary)
|
||||
Text("Ничего не играет")
|
||||
.font(.title2)
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Layout
|
||||
|
||||
private func content(for track: Track) -> some View {
|
||||
VStack(spacing: 0) {
|
||||
// The tab bar floats over the content: this strip reserves room for
|
||||
// it and gives focus a step on the way back up to it.
|
||||
Color.clear
|
||||
.frame(height: 96)
|
||||
.contentShape(Rectangle())
|
||||
.focusable()
|
||||
.focused($focus, equals: .chrome)
|
||||
|
||||
HStack(alignment: .top, spacing: showsLyrics ? 72 : 0) {
|
||||
details(for: track)
|
||||
.frame(width: detailsWidth, height: stageHeight, alignment: .top)
|
||||
.clipped()
|
||||
|
||||
LyricsPane(lyrics: player.lyricsState == .ready ? player.lyrics : nil)
|
||||
.frame(
|
||||
width: showsLyrics ? lyricsWidth : 0,
|
||||
height: stageHeight,
|
||||
alignment: .topLeading
|
||||
)
|
||||
.clipped()
|
||||
.opacity(showsLyrics ? 1 : 0)
|
||||
}
|
||||
.frame(
|
||||
maxWidth: .infinity,
|
||||
maxHeight: .infinity,
|
||||
alignment: showsLyrics ? .topLeading : .top
|
||||
)
|
||||
|
||||
transportBar
|
||||
}
|
||||
.padding(.bottom, 24)
|
||||
}
|
||||
|
||||
private var showsLyrics: Bool {
|
||||
player.current?.lyricsInfo?.hasSynced ?? true
|
||||
}
|
||||
|
||||
private func details(for track: Track) -> some View {
|
||||
VStack(alignment: .leading, spacing: 24) {
|
||||
CoverImage(url: track.coverURL(size: 400), corner: 18)
|
||||
.frame(width: 390, height: 390)
|
||||
|
||||
VStack(alignment: .leading, spacing: 8) {
|
||||
sourceLabel
|
||||
Text(track.displayTitle)
|
||||
.font(.system(size: 40, weight: .bold))
|
||||
.lineLimit(2)
|
||||
Text(track.artistNames)
|
||||
.font(.system(size: 26))
|
||||
.foregroundStyle(.white.opacity(0.65))
|
||||
.lineLimit(1)
|
||||
}
|
||||
|
||||
Spacer(minLength: 0)
|
||||
}
|
||||
}
|
||||
|
||||
@ViewBuilder
|
||||
private var sourceLabel: some View {
|
||||
switch player.source {
|
||||
case let .rotor(_, name):
|
||||
Label(name.uppercased(), systemImage: "dot.radiowaves.left.and.right")
|
||||
.font(.caption.weight(.semibold))
|
||||
.foregroundStyle(Color.ymAccent)
|
||||
case let .collection(name):
|
||||
Text(name.uppercased())
|
||||
.font(.caption.weight(.semibold))
|
||||
.foregroundStyle(Color.ymAccent)
|
||||
case .none:
|
||||
EmptyView()
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Transport
|
||||
|
||||
private var transportBar: some View {
|
||||
VStack(alignment: .trailing, spacing: 16) {
|
||||
HStack(spacing: 20) {
|
||||
Spacer(minLength: 0)
|
||||
|
||||
Button { player.previous() } label: {
|
||||
Image(systemName: "backward.fill")
|
||||
}
|
||||
.buttonStyle(GlyphButtonStyle())
|
||||
.focused($focus, equals: .previous)
|
||||
|
||||
Button { player.togglePlayPause() } label: {
|
||||
Image(systemName: player.isPlaying ? "pause.fill" : "play.fill")
|
||||
}
|
||||
.buttonStyle(GlyphButtonStyle(prominent: true))
|
||||
.focused($focus, equals: .play)
|
||||
|
||||
Button { player.next() } label: {
|
||||
Image(systemName: "forward.fill")
|
||||
}
|
||||
.buttonStyle(GlyphButtonStyle())
|
||||
.focused($focus, equals: .next)
|
||||
|
||||
Button { toggleLike() } label: {
|
||||
Image(systemName: isLiked ? "heart.fill" : "heart")
|
||||
}
|
||||
.buttonStyle(GlyphButtonStyle(tinted: isLiked))
|
||||
.focused($focus, equals: .like)
|
||||
.disabled(likeBusy)
|
||||
}
|
||||
|
||||
Scrubber(
|
||||
seek: { player.seek(to: $0) },
|
||||
togglePlayPause: { player.togglePlayPause() }
|
||||
)
|
||||
.focused($focus, equals: .scrubber)
|
||||
}
|
||||
.focusSection()
|
||||
}
|
||||
|
||||
private var isLiked: Bool { player.isLiked(player.current) }
|
||||
|
||||
private func toggleLike() {
|
||||
likeBusy = true
|
||||
Task {
|
||||
await player.toggleLikeCurrent()
|
||||
likeBusy = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A glyph that takes a solid disc on focus. No scaling: growing a focused view
|
||||
/// makes tvOS swap in a snapshot of the screen.
|
||||
private struct GlyphButtonStyle: ButtonStyle {
|
||||
var tinted = false
|
||||
var prominent = false
|
||||
|
||||
func makeBody(configuration: Configuration) -> some View {
|
||||
StyleBody(configuration: configuration, tinted: tinted, prominent: prominent)
|
||||
}
|
||||
|
||||
private struct StyleBody: View {
|
||||
@Environment(\.isFocused) private var isFocused
|
||||
@Environment(\.isEnabled) private var isEnabled
|
||||
let configuration: ButtonStyleConfiguration
|
||||
let tinted: Bool
|
||||
let prominent: Bool
|
||||
|
||||
private var foreground: Color {
|
||||
if isFocused { return .black }
|
||||
return tinted ? .ymAccent : .white
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
configuration.label
|
||||
.font(.system(size: prominent ? 34 : 28, weight: .semibold))
|
||||
.foregroundStyle(foreground)
|
||||
.opacity(isEnabled ? (configuration.isPressed ? 0.7 : 1) : 0.3)
|
||||
.frame(width: prominent ? 88 : 72, height: prominent ? 88 : 72)
|
||||
.background {
|
||||
Circle().fill(
|
||||
isFocused
|
||||
? AnyShapeStyle(.white)
|
||||
: AnyShapeStyle(.white.opacity(prominent ? 0.22 : 0.12))
|
||||
)
|
||||
}
|
||||
.animation(.easeOut(duration: 0.15), value: isFocused)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
import SwiftUI
|
||||
|
||||
/// Full-width transport bar.
|
||||
///
|
||||
/// Swipes in the same direction build up speed, the target is previewed while
|
||||
/// scrubbing, and the seek is committed once the swiping stops.
|
||||
struct Scrubber: View {
|
||||
let seek: (Double) -> Void
|
||||
let togglePlayPause: () -> Void
|
||||
|
||||
private let steps: [Double] = [5, 10, 20, 30, 45, 60]
|
||||
private let streakWindow: TimeInterval = 0.4
|
||||
private let commitDelay: UInt64 = 500_000_000
|
||||
|
||||
@ObservedObject private var clock = PlayerController.shared.clock
|
||||
@FocusState private var focused: Bool
|
||||
|
||||
@State private var preview: Double?
|
||||
@State private var lastMove = Date.distantPast
|
||||
@State private var streak = 0
|
||||
@State private var commitTask: Task<Void, Never>?
|
||||
|
||||
private var position: Double { preview ?? clock.currentTime }
|
||||
|
||||
private var fraction: Double {
|
||||
guard clock.duration > 0 else { return 0 }
|
||||
return min(max(position / clock.duration, 0), 1)
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
VStack(spacing: 12) {
|
||||
bar.frame(height: 16)
|
||||
|
||||
HStack {
|
||||
Text(formatTime(position))
|
||||
Spacer()
|
||||
Text("-" + formatTime(max(clock.duration - position, 0)))
|
||||
}
|
||||
.font(.system(size: 24, weight: .medium).monospacedDigit())
|
||||
.foregroundStyle(.white.opacity(focused ? 0.95 : 0.55))
|
||||
}
|
||||
.padding(.vertical, 10)
|
||||
.contentShape(Rectangle())
|
||||
.focusable()
|
||||
.focused($focused)
|
||||
.onMoveCommand { direction in
|
||||
switch direction {
|
||||
case .left: nudge(-1)
|
||||
case .right: nudge(1)
|
||||
default: break
|
||||
}
|
||||
}
|
||||
.onExitCommand { cancelScrub() }
|
||||
.onPlayPauseCommand { togglePlayPause() }
|
||||
.onChange(of: focused) { _, isFocused in
|
||||
if !isFocused { cancelScrub() }
|
||||
}
|
||||
}
|
||||
|
||||
/// Fixed height; only the drawing inside reacts to focus, so taking focus
|
||||
/// never re-lays-out the screen.
|
||||
private var bar: some View {
|
||||
GeometryReader { geo in
|
||||
let height: CGFloat = focused ? 14 : 8
|
||||
let knob: CGFloat = preview == nil ? 26 : 34
|
||||
|
||||
ZStack(alignment: .leading) {
|
||||
Capsule()
|
||||
.fill(.white.opacity(focused ? 0.32 : 0.2))
|
||||
.frame(height: height)
|
||||
Capsule()
|
||||
.fill(.white)
|
||||
.frame(width: geo.size.width * fraction, height: height)
|
||||
if focused {
|
||||
Circle()
|
||||
.fill(.white)
|
||||
.frame(width: knob, height: knob)
|
||||
.offset(x: geo.size.width * fraction - knob / 2)
|
||||
}
|
||||
}
|
||||
.frame(width: geo.size.width, height: geo.size.height, alignment: .leading)
|
||||
}
|
||||
}
|
||||
|
||||
private func nudge(_ direction: Double) {
|
||||
guard clock.duration > 0 else { return }
|
||||
|
||||
let now = Date()
|
||||
streak = now.timeIntervalSince(lastMove) < streakWindow
|
||||
? min(streak + 1, steps.count - 1)
|
||||
: 0
|
||||
lastMove = now
|
||||
|
||||
let target = (preview ?? clock.currentTime) + direction * steps[streak]
|
||||
preview = min(max(target, 0), clock.duration)
|
||||
|
||||
commitTask?.cancel()
|
||||
commitTask = Task {
|
||||
try? await Task.sleep(nanoseconds: commitDelay)
|
||||
guard !Task.isCancelled, let target = preview else { return }
|
||||
seek(target)
|
||||
preview = nil
|
||||
streak = 0
|
||||
}
|
||||
}
|
||||
|
||||
private func cancelScrub() {
|
||||
commitTask?.cancel()
|
||||
preview = nil
|
||||
streak = 0
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user