Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.
C
feat: block artists and skip their tracks
Vojtěch Škopekfb99f8eef02e
diff --git a/docs/tweaks.md b/docs/tweaks.mdindex 69f45222..f5e59394 100644--- a/docs/tweaks.md+++ b/docs/tweaks.md@@ -42,6 +42,7 @@ app:AdBlock/ EeveeSpotify's ad blocking: the ad and upsell services silenced (AdServices.x), ad components out of theHub JSON (AdHubs.x) and the feeds (Feeds.m), Premium pop-ups dropped (AdPopups.x), and the responsesrewritten on the way in (AdNetwork.x, Premium.m over the protobuf walker in Protobuf.m)+ ArtistBlock/ tracks by blocked artists skipped as they start (ArtistSkip.x), the list and the Blocked artists page under PlayerOnboarding/ the welcome tour over Home on the first launch (Onboarding.x, the pages in Tour.m), offered again from the Mod pageAbout/ the update check and the Mod page: the build, its updates, the links and the resetdiff --git a/tweak/Sources/Features/ArtistBlock/ArtistBlock.h b/tweak/Sources/Features/ArtistBlock/ArtistBlock.hnew file mode 100644index 00000000..3425697d--- /dev/null+++ b/tweak/Sources/Features/ArtistBlock/ArtistBlock.h@@ -0,0 +1,22 @@+// Artist block: tracks by artists on the list are skipped as they come up (ArtistSkip.x). An artist+// is kept by URI, with the name it had when it was blocked, so a rename or a namesake changes nothing.+#import <UIKit/UIKit.h>++#define SGKeyArtistBlock @"spotifyglass.artistBlock"+#define SGKeyArtistBlockFeatured @"spotifyglass.artistBlock.featured" // off: the main artist only+#define SGKeyArtistBlockList @"spotifyglass.artistBlock.list"++extern NSString *const SGArtistURI; // NSString, spotify:artist:…+extern NSString *const SGArtistName; // NSString++NSArray<NSDictionary *> *SGBlockedArtists(void);+void SGBlockArtist(NSDictionary *artist);+void SGUnblockArtist(NSString *uri);+BOOL SGArtistBlocked(NSString *uri);++// The artists of a track, the main one first, as dictionaries of the keys above.+NSArray<NSDictionary *> *SGArtistsOfTrack(id track);+// ArtistSkip.x: the artists of the track playing now, empty before the player has reported one.+NSArray<NSDictionary *> *SGPlayingArtists(void);++UIViewController *SGArtistBlockSettingsPage(void);diff --git a/tweak/Sources/Features/ArtistBlock/ArtistBlock.m b/tweak/Sources/Features/ArtistBlock/ArtistBlock.mnew file mode 100644index 00000000..998096d2--- /dev/null+++ b/tweak/Sources/Features/ArtistBlock/ArtistBlock.m@@ -0,0 +1,68 @@+#import "ArtistBlock.h"+#import "Headers/SPTPlayer.h"++NSString *const SGArtistURI = @"uri";+NSString *const SGArtistName = @"name";++static const NSInteger kMaxCredits = 32;++NSArray<NSDictionary *> *SGBlockedArtists(void) {+ NSArray *list = [NSUserDefaults.standardUserDefaults arrayForKey:SGKeyArtistBlockList];+ return list ?: @[];+}++static void save(NSArray<NSDictionary *> *list) {+ [NSUserDefaults.standardUserDefaults setObject:list forKey:SGKeyArtistBlockList];+}++BOOL SGArtistBlocked(NSString *uri) {+ if (!uri.length) return NO;+ for (NSDictionary *artist in SGBlockedArtists()) {+ if ([artist[SGArtistURI] isEqualToString:uri]) return YES;+ }+ return NO;+}++void SGBlockArtist(NSDictionary *artist) {+ if (SGArtistBlocked(artist[SGArtistURI])) return;+ save([SGBlockedArtists() arrayByAddingObject:artist]);+}++void SGUnblockArtist(NSString *uri) {+ NSPredicate *other = [NSPredicate predicateWithBlock:^BOOL(NSDictionary *artist, NSDictionary *bindings) {+ return ![artist[SGArtistURI] isEqualToString:uri];+ }];+ save([SGBlockedArtists() filteredArrayUsingPredicate:other]);+}++static NSString *uriString(id value) {+ if ([value isKindOfClass:NSURL.class]) return ((NSURL *)value).absoluteString;+ return [value isKindOfClass:NSString.class] ? value : nil;+}++static void addArtist(NSMutableArray<NSDictionary *> *artists, NSString *uri, NSString *name) {+ if (![uri hasPrefix:@"spotify:artist:"]) return;+ for (NSDictionary *artist in artists) {+ if ([artist[SGArtistURI] isEqualToString:uri]) return;+ }+ [artists addObject:@{SGArtistURI: uri, SGArtistName: name.length ? name : uri}];+}++static NSString *nameString(id value) {+ return [value isKindOfClass:NSString.class] ? value : nil;+}++NSArray<NSDictionary *> *SGArtistsOfTrack(id object) {+ if (![object respondsToSelector:@selector(artistURI)] || ![object respondsToSelector:@selector(metadata)]) return @[];+ SPTPlayerTrack *track = object;+ NSDictionary *metadata = [track.metadata isKindOfClass:NSDictionary.class] ? track.metadata : nil;+ NSMutableArray<NSDictionary *> *artists = [NSMutableArray array];+ addArtist(artists, uriString(track.artistURI), nameString(track.artistName));+ addArtist(artists, uriString(metadata[@"artist_uri"]), nameString(metadata[@"artist_name"]));+ for (NSInteger i = 1; i < kMaxCredits; i++) {+ NSString *uri = uriString(metadata[[NSString stringWithFormat:@"artist_uri:%ld", (long)i]]);+ if (!uri) break;+ addArtist(artists, uri, nameString(metadata[[NSString stringWithFormat:@"artist_name:%ld", (long)i]]));+ }+ return artists;+}diff --git a/tweak/Sources/Features/ArtistBlock/ArtistBlockSettings.m b/tweak/Sources/Features/ArtistBlock/ArtistBlockSettings.mnew file mode 100644index 00000000..a723aaa2--- /dev/null+++ b/tweak/Sources/Features/ArtistBlock/ArtistBlockSettings.m@@ -0,0 +1,166 @@+// The Blocked artists page: the switch, whether a featured credit counts, blocking someone off the+// track playing now, and the list, swiped to unblock. The hook reads all of it per track, so a change+// needs no restart.+#import "Core/SGCore.h"+#import "Settings/SGPage.h"+#import "Settings/SGPageStyle.h"+#import "ArtistBlock.h"++typedef NS_ENUM(NSInteger, SGArtistSection) {+ SGArtistSectionSwitch,+ SGArtistSectionMode,+ SGArtistSectionAdd,+ SGArtistSectionList,+ SGArtistSectionCount,+};++@interface SGArtistBlockPage : SGPage+@end++@implementation SGArtistBlockPage {+ NSArray<NSDictionary *> *_blocked;+}++- (instancetype)init {+ if (!(self = [super initWithStyle:UITableViewStyleInsetGrouped])) return nil;+ self.title = @"Blocked Artists";+ _blocked = SGBlockedArtists();+ return self;+}++- (void)viewDidLayoutSubviews {+ [super viewDidLayoutSubviews];+ SGInsetForBars(self.tableView);+}++- (void)reload {+ _blocked = SGBlockedArtists();+ [self.tableView reloadData];+}++- (NSInteger)numberOfSectionsInTableView:(UITableView *)table {+ return SGArtistSectionCount;+}++- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {+ if (section == SGArtistSectionMode) return 2;+ if (section == SGArtistSectionList) return MAX((NSInteger)_blocked.count, 1);+ return 1;+}++- (UIView *)tableView:(UITableView *)table viewForHeaderInSection:(NSInteger)section {+ if (section == SGArtistSectionMode) return SGSectionHeader(table, @"Skip when the artist is");+ if (section == SGArtistSectionList) return SGSectionHeader(table, @"Blocked");+ return nil;+}++- (CGFloat)tableView:(UITableView *)table heightForHeaderInSection:(NSInteger)section {+ return section == SGArtistSectionMode || section == SGArtistSectionList ? SGSectionHeaderHeight : CGFLOAT_MIN;+}++- (CGFloat)tableView:(UITableView *)table heightForFooterInSection:(NSInteger)section {+ return CGFLOAT_MIN;+}++- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)path {+ UITableViewCell *cell = SGDequeueCell(table, @"artist");+ switch (path.section) {+ case SGArtistSectionSwitch: {+ SGFillCell(cell, @"Skip blocked artists", @"Their tracks are skipped as soon as they start", nil, nil);+ UISwitch *toggle = [UISwitch new];+ toggle.onTintColor = SGGreen();+ toggle.on = SGFlag(SGKeyArtistBlock, NO);+ [toggle addTarget:self action:@selector(toggled:) forControlEvents:UIControlEventValueChanged];+ cell.accessoryView = toggle;+ break;+ }+ case SGArtistSectionMode: {+ BOOL featured = path.row == 1;+ SGFillCell(cell, featured ? @"Main or featured" : @"The main artist",+ featured ? @"Any credit on the track" : @"The first name on the track", nil, nil);+ cell.selectionStyle = UITableViewCellSelectionStyleDefault;+ if (featured == SGFlag(SGKeyArtistBlockFeatured, NO)) {+ UIImageView *tick = SGSymbolView(@"checkmark", 13, UIImageSymbolWeightSemibold, 16);+ tick.tintColor = SGGreen();+ cell.accessoryView = tick;+ }+ break;+ }+ case SGArtistSectionAdd:+ SGFillCell(cell, @"Block from what's playing…", @"The main artist or anyone featured", nil, @"person.crop.circle.badge.xmark");+ cell.selectionStyle = UITableViewCellSelectionStyleDefault;+ break;+ default:+ if (!_blocked.count) {+ SGFillCell(cell, @"No one yet", nil, SGGrey(), nil);+ break;+ }+ NSDictionary *artist = _blocked[(NSUInteger)path.row];+ SGFillCell(cell, artist[SGArtistName], artist[SGArtistURI], nil, nil);+ break;+ }+ return cell;+}++- (BOOL)tableView:(UITableView *)table canEditRowAtIndexPath:(NSIndexPath *)path {+ return path.section == SGArtistSectionList && _blocked.count > 0;+}++- (NSString *)tableView:(UITableView *)table titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)path {+ return @"Unblock";+}++- (void)tableView:(UITableView *)table commitEditingStyle:(UITableViewCellEditingStyle)style forRowAtIndexPath:(NSIndexPath *)path {+ if (style != UITableViewCellEditingStyleDelete) return;+ SGUnblockArtist(_blocked[(NSUInteger)path.row][SGArtistURI]);+ [self reload];+}++- (void)tableView:(UITableView *)table didSelectRowAtIndexPath:(NSIndexPath *)path {+ [table deselectRowAtIndexPath:path animated:YES];+ if (path.section == SGArtistSectionMode) {+ SGSetEnabled(SGKeyArtistBlockFeatured, path.row == 1);+ [self reload];+ } else if (path.section == SGArtistSectionAdd) {+ [self pickFromPlaying:[table cellForRowAtIndexPath:path]];+ }+}++- (void)pickFromPlaying:(UIView *)source {+ NSArray<NSDictionary *> *artists = SGPlayingArtists();+ if (!artists.count) {+ UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Nothing playing"+ message:@"Play a track by the artist first, then come back here."+ preferredStyle:UIAlertControllerStyleAlert];+ [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:nil]];+ [self presentViewController:alert animated:YES completion:nil];+ return;+ }+ UIAlertController *sheet = [UIAlertController alertControllerWithTitle:@"Block"+ message:nil+ preferredStyle:UIAlertControllerStyleActionSheet];+ [artists enumerateObjectsUsingBlock:^(NSDictionary *artist, NSUInteger index, BOOL *stop) {+ BOOL blocked = SGArtistBlocked(artist[SGArtistURI]);+ NSString *title = index == 0 ? artist[SGArtistName] : [NSString stringWithFormat:@"%@ (featured)", artist[SGArtistName]];+ UIAlertAction *action = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleDestructive handler:^(UIAlertAction *a) {+ SGBlockArtist(artist);+ [self reload];+ }];+ action.enabled = !blocked;+ [sheet addAction:action];+ }];+ [sheet addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];+ sheet.popoverPresentationController.sourceView = source;+ sheet.popoverPresentationController.sourceRect = source.bounds;+ [self presentViewController:sheet animated:YES completion:nil];+}++- (void)toggled:(UISwitch *)toggle {+ SGSetEnabled(SGKeyArtistBlock, toggle.on);+}++@end++UIViewController *SGArtistBlockSettingsPage(void) {+ return [SGArtistBlockPage new];+}diff --git a/tweak/Sources/Features/ArtistBlock/ArtistSkip.x b/tweak/Sources/Features/ArtistBlock/ArtistSkip.xnew file mode 100644index 00000000..64cf031b--- /dev/null+++ b/tweak/Sources/Features/ArtistBlock/ArtistSkip.x@@ -0,0 +1,112 @@+// Skips a track by a blocked artist the moment the player moves onto it, whatever started it: the+// queue, autoplay, a radio or a tap. The track is read off SPTEsperantoPlayer, the app's player,+// which the now playing bar asks for its state from launch on; the full screen player's playback+// controller only exists while that player does, so it is the skip's first choice, not the source. A+// track change is heard from the lock screen's now playing info, which Spotify sets on every track.+// A track is looked at once, so an update within it does not skip twice.+#import <MediaPlayer/MediaPlayer.h>+#import "Core/SGCore.h"+#import "ArtistBlock.h"+#import "Headers/SPTPlayer.h"+#import "Headers/SPTNowPlayingPlaybackController.h"++static __weak id sg_player;+static __weak SPTNowPlayingPlaybackControllerImplementation *sg_controller;+static NSString *sg_seenTrack;++static SPTPlayerTrack *playingTrack(BOOL explain) {+ id player = sg_player;+ SPTPlayerState *state = [player respondsToSelector:@selector(state)] ? [(id<SPTPlayer>)player state] : nil;+ SPTPlayerTrack *track = [state respondsToSelector:@selector(track)] ? state.track : nil;+ if (!track && explain) {+ SGLog(@"artist block: no track, player %@ state %@",+ player ? NSStringFromClass([player class]) : @"nil", state ? NSStringFromClass(state.class) : @"nil");+ }+ return track;+}++NSArray<NSDictionary *> *SGPlayingArtists(void) {+ SPTPlayerTrack *track = playingTrack(YES);+ NSArray<NSDictionary *> *artists = SGArtistsOfTrack(track);+ if (track && !artists.count) {+ SGLog(@"artist block: no artists in %@, artistURI %@ (%@)", NSStringFromClass(track.class),+ [track respondsToSelector:@selector(artistURI)] ? track.artistURI : @"-",+ [track respondsToSelector:@selector(artistURI)] ? NSStringFromClass([track.artistURI class]) : @"-");+ }+ return artists;+}++static NSString *trackKey(SPTPlayerTrack *track) {+ id uri = [track respondsToSelector:@selector(URI)] ? track.URI : nil;+ return [uri isKindOfClass:NSURL.class] ? ((NSURL *)uri).absoluteString : [uri description];+}++static NSDictionary *blockedArtistOf(SPTPlayerTrack *track) {+ NSArray<NSDictionary *> *artists = SGArtistsOfTrack(track);+ if (!SGFlag(SGKeyArtistBlockFeatured, NO) && artists.count > 1) artists = @[artists.firstObject];+ for (NSDictionary *artist in artists) {+ if (SGArtistBlocked(artist[SGArtistURI])) return artist;+ }+ return nil;+}++static void skip(SPTPlayerTrack *track, NSDictionary *artist) {+ SPTNowPlayingPlaybackControllerImplementation *controller = sg_controller;+ id player = sg_player;+ if (controller.canSkipNext) {+ [controller skipToNextWhileDragging:NO];+ } else if ([player respondsToSelector:@selector(skipToNextTrack)]) {+ [(id<SPTPlayer>)player skipToNextTrack];+ } else {+ SGLog(@"artist block: %@ by %@ cannot be skipped", track.trackTitle, artist[SGArtistName]);+ return;+ }+ SGLog(@"artist block: skipped %@ by %@", track.trackTitle, artist[SGArtistName]);+}++static void trackMayHaveChanged(void) {+ SPTPlayerTrack *track = playingTrack(NO);+ NSString *key = trackKey(track);+ if (!key || [key isEqualToString:sg_seenTrack]) return;+ sg_seenTrack = key;++ static BOOL logged;+ if (!logged) {+ logged = YES;+ SGLogLong(@"artist block: track metadata", track.metadata.description);+ }++ if (!SGFlag(SGKeyArtistBlock, NO)) return;+ NSDictionary *artist = blockedArtistOf(track);+ if (artist) skip(track, artist);+}++%hook SPTEsperantoPlayer+- (id)state {+ if (!sg_player) {+ sg_player = self;+ SGLog(@"artist block: player found");+ }+ return %orig;+}+%end++%hook SPTNowPlayingPlaybackControllerImplementation+- (id)initWithPlayer:(id)player testManager:(id)manager inStreamClient:(id)client {+ id controller = %orig;+ sg_controller = controller;+ return controller;+}+%end++%hook MPNowPlayingInfoCenter+- (void)setNowPlayingInfo:(NSDictionary *)info {+ %orig;+ dispatch_async(dispatch_get_main_queue(), ^{ trackMayHaveChanged(); });+}+%end++%ctor {+ %init;+ SGRequireClasses(@[@"SPTEsperantoPlayer", @"SPTNowPlayingPlaybackControllerImplementation", @"MPNowPlayingInfoCenter"]);+}diff --git a/tweak/Sources/Features/NowPlaying/NowPlayingSettings.m b/tweak/Sources/Features/NowPlaying/NowPlayingSettings.mindex 9ffc7ad9..8dba0bc2 100644--- a/tweak/Sources/Features/NowPlaying/NowPlayingSettings.m+++ b/tweak/Sources/Features/NowPlaying/NowPlayingSettings.m@@ -3,6 +3,7 @@#import "Features/Declutter/Declutter.h"#import "Features/Flags/Flags.h"#import "Features/Gestures/Gestures.h"+#import "Features/ArtistBlock/ArtistBlock.h"static UIViewController *lyricsPage(void) {return [[SGModPage alloc] initWithTitle:@"Lyrics" intro:SGRestartNote sections:@[@@ -23,6 +24,7 @@ UIViewController *SGNowPlayingSettingsPage(void) {SGSection(nil, @[SGPageRow(@"Gestures", ^UIViewController *{ return SGGesturesSettingsPage(); }),SGPageRow(@"Lyrics", ^UIViewController *{ return lyricsPage(); }),+ SGPageRow(@"Blocked artists", ^UIViewController *{ return SGArtistBlockSettingsPage(); }),]),SGSection(@"Liquid Glass", @[SGOptionRow(@"Now playing bar", @"Glass card with round artwork", SGKeyNowPlayingBar),diff --git a/tweak/Sources/Headers/SPTPlayer.h b/tweak/Sources/Headers/SPTPlayer.hnew file mode 100644index 00000000..c0024f53--- /dev/null+++ b/tweak/Sources/Headers/SPTPlayer.h@@ -0,0 +1,21 @@+// SPTEsperantoPlayer, the app's player, and the state it reports.+// The URIs are typed id: the runtime says only that they are objects, so a reader takes NSURL or NSString.+#import <Foundation/Foundation.h>++@interface SPTPlayerTrack : NSObject+@property (nonatomic, readonly) id URI;+@property (nonatomic, readonly) id artistURI;+@property (nonatomic, readonly) NSString *artistName;+@property (nonatomic, readonly) NSString *trackTitle;+// Every credited artist: artist_uri and artist_name, then artist_uri:1, artist_name:1 and on.+@property (nonatomic, readonly) NSDictionary<NSString *, NSString *> *metadata;+@end++@interface SPTPlayerState : NSObject+@property (nonatomic, readonly) SPTPlayerTrack *track;+@end++@protocol SPTPlayer <NSObject>+@property (nonatomic, readonly) SPTPlayerState *state;+- (void)skipToNextTrack;+@end