-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArtists.swift
More file actions
77 lines (67 loc) · 1.37 KB
/
Artists.swift
File metadata and controls
77 lines (67 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import SwiftUI
import AVFoundation
struct Artists: View {
@State private var artists: [Artist] = []
@State private var isLoading = true
var body: some View {
VStack {
if isLoading {
ProgressView("Loading artists...")
} else if artists.isEmpty {
Text("No artists found")
.font(.headline)
.foregroundColor(.gray)
.padding()
} else {
List {
ForEach(artists) { artist in
NavigationLink(destination: ArtistDetailView(artist: artist)) {
Text(artist.name)
}
}
}
}
}
.navigationTitle("Artists")
.onAppear {
loadArtistsJ()
}
}
private func loadArtistsJ() {
isLoading = true
Task {
artists = await loadArtistsFromJson()
}
isLoading = false
}
private func loadArtists() {
isLoading = true
Task {
let songs = await loadSongsFromJson()
let groupedArtists = groupSongsByArtist(songs)
await MainActor.run {
artists = groupedArtists
isLoading = false
}
}
}
}
struct ArtistDetailView: View {
let artist: Artist
var sortedSongs: [Song] {
artist.songs.sorted(by: { $0.title < $1.title })
}
var body: some View {
VStack {
List {
ForEach(sortedSongs) { song in
NavigationLink(destination: AudioPlayerView(song: song, allSongs: sortedSongs)) {
Text(song.title)
.font(.body)
}
}
}
}
.navigationTitle(artist.name)
}
}