diff --git a/Sources/EeveeSpotify/DataLoaderServiceHooks.x.swift b/Sources/EeveeSpotify/DataLoaderServiceHooks.x.swift index 5920acbd..4ef53c6f 100644 --- a/Sources/EeveeSpotify/DataLoaderServiceHooks.x.swift +++ b/Sources/EeveeSpotify/DataLoaderServiceHooks.x.swift @@ -33,7 +33,7 @@ class SPTDataLoaderServiceHook: ClassHook { orig.URLSession( session, dataTask: task, - didReceiveData: try getCurrentTrackLyricsData( + didReceiveData: try getLyricsForCurrentTrack( originalLyrics: try? Lyrics(serializedData: buffer) ) ) @@ -90,7 +90,7 @@ class SPTDataLoaderServiceHook: ClassHook { )! do { - let lyricsData = try getCurrentTrackLyricsData() + let lyricsData = try getLyricsForCurrentTrack() orig.URLSession( session, diff --git a/Sources/EeveeSpotify/Lyrics/CustomLyrics.x.swift b/Sources/EeveeSpotify/Lyrics/CustomLyrics.x.swift index d76a32e3..36e2e7dc 100644 --- a/Sources/EeveeSpotify/Lyrics/CustomLyrics.x.swift +++ b/Sources/EeveeSpotify/Lyrics/CustomLyrics.x.swift @@ -1,6 +1,17 @@ import Orion import SwiftUI +class SPTPlayerTrackHook: ClassHook { + static let targetName = "SPTPlayerTrack" + + func metadata() -> [String:String] { + var meta = orig.metadata() + + meta["has_lyrics"] = "true" + return meta + } +} + class LyricsScrollProviderHook: ClassHook { static var targetName: String { @@ -42,6 +53,7 @@ class LyricsFullscreenViewControllerHook: ClassHook { // +private var preloadedLyrics: Lyrics? = nil private var lastLyricsState = LyricsLoadingState() private var hasShownRestrictedPopUp = false @@ -127,8 +139,9 @@ class LyricsOnlyViewControllerHook: ClassHook { } } -func getCurrentTrackLyricsData(originalLyrics: Lyrics? = nil) throws -> Data { - +// + +private func loadLyricsForCurrentTrack() throws { guard let track = HookedInstances.currentTrack else { throw LyricsError.NoCurrentTrack } @@ -217,28 +230,47 @@ func getCurrentTrackLyricsData(originalLyrics: Lyrics? = nil) throws -> Data { lastLyricsState.wasRomanized = lyricsDto.romanization == .romanized || (lyricsDto.romanization == .canBeRomanized && UserDefaults.lyricsOptions.romanization) + lastLyricsState.loadedSuccessfully = true + NSLog("eevee doooo") let lyrics = Lyrics.with { - $0.colors = getLyricsColors() $0.data = lyricsDto.toLyricsData(source: source.description) } + + preloadedLyrics = lyrics +} - return try lyrics.serializedData() +func getLyricsForCurrentTrack(originalLyrics: Lyrics? = nil) throws -> Data { + guard let track = HookedInstances.currentTrack else { + throw LyricsError.NoCurrentTrack + } - func getLyricsColors() -> LyricsColors { - let lyricsColorsSettings = UserDefaults.lyricsColors - - if lyricsColorsSettings.displayOriginalColors, let originalLyrics = originalLyrics { - return originalLyrics.colors - } - - return LyricsColors.with { - $0.backgroundColor = lyricsColorsSettings.useStaticColor - ? Color(hex: lyricsColorsSettings.staticColor).uInt32 - : Color(hex: track.extractedColorHex()) - .normalized(lyricsColorsSettings.normalizationFactor) - .uInt32 - $0.lineColor = Color.black.uInt32 - $0.activeLineColor = Color.white.uInt32 - } + var lyrics = preloadedLyrics + + if lyrics == nil { + try loadLyricsForCurrentTrack() + lyrics = preloadedLyrics + } + + guard var lyrics = lyrics else { + throw LyricsError.UnknownError } + + let lyricsColorsSettings = UserDefaults.lyricsColors + + if lyricsColorsSettings.displayOriginalColors, let originalLyrics = originalLyrics { + lyrics.colors = originalLyrics.colors + } + + lyrics.colors = LyricsColors.with { + $0.backgroundColor = lyricsColorsSettings.useStaticColor + ? Color(hex: lyricsColorsSettings.staticColor).uInt32 + : Color(hex: track.extractedColorHex()) + .normalized(lyricsColorsSettings.normalizationFactor) + .uInt32 + $0.lineColor = Color.black.uInt32 + $0.activeLineColor = Color.white.uInt32 + } + + preloadedLyrics = nil + return try lyrics.serializedData() } diff --git a/Sources/EeveeSpotify/Lyrics/Models/LyricsLoadingState.swift b/Sources/EeveeSpotify/Lyrics/Models/LyricsLoadingState.swift index 1fa15b73..8fb4c65c 100644 --- a/Sources/EeveeSpotify/Lyrics/Models/LyricsLoadingState.swift +++ b/Sources/EeveeSpotify/Lyrics/Models/LyricsLoadingState.swift @@ -4,4 +4,5 @@ struct LyricsLoadingState { var wasRomanized = false var areEmpty = false var fallbackError: LyricsError? = nil + var loadedSuccessfully = false }