From b02e0689a7b796f8202bcc9635e285d5e39e0388 Mon Sep 17 00:00:00 2001 From: eevee Date: Sat, 11 May 2024 19:02:05 +0300 Subject: [PATCH] Genius Fallback & slightly improved colors brightness --- Sources/EeveeSpotify/CustomLyrics.x.swift | 39 +++++++++++++++---- .../Models/Extensions/Color+Extension.swift | 2 +- .../Extensions/UserDefaults+Extension.swift | 14 +++++-- Sources/EeveeSpotify/OpenInSpotify.x.swift | 27 ------------- .../Views/EeveeSettingsView.swift | 14 +++++++ 5 files changed, 57 insertions(+), 39 deletions(-) delete mode 100644 Sources/EeveeSpotify/OpenInSpotify.x.swift diff --git a/Sources/EeveeSpotify/CustomLyrics.x.swift b/Sources/EeveeSpotify/CustomLyrics.x.swift index 400437f..5ffae4c 100644 --- a/Sources/EeveeSpotify/CustomLyrics.x.swift +++ b/Sources/EeveeSpotify/CustomLyrics.x.swift @@ -89,14 +89,37 @@ func getCurrentTrackLyricsData() throws -> Data { throw LyricsError.NoCurrentTrack } - let source = UserDefaults.lyricsSource + var source = UserDefaults.lyricsSource - let plainLyrics = try LyricsRepository.getLyrics( - title: track.trackTitle(), - artist: track.artistTitle(), - spotifyTrackId: track.URI().spt_trackIdentifier(), - source: source - ) + let plainLyrics: PlainLyrics? + + do { + plainLyrics = try LyricsRepository.getLyrics( + title: track.trackTitle(), + artist: track.artistTitle(), + spotifyTrackId: track.URI().spt_trackIdentifier(), + source: source + ) + } + + catch { + + if source != .genius && UserDefaults.geniusFallback { + + NSLog("[EeveeSpotify] Unable to load lyrics from \(source), trying Genius as fallback") + source = .genius + + plainLyrics = try LyricsRepository.getLyrics( + title: track.trackTitle(), + artist: track.artistTitle(), + spotifyTrackId: track.URI().spt_trackIdentifier(), + source: source + ) + } + else { + throw error + } + } let lyrics = try Lyrics.with { $0.colors = LyricsColors.with { @@ -104,7 +127,7 @@ func getCurrentTrackLyricsData() throws -> Data { $0.lineColor = Color.black.uInt32 $0.activeLineColor = Color.white.uInt32 } - $0.data = try LyricsHelper.composeLyricsData(plainLyrics, source: source) + $0.data = try LyricsHelper.composeLyricsData(plainLyrics!, source: source) } return try lyrics.serializedData() diff --git a/Sources/EeveeSpotify/Models/Extensions/Color+Extension.swift b/Sources/EeveeSpotify/Models/Extensions/Color+Extension.swift index 309990a..b9e5f48 100644 --- a/Sources/EeveeSpotify/Models/Extensions/Color+Extension.swift +++ b/Sources/EeveeSpotify/Models/Extensions/Color+Extension.swift @@ -55,7 +55,7 @@ extension Color { var normalized: Color { brightness < 0.5 - ? self.lighter(by: 0.5) + ? self.lighter(by: 0.35) : self.darker(by: brightness - 0.5) } diff --git a/Sources/EeveeSpotify/Models/Extensions/UserDefaults+Extension.swift b/Sources/EeveeSpotify/Models/Extensions/UserDefaults+Extension.swift index 75f737d..640a79d 100644 --- a/Sources/EeveeSpotify/Models/Extensions/UserDefaults+Extension.swift +++ b/Sources/EeveeSpotify/Models/Extensions/UserDefaults+Extension.swift @@ -6,9 +6,9 @@ extension UserDefaults { private static let lyricsSourceKey = "lyricsSource" private static let musixmatchTokenKey = "musixmatchToken" - - static var lyricsSource: LyricsSource { + private static let geniusFallbackKey = "geniusFallback" + static var lyricsSource: LyricsSource { get { if let rawValue = defaults.object(forKey: lyricsSourceKey) as? Int { return LyricsSource(rawValue: rawValue)! @@ -22,7 +22,6 @@ extension UserDefaults { } static var musixmatchToken: String { - get { defaults.string(forKey: musixmatchTokenKey) ?? "" } @@ -30,4 +29,13 @@ extension UserDefaults { defaults.set(token, forKey: musixmatchTokenKey) } } + + static var geniusFallback: Bool { + get { + defaults.object(forKey: geniusFallbackKey) as? Bool ?? true + } + set (fallback) { + defaults.set(fallback, forKey: geniusFallbackKey) + } + } } \ No newline at end of file diff --git a/Sources/EeveeSpotify/OpenInSpotify.x.swift b/Sources/EeveeSpotify/OpenInSpotify.x.swift deleted file mode 100644 index 76cdae9..0000000 --- a/Sources/EeveeSpotify/OpenInSpotify.x.swift +++ /dev/null @@ -1,27 +0,0 @@ -import Orion -import UIKit - -class SpotifySceneDelegateHook: ClassHook { - - static let targetName = "MusicApp_ContainerWiring.SpotifySceneDelegate" - - func scene(_ scene: UIScene, continueUserActivity userActivity: NSUserActivity) { - orig.scene(scene, continueUserActivity: userActivity) - } - - func scene(_ scene: UIScene, openURLContexts URLContexts: Set) { - - let url = URLContexts.first!.url - - if url.host == "eevee" { - - let userActivity = NSUserActivity(activityType: NSUserActivityTypeBrowsingWeb) - userActivity.webpageURL = URL(string: "https:/\(url.path)") - - orig.scene(scene, continueUserActivity: userActivity) - return - } - - orig.scene(scene, openURLContexts: URLContexts) - } -} diff --git a/Sources/EeveeSpotify/Views/EeveeSettingsView.swift b/Sources/EeveeSpotify/Views/EeveeSettingsView.swift index e38f34d..ef53db5 100644 --- a/Sources/EeveeSpotify/Views/EeveeSettingsView.swift +++ b/Sources/EeveeSpotify/Views/EeveeSettingsView.swift @@ -73,6 +73,20 @@ If the tweak is unable to find a song or process the lyrics, you'll see the orig .frame(maxWidth: .infinity, alignment: .leading) } } + + if lyricsSource != .genius { + Section( + footer: Text("Load lyrics from Genius if there is a problem with \(lyricsSource).") + ) { + Toggle( + "Genius Fallback", + isOn: Binding( + get: { UserDefaults.geniusFallback }, + set: { UserDefaults.geniusFallback = $0 } + ) + ) + } + } } .padding(.bottom, 50)