Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.
C
fix: Top picks finds the Cards from shelf before it is on screen (#42)
My-Name-Is-Jeff7c53f20402b1
diff --git a/docs/tweaks.md b/docs/tweaks.mdindex b790346d..591607a3 100644--- a/docs/tweaks.md+++ b/docs/tweaks.md@@ -576,7 +576,7 @@ Redesigned:continuous corners on the covers, and, under the Home look switch (the Home page's first row; off, none ofits hooks install and Home is as before it): at the trailing edge a glass pill of the filter (a menu thatdrives Spotify's own chips, with the device picker and the top shelf's Show all) and the avatar, Top picks- (the first shelf, or the one Cards from names, moved above the shortcuts and drawn as large cards in their+ (the first shelf, or the one Cards from names, found in Spotify's feed model before its cells are built, moved above the shortcuts and drawn as large cards in theircovers' colors with no heading, the page tinted by the first card) and each shortcut tile as a glass pillin its cover's color with its name in bold on up to two lines; in FLEX builds a meter of each scroll'sframes and the hooks' time (Home.h lists its files)diff --git a/tweak/Sources/Redesigned/Home/Home.h b/tweak/Sources/Redesigned/Home/Home.hindex b74c6acb..384c6131 100644--- a/tweak/Sources/Redesigned/Home/Home.h+++ b/tweak/Sources/Redesigned/Home/Home.h@@ -6,6 +6,7 @@// stay only when switched on; episode cards and any new kind collapse. The DJ loses its// heading, and the flags the redesign forces on Home// HomeShelves.m a shelf's identity from its heading, the shelves seen lately and which are hidden+// HomeFeed.swift every section's shelf identity from Spotify's model of the feed, built or not// HomeSettings.m the Home page of Mod Settings: Top picks, what stays on Home, one switch per shelf// HomeHeader.x a large title where the filter pills were, the avatar at the trailing edge, no scrim// HomeHeadings.x the shelves' headings in the Music app's size, drawn over Spotify's@@ -53,6 +54,8 @@ UIViewController *SGRHomeSettingsPage(void); // for Mod Settings (App/ModSetti// the small line over the title when there is one ("More like …", for any artist), else the title ("Recents",// an artist's name). nil while the shelf has no heading to read. Language-bound, as the headings are.NSString *SGRHomeShelfIdentity(UIView *shelf);+// The identity a heading of these two lines gives: "More like …" for "More like" over "Chief Keef", else the title.+NSString *SGRHomeShelfIdentityFrom(NSString *small, NSString *title);NSString *SGRHomeShelfTitle(UIView *shelf); // the heading's title alone ("Chief Keef" under "More like")void SGRHomeNoteShelf(NSString *identity); // seen on Home nowNSArray<NSString *> *SGRHomeShelvesSeen(void); // seen within 30 days, in the order first seen@@ -108,6 +111,7 @@ typedef NS_ENUM(NSUInteger, SGRHomeProbe) {SGRHomeProbeShelfLayout, // the shelf's layoutSubviews hook (TouchCancellingCollectionView)SGRHomeProbeLargeCard, // SGRHomeStyleLargeCardSGRHomeProbeTint, // SGRHomeTintForCover asks (cache misses go off the main thread)+ SGRHomeProbeFeed, // the walk of the feed's model for the shelves' headings (HomeFeed.swift)SGRHomeProbeCount,};CFTimeInterval SGRHomeProbeBegin(void);diff --git a/tweak/Sources/Redesigned/Home/HomeFeed.swift b/tweak/Sources/Redesigned/Home/HomeFeed.swiftnew file mode 100644index 00000000..f507efbf--- /dev/null+++ b/tweak/Sources/Redesigned/Home/HomeFeed.swift@@ -0,0 +1,85 @@+// Home redesign: every section's shelf heading from the model behind Home's list, for the sections whose cells+// are not built yet. Spotify builds a section's views only as it nears the screen, so a heading a shelf further+// down has is not there to read (HomeShelves.m); the list's content snapshot holds every section's props.+//+// The path, from the page's controller (Home_EvoPageImpl.EvoLoadableResourceViewController, the list's+// delegate), by the stored properties' names (in the 9.1.78, 9.1.88 and 9.1.90 binaries' reflection strings):+// collectionViewRenderer: Element_List.CollectionViewRenderer+// .contentDataSource: Element_List.ContentDataSource (not the list's own dataSource, which is UIKit's diffable one)+// .contentSnapshot: .sections, one per section of the list, each with .items (ItemIdentifier),+// and .items, ItemIdentifier -> Section.Item+// Section.Item .item.configuration.props: Home_EvoPageImpl.HomeStructure, one case per kind (shelf: ShelfData,+// recents: RecentsData, ...), its payload's .heading: HomeHeading?+// HomeHeading .withText(text:...) or .enrichedHeading(text:..., enrichedTextRange:),+// the range being the large part ("Chief Keef" in "More like Chief Keef")+// On 9.1.90: section 11 is .shelf, heading .withText("Your favorite artists"); section 12 .enrichedHeading("More+// like Alexis Weissenberg"), which SGRHomeShelfIdentity reads off the view as "More like …".+//+// The two objects on the way (the renderer, the data source) are read by ivar: Mirror on a class loads every+// stored property up to the one asked for, and a dangling unowned one among them traps. The data source's own+// stored properties (two subscriptions, the content, the previous snapshot) are read that way once more for the+// snapshot, a struct no ivar read gives. Mirror reads what is there and nothing else, so a version that names+// these otherwise gives nil and Top picks goes by the headings on screen as before. Main thread only.+import UIKit++@objc(SGRHomeFeed)+final class SGRHomeFeed: NSObject {+ private static func ivar(_ object: AnyObject, _ name: String) -> AnyObject? {+ guard let ivar = class_getInstanceVariable(type(of: object), name) else { return nil }+ return object_getIvar(object, ivar) as AnyObject?+ }++ private static func child(_ value: Any, _ label: String) -> Any? {+ var mirror: Mirror? = Mirror(reflecting: value)+ while let current = mirror {+ if let found = current.children.first(where: { $0.label == label }) { return found.value }+ mirror = current.superclassMirror+ }+ return nil+ }++ private static func some(_ value: Any?) -> Any? {+ guard let value else { return nil }+ let mirror = Mirror(reflecting: value)+ guard mirror.displayStyle == .optional else { return value }+ return mirror.children.first?.value+ }++ // The heading's small line and its title, the way the view shows them: the large part is the title, the rest+ // the line over it ("More like" over "Chief Keef"); with no large part the whole text is the title. A range+ // past the text's end is not this text's, and the whole text is the title.+ static func parts(text: String, large: Range<String.Index>?) -> [String] {+ guard let large, large.upperBound <= text.endIndex else { return ["", text.trimmingCharacters(in: .whitespaces)] }+ let small = (text[..<large.lowerBound] + text[large.upperBound...]).trimmingCharacters(in: .whitespaces)+ return [small, text[large].trimmingCharacters(in: .whitespaces)]+ }++ private static func parts(props: Any) -> [String]? {+ guard let payload = Mirror(reflecting: props).children.first?.value,+ let heading = some(child(payload, "heading")),+ let fields = Mirror(reflecting: heading).children.first?.value,+ let text = child(fields, "text") as? String else { return nil }+ return parts(text: text, large: some(child(fields, "enrichedTextRange")) as? Range<String.Index>)+ }++ // One entry per section of the list: [small line, title] of its heading, or NSNull where it has none. nil when+ // the model is not where it was found. About 0.6 ms on 9.1.90 (30 sections).+ @objc(headingsOf:) static func headings(of page: AnyObject?) -> NSArray? {+ guard let page, let renderer = ivar(page, "collectionViewRenderer"), let source = ivar(renderer, "contentDataSource"),+ let snapshot = child(source, "contentSnapshot"),+ let sections = child(snapshot, "sections"),+ let items = child(snapshot, "items") else { return nil }+ var byItem: [AnyHashable: Any] = [:]+ for pair in Mirror(reflecting: items).children {+ let both = Mirror(reflecting: pair.value).children.map(\.value)+ if both.count == 2, let item = both[0] as? AnyHashable { byItem[item] = both[1] }+ }+ let headings = Mirror(reflecting: sections).children.map { section -> Any in+ guard let first = child(section.value, "items").flatMap({ Mirror(reflecting: $0).children.first?.value as? AnyHashable }),+ let entry = byItem[first], let item = child(entry, "item"),+ let configuration = child(item, "configuration"), let props = child(configuration, "props") else { return NSNull() }+ return parts(props: props) ?? NSNull()+ }+ return headings as NSArray+ }+}diff --git a/tweak/Sources/Redesigned/Home/HomePerf.x b/tweak/Sources/Redesigned/Home/HomePerf.xindex 3c7450e8..b4c7aa75 100644--- a/tweak/Sources/Redesigned/Home/HomePerf.x+++ b/tweak/Sources/Redesigned/Home/HomePerf.x@@ -24,7 +24,7 @@ static const NSUInteger kLongFramesDescribed = 5;// Section sizing includes Spotify's own measuring of the cell, which the hook calls through.static const char *const kProbeNames[SGRHomeProbeCount] = {"section sizing", "header", "headings", "cards", "tiles",- "labels", "labels matched", "indicator shown", "list attrs", "shelf layout", "large card", "tint asks"};+ "labels", "labels matched", "indicator shown", "list attrs", "shelf layout", "large card", "tint asks", "feed model"};static NSUInteger sg_calls[SGRHomeProbeCount];static CFTimeInterval sg_time[SGRHomeProbeCount];static __weak UIScrollView *sg_list;diff --git a/tweak/Sources/Redesigned/Home/HomeShelves.m b/tweak/Sources/Redesigned/Home/HomeShelves.mindex f2379c07..4629b0c4 100644--- a/tweak/Sources/Redesigned/Home/HomeShelves.m+++ b/tweak/Sources/Redesigned/Home/HomeShelves.m@@ -75,11 +75,14 @@ static NSString *trimmed(UILabel *label) {return [label.text stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceCharacterSet];}+NSString *SGRHomeShelfIdentityFrom(NSString *small, NSString *title) {+ return small.length ? [small stringByAppendingString:@" …"] : title;+}+NSString *SGRHomeShelfIdentity(UIView *shelf) {UILabel *title = nil, *over = nil;if (!headingLabels(shelf, &title, &over)) return nil;- NSString *small = trimmed(over);- return small.length ? [small stringByAppendingString:@" …"] : trimmed(title);+ return SGRHomeShelfIdentityFrom(trimmed(over), trimmed(title));}NSString *SGRHomeShelfTitle(UIView *shelf) {diff --git a/tweak/Sources/Redesigned/Home/HomeTopPicks.x b/tweak/Sources/Redesigned/Home/HomeTopPicks.xindex d56eddb8..dd789c9c 100644--- a/tweak/Sources/Redesigned/Home/HomeTopPicks.x+++ b/tweak/Sources/Redesigned/Home/HomeTopPicks.x@@ -11,7 +11,9 @@// the card grows: the card to its new width plus kFooter, the row that holds the cards by the same amount.// The page's cell is made taller by the same amount.//-// The shelf drawn large is the one nearest the top of the page: the lowest index any shelf has been measured at.+// The shelf drawn large is the one nearest the top of the page: the lowest index any shelf has been measured at. One+// Cards from names is taken from the feed's model (HomeFeed.swift) as soon as any section is measured, since its own+// cell is built only once it nears the screen; the heading on screen still decides when the model can't be read.// Cells are reused across shelves, so each measuring sets or clears the mark on the layout it holds.//// A large card (SGRHomeStyleLargeCard, from HomeCards.x on every layout of the card) keeps Spotify's button, cover@@ -270,10 +272,79 @@ static void move(UICollectionViewLayoutAttributes *attributes, CGRect grid, CGRe}%end+#pragma mark - the shelf Cards from names, from the feed++// HomeFeed.swift.+@interface SGRHomeFeed : NSObject++ (NSArray *)headingsOf:(id)page;+@end++// The model's identities as last read (NSNull for a section with no heading) and the list's section count then.+// Spotify's feed comes again with a new count nearly always (30 to 31 at launch, 18, 20, 21 through a refresh on+// 9.1.90); one that keeps the count is caught by the first heading read off a cell that disagrees with it.+static NSArray *sg_feed;+static NSInteger sg_feedSections = -1;+// The model the top was taken from, while no heading has borne it out; and the model a heading read off the top's+// own cell contradicted, with the Cards from it was for: that answer is not taken again, or the top would be lost+// and found on every pass.+static NSArray *sg_topFeed, *sg_overruled;+static NSString *sg_overruledFrom;++// One identity per section of the list from the model, nil when the model can't be read or does not match the+// list's sections (a snapshot the list has not taken yet).+static NSArray *feed(UICollectionView *list) {+ NSInteger sections = list.numberOfSections;+ if (sg_feed && sg_feedSections == sections) return sg_feed;+ CFTimeInterval began = SGRHomeProbeBegin();+ NSArray *headings = [SGRHomeFeed headingsOf:list.delegate];+ NSMutableArray *identities = [NSMutableArray arrayWithCapacity:headings.count];+ for (id heading in headings) {+ [identities addObject:[heading isKindOfClass:NSArray.class] ? SGRHomeShelfIdentityFrom(heading[0], heading[1]) : NSNull.null];+ }+ SGRHomeProbeEnd(SGRHomeProbeFeed, began);+ sg_feed = headings && (NSInteger)headings.count == sections ? identities : nil;+ sg_feedSections = sections;+ return sg_feed;+}++// The section's identity in the model, nil for none or when the model can't be read.+static NSString *feedIdentity(UICollectionView *list, NSInteger section) {+ NSArray *identities = feed(list);+ id identity = section < (NSInteger)identities.count ? identities[section] : nil;+ return [identity isKindOfClass:NSString.class] ? identity : nil;+}++// A heading read off a shelf's cell that is not what the model last said of its section: the model moved on under+// the same section count, and is read again.+static void feedRead(UICollectionView *list, NSInteger section, NSString *onScreen) {+ if (!sg_feed || sg_feedSections != list.numberOfSections || section >= (NSInteger)sg_feed.count) return;+ id entry = sg_feed[section];+ if ([entry isKindOfClass:NSString.class] && [entry isEqualToString:onScreen]) return;+ SGLog(@"redesign home: the feed's model moved on under section %ld (\"%@\" on screen, %@ in the model)", (long)section, onScreen, entry);+ sg_feed = nil;+}++// With no top yet and Cards from naming a shelf, the top is that shelf's section in the model, measured or not: the+// swap brings its cell on screen, and its measuring draws it large.+static void topFromFeed(UICollectionView *list) {+ NSString *from = SGRHomeTopPicksFrom();+ if (sg_topIndex != NSIntegerMax || !from || SGRHomeShelfHidden(from)) return;+ NSArray *identities = feed(list);+ NSUInteger section = [identities indexOfObject:from];+ if (section == NSNotFound || ([identities isEqualToArray:sg_overruled] && [from isEqualToString:sg_overruledFrom])) return;+ sg_topIndex = (NSInteger)section * 10000;+ sg_topFeed = identities;+ sg_blocksKnown = NO;+ // Mid-measure here: the list lays out again once this pass is over, so the swap shows now and not on a scroll.+ __weak UICollectionView *weakList = list;+ dispatch_async(dispatch_get_main_queue(), ^{ [weakList.collectionViewLayout invalidateLayout]; });+}+// The top shelf is no longer one (hidden, not the one Cards from names, or gone with the feed): the page's color goes,// and every shelf on screen is measured again, so the next one takes the place now rather than on the next scroll.static void topLost(UICollectionView *list) {sg_topIndex = NSIntegerMax;+ sg_topFeed = nil;sg_blocksKnown = NO;sg_largeShelf = nil;SGRHomeSetPageTint(nil);@@ -306,6 +377,12 @@ static void dropLarge(UICollectionView *shelf) {if (![NSStringFromClass(object_getClass(content)) containsString:@"Home_EvoPageImpl13HomeStructure"]) return %orig;UIView *root = content.subviews.firstObject;NSInteger index = attributes.indexPath.section * 10000 + attributes.indexPath.item;+ UIView *list = ((UIView *)self).superview;+ while (list && ![list isKindOfClass:UICollectionView.class]) list = list.superview;+ if (list) objc_setAssociatedObject(((UICollectionView *)list).collectionViewLayout, &kListKey, @YES, OBJC_ASSOCIATION_RETAIN_NONATOMIC);+ // The heading on screen, read before the measuring sizes the cell; nil while the shelf has none to read, when+ // the model's heading for the section stands in.+ NSString *onScreen = nil;// Spotify reuses a shelf's view for another section as the feed scrolls (and for another shelf at the same index// when the feed comes again), and its layout, which carries the mark and the sizes read from it, comes along. A// view serving a new shelf drops both before Spotify lays it out for this measuring: the other shelf's cards are@@ -314,7 +391,10 @@ static void dropLarge(UICollectionView *shelf) {UICollectionViewLayout *layout = ((UICollectionView *)root).collectionViewLayout;BOOL marked = objc_getAssociatedObject(layout, &kLargeKey) != nil;if (marked && index != sg_topIndex) dropLarge((UICollectionView *)root);- NSString *served = SGRHomeShelfIdentity(root) ?: [@(index) stringValue];+ onScreen = SGRHomeShelfIdentity(root);+ if (list && onScreen) feedRead((UICollectionView *)list, attributes.indexPath.section, onScreen);+ // The same name before and after the heading comes in, so the sizes and the mark stay with the shelf.+ NSString *served = onScreen ?: (list ? feedIdentity((UICollectionView *)list, attributes.indexPath.section) : nil) ?: [@(index) stringValue];if (![served isEqualToString:objc_getAssociatedObject(layout, &kServedKey)]) {objc_setAssociatedObject(layout, &kServedKey, served, OBJC_ASSOCIATION_RETAIN_NONATOMIC);char *keys[] = {&kNaturalKey, &kGrowKey, &kOffsetKey, &kAskedKey};@@ -323,9 +403,7 @@ static void dropLarge(UICollectionView *shelf) {}}UICollectionViewLayoutAttributes *result = %orig;- UIView *list = ((UIView *)self).superview;- while (list && ![list isKindOfClass:UICollectionView.class]) list = list.superview;- if (list) objc_setAssociatedObject(((UICollectionView *)list).collectionViewLayout, &kListKey, @YES, OBJC_ASSOCIATION_RETAIN_NONATOMIC);+ if (list) topFromFeed((UICollectionView *)list);if (!shelf || ![root isKindOfClass:shelf] || result.size.height <= kHeading) {// The top's place holds something else now (the feed came again): the next shelf measured takes it.if (index == sg_topIndex) topLost((UICollectionView *)list);@@ -340,8 +418,17 @@ static void dropLarge(UICollectionView *shelf) {}UICollectionViewLayout *layout = ((UICollectionView *)root).collectionViewLayout;// The shelf drawn large: the one Cards from names, else the one nearest the top; never one switched off.- NSString *identity = SGRHomeShelfIdentity(root), *from = SGRHomeTopPicksFrom();+ NSString *identity = onScreen ?: feedIdentity((UICollectionView *)list, attributes.indexPath.section), *from = SGRHomeTopPicksFrom();BOOL candidate = !SGRHomeShelfHidden(identity) && (!from || [identity isEqualToString:from]);+ // The heading bears the model's top out, or reads otherwise: then that model's answer is not taken again.+ if (onScreen && index == sg_topIndex && sg_topFeed) {+ if (!candidate) {+ sg_overruled = sg_topFeed;+ sg_overruledFrom = from;+ SGLog(@"redesign home: top picks from the model read \"%@\" on screen, not \"%@\": going by headings until the feed changes", onScreen, from);+ }+ sg_topFeed = nil;+ }if (candidate && index < sg_topIndex) sg_topIndex = index;else if (!candidate && index == sg_topIndex) topLost((UICollectionView *)list);BOOL large = index == sg_topIndex, was = objc_getAssociatedObject(layout, &kLargeKey) != nil;