Files
YaMusek/Sources/Views/Components.swift
T
Thorgathis acd50280bc 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.
2026-09-17 20:38:19 +03:00

190 lines
5.8 KiB
Swift

import SwiftUI
/// Yandex Music yellow, used sparingly as the single accent.
extension Color {
static let ymAccent = Color(red: 1.0, green: 0.87, blue: 0.16)
static let ymCard = Color.white.opacity(0.08)
}
struct CoverImage: View {
let url: URL?
var corner: CGFloat = 12
@ViewBuilder
var body: some View {
let image = AsyncImage(url: url) { phase in
switch phase {
case let .success(image):
image.resizable().aspectRatio(contentMode: .fill)
default:
Color.ymCard
}
}
if corner > 0 {
image.clipShape(RoundedRectangle(cornerRadius: corner))
} else {
image.clipped()
}
}
}
/// A poster-style button used for playlists and stations.
struct CardButton: View {
let title: String
let subtitle: String?
let imageURL: URL?
var width: CGFloat = 280
let action: () -> Void
var body: some View {
Button(action: action) {
CoverImage(url: imageURL, corner: 12)
.frame(width: width, height: width)
}
.buttonStyle(CardButtonStyle())
.overlay(alignment: .bottomLeading) {
VStack(alignment: .leading, spacing: 2) {
Text(title)
.font(.caption)
.lineLimit(1)
if let subtitle {
Text(subtitle)
.font(.caption2)
.foregroundStyle(.secondary)
.lineLimit(1)
}
}
.frame(width: width, alignment: .leading)
.offset(y: width * 0.22)
}
.padding(.bottom, subtitle == nil ? 46 : 66)
}
}
struct TrackRow: View {
let track: Track
let isCurrent: Bool
let action: () -> Void
var body: some View {
Button(action: action) {
HStack(spacing: 20) {
CoverImage(url: track.coverURL(size: 100))
.frame(width: 70, height: 70)
VStack(alignment: .leading, spacing: 4) {
Text(track.displayTitle)
.font(.headline)
.lineLimit(1)
Text(track.artistNames)
.font(.subheadline)
.foregroundStyle(.secondary)
.lineLimit(1)
}
Spacer(minLength: 12)
if isCurrent {
Image(systemName: "waveform")
.foregroundStyle(Color.ymAccent)
}
if track.available == false {
Image(systemName: "nosign").foregroundStyle(.secondary)
} else {
Text(formatTime(track.duration))
.font(.subheadline.monospacedDigit())
.foregroundStyle(.secondary)
}
}
.padding(.vertical, 6)
}
.buttonStyle(RowButtonStyle())
}
}
/// A list row that actually shows where the focus is — `.plain` on tvOS does not.
struct RowButtonStyle: ButtonStyle {
func makeBody(configuration: Configuration) -> some View {
StyleBody(configuration: configuration)
}
private struct StyleBody: View {
@Environment(\.isFocused) private var isFocused
let configuration: ButtonStyleConfiguration
var body: some View {
configuration.label
.padding(.horizontal, 24)
.padding(.vertical, 8)
.background {
RoundedRectangle(cornerRadius: 16, style: .continuous)
.fill(.white.opacity(isFocused ? 0.16 : 0))
}
// No scaling here: a row that grows on focus spills over its
// neighbours in a tightly packed list.
.opacity(configuration.isPressed ? 0.7 : 1)
.animation(.easeOut(duration: 0.15), value: isFocused)
}
}
}
/// Round artwork (artists) that focuses without a square card behind it.
struct CircleCardStyle: ButtonStyle {
func makeBody(configuration: Configuration) -> some View {
StyleBody(configuration: configuration)
}
private struct StyleBody: View {
@Environment(\.isFocused) private var isFocused
let configuration: ButtonStyleConfiguration
var body: some View {
configuration.label
.overlay {
if isFocused {
Circle().strokeBorder(.white.opacity(0.95), lineWidth: 5)
}
}
.scaleEffect(configuration.isPressed ? 0.96 : (isFocused ? 1.05 : 1))
.animation(.spring(response: 0.3, dampingFraction: 0.7), value: isFocused)
}
}
}
struct ErrorBanner: View {
let message: String
var body: some View {
HStack(alignment: .top, spacing: 12) {
Image(systemName: "exclamationmark.triangle.fill")
.foregroundStyle(.orange)
Text(message)
.font(.footnote)
.foregroundStyle(.white.opacity(0.9))
.multilineTextAlignment(.leading)
Spacer(minLength: 0)
}
.padding(16)
.background(Color.white.opacity(0.1), in: RoundedRectangle(cornerRadius: 12, style: .continuous))
}
}
struct LoadingState: View {
let text: String
var body: some View {
VStack(spacing: 16) {
ProgressView()
Text(text).foregroundStyle(.secondary)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
func formatTime(_ seconds: Double) -> String {
guard seconds.isFinite, seconds >= 0 else { return "--:--" }
let total = Int(seconds.rounded())
return String(format: "%d:%02d", total / 60, total % 60)
}