Skip to content

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 local file's own cover replaces the placeholder in the player

My-Name-Is-Jeff3d55b1b89bae
diff --git a/docs/tweaks.md b/docs/tweaks.md
index 51d1380c..47ca4448 100644
--- a/docs/tweaks.md
+++ b/docs/tweaks.md
@@ -135,6 +135,11 @@ Shared:
[ti:] and [ar:], or a name of "Artist - Title", folded; an import or a delete has the engine forget
what it kept for local files and ask again. SGImportedLRCAsk is the same as a source for the
order, which moves it on top at the first import and at every link once LyricsSources lists it.
+ Where Spotify's player shows its placeholder for a local file, the file's own cover is found
+ (LocalCover.m) in the image fields of its metadata or, while the titles and artists agree, in the
+ system's now playing artwork, which the lock screen shows; the native look puts it into the front
+ cover's empty image view (Native/LocalFiles/LocalCover.x), the redesign's artwork bridge takes it
+ as the picture of a local file whose metadata names none. A cover picked in Edit info wins.
The model and the LRC reading are checked on the Mac against harness/local-files/, the row and
the editor in the simulator against its sim/
Navigation/ the page transition fix (PageTransition.x) and opening a spotify: link (Links.x)
@@ -251,7 +256,8 @@ Native:
LocalFiles/ a lyrics button on the player's footer for a local file with an LRC file linked to it, a fifth of the
way in from the leading edge, which opens a page of the lines on black that follows the song
(LocalLyrics.h lists its files); the file's metadata says has_lyrics. Checked in the simulator
- against harness/local-files/lyrics/
+ against harness/local-files/lyrics/. And the file's own cover where the player shows
+ Spotify's placeholder (LocalCover.x)
Home/ the Home gradient, Home's sections and pills hidden (HomeDeclutter.x), the Home & Library page
Playlist/ the playlist header and pills, hidden one switch each
Album/, Artist/ their pages' parts hidden, and the cover or photo behind their headers
diff --git a/harness/player/stubs.m b/harness/player/stubs.m
index 6970103a..679c8383 100644
--- a/harness/player/stubs.m
+++ b/harness/player/stubs.m
@@ -142,6 +142,7 @@ UIViewController *SGTopController(void) { return UIApplication.sharedApplication
#import "Shared/LocalFiles/LocalFiles.h"
NSNotificationName const SGLocalFileEditsDidChangeNotification = @"harness.localFileEditsDidChange";
NSString *SGLocalFileCoverInURL(NSString *url) { return nil; }
+UIImage *SGLocalFileFallbackCover(SPTPlayerTrack *track) { return nil; }
// Speed and pitch: a number the harness keeps, with no audio behind it.
static double sg_speed = 1;
diff --git a/tweak/Sources/Native/LocalFiles/LocalCover.x b/tweak/Sources/Native/LocalFiles/LocalCover.x
new file mode 100644
index 00000000..a974d7e8
--- /dev/null
+++ b/tweak/Sources/Native/LocalFiles/LocalCover.x
@@ -0,0 +1,112 @@
+// A local file's own cover where the native player shows Spotify's placeholder (Shared/LocalFiles/
+// LocalCover.m finds it). Spotify's player keeps a list of covers scrolled sideways, a cell per track in
+// the queue (Native/Player/Player.x), and the cell under the list's middle is the one playing; the cover
+// is the largest image view in it, 354pt across, with a 64pt glyph standing in while it has no picture.
+// The cover is put into that image view only while it has none. A cover-tilt view outside the list (the
+// cover inspected full screen) gets the same.
+//
+// A picture of the mod's is remembered with the track it was put in for, so a cell reused for another
+// track loses it, and Spotify's own picture, once it comes, is never replaced.
+#import "Core/SGCore.h"
+#import "Headers/SPTPlayer.h"
+#import "Shared/LocalFiles/LocalFiles.h"
+#import "Shared/Player/PlayerState.h"
+
+static const CGFloat kCoverMinWidth = 200;
+static char kImageKey, kTrackKey;
+static __weak UIScrollView *sg_list;
+
+static BOOL isCoverCell(UIView *view) {
+ static Class cell, legacy;
+ static dispatch_once_t once;
+ dispatch_once(&once, ^{
+ cell = NSClassFromString(@"_TtC28NowPlaying_ContentLayersImpl16CoverArtCellImpl");
+ legacy = NSClassFromString(@"_TtC28NowPlaying_ContentLayersImpl22LegacyCoverArtCellImpl");
+ });
+ return (cell && [view isKindOfClass:cell]) || (legacy && [view isKindOfClass:legacy]);
+}
+
+static UIImageView *largestImageIn(UIView *root) {
+ __block UIImageView *largest = nil;
+ __block CGFloat widest = kCoverMinWidth;
+ SGForEachView(root, ^(UIView *view) {
+ if (![view isKindOfClass:UIImageView.class] || view.bounds.size.width < widest) return;
+ widest = view.bounds.size.width;
+ largest = (UIImageView *)view;
+ });
+ return largest;
+}
+
+static void fill(UIImageView *view) {
+ if (!view) return;
+ BOOL ours = view.image && view.image == objc_getAssociatedObject(view, &kImageKey);
+ if (view.image && !ours) return;
+ SPTPlayerTrack *track = SGPlayerState().track;
+ NSString *uri = SGURIString(track.URI);
+ if (ours && [uri isEqualToString:objc_getAssociatedObject(view, &kTrackKey)]) return;
+ UIImage *cover = SGLocalFileFallbackCover(track);
+ if (!cover && !ours) return;
+ view.image = cover;
+ objc_setAssociatedObject(view, &kImageKey, cover, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
+ objc_setAssociatedObject(view, &kTrackKey, uri, OBJC_ASSOCIATION_COPY_NONATOMIC);
+}
+
+static void fillFront(UIScrollView *list) {
+ CGFloat middle = list.contentOffset.x + list.bounds.size.width / 2;
+ for (UIView *cell in list.subviews) {
+ if (!isCoverCell(cell) || middle < CGRectGetMinX(cell.frame) || middle > CGRectGetMaxX(cell.frame)) continue;
+ sg_list = list;
+ fill(largestImageIn(cell));
+ }
+}
+
+// The track changing, or the now playing info catching up with it a moment later, lays nothing out.
+@interface SGLocalCoverWatcher : NSObject <SGPlayerStateObserver>
+@end
+
+@implementation SGLocalCoverWatcher
+- (void)playerStateDidChange:(SPTPlayerState *)state {
+ for (NSNumber *delay in @[@0, @1.5]) {
+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay.doubleValue * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
+ UIScrollView *list = sg_list;
+ if (list.window) fillFront(list);
+ });
+ }
+}
+@end
+
+%hook _TtC35NowPlaying_ContentLayerPlatformImpl24AccessibleCollectionView
+- (void)layoutSubviews {
+ %orig;
+ fillFront((UIScrollView *)self);
+}
+%end
+
+%hook _TtC35CreativeWorkCommons_CoverArtTiltKit16CoverArtTiltView
+- (void)layoutSubviews {
+ %orig;
+ UIView *tilt = (UIView *)self;
+ if (tilt.bounds.size.width < kCoverMinWidth) return;
+ // In the list the front cell is the list's to fill; elsewhere, only inside the player, since album
+ // pages show their covers in the same view.
+ BOOL player = NO;
+ for (UIView *view = tilt.superview; view; view = view.superview) {
+ if (isCoverCell(view)) return;
+ player = player || [NSStringFromClass(view.class) containsString:@"NowPlaying"];
+ }
+ if (player) fill(largestImageIn(tilt));
+}
+%end
+
+%ctor {
+ if (!SGNativeUI()) return;
+ %init;
+ static SGLocalCoverWatcher *watcher;
+ watcher = [SGLocalCoverWatcher new];
+ SGAddPlayerStateObserver(watcher);
+ SGRequireClasses(@[
+ @"_TtC35NowPlaying_ContentLayerPlatformImpl24AccessibleCollectionView",
+ @"_TtC35CreativeWorkCommons_CoverArtTiltKit16CoverArtTiltView",
+ @"_TtC28NowPlaying_ContentLayersImpl16CoverArtCellImpl",
+ ]);
+}
diff --git a/tweak/Sources/Redesigned/Kit/SGRBridges.x b/tweak/Sources/Redesigned/Kit/SGRBridges.x
index e781e80a..a4ca6c69 100644
--- a/tweak/Sources/Redesigned/Kit/SGRBridges.x
+++ b/tweak/Sources/Redesigned/Kit/SGRBridges.x
@@ -146,6 +146,18 @@ static void fetchPicture(NSString *key, NSURL *url, NSUInteger attempt) {
[sg_fetch resume];
}
+// A local file whose metadata names no picture: its own cover, found where Spotify shows it elsewhere
+// (Shared/LocalFiles/LocalCover.m), as the picture the track names, so a screen's placeholder never
+// replaces it. A cover picked in Edit info is named by the metadata and never gets here. NO when there
+// is none to publish.
+static BOOL publishLocalCover(void) {
+ if (![sg_wantedKey hasPrefix:@"track:"]) return NO;
+ UIImage *cover = SGLocalFileFallbackCover(SGPlayerState().track);
+ if (!cover) return NO;
+ if (cover != sg_artwork) publish(cover, sg_wantedKey, SGRArtworkQualityExact);
+ return YES;
+}
+
// Brings the playing track's picture up to date with the player's state, and fetches a picture newly
// wanted. Called on every state report and before any view read is believed, whichever comes first.
static void followPlayer(void) {
@@ -164,6 +176,7 @@ static void followPlayer(void) {
sg_fetch = nil;
if ([key isEqualToString:sg_artworkKey] && sg_artworkQuality == SGRArtworkQualityExact) return;
if (url) fetchPicture(key, url, 0);
+ else publishLocalCover();
}
void SGRSetNowPlayingArtwork(UIImage *image, NSString *trackURI, SGRArtworkQuality quality) {
@@ -210,6 +223,8 @@ static char kBarCardKey, kBarImageKey;
static __weak UIView *sg_barView;
static void publishBarArtwork(void) {
+ followPlayer();
+ if (publishLocalCover()) return;
UIView *card = SGRFindByIdentifier(sg_barView, @"SPTNowPlayingBar", &kBarCardKey);
UIView *holder = SGRFindByIdentifier(card, @"Encore.ImageView", &kBarImageKey);
UIImageView *cover = nil;
diff --git a/tweak/Sources/Shared/LocalFiles/LocalCover.m b/tweak/Sources/Shared/LocalFiles/LocalCover.m
new file mode 100644
index 00000000..19130ed9
--- /dev/null
+++ b/tweak/Sources/Shared/LocalFiles/LocalCover.m
@@ -0,0 +1,91 @@
+// A local file's own cover for where Spotify's player shows its placeholder (LocalFiles.h). The lock
+// screen and the queue show the cover of such a file, so it is there to find: first in the image fields
+// of the track's metadata, read as a picture or a readable file, then in the system's now playing info,
+// taken only while its title and artist agree with the track's.
+//
+// Threading: main thread. A picture found is kept per track; a miss is tried again after a second, since
+// the now playing info may not have caught up with the track yet.
+#import <MediaPlayer/MediaPlayer.h>
+#import <objc/message.h>
+#import "Core/SGCore.h"
+#import "Headers/SPTPlayer.h"
+#import "Shared/Player/PlayerState.h"
+#import "LocalFiles.h"
+
+static const NSUInteger kKept = 8;
+static const NSTimeInterval kRetry = 1;
+static const CGFloat kSide = 1200;
+
+static NSMutableDictionary<NSString *, UIImage *> *sg_found;
+static NSMutableDictionary<NSString *, NSDate *> *sg_missed;
+
+// A file URL, an absolute path, or Spotify's own reading of a value (-[NSURL spt_localFileImagePath],
+// asked only when the object answers it as a getter of an object).
+static NSString *pathIn(id value) {
+ if ([value isKindOfClass:NSString.class] && [value hasPrefix:@"/"]) return value;
+ NSURL *url = [value isKindOfClass:NSURL.class] ? value : [value isKindOfClass:NSString.class] ? [NSURL URLWithString:value] : nil;
+ if (url.isFileURL) return url.path;
+ SEL getter = NSSelectorFromString(@"spt_localFileImagePath");
+ if (![url respondsToSelector:getter]) return nil;
+ NSMethodSignature *signature = [url methodSignatureForSelector:getter];
+ if (signature.numberOfArguments != 2 || signature.methodReturnType[0] != '@') return nil;
+ id path = ((id (*)(id, SEL))objc_msgSend)(url, getter);
+ return [path isKindOfClass:NSString.class] && [path length] ? path : nil;
+}
+
+static UIImage *fromMetadata(NSDictionary *metadata) {
+ for (NSString *field in @[@"image_xlarge_url", @"image_large_url", @"image_url", @"image_small_url"]) {
+ id value = metadata[field];
+ if ([value isKindOfClass:UIImage.class]) return value;
+ NSString *path = pathIn(value);
+ if (!path || ![NSFileManager.defaultManager isReadableFileAtPath:path]) continue;
+ UIImage *image = [UIImage imageWithContentsOfFile:path];
+ if (image) return image;
+ }
+ return nil;
+}
+
+// Case, diacritics and width ignored; a side with no value agrees with anything.
+static BOOL agrees(id mine, id theirs) {
+ if (![mine isKindOfClass:NSString.class] || ![theirs isKindOfClass:NSString.class] || ![mine length] || ![theirs length]) return YES;
+ return [mine compare:theirs options:NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch | NSWidthInsensitiveSearch] == NSOrderedSame;
+}
+
+static UIImage *fromNowPlaying(SPTPlayerTrack *track) {
+ NSDictionary *info = MPNowPlayingInfoCenter.defaultCenter.nowPlayingInfo;
+ MPMediaItemArtwork *artwork = info[MPMediaItemPropertyArtwork];
+ if (![artwork isKindOfClass:MPMediaItemArtwork.class]) return nil;
+ if (!agrees(track.trackTitle, info[MPMediaItemPropertyTitle]) || !agrees(track.artistName, info[MPMediaItemPropertyArtist])) return nil;
+ return [artwork imageWithSize:CGSizeMake(kSide, kSide)];
+}
+
+UIImage *SGLocalFileFallbackCover(SPTPlayerTrack *track) {
+ NSString *uri = SGURIString(track.URI);
+ // A cover picked in Edit info comes through Spotify's own loader and wins.
+ if (!SGLocalFileIs(uri) || SGLocalFileCoverPath(SGLocalFileEditFor(uri))) return nil;
+ if (!sg_found) {
+ sg_found = [NSMutableDictionary dictionary];
+ sg_missed = [NSMutableDictionary dictionary];
+ }
+ UIImage *image = sg_found[uri];
+ if (image) return image;
+ NSDate *missed = sg_missed[uri];
+ if (missed && -missed.timeIntervalSinceNow < kRetry) return nil;
+ NSDictionary *metadata = [track.metadata isKindOfClass:NSDictionary.class] ? track.metadata : nil;
+ image = fromMetadata(metadata);
+ NSString *source = @"its metadata";
+ if (!image) {
+ image = fromNowPlaying(track);
+ source = @"the system's now playing";
+ }
+ if (!image.CGImage && !image.CIImage) {
+ if (sg_missed.count >= kKept * 4) [sg_missed removeAllObjects];
+ sg_missed[uri] = NSDate.date;
+ return nil;
+ }
+ if (sg_found.count >= kKept) [sg_found removeAllObjects];
+ sg_found[uri] = image;
+ [sg_missed removeObjectForKey:uri];
+ SGLog(@"local files: %@ has its cover from %@", uri, source);
+ return image;
+}
diff --git a/tweak/Sources/Shared/LocalFiles/LocalFiles.h b/tweak/Sources/Shared/LocalFiles/LocalFiles.h
index fb732647..ece42600 100644
--- a/tweak/Sources/Shared/LocalFiles/LocalFiles.h
+++ b/tweak/Sources/Shared/LocalFiles/LocalFiles.h
@@ -7,6 +7,7 @@
// the system's now playing artwork
// EditInfoMenu.x Edit info in the player's ⋯ menu while a local file plays, and its editor
// LocalLyrics.h the user's own .lrc files, linked to a local file or matched by its names
+// LocalCover.m the file's own cover, found where Spotify's player shows a placeholder
//
// The file itself is never written: Spotify read its tags when it scanned it, and writing them back
// would take a tag writer per format. So an edit is stored by URI and laid over what Spotify read.
@@ -51,3 +52,9 @@ extern NSNotificationName const SGLocalFileEditsDidChangeNotification;
// EditInfoMenu.x: the editor for a local file, presented over `presenter`.
void SGLocalFilePresentEditor(UIViewController *presenter, NSString *uri);
+
+// LocalCover.m: the file's own cover for a local track whose player shows Spotify's placeholder, from the
+// image fields of its metadata or the system's now playing artwork; nil for any other track, for one with a
+// cover picked in Edit info, and until one is found. Main thread.
+@class SPTPlayerTrack;
+UIImage *SGLocalFileFallbackCover(SPTPlayerTrack *track);