Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.
C
feat: pick the lyrics source for the playing song from the redesigned player's ⋯
My-Name-Is-Jeff654a305d48e0
diff --git a/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m b/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.mindex 5c86efd6..99d10848 100644--- a/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m+++ b/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m@@ -1373,6 +1373,7 @@ typedef struct {[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(restyle) name:SGRLyricsTextDidChangeNotification object:nil];[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(restyle) name:SGLyricsRomanisedDidChangeNotification object:nil];[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(translationsChanged) name:SGLyricsTranslationsDidChangeNotification object:nil];+ [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(sourceChosen:) name:SGLyricsSourceChosenNotification object:nil];[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(lookChanged) name:SGRLyricsLookDidChangeNotification object:nil];// A locked phone leaves the card in its window, so the link has to be put down by the app going// away rather than by the view going: see scheduleLink.@@ -2034,6 +2035,18 @@ static BOOL anyUntranslated(NSArray<SGKaraokeLine *> *lines) {}// The saved translations deleted, or Translate any song switched: the song is synced again and redrawn.+// A source picked from the player's ⋯ replaces the lines of the song playing, which the page otherwise keeps+// through any later answer: they are dropped as on a change of track, and the next tick takes the new ones.+- (void)sourceChosen:(NSNotification *)note {+ if (_sample || ![note.object isEqual:_track]) return;+ _lines = nil;+ _passedOver = nil;+ _builtWidth = 0;+ [self creditTo:nil];+ [self dropLineViews];+ [self offerExtras];+}+- (void)translationsChanged {if (!_lines) return;[self timeLines];diff --git a/tweak/Sources/Redesigned/Player/PlayerMenu.m b/tweak/Sources/Redesigned/Player/PlayerMenu.mindex da07b76c..8938bd35 100644--- a/tweak/Sources/Redesigned/Player/PlayerMenu.m+++ b/tweak/Sources/Redesigned/Player/PlayerMenu.m@@ -7,12 +7,16 @@// Show Fluid Artwork, Show Animated Artwork, Show Visualizer// a button for each of the two the background is not, while it is one of the// three, named for what it switches to+// Lyrics Source a song's lyrics from another source than the one the order chose (issue #67): its+// items are the sources with lines for the song, asked only once the submenu opens//// The item says under its name what is not as Spotify plays it, so the menu reads as a settings summary.#import "Core/SGCore.h"#import "Redesigned/ContextMenu/ContextMenu.h"#import "Shared/Player/PlayerState.h"#import "Shared/Player/SpeedPitch.h"+#import "Shared/Lyrics/Lyrics.h"+#import "Shared/LyricsSources/LyricsSources.h"#import "Player.h"static __weak UIView *sg_moreButton; // the ⋯ the sliders' panel comes from@@ -51,6 +55,56 @@ static UIAction *soundMenu(void) {return open;}+// How long the submenu shows its loading row for the slowest sources before it lists the ones that answered.+static const NSTimeInterval kOffersWait = 4;+static NSString *sheetKind(void);++static NSString *timingText(NSArray<SGKaraokeLine *> *lines) {+ switch (SGKaraokeLinesTiming(lines)) {+ case SGKaraokeTimingWords: return @"Word by word";+ case SGKaraokeTimingLine: return @"Line by line";+ default: return @"Not synced";+ }+}++// The sources with lines for the track, in the order, the one shown checked: the one picked, else the one the+// credit names.+static NSArray<UIMenuElement *> *sourceItems(NSString *track, NSDictionary<NSString *, SGLyricsResult *> *offers) {+ NSString *current = SGLyricsChosenSource(track);+ NSString *credit = SGLyricsCreditFor(track);+ NSMutableArray<UIMenuElement *> *items = [NSMutableArray array];+ for (NSString *key in SGLyricsOrder()) {+ SGLyricsResult *offer = offers[key];+ if (!offer) continue;+ SGLyricsProvider *provider = SGLyricsProviderFor(key);+ if (!current && [credit containsString:provider.name]) current = key;+ UIAction *pick = [UIAction actionWithTitle:provider.name image:nil identifier:nil+ handler:^(UIAction *action) { SGLyricsChooseSource(track, key); }];+ pick.subtitle = timingText(offer.karaokeLines);+ pick.state = [key isEqualToString:current] ? UIMenuElementStateOn : UIMenuElementStateOff;+ [items addObject:pick];+ }+ if (!items.count) {+ UIAction *none = [UIAction actionWithTitle:@"No Source Has Lyrics" image:nil identifier:nil handler:^(UIAction *action) {}];+ none.attributes = UIMenuElementAttributesDisabled;+ [items addObject:none];+ }+ return items;+}++static UIMenu *lyricsSourceMenu(void) {+ NSString *track = SGKaraokePlayingTrack();+ if (!SGLyricsEnabled() || !track.length || [sheetKind() isEqualToString:@"episode"]) return nil;+ UIDeferredMenuElement *sources = [UIDeferredMenuElement elementWithUncachedProvider:^(void (^completion)(NSArray<UIMenuElement *> *)) {+ SGLyricsOffers(track, kOffersWait, ^(NSDictionary<NSString *, SGLyricsResult *> *offers) {+ completion(sourceItems(track, offers));+ });+ }];+ UIMenu *menu = [UIMenu menuWithTitle:@"Lyrics Source" image:symbol(@"quote.bubble") identifier:nil options:0 children:@[sources]];+ menu.subtitle = SGLyricsCreditFor(track);+ return menu;+}+static NSArray<UIMenuElement *> *playerItems(void) {NSMutableArray<UIMenuElement *> *items = [NSMutableArray arrayWithObject:soundMenu()];if (SGPlayerMenuOffersAnimatedArtwork()) {@@ -68,6 +122,8 @@ static NSArray<UIMenuElement *> *playerItems(void) {handler:^(UIAction *action) { SGRPlayerMenuSetBackground(kind); }]];}}+ UIMenu *lyrics = lyricsSourceMenu();+ if (lyrics) [items addObject:lyrics];return items;}diff --git a/tweak/Sources/Shared/LyricsSources/LyricsPick.m b/tweak/Sources/Shared/LyricsSources/LyricsPick.mnew file mode 100644index 00000000..381d1279--- /dev/null+++ b/tweak/Sources/Shared/LyricsSources/LyricsPick.m@@ -0,0 +1,107 @@+// A source picked for one track (issue #67). The walk stops at the first source with timed lyrics, so a source+// lower in the order that has the song better timed is never asked; the redesigned player's ⋯ asks them all for+// the track playing, lists the ones with lines, and keeps the one picked as the track's lyrics.+#import "Core/SGCore.h"+#import "LyricsSources.h"+#import "Shared/Lyrics/Lyrics.h"++NSNotificationName const SGLyricsSourceChosenNotification = @"spotifyglass.lyricsSourceChosen";++// Each track's asks are kept for the menu opened on it again; the picks are kept for the replays.+static const NSUInteger kAskedTracks = 10, kChosenTracks = 200;++// Every source asked for one track, and who waits for them all.+@interface SGLyricsOfferAsk : NSObject+@property (nonatomic, copy) NSArray<NSString *> *order;+@property (nonatomic, strong) NSMutableDictionary<NSString *, id> *answers; // a result with lines, else NSNull+@property (nonatomic, strong) NSMutableArray<void (^)(void)> *waiting;+@end++@implementation SGLyricsOfferAsk+@end++static NSMutableDictionary<NSString *, SGLyricsOfferAsk *> *sg_asks;+static NSMutableDictionary<NSString *, NSString *> *sg_chosen;++NSString *SGLyricsChosenSource(NSString *trackID) {+ return trackID ? sg_chosen[trackID] : nil;+}++static NSDictionary<NSString *, SGLyricsResult *> *offersIn(SGLyricsOfferAsk *ask) {+ NSMutableDictionary<NSString *, SGLyricsResult *> *offers = [NSMutableDictionary dictionary];+ [ask.answers enumerateKeysAndObjectsUsingBlock:^(NSString *key, id answer, BOOL *stop) {+ if (answer != NSNull.null) offers[key] = answer;+ }];+ return offers;+}++static void answer(SGLyricsOfferAsk *ask, NSString *key, SGLyricsResult *result) {+ ask.answers[key] = result.karaokeLines.count ? result : NSNull.null;+ if (ask.answers.count < ask.order.count) return;+ NSArray *waiting = ask.waiting;+ ask.waiting = nil;+ for (void (^done)(void) in waiting) done();+}++static SGLyricsOfferAsk *askAll(NSString *trackID) {+ SGLyricsOfferAsk *ask = [SGLyricsOfferAsk new];+ ask.order = SGLyricsOrder();+ ask.answers = [NSMutableDictionary dictionary];+ ask.waiting = [NSMutableArray array];+ SGLyricsQuery *named = SGLyricsQueryFor(trackID);+ SGLog(@"lyrics: asking %@ at once for %@, for the player's ⋯", [ask.order componentsJoinedByString:@", "], trackID);+ for (NSString *key in ask.order) {+ SGLyricsProvider *provider = SGLyricsProviderFor(key);+ // A query each, so what one source learns does not change what the next is asked.+ SGLyricsQuery *query = SGLyricsQueryFor(trackID);+ if (provider.needsName && !(named.title.length && named.artist.length)) {+ answer(ask, key, nil);+ continue;+ }+ provider.ask(query, ^(SGLyricsResult *result) {+ if (result.karaokeLines.count && !result.provider) result.provider = provider.name;+ SGLog(@"lyrics: %@ offers %lu lines for %@", key, (unsigned long)result.karaokeLines.count, trackID);+ answer(ask, key, result);+ });+ }+ return ask;+}++void SGLyricsOffers(NSString *trackID, NSTimeInterval wait, void (^done)(NSDictionary<NSString *, SGLyricsResult *> *offers)) {+ if (!sg_asks) sg_asks = [NSMutableDictionary dictionary];+ SGLyricsOfferAsk *ask = sg_asks[trackID];+ if (!ask) {+ if (sg_asks.count >= kAskedTracks) [sg_asks removeAllObjects];+ ask = sg_asks[trackID] = askAll(trackID);+ }+ __block BOOL called = NO;+ void (^once)(void) = ^{+ if (called) return;+ called = YES;+ done(offersIn(ask));+ };+ if (!ask.waiting) {+ once();+ return;+ }+ [ask.waiting addObject:once];+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(wait * NSEC_PER_SEC)), dispatch_get_main_queue(), once);+}++void SGLyricsChooseSource(NSString *trackID, NSString *key) {+ id result = sg_asks[trackID].answers[key];+ if (![result isKindOfClass:SGLyricsResult.class]) return;+ SGLyricsResult *lyrics = result;+ if (!sg_chosen) sg_chosen = [NSMutableDictionary dictionary];+ if (sg_chosen.count >= kChosenTracks) [sg_chosen removeAllObjects];+ sg_chosen[trackID] = key;+ SGLog(@"lyrics: %@ picked for %@, %lu lines", key, trackID, (unsigned long)lyrics.karaokeLines.count);+ SGLyricsKeepChosen(trackID, lyrics);+ // The credit before the lines, which the lyrics page asks for once it has them.+ SGLyricsSetCredit(trackID, lyrics.provider);+ SGKaraokeKeepLines(trackID, lyrics.karaokeLines);+ // Queued behind the keep above, so a page that drops its lines on this finds the new ones kept.+ dispatch_async(dispatch_get_main_queue(), ^{+ [NSNotificationCenter.defaultCenter postNotificationName:SGLyricsSourceChosenNotification object:trackID];+ });+}diff --git a/tweak/Sources/Shared/LyricsSources/LyricsSources.h b/tweak/Sources/Shared/LyricsSources/LyricsSources.hindex 9691437d..cc021543 100644--- a/tweak/Sources/Shared/LyricsSources/LyricsSources.h+++ b/tweak/Sources/Shared/LyricsSources/LyricsSources.h@@ -228,3 +228,18 @@ SGModRow *SGSpicyLyricsKeyRow(void);SGLyricsResult *SGSpicyLyricsResult(id root);UIViewController *SGLyricsSourcesPage(void); // the ordered list on the Lyrics page++// LyricsPick.m. The source picked for a track from the redesigned player's ⋯, which may be lower in the order+// than the one that answered (issue #67). The pick is a track's id against a source's key, kept until Spotify+// quits; a track's walk asks its picked source first, the rest of the order after it. Main queue.+NSString *SGLyricsChosenSource(NSString *trackID);+// Every source in the order is asked for the track at once, each once a launch, and `done` gets the ones with+// lines, by key, when all have answered or `wait` has passed.+void SGLyricsOffers(NSString *trackID, NSTimeInterval wait, void (^done)(NSDictionary<NSString *, SGLyricsResult *> *offers));+// Makes the offer of `key` the track's lyrics now, everywhere they show, and posts+// SGLyricsSourceChosenNotification (object the track's id) once its lines are kept.+void SGLyricsChooseSource(NSString *trackID, NSString *key);+extern NSNotificationName const SGLyricsSourceChosenNotification;+// LyricsSources.m, for LyricsPick.m: the query a walk would start with, and a picked answer kept as the walk's.+SGLyricsQuery *SGLyricsQueryFor(NSString *trackID);+void SGLyricsKeepChosen(NSString *trackID, SGLyricsResult *result);diff --git a/tweak/Sources/Shared/LyricsSources/LyricsSources.m b/tweak/Sources/Shared/LyricsSources/LyricsSources.mindex ab7878c6..9a353299 100644--- a/tweak/Sources/Shared/LyricsSources/LyricsSources.m+++ b/tweak/Sources/Shared/LyricsSources/LyricsSources.m@@ -486,10 +486,16 @@ static void step(SGLyricsWalk *walk) {}static void startWalk(NSString *trackID, SGLyricsQuery *query) {+ NSMutableArray<NSString *> *order = [SGLyricsOrder() mutableCopy];+ NSString *chosen = SGLyricsChosenSource(trackID);+ if (chosen && [order containsObject:chosen]) {+ [order removeObject:chosen];+ [order insertObject:chosen atIndex:0];+ }SGLog(@"lyrics: asking %@ for %@ as \"%@\" by \"%@\", album \"%@\", %lds",- [SGLyricsOrder() componentsJoinedByString:@", "], trackID, query.title, query.artist, query.album, (long)query.seconds);+ [order componentsJoinedByString:@", "], trackID, query.title, query.artist, query.album, (long)query.seconds);SGLyricsWalk *walk = [SGLyricsWalk new];- walk.order = SGLyricsOrder();+ walk.order = order;walk.query = query;walk.merged = [SGLyricsResult new];walk.passedOver = [NSMutableArray array];@@ -643,3 +649,27 @@ UIViewController *SGLyricsSourcesPage(void) {return SGSourcesPageMake(@"Lyrics sources", @"Asked top to bottom until one has word timing. Sources get only the track, never your account.",all, ^NSArray<NSString *> * { return SGLyricsOrder(); }, ^(NSArray<NSString *> *keys) { SGLyricsSetOrder(keys); });}++#pragma mark - a source picked for a track (LyricsPick.m)++SGLyricsQuery *SGLyricsQueryFor(NSString *trackID) {+ return queryFor(trackID);+}++// The picked answer stands in for the walk's, so a later request for the track, Spotify's own page's included,+// gets the same lines. A source that times words but has no page text gets the page lines made from its own.+void SGLyricsKeepChosen(NSString *trackID, SGLyricsResult *result) {+ setUp();+ if (!result.texts.count && result.karaokeLines.count) {+ NSArray<NSNumber *> *starts;+ NSArray<NSString *> *texts;+ SGLyricsPageLines(result.karaokeLines, &starts, &texts);+ result.starts = starts;+ result.texts = texts;+ result.synced = SGKaraokeLinesTiming(result.karaokeLines) != SGKaraokeTimingNone;+ }+ noteCredit(result);+ if (sg_kept.count >= kKeptTracks) [sg_kept removeAllObjects];+ sg_kept[trackID] = result;+ @synchronized (sg_missing) { [sg_missing removeObject:trackID]; }+}