diff --git a/Sources/EeveeSpotify/Settings/Views/Sections/EeveeLyricsSettingsView+LyricsSourceSection.swift b/Sources/EeveeSpotify/Settings/Views/Sections/EeveeLyricsSettingsView+LyricsSourceSection.swift index 7acf63f..884bb18 100644 --- a/Sources/EeveeSpotify/Settings/Views/Sections/EeveeLyricsSettingsView+LyricsSourceSection.swift +++ b/Sources/EeveeSpotify/Settings/Views/Sections/EeveeLyricsSettingsView+LyricsSourceSection.swift @@ -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) @@ -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: []) @@ -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( + get: { errorMessage != nil }, + set: { if !$0 { errorMessage = nil } } + )) { + Alert(title: Text("error".localized), message: Text(errorMessage ?? ""), dismissButton: .default(Text("OK".localized))) + } + } +}