-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSongView.swift
136 lines (119 loc) · 4.19 KB
/
SongView.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
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
import SwiftUI
import WebKit
// Music Model
struct Music: Codable, Identifiable {
var id: String { title }
let title: String
let enTitle: String
let releaseDate: String
let youtubeID: String
let spotifyID: String
}
// 讀取歌曲資料
func loadMusic() -> [Music] {
guard let url = Bundle.main.url(forResource: "music", withExtension: "json"),
let data = try? Data(contentsOf: url),
let music = try? JSONDecoder().decode([Music].self, from: data) else {
return []
}
return music
}
struct SongView: View {
let songs: [Music]
let columns = [
GridItem(.adaptive(minimum: 150))
]
var body: some View {
ScrollView {
LazyVGrid(columns: columns, spacing: 20) {
ForEach(songs) { song in
NavigationLink(destination: SongDetailView(title: song.title)) {
VStack {
Image(song.enTitle.replacingOccurrences(of: " ", with: ""))
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: 150, height: 150)
.cornerRadius(10)
Text(song.title)
.font(.headline)
.lineLimit(1)
}
.padding()
}
}
}
.padding(.horizontal)
}
.navigationTitle("所有歌曲")
}
}
// Song Detail View
struct SongDetailView: View {
let title: String
@State private var song: Music?
@Environment(\.openURL) private var openURL
var body: some View {
BackgroundContainer {
VStack {
if let song = song {
Spacer()
// 顯示圖片
Image(song.enTitle.replacingOccurrences(of: " ", with: ""))
.resizable()
.aspectRatio(contentMode: .fit)
.frame(maxHeight: 250)
.cornerRadius(10)
.shadow(radius: 5)
// 顯示歌曲標題
Text(song.title)
.font(.largeTitle)
.bold()
.multilineTextAlignment(.center)
// 顯示英文標題
Text(song.enTitle)
.font(.title2)
.foregroundColor(.gray)
.multilineTextAlignment(.center)
// 顯示發行時間
Text("發行時間: \(song.releaseDate)")
.font(.subheadline)
.foregroundColor(.secondary)
.padding(.top, 5)
Spacer()
// 顯示 Spotify 播放器
SpotifyPlayerView(spotifyID: song.spotifyID)
.frame(height: 80)
.cornerRadius(10)
.shadow(radius: 5)
} else {
Text("正在載入歌曲資訊...")
.font(.headline)
.padding(.top, 50)
}
}
.padding(.horizontal)
.onAppear {
loadSong()
}
}
}
// 根據 title 載入對應的歌曲
private func loadSong() {
let allSongs = loadMusic()
self.song = allSongs.first { $0.title == title }
}
}
// Spotify 播放器
struct SpotifyPlayerView: UIViewRepresentable {
let spotifyID: String
var type: String = "track"
func makeUIView(context: Context) -> WKWebView {
WKWebView()
}
func updateUIView(_ uiView: WKWebView, context: Context) {
if let url = URL(string: "https://open.spotify.com/embed/\(type)/\(spotifyID)?utm_source=generator") {
let request = URLRequest(url: url)
uiView.load(request)
}
}
}