Skip to content

Commit

Permalink
Update EeveeLyricsSettingsView+LyricsSourceSection.swift
Browse files Browse the repository at this point in the history
  • Loading branch information
ElliotCHEN37 committed Oct 31, 2024
1 parent 5fa1b6b commit 712e7d8
Showing 1 changed file with 26 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
import SwiftUI

struct EeveeLyricsSettingsView: View {
@State private var musixmatchToken = UserDefaults.musixmatchToken
@State private var lyricsSource = UserDefaults.lyricsSource
@State private var isFetchingToken: Bool = false

var body: some View {
Form {
LyricsSourceSection()
}
}

private func lyricsSourceFooter() -> some View {
extension EeveeLyricsSettingsView {
func lyricsSourceFooter() -> some View {
var text = "lyrics_source_description".localized

text.append("\n\n")
text.append("petitlyrics_description".localized)

text.append("\n\n")
text.append("lyrics_additional_info".localized)

return Text(text)
}

@ViewBuilder private func LyricsSourceSection() -> some View {
@ViewBuilder func LyricsSourceSection() -> some View {
Section(footer: Text("restart_is_required_description".localized)) {
Toggle(
"do_not_replace_lyrics".localized,
Expand All @@ -30,42 +23,46 @@ struct EeveeLyricsSettingsView: View {
)
)
}
.onChange(of: lyricsSource) { newSource in
.onChange(of: lyricsSource) { [lyricsSource] newSource in
if newSource == .musixmatch && musixmatchToken.isEmpty {
showMusixmatchTokenAlert(lyricsSource)
return
}

UserDefaults.lyricsSource = newSource
}

if lyricsSource.isReplacing {
Section(footer: lyricsSourceFooter()) {
Picker(
"lyrics_source".localized,
selection: $lyricsSource
) {
ForEach(LyricsSource.allCases, id: \.self) { source in
Text(source.description).tag(source)
ForEach(LyricsSource.allCases, id: \.self) { lyricsSource in
Text(lyricsSource.description).tag(lyricsSource)
}
}

if lyricsSource == .musixmatch {
VStack(alignment: .leading, spacing: 5) {
Text("musixmatch_user_token".localized)

TextField("user_token_placeholder".localized, text: $musixmatchToken)
.foregroundColor(.gray)

Button(isFetchingToken ? "獲取中..." : "獲取用戶令牌") {
Button("獲取用戶令牌") {
fetchUserToken()
}
.disabled(isFetchingToken)
.padding(.top, 5)
}
.frame(maxWidth: .infinity, alignment: .leading)
}
}
.onChange(of: musixmatchToken) { input in
if input.isEmpty { return }
if input.isEmpty {
return
}

if let token = getMusixmatchToken(input) {
UserDefaults.musixmatchToken = token
self.musixmatchToken = token
Expand All @@ -75,48 +72,38 @@ struct EeveeLyricsSettingsView: View {
}
}
}

private func fetchUserToken() {
isFetchingToken = true
let urlString: String

if UIDevice.current.userInterfaceIdiom == .phone {
urlString = "https://apic.musixmatch.com/ws/1.1/token.get?app_id=mac-ios-v2.0"
} else {
urlString = "https://apic.musixmatch.com/ws/1.1/token.get?app_id=mac-ios-ipad-v1.0"
}

guard let url = URL(string: urlString) else {
isFetchingToken = false
return
}


guard let url = URL(string: urlString) else { return }

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

guard let data = data else {
DispatchQueue.main.async { self.isFetchingToken = false }
return
}


guard let data = data else { return }

do {
let json = try JSONSerialization.jsonObject(with: data, options: [])
if let dictionary = json as? [String: Any],
let message = dictionary["message"] as? [String: Any],
let body = message["body"] as? [String: Any],
let userToken = body["user_token"] as? String {

DispatchQueue.main.async {
self.musixmatchToken = userToken
self.isFetchingToken = false
}
}
} catch {
print("Error parsing JSON: \(error)")
DispatchQueue.main.async { self.isFetchingToken = false }
}
}.resume()
}
Expand Down

0 comments on commit 712e7d8

Please sign in to comment.