forked from HammerPot/Maple-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlbums.swift
More file actions
165 lines (147 loc) · 3.94 KB
/
Albums.swift
File metadata and controls
165 lines (147 loc) · 3.94 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import SwiftUI
import AVFoundation
struct Albums: View {
@State private var albums: [Album] = []
@State private var isLoading = true
private let columns = [
GridItem(.adaptive(minimum: 160), spacing: 16)
]
var body: some View {
ScrollView {
if isLoading {
ProgressView("Loading albums...")
.padding()
} else if albums.isEmpty {
Text("No albums found")
.font(.headline)
.foregroundColor(.gray)
.padding()
} else {
LazyVGrid(columns: columns, spacing: 16) {
ForEach(albums) { album in
NavigationLink(destination: AlbumDetailView(album: album)) {
VStack(alignment: .leading) {
if let artwork = album.artwork {
let artworkPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent(artwork)
if let uiImage = UIImage(contentsOfFile: artworkPath.path){
Image(uiImage: uiImage)
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: 160, height: 160)
.cornerRadius(8)
}
} else {
Rectangle()
.fill(Color.gray.opacity(0.2))
.frame(width: 160, height: 160)
.cornerRadius(8)
.overlay(
Image(systemName: "music.note")
.font(.system(size: 40))
.foregroundColor(.gray)
)
}
Text(album.name)
.font(.headline)
.foregroundColor(.primary)
.lineLimit(1)
Text(album.artist)
.font(.subheadline)
.foregroundColor(.secondary)
.lineLimit(1)
Text("\(album.songs.count) tracks")
.font(.caption)
.foregroundColor(.secondary)
}
.frame(width: 160)
}
}
}
.padding()
}
}
.navigationTitle("Albums")
.onAppear {
loadAlbumsJ()
}
}
private func loadAlbumsJ() {
isLoading = true
Task {
albums = await loadAlbumsFromJson()
}
isLoading = false
}
private func loadAlbums() {
isLoading = true
Task {
let songs = await loadSongsFromJson()
let groupedAlbums = groupSongsByAlbum(songs)
await MainActor.run {
albums = groupedAlbums
isLoading = false
}
}
}
}
struct AlbumDetailView: View {
let album: Album
var sortedSongs: [Song] {
album.songs.sorted(by: {
if $0.discNumber != $1.discNumber {
return $0.discNumber < $1.discNumber
} else {
return $0.trackNumber < $1.trackNumber
}
})
}
var body: some View {
VStack {
List {
Section{
ForEach(sortedSongs) { song in
NavigationLink(destination: AudioPlayerView(song: song, allSongs: sortedSongs)) {
HStack {
Text("\(song.trackNumber).")
.font(.caption)
.foregroundColor(.secondary)
.frame(width: 25, alignment: .trailing)
Text(song.title)
.font(.body)
Spacer()
Text("(Disc \(song.discNumber))")
.font(.caption)
.foregroundColor(.secondary)
}
}
}
} header: {
VStack(alignment: .leading, spacing: 8) {
if let artwork = album.artwork {
let artworkPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent(artwork)
if let uiImage = UIImage(contentsOfFile: artworkPath.path){
Image(uiImage: uiImage)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(maxWidth: .infinity)
.cornerRadius(8)
.padding(.horizontal)
}
}
Text(album.name)
.font(.title)
.bold()
.foregroundColor(.primary)
.frame(maxWidth: .infinity, alignment: .center)
Text(album.artist)
.font(.title2)
.foregroundColor(.secondary)
.frame(maxWidth: .infinity, alignment: .center)
}
.frame(maxWidth: .infinity, alignment: .leading)
.padding(.horizontal)
}
}
}
}
}