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.
113 lines
3.6 KiB
Swift
113 lines
3.6 KiB
Swift
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
|
|
}
|
|
}
|