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.
158 lines
5.8 KiB
Swift
158 lines
5.8 KiB
Swift
import SwiftUI
|
|
|
|
struct SearchView: View {
|
|
@EnvironmentObject private var player: PlayerController
|
|
|
|
@State private var query = ""
|
|
@State private var tracks: [Track] = []
|
|
@State private var albums: [Album] = []
|
|
@State private var artists: [Artist] = []
|
|
@State private var isSearching = false
|
|
@State private var searchTask: Task<Void, Never>?
|
|
|
|
private let grid = [GridItem(.adaptive(minimum: 280), spacing: 60)]
|
|
|
|
var body: some View {
|
|
NavigationStack {
|
|
results
|
|
// tvOS turns this into the system search scene: keyboard on the
|
|
// left, these results on the right, exactly like Apple Music.
|
|
.searchable(text: $query, prompt: "Исполнители, треки, альбомы")
|
|
.musicDestinations()
|
|
}
|
|
.onChange(of: query) { _, new in scheduleSearch(for: new) }
|
|
}
|
|
|
|
@ViewBuilder
|
|
private var results: some View {
|
|
ScrollView {
|
|
VStack(alignment: .leading, spacing: 50) {
|
|
if isSearching {
|
|
LoadingState(text: "Ищем…").frame(height: 400)
|
|
} else if query.trimmingCharacters(in: .whitespaces).isEmpty {
|
|
placeholder
|
|
} else if tracks.isEmpty, albums.isEmpty, artists.isEmpty {
|
|
placeholder
|
|
} else {
|
|
if !artists.isEmpty { artistSection.focusSection() }
|
|
if !tracks.isEmpty { trackSection.focusSection() }
|
|
if !albums.isEmpty { albumSection.focusSection() }
|
|
}
|
|
}
|
|
.padding(.vertical, 40)
|
|
}
|
|
}
|
|
|
|
private var placeholder: some View {
|
|
VStack(spacing: 16) {
|
|
Image(systemName: "magnifyingglass")
|
|
.font(.system(size: 72))
|
|
.foregroundStyle(.secondary)
|
|
Text(query.isEmpty ? "Что послушаем?" : "Ничего не нашлось")
|
|
.font(.title2)
|
|
.foregroundStyle(.secondary)
|
|
}
|
|
.frame(maxWidth: .infinity)
|
|
.padding(.top, 120)
|
|
}
|
|
|
|
private var artistSection: some View {
|
|
VStack(alignment: .leading, spacing: 20) {
|
|
Text("Исполнители").font(.title2.weight(.semibold))
|
|
ScrollView(.horizontal, showsIndicators: false) {
|
|
HStack(alignment: .top, spacing: 60) {
|
|
ForEach(artists.prefix(12), id: \.id) { artist in
|
|
PosterLink(
|
|
title: artist.name,
|
|
subtitle: nil,
|
|
imageURL: artist.coverURL(size: 200),
|
|
route: .artist(artist),
|
|
circular: true
|
|
)
|
|
}
|
|
}
|
|
.padding(.vertical, 24)
|
|
}
|
|
.scrollClipDisabled()
|
|
}
|
|
}
|
|
|
|
private var trackSection: some View {
|
|
VStack(alignment: .leading, spacing: 20) {
|
|
HStack(alignment: .firstTextBaseline) {
|
|
Text("Треки").font(.title2.weight(.semibold))
|
|
Spacer()
|
|
Button {
|
|
play(tracks, from: 0, title: "Поиск: \(query)")
|
|
} label: {
|
|
Label("Слушать всё", systemImage: "play.fill").font(.callout)
|
|
}
|
|
.buttonStyle(.plain)
|
|
}
|
|
|
|
LazyVGrid(columns: grid, spacing: 60) {
|
|
ForEach(Array(tracks.prefix(24).enumerated()), id: \.offset) { pair in
|
|
CardButton(
|
|
title: pair.element.displayTitle,
|
|
subtitle: pair.element.artistNames,
|
|
imageURL: pair.element.coverURL(size: 200)
|
|
) {
|
|
play(tracks, from: pair.offset, title: "Поиск: \(query)")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private var albumSection: some View {
|
|
VStack(alignment: .leading, spacing: 20) {
|
|
Text("Альбомы").font(.title2.weight(.semibold))
|
|
LazyVGrid(columns: grid, spacing: 60) {
|
|
ForEach(albums.prefix(12), id: \.id) { album in
|
|
PosterLink(
|
|
title: album.displayTitle,
|
|
subtitle: album.artistNames,
|
|
imageURL: album.coverURL(size: 200),
|
|
route: .album(album)
|
|
)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - Actions
|
|
|
|
private func play(_ list: [Track], from index: Int, title: String) {
|
|
player.play(tracks: list, startAt: index, source: .collection(title))
|
|
}
|
|
|
|
// MARK: - Search
|
|
|
|
private func scheduleSearch(for text: String) {
|
|
searchTask?.cancel()
|
|
let trimmed = text.trimmingCharacters(in: .whitespaces)
|
|
guard trimmed.count >= 2 else {
|
|
tracks = []; albums = []; artists = []
|
|
isSearching = false
|
|
return
|
|
}
|
|
isSearching = true
|
|
searchTask = Task {
|
|
// Let the on-screen keyboard settle before hitting the API.
|
|
try? await Task.sleep(nanoseconds: 450_000_000)
|
|
guard !Task.isCancelled else { return }
|
|
do {
|
|
let result = try await YMClient.shared.search(trimmed, type: "all")
|
|
guard !Task.isCancelled else { return }
|
|
tracks = result.tracks?.results ?? []
|
|
albums = result.albums?.results ?? []
|
|
artists = result.artists?.results ?? []
|
|
} catch {
|
|
guard !Task.isCancelled else { return }
|
|
log.error("\(error.localizedDescription)")
|
|
}
|
|
isSearching = false
|
|
}
|
|
}
|
|
}
|