Skip to content

Commit

Permalink
trying sth new
Browse files Browse the repository at this point in the history
  • Loading branch information
ElliotCHEN37 committed Oct 31, 2024
1 parent 712e7d8 commit f92c8c7
Showing 1 changed file with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ extension EeveeLyricsSettingsView {
TextField("user_token_placeholder".localized, text: $musixmatchToken)
.foregroundColor(.gray)

Button("獲取用戶令牌") {
Button("obtain_user_token".localized) {
fetchUserToken()
}
.padding(.top, 5)
Expand Down Expand Up @@ -85,11 +85,18 @@ extension EeveeLyricsSettingsView {

URLSession.shared.dataTask(with: url) { data, response, error in
if let error = error {
print("Error fetching user token: \(error)")
DispatchQueue.main.async {
self.errorMessage = "error_fetching_user_token".localized + ": \(error.localizedDescription)"
}
return
}

guard let data = data else { return }
guard let data = data else {
DispatchQueue.main.async {
self.errorMessage = "no_data_received".localized
}
return
}

do {
let json = try JSONSerialization.jsonObject(with: data, options: [])
Expand All @@ -101,10 +108,35 @@ extension EeveeLyricsSettingsView {
DispatchQueue.main.async {
self.musixmatchToken = userToken
}
} else {
DispatchQueue.main.async {
self.errorMessage = "failed_to_parse_user_token".localized
}
}
} catch {
print("Error parsing JSON: \(error)")
DispatchQueue.main.async {
self.errorMessage = "error_parsing_json".localized + ": \(error.localizedDescription)"
}
}
}.resume()
}
}

struct ContentView: View {
@State private var showErrorAlert = false

var body: some View {
VStack {
if let errorMessage = errorMessage {
Text(errorMessage)
.foregroundColor(.red)
}
}
.alert(isPresented: Binding<Bool>(
get: { errorMessage != nil },
set: { if !$0 { errorMessage = nil } }
)) {
Alert(title: Text("error".localized), message: Text(errorMessage ?? ""), dismissButton: .default(Text("OK".localized)))
}
}
}

0 comments on commit f92c8c7

Please sign in to comment.