From f79ee3d51df6b00c4c32e58b0af7c4451cd9ee7c Mon Sep 17 00:00:00 2001 From: Luca LeBlanc <67206487+yodaluca23@users.noreply.github.com> Date: Thu, 4 Jul 2024 23:19:55 -0500 Subject: [PATCH] Use Romaji Lyrics when avaible with Genius toggle. (#227) * Update GeniusLyricsRepository.swift * Update GeniusLyricsRepository.swift * Update EeveeSettingsViewController+LyricsSections.swift * Update UserDefaults+Extension.swift * Update GeniusLyricsRepository.swift * Update GeniusLyricsRepository.swift * Update GeniusLyricsRepository.swift * Update UserDefaults+Extension.swift * Update EeveeSettingsViewController+LyricsSections.swift * Update EeveeSettingsViewController+LyricsSections.swift --- .../Repositories/GeniusLyricsRepository.swift | 34 +++++++++++++++---- .../Extensions/UserDefaults+Extension.swift | 10 ++++++ ...ettingsViewController+LyricsSections.swift | 12 +++++++ 3 files changed, 49 insertions(+), 7 deletions(-) diff --git a/Sources/EeveeSpotify/Lyrics/Repositories/GeniusLyricsRepository.swift b/Sources/EeveeSpotify/Lyrics/Repositories/GeniusLyricsRepository.swift index 0af1b92..62b041e 100644 --- a/Sources/EeveeSpotify/Lyrics/Repositories/GeniusLyricsRepository.swift +++ b/Sources/EeveeSpotify/Lyrics/Repositories/GeniusLyricsRepository.swift @@ -104,19 +104,39 @@ struct GeniusLyricsRepository: LyricsRepository { func getLyrics(_ query: LyricsSearchQuery) throws -> LyricsDto { let strippedTitle = query.title.strippedTrackTitle - let hits = try searchSong("\(strippedTitle) \(query.primaryArtist)") + var queries = [ + "\(strippedTitle) \(query.primaryArtist)" + ] - guard let song = mostRelevantHitResult(hits: hits, strippedTitle: strippedTitle) else { + if UserDefaults.romanizedLyrics { + queries = [ + "\(strippedTitle) \(query.primaryArtist) (Romanized)", + "\(strippedTitle) \(query.primaryArtist)" + ] + } + var hits: [GeniusHit] = [] + + for searchQuery in queries { + do { + hits = try searchSong(searchQuery) + if !hits.isEmpty { + break + } + } catch { + // Continue to the next query if the current one fails + } + } + + guard !hits.isEmpty, + let song = mostRelevantHitResult(hits: hits, strippedTitle: strippedTitle) else { throw LyricsError.NoSuchSong } - + let songInfo = try getSongInfo(song.id) let plainLines = songInfo.lyrics.plain.components(separatedBy: "\n") - + return LyricsDto( - lines: mapLyricsLines(plainLines).map { - line in LyricsLineDto(content: line) - }, + lines: mapLyricsLines(plainLines).map { line in LyricsLineDto(content: line) }, timeSynced: false ) } diff --git a/Sources/EeveeSpotify/Models/Extensions/UserDefaults+Extension.swift b/Sources/EeveeSpotify/Models/Extensions/UserDefaults+Extension.swift index e5ad1b3..a1866d9 100644 --- a/Sources/EeveeSpotify/Models/Extensions/UserDefaults+Extension.swift +++ b/Sources/EeveeSpotify/Models/Extensions/UserDefaults+Extension.swift @@ -7,6 +7,7 @@ extension UserDefaults { private static let lyricsSourceKey = "lyricsSource" private static let musixmatchTokenKey = "musixmatchToken" private static let geniusFallbackKey = "geniusFallback" + private static let romanizedLyricsKey = "romanizedLyrics" private static let fallbackReasonsKey = "fallbackReasons" private static let darkPopUpsKey = "darkPopUps" private static let patchTypeKey = "patchType" @@ -43,6 +44,15 @@ extension UserDefaults { defaults.set(fallback, forKey: geniusFallbackKey) } } + + static var romanizedLyrics: Bool { + get { + defaults.object(forKey: romanizedLyricsKey) as? Bool ?? false + } + set (romanized) { + defaults.set(romanized, forKey: romanizedLyricsKey) + } + } static var fallbackReasons: Bool { get { diff --git a/Sources/EeveeSpotify/Settings/Views/EeveeSettingsViewController+LyricsSections.swift b/Sources/EeveeSpotify/Settings/Views/EeveeSettingsViewController+LyricsSections.swift index 8e4dc8b..3db2dc9 100644 --- a/Sources/EeveeSpotify/Settings/Views/EeveeSettingsViewController+LyricsSections.swift +++ b/Sources/EeveeSpotify/Settings/Views/EeveeSettingsViewController+LyricsSections.swift @@ -104,6 +104,18 @@ If the tweak is unable to find a song or process the lyrics, you'll see a "Could UserDefaults.lyricsSource = newSource } + + if lyricsSource == .genius { + Section { + Toggle( + "Use Romanized (Romaji) Lyrics when Available", + isOn: Binding( + get: { UserDefaults.romanizedLyrics }, + set: { UserDefaults.romanizedLyrics = $0 } + ) + ) + } + } if lyricsSource != .genius { Section(