Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.
C
fix: a navbar tab opens its page instead of "can't open this type of link" (#66)
Vojtěch Škopekb2288b320ca6
diff --git a/tweak/Sources/Native/Navbar/NavbarSettings.m b/tweak/Sources/Native/Navbar/NavbarSettings.mindex 55ba04db..420053a6 100644--- a/tweak/Sources/Native/Navbar/NavbarSettings.m+++ b/tweak/Sources/Native/Navbar/NavbarSettings.m@@ -11,7 +11,7 @@ static NSArray<NSDictionary *> *tabPresets(void) {@{SGNavbarTitle: @"Search", SGNavbarURI: @"spotify:search", SGNavbarIcon: @"search"},@{SGNavbarTitle: @"Your Library", SGNavbarURI: @"spotify:collection", SGNavbarIcon: @"collection"},@{SGNavbarTitle: @"Liked Songs", SGNavbarURI: @"spotify:collection:tracks", SGNavbarIcon: @"heart"},- @{SGNavbarTitle: @"Playlists", SGNavbarURI: @"spotify:collection:playlists", SGNavbarIcon: @"playlist"},+ @{SGNavbarTitle: @"Playlists", SGNavbarURI: @"spotify:playlists", SGNavbarIcon: @"playlist"},@{SGNavbarTitle: @"Albums", SGNavbarURI: @"spotify:collection:albums", SGNavbarIcon: @"album"},@{SGNavbarTitle: @"Artists", SGNavbarURI: @"spotify:collection:artists", SGNavbarIcon: @"artist"},@{SGNavbarTitle: @"Podcasts", SGNavbarURI: @"spotify:collection:podcasts", SGNavbarIcon: @"podcasts"},diff --git a/tweak/Sources/Redesigned/Navbar/Navbar.h b/tweak/Sources/Redesigned/Navbar/Navbar.hindex 26e6df28..e49f59bf 100644--- a/tweak/Sources/Redesigned/Navbar/Navbar.h+++ b/tweak/Sources/Redesigned/Navbar/Navbar.h@@ -18,6 +18,10 @@ void SGRSetNavbarLayout(NSArray<NSDictionary *> *layout);// Spotify's own tabs in Spotify's order, as Navbar.x last saw them on the bar.NSArray<NSString *> *SGRNavbarStock(void);void SGRSetNavbarStock(NSArray<NSString *> *stock);+// What a tab of the mod's own opens: its URI as typed or pasted, share links made spotify: URIs, and+// the URIs of presets that never opened (spotify:collection:playlists, up to 0.20) moved to the ones+// that replaced them, so a tab saved back then works without being added again.+NSURL *SGRNavbarTabURL(NSString *uri);// Navbar.x, called from the tab bar's layout passes in TabBar.x.void SGRComposeTabBar(UIView *tabBar);diff --git a/tweak/Sources/Redesigned/Navbar/Navbar.x b/tweak/Sources/Redesigned/Navbar/Navbar.xindex a781eeef..8c295216 100644--- a/tweak/Sources/Redesigned/Navbar/Navbar.x+++ b/tweak/Sources/Redesigned/Navbar/Navbar.x@@ -126,9 +126,14 @@ static UIView *iconView(NSString *name) {self.alpha = highlighted ? 0.5 : 1;}-// Through the app's own link dispatcher (Shared/Navigation/Links.h).+// Through the app's own link dispatcher (Shared/Navigation/Links.h). What the dispatcher makes of the+// URI goes to the log first, so a tab that ends in Spotify's "Couldn't open link" says why.- (void)open {- NSURL *url = self.uri.length ? [NSURL URLWithString:self.uri] : nil;+ NSURL *url = SGRNavbarTabURL(self.uri);+ NSString *via = nil;+ SGLinkRoute route = SGSpotifyURIRoute(url, &via);+ SGLog(@"navbar: open %@ -> %@", url.absoluteString ?: self.uri,+ route == SGLinkRouteOpens ? via : route == SGLinkRouteNone ? @"no handler" : @"unknown");if (!SGOpenSpotifyURI(url)) SGLog(@"navbar: cannot open %@, dispatcher %@", self.uri, SGLinkDispatcher());}diff --git a/tweak/Sources/Redesigned/Navbar/NavbarLayout.m b/tweak/Sources/Redesigned/Navbar/NavbarLayout.mindex a31aa611..7009ff1a 100644--- a/tweak/Sources/Redesigned/Navbar/NavbarLayout.m+++ b/tweak/Sources/Redesigned/Navbar/NavbarLayout.m@@ -1,5 +1,6 @@// The saved composition of the redesign's bar, as NSUserDefaults property lists, apart from the native look's.#import "Navbar.h"+#import "Shared/Navigation/Links.h"NSString *const SGRNavbarID = @"id";NSString *const SGRNavbarTitle = @"title";@@ -32,3 +33,9 @@ NSArray<NSString *> *SGRNavbarStock(void) {void SGRSetNavbarStock(NSArray<NSString *> *stock) {[NSUserDefaults.standardUserDefaults setObject:stock ?: @[] forKey:kNavbarStock];}++NSURL *SGRNavbarTabURL(NSString *uri) {+ NSURL *url = SGSpotifyURIFromText(uri);+ if ([url.absoluteString isEqualToString:@"spotify:collection:playlists"]) return [NSURL URLWithString:@"spotify:playlists"];+ return url;+}diff --git a/tweak/Sources/Redesigned/Navbar/NavbarSettings.m b/tweak/Sources/Redesigned/Navbar/NavbarSettings.mindex d58bae3c..8e324858 100644--- a/tweak/Sources/Redesigned/Navbar/NavbarSettings.m+++ b/tweak/Sources/Redesigned/Navbar/NavbarSettings.m@@ -2,16 +2,20 @@#import "Settings/SGPage.h"#import "Settings/SGPageStyle.h"#import "Navbar.h"+#import "Shared/Navigation/Links.h"// What "Add a tab" offers: URIs Spotify's own router resolves to a page of its own, each with the-// name of the SPTEncoreIcon class method that draws its glyph.+// name of the SPTEncoreIcon class method that draws its glyph. Playlists was spotify:collection:playlists+// until 0.20, which 9.1.78 knows only from its old iPad sidebar table and has no handler for (#66);+// spotify:playlists is the form its collection URI parser lists, next to spotify:playlists:by-you.+// The picker still asks the dispatcher about each one and leaves out what it has nowhere to send.static NSArray<NSDictionary *> *tabPresets(void) {return @[@{SGRNavbarTitle: @"Home", SGRNavbarURI: @"spotify:home", SGRNavbarIcon: @"home"},@{SGRNavbarTitle: @"Search", SGRNavbarURI: @"spotify:search", SGRNavbarIcon: @"search"},@{SGRNavbarTitle: @"Your Library", SGRNavbarURI: @"spotify:collection", SGRNavbarIcon: @"collection"},@{SGRNavbarTitle: @"Liked Songs", SGRNavbarURI: @"spotify:collection:tracks", SGRNavbarIcon: @"heart"},- @{SGRNavbarTitle: @"Playlists", SGRNavbarURI: @"spotify:collection:playlists", SGRNavbarIcon: @"playlist"},+ @{SGRNavbarTitle: @"Playlists", SGRNavbarURI: @"spotify:playlists", SGRNavbarIcon: @"playlist"},@{SGRNavbarTitle: @"Albums", SGRNavbarURI: @"spotify:collection:albums", SGRNavbarIcon: @"album"},@{SGRNavbarTitle: @"Artists", SGRNavbarURI: @"spotify:collection:artists", SGRNavbarIcon: @"artist"},@{SGRNavbarTitle: @"Podcasts", SGRNavbarURI: @"spotify:collection:podcasts", SGRNavbarIcon: @"podcasts"},@@ -59,21 +63,45 @@ static void appendTab(NSDictionary *tab) {@interface SGRTabPickerPage : SGPage@end+// The presets the dispatcher can send somewhere, each verdict logged. When it cannot be asked (not set+// up yet, or 9.1.78's registry is not where it was) every preset stays, as before.+static NSArray<NSDictionary *> *openablePresets(void) {+ NSMutableArray<NSDictionary *> *kept = [NSMutableArray array];+ for (NSDictionary *tab in tabPresets()) {+ NSString *via = nil;+ SGLinkRoute route = SGSpotifyURIRoute([NSURL URLWithString:tab[SGRNavbarURI]], &via);+ SGLog(@"navbar: preset %@ -> %@", tab[SGRNavbarURI],+ route == SGLinkRouteOpens ? via : route == SGLinkRouteNone ? @"no handler, left out" : @"unknown");+ if (route != SGLinkRouteNone) [kept addObject:tab];+ }+ return kept;+}+@implementation SGRTabPickerPage {UIView *_footer;+ NSArray<NSDictionary *> *_presets;}- (instancetype)init {if (!(self = [super initWithStyle:UITableViewStyleInsetGrouped])) return nil;self.title = @"Add a Tab";+ _presets = openablePresets();return self;}+// Said here rather than by Spotify's alert on the bar later, every time the tab is tapped.+- (void)refuse:(NSString *)uri {+ NSString *message = [NSString stringWithFormat:@"Spotify has nowhere to open %@, so it would not work as a tab.", uri];+ UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Can't open that link" message:message preferredStyle:UIAlertControllerStyleAlert];+ [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:nil]];+ [self presentViewController:alert animated:YES completion:nil];+}+- (void)viewDidLoad {[super viewDidLoad];_footer = SGNote(@"Anything Spotify can open by link works, so a playlist, an artist or a page of "- "your own goes on the bar the same way. Icons are Spotify's own: home, search, "- "collection, heart, playlist, album, artist, podcasts, audiobook, downloaded, "+ "your own goes on the bar the same way: a spotify: URI, or a share link pasted as "+ "it is. Icons are Spotify's own: home, search, collection, heart, playlist, album, artist, podcasts, audiobook, downloaded, ""bookmark, browse, star, user, events, queue, plus, radio, gears, spotifyLogo.");self.tableView.tableFooterView = _footer;}@@ -93,7 +121,7 @@ static void appendTab(NSDictionary *tab) {}- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {- return section == 0 ? (NSInteger)tabPresets().count : 1;+ return section == 0 ? (NSInteger)_presets.count : 1;}- (UIView *)tableView:(UITableView *)table viewForHeaderInSection:(NSInteger)section {@@ -111,7 +139,7 @@ static void appendTab(NSDictionary *tab) {- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)path {UITableViewCell *cell = SGDequeueCell(table, @"pick");if (path.section == 0) {- NSDictionary *tab = tabPresets()[(NSUInteger)path.row];+ NSDictionary *tab = _presets[(NSUInteger)path.row];SGFillCell(cell, tab[SGRNavbarTitle], tab[SGRNavbarURI], nil, nil);} else {SGFillCell(cell, @"Any link…", @"A name, a URI of your own and an icon", nil, @"link");@@ -123,7 +151,7 @@ static void appendTab(NSDictionary *tab) {- (void)tableView:(UITableView *)table didSelectRowAtIndexPath:(NSIndexPath *)path {[table deselectRowAtIndexPath:path animated:YES];if (path.section == 0) {- appendTab(tabPresets()[(NSUInteger)path.row]);+ appendTab(_presets[(NSUInteger)path.row]);[self.navigationController popViewControllerAnimated:YES];return;}@@ -142,8 +170,16 @@ static void appendTab(NSDictionary *tab) {}];[alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];[alert addAction:[UIAlertAction actionWithTitle:@"Add" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {- NSString *title = alert.textFields[0].text, *uri = alert.textFields[1].text, *icon = alert.textFields[2].text;+ NSString *title = alert.textFields[0].text, *icon = alert.textFields[2].text;+ NSString *uri = SGSpotifyURIFromText(alert.textFields[1].text).absoluteString;if (!uri.length) return;+ NSString *via = nil;+ SGLinkRoute route = SGSpotifyURIRoute([NSURL URLWithString:uri], &via);+ SGLog(@"navbar: custom %@ -> %@", uri, route == SGLinkRouteOpens ? via : route == SGLinkRouteNone ? @"no handler" : @"unknown");+ if (route == SGLinkRouteNone) {+ [self refuse:uri];+ return;+ }appendTab(@{SGRNavbarTitle: title.length ? title : uri, SGRNavbarURI: uri, SGRNavbarIcon: icon.length ? icon : @"star"});[self.navigationController popViewControllerAnimated:YES];}]];diff --git a/tweak/Sources/Shared/Navigation/Links.h b/tweak/Sources/Shared/Navigation/Links.hindex 0e9d2c0f..003dbf23 100644--- a/tweak/Sources/Shared/Navigation/Links.h+++ b/tweak/Sources/Shared/Navigation/Links.h@@ -5,3 +5,21 @@// NO before the dispatcher is set up, or for a nil URI.BOOL SGOpenSpotifyURI(NSURL *uri);id SGLinkDispatcher(void); // SPTLinkDispatcherImplementation, nil until it is set up++// Whether the dispatcher has somewhere to send a URI, asked the way it asks itself before it opens+// one: every handler in its URI subtype registry is asked -URISubtypeHandlerCanHandleURI:, then the+// same again for the URI its fallback resolver remaps an unhandled one to. None of them saying yes is+// what ends in Spotify's "Couldn't open link" alert.+typedef NS_ENUM(NSInteger, SGLinkRoute) {+ SGLinkRouteUnknown = -1, // the dispatcher or its registry is not there to ask+ SGLinkRouteNone = 0,+ SGLinkRouteOpens = 1,+};+// `via`, when given, is the class of the handler that said yes, prefixed "remap <uri> " when it took+// the fallback resolver's URI; nil otherwise.+SGLinkRoute SGSpotifyURIRoute(NSURL *uri, NSString **via);++// What someone typed or pasted as a link, as a spotify: URI: surrounding blanks trimmed, and a share+// link (https://open.spotify.com/[intl-xx/]playlist/<id>?si=…) turned into spotify:playlist:<id>.+// nil when there is nothing a URI can be made of.+NSURL *SGSpotifyURIFromText(NSString *text);diff --git a/tweak/Sources/Shared/Navigation/Links.x b/tweak/Sources/Shared/Navigation/Links.xindex cd50014f..0b828604 100644--- a/tweak/Sources/Shared/Navigation/Links.x+++ b/tweak/Sources/Shared/Navigation/Links.x@@ -3,6 +3,7 @@#import "Core/SGCore.h"#import "Headers/SPTLinkDispatcherImplementation.h"#import "Links.h"+#import <objc/message.h>static __weak SPTLinkDispatcherImplementation *sg_linkDispatcher;@@ -17,6 +18,84 @@ BOOL SGOpenSpotifyURI(NSURL *uri) {return YES;}+#pragma mark - asking before opening++// The chain a spotify: link takes in 9.1.78, read off the binary: -[SPTLinkDispatcherImplementation+// navigateToURI:…] hands it to the URI scheme dispatcher, whose "spotify" handler is the dispatcher's+// -spotifyLinkHandler (SPTSpotifyLinkHandler). That one passes the cleaned URI to its+// -URISubtypeRegistry (SPTURISubtypeRegistryImplementation), which keeps the handlers of its -handlers+// hash table that answer -URISubtypeHandlerCanHandleURI: YES and opens with the first by priority;+// pages go through Navigation's PageResolverURISubtypeHandler. With no taker, -handleSpotifyURL:+// tries again once with -remapURI: (its fallback resolver's URI), and only then shows the alert.+static id sg_send(id target, NSString *selector) {+ SEL sel = NSSelectorFromString(selector);+ if (![target respondsToSelector:sel]) return nil;+ return ((id (*)(id, SEL))objc_msgSend)(target, sel);+}++static id sg_send1(id target, NSString *selector, id arg) {+ SEL sel = NSSelectorFromString(selector);+ if (![target respondsToSelector:sel]) return nil;+ return ((id (*)(id, SEL, id))objc_msgSend)(target, sel, arg);+}++// The class of the first handler that takes `uri`, nil for none. A handler whose name says it only+// listens (Jam's SGSNavigationURIHandledEventSource) is not asked, so a question is not taken for a+// navigation.+static NSString *takerFor(id<NSFastEnumeration> handlers, NSURL *uri, NSUInteger *asked) {+ SEL canHandle = NSSelectorFromString(@"URISubtypeHandlerCanHandleURI:");+ for (id handler in handlers) {+ NSString *name = NSStringFromClass([handler class]);+ if ([name containsString:@"EventSource"] || ![handler respondsToSelector:canHandle]) continue;+ (*asked)++;+ if (((BOOL (*)(id, SEL, id))objc_msgSend)(handler, canHandle, uri)) return name;+ }+ return nil;+}++SGLinkRoute SGSpotifyURIRoute(NSURL *uri, NSString **via) {+ if (via) *via = nil;+ if (!uri) return SGLinkRouteNone;+ id linkHandler = sg_send(sg_linkDispatcher, @"spotifyLinkHandler");+ id handlers = sg_send(sg_send(linkHandler, @"URISubtypeRegistry"), @"handlers");+ if (![handlers conformsToProtocol:@protocol(NSFastEnumeration)]) return SGLinkRouteUnknown;+ @try {+ NSURL *clean = sg_send(uri, @"spt_normalizedSpotifyURI");+ if (![clean isKindOfClass:NSURL.class]) clean = uri;+ NSUInteger asked = 0;+ NSString *taker = takerFor(handlers, clean, &asked);+ if (!taker) {+ NSURL *remapped = sg_send1(linkHandler, @"remapURI:", clean);+ if ([remapped isKindOfClass:NSURL.class] && ![remapped isEqual:clean]) {+ taker = takerFor(handlers, remapped, &asked);+ if (taker) taker = [NSString stringWithFormat:@"remap %@ %@", remapped.absoluteString, taker];+ }+ }+ if (!asked) return SGLinkRouteUnknown;+ if (via) *via = taker;+ return taker ? SGLinkRouteOpens : SGLinkRouteNone;+ } @catch (NSException *e) {+ SGLog(@"links: asking about %@ threw %@", uri, e.reason);+ return SGLinkRouteUnknown;+ }+}++NSURL *SGSpotifyURIFromText(NSString *text) {+ NSString *trimmed = [text stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];+ if (!trimmed.length) return nil;+ NSURLComponents *parts = [NSURLComponents componentsWithString:trimmed];+ NSString *host = parts.host.lowercaseString;+ if ([parts.scheme.lowercaseString hasPrefix:@"http"] && [host isEqualToString:@"open.spotify.com"]) {+ NSMutableArray<NSString *> *path = [NSMutableArray array];+ for (NSString *piece in [parts.path componentsSeparatedByString:@"/"]) {+ if (piece.length) [path addObject:piece];+ }+ if (path.count && [path.firstObject hasPrefix:@"intl-"]) [path removeObjectAtIndex:0];+ if (path.count >= 2) return [NSURL URLWithString:[@"spotify:" stringByAppendingString:[path componentsJoinedByString:@":"]]];+ }+ return [NSURL URLWithString:trimmed];+}+%hook SPTLinkDispatcherImplementation- (void)setMainUILoaded:(BOOL)loaded {%orig;