-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlbumView.swift
85 lines (77 loc) · 2.66 KB
/
AlbumView.swift
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
import SwiftUI
// Album Model
struct Album: Codable, Identifiable {
let id: Int
let name: String
let cover: String
let releaseDate: String
let tracks: [String]
let youtubeID: String
let spotifyID: String
}
// 讀取專輯資料
func loadAlbums() -> [Album] {
guard let url = Bundle.main.url(forResource: "album", withExtension: "json"),
let data = try? Data(contentsOf: url),
let albums = try? JSONDecoder().decode([Album].self, from: data) else {
return []
}
return albums
}
struct AlbumView: View {
let albums: [Album]
let columns = [
GridItem(.adaptive(minimum: 200))
]
var body: some View {
ScrollView {
LazyVGrid(columns: columns, spacing: 20) {
ForEach(albums) { album in
NavigationLink(destination: AlbumDetailView(album: album)) {
VStack {
Image(album.cover)
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: 200, height: 200)
.cornerRadius(10)
Text(album.name)
.font(.headline)
.lineLimit(1)
Text(album.releaseDate)
.font(.subheadline)
.foregroundColor(.gray)
}
.padding()
}
}
}
.padding(.horizontal)
}
.navigationTitle("專輯")
}
}
struct AlbumDetailView: View {
let album: Album
var body: some View {
BackgroundContainer {
List {
Section {
SpotifyPlayerView(spotifyID: album.spotifyID, type: "album")
.frame(height: 152)
.cornerRadius(10)
.shadow(radius: 5)
.listRowInsets(EdgeInsets()) // 移除內建的 section 內邊距
.listRowBackground(Color.clear) // 確保背景透明
}
Section(header: Text("曲目列表")) {
ForEach(album.tracks, id: \.self) { track in
NavigationLink(track, destination: SongDetailView(title: track))
}
}
}
.listStyle(InsetGroupedListStyle())
.scrollContentBackground(.hidden)
.navigationTitle(album.name)
}
}
}