Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.
C
fix: add to library turns into a checkmark, and Follow no longer flips from a glyph to a word (#86, #88)
Vojtěch Škopeke524034ed886
diff --git a/harness/album/main.m b/harness/album/main.mindex 797d6f76..72d8e4b5 100644--- a/harness/album/main.m+++ b/harness/album/main.m@@ -106,6 +106,10 @@ static UIView *actionButton(UIView *row, CGRect frame, NSString *identifier, NSSmockDownloadButton(element);return element;}+ if ([identifier isEqualToString:@"Components.UI.AddToButton"]) {+ mockAddToButton(element);+ return element;+ }UIView *button = box(element, MockEncoreButton.class, element.bounds, identifier);button.accessibilityLabel = a11y;UIImageView *glyph = [[UIImageView alloc] initWithFrame:CGRectInset(button.bounds, 12, 12)];@@ -143,6 +147,33 @@ static UIView *actionButton(UIView *row, CGRect frame, NSString *identifier, NSS@end// What the redesign's row shows on Play's right: the label of the Kit's last round button, the trailing one.+static UIControl *trailingButton(UIView *root) {+ NSMutableArray<UIView *> *stack = [NSMutableArray arrayWithObject:root];+ while (stack.count) {+ UIView *v = stack.lastObject;+ [stack removeLastObject];+ if ([NSStringFromClass(v.class) isEqualToString:@"SGRHeaderInfo"]) {+ UIControl *trailing = nil;+ for (UIView *sub in v.subviews) {+ if ([NSStringFromClass(sub.class) isEqualToString:@"SGRMirrorButton"]) trailing = (UIControl *)sub;+ }+ return trailing;+ }+ [stack addObjectsFromArray:v.subviews];+ }+ return nil;+}++// The trailing button's label and the symbol it draws.+static NSString *trailingState(UIView *root) {+ UIControl *trailing = trailingButton(root);+ UIImageView *glyph = nil;+ for (UIView *sub in trailing.subviews) {+ if ([sub isKindOfClass:UIImageView.class] && !sub.hidden) glyph = (UIImageView *)sub;+ }+ return [NSString stringWithFormat:@"\"%@\" %@ tint %@", trailing.accessibilityLabel, glyph.image, glyph.tintColor];+}+static NSString *trailingLabel(UIView *root) {NSMutableArray<UIView *> *stack = [NSMutableArray arrayWithObject:root];while (stack.count) {@@ -402,6 +433,16 @@ static NSString *trailingLabel(UIView *root) {[self.window makeKeyAndVisible];if (downloads) downloadScript();+ // `addto`: saved from elsewhere at 3 s (the ⋯ sheet, nothing laid out), then Play's right tapped at 7 s.+ if ([NSProcessInfo.processInfo.arguments containsObject:@"addto"]) {+ UIView *rootView = root.view;+ for (NSNumber *when in @[@2, @6.5, @8.5]) {+ at(when.doubleValue, ^{ NSLog(@"[harness] add-to at %@s: %@", when, trailingState(rootView)); });+ }+ at(3, ^{ setAddTo(1); NSLog(@"[harness] add-to: saved from elsewhere"); });+ at(7, ^{ [trailingButton(rootView) sendActionsForControlEvents:UIControlEventTouchUpInside]; });+ }+// In `late`, add arrives at 2.5 s, after every pass of the header's and the metadata's re-reads: an arranged// subview of the row, which lays out the row and nothing above it.if (late) {diff --git a/harness/artist/main.m b/harness/artist/main.mindex 8b74cc32..83917afe 100644--- a/harness/artist/main.m+++ b/harness/artist/main.m@@ -4,6 +4,8 @@// the Follow button's word and then the photo back, one after the other, the way a page opened for the first// time gets them -- each has to reach the redesign on its own (issue #52), and the artwork view carries// Encore's Swift class name so that it can only be watched the way the phone makes the Kit watch it.+// `relabel` builds Follow as Encore's Swift button with an empty content view, and adds the label with the+// word in it a second later rather than filling one that was there.#import <UIKit/UIKit.h>#pragma mark - Spotify's classes, by name@@ -48,6 +50,10 @@@interface MockButton : UIControl @end@implementation MockButton @end+// Encore's text button (trees/lyrics.txt:615): a Swift class, so only its plain content view can be watched.+@interface _TtCCE16Encore_ButtonKitO16EncoreFoundation6Encore6Button9Secondary : UIControl @end+@implementation _TtCCE16Encore_ButtonKitO16EncoreFoundation6Encore6Button9Secondary @end+// Encore's image view, the one every picture Spotify loads arrives in (01.txt:48). Its name is the point of// the mock: a Swift one, which the Kit refuses to give an instance its own subclass of, so the harness// watches the photo the way the phone has to -- through an override on the class.@@ -187,6 +193,7 @@ static NSArray<NSArray *> *musicList(void) {CGFloat W = self.window.bounds.size.width, H = self.window.bounds.size.height;BOOL collapsed = [NSProcessInfo.processInfo.arguments containsObject:@"collapsed"];BOOL late = [NSProcessInfo.processInfo.arguments containsObject:@"late"];+ BOOL relabel = [NSProcessInfo.processInfo.arguments containsObject:@"relabel"];UIViewController *root = [UIViewController new];root.view.backgroundColor = [UIColor colorWithRed:0.07 green:0.07 blue:0.07 alpha:1];@@ -222,13 +229,15 @@ static NSArray<NSArray *> *musicList(void) {@"Components.Header.UI.Metadata");UIView *row = box(header, UIStackView.class, CGRectMake(4, 456, 394, 48), nil);- UIView *follow = box(row, MockButton.class, CGRectMake(70, 8, 66, 32), @"Curation.FollowButtonElementKit.FollowButton");+ Class followClass = relabel ? _TtCCE16Encore_ButtonKitO16EncoreFoundation6Encore6Button9Secondary.class : MockButton.class;+ UIView *follow = box(row, followClass, CGRectMake(70, 8, 66, 32), @"Curation.FollowButtonElementKit.FollowButton");follow.accessibilityLabel = @"Follow";follow.layer.borderColor = UIColor.grayColor.CGColor;follow.layer.borderWidth = 1;follow.layer.cornerRadius = 16;- UILabel *followWord = label(follow, CGRectMake(16, 8, 40, 16), late ? @"" : @"Follow", 11, UIColor.whiteColor, @"Encore.Label");- if (late) follow.accessibilityLabel = nil;+ UIView *followContent = relabel ? box(follow, UIView.class, follow.bounds, nil) : nil;+ UILabel *followWord = relabel ? nil : label(follow, CGRectMake(16, 8, 40, 16), late ? @"" : @"Follow", 11, UIColor.whiteColor, @"Encore.Label");+ if (late || relabel) follow.accessibilityLabel = nil;[(UIControl *)follow addTarget:self action:@selector(toggleFollow:) forControlEvents:UIControlEventTouchUpInside];glyph(box(row, MockButton.class, CGRectMake(148, 0, 48, 48), @"Components.UI.ContextMenuButton-7n2wHs1TKAczGzO7Dd2rGr"), @"ellipsis", 12);glyph(box(row, MockButton.class, CGRectMake(278, 0, 48, 48), @"Components.UI.ShuffleButton"), @"shuffle", 12);@@ -281,6 +290,42 @@ static NSArray<NSArray *> *musicList(void) {[self.window makeKeyAndVisible];+ if (relabel) {+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{+ label(followContent, CGRectMake(16, 8, 40, 16), @"Follow", 11, UIColor.whiteColor, @"Encore.Label");+ follow.accessibilityLabel = @"Follow";+ NSLog(@"[harness] relabel: a new label with \"Follow\" is in Spotify's button now");+ });+ }+ // What the redesign's Follow draws: nothing before the word, the word after it.+ // A tap on it at 2.5 s, which Spotify answers with Following.+ for (NSNumber *when in @[@0.5, @1.5, @2.5, @3.0]) {+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(when.doubleValue * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{+ NSMutableArray *lines = [NSMutableArray array];+ UIControl *tapped = nil;+ NSMutableArray *queue = [NSMutableArray arrayWithObject:container];+ while (queue.count) {+ UIView *v = queue.firstObject;+ [queue removeObjectAtIndex:0];+ [queue addObjectsFromArray:v.subviews];+ if (![NSStringFromClass(v.class) isEqualToString:@"SGRMirrorButton"] || v.frame.origin.x < 200) continue;+ tapped = (UIControl *)v;+ NSMutableArray *drawn = [NSMutableArray array];+ for (UIView *sub in v.subviews) {+ if (sub.hidden || sub.alpha < 0.01) continue;+ NSString *what = NSStringFromClass(sub.class);+ if ([sub isKindOfClass:UILabel.class]) what = [NSString stringWithFormat:@"\"%@\"", ((UILabel *)sub).text];+ else if ([sub isKindOfClass:UIImageView.class]) what = ((UIImageView *)sub).image ? @"glyph" : @"empty image view";+ [drawn addObject:what];+ }+ [lines addObject:[NSString stringWithFormat:@"%@ touches %d draws %@", NSStringFromCGRect(v.frame),+ v.userInteractionEnabled, [drawn componentsJoinedByString:@", "]]];+ }+ NSLog(@"[harness] follow at %.1fs: %@", when.doubleValue, [lines componentsJoinedByString:@" | "]);+ if (when.doubleValue == 2.5) [tapped sendActionsForControlEvents:UIControlEventTouchUpInside];+ });+ }+// The photo and the follow state land after the header has laid out, and neither lays it out again.if (late) {dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{@@ -326,8 +371,12 @@ static NSArray<NSArray *> *musicList(void) {// Spotify's Follow turns into Following, which the redesign's word has to follow.- (void)toggleFollow:(UIControl *)follow {UILabel *word = nil;- for (UIView *v in follow.subviews) {- if (v.subviews.firstObject && [v.subviews.firstObject isKindOfClass:UILabel.class]) word = (UILabel *)v.subviews.firstObject;+ NSMutableArray *queue = [NSMutableArray arrayWithObject:follow];+ while (queue.count && !word) {+ UIView *v = queue.firstObject;+ [queue removeObjectAtIndex:0];+ if ([v isKindOfClass:UILabel.class]) word = (UILabel *)v;+ [queue addObjectsFromArray:v.subviews];}word.text = [word.text isEqualToString:@"Follow"] ? @"Following" : @"Follow";NSLog(@"[harness] Spotify's Follow fired, now \"%@\"", word.text);diff --git a/harness/download-mock.h b/harness/download-mock.hindex 33b0ee5d..25b0f936 100644--- a/harness/download-mock.h+++ b/harness/download-mock.h@@ -105,6 +105,58 @@ static void setShuffle(BOOL on) {sgh_shuffleGlyph.image = shuffleImage(on);}+// The add-to button (Components.UI.AddToButton, 9.1.78 binary): a UIButton drawn by Lottie whose action target+// is Encore's private AddToButton, keeping `currentStatus` (one byte: notAdded, added). Spotify's own+// -performAction saves or removes; the mock's flips the byte, as the saved state would come back.+@interface _TtC28EncoreConsumerMobile_BaseKitP33_23B9F07423DE1078C0EAAD54E8754BE611AddToButton : NSObject {+@public+ __unsafe_unretained UIView *microInteractionView;+ __unsafe_unretained UIView *uiView;+ __unsafe_unretained UIButton *uiButton;+ uint8_t configuration;+ uint8_t currentStatus;+}+- (void)performAction;+@end++static void setAddTo(uint8_t status);++@implementation _TtC28EncoreConsumerMobile_BaseKitP33_23B9F07423DE1078C0EAAD54E8754BE611AddToButton+- (void)performAction {+ setAddTo(!currentStatus);+ NSLog(@"[harness] Spotify's add-to button fired, now %d", currentStatus);+}+@end++typedef _TtC28EncoreConsumerMobile_BaseKitP33_23B9F07423DE1078C0EAAD54E8754BE611AddToButton MockAddToOwner;++static MockAddToOwner *sgh_addToOwner;++static void setAddTo(uint8_t status) {+ sgh_addToOwner->currentStatus = status;+ sgh_addToOwner->uiButton.accessibilityLabel = status ? @"Remove from Your Library" : @"Save to Your Library";+}++// The add-to button as Spotify builds it, inside `host` (48x48), not saved.+static UIButton *mockAddToButton(UIView *host) {+ UIButton *button = [[UIButton alloc] initWithFrame:host.bounds];+ button.accessibilityIdentifier = @"Components.UI.AddToButton";+ [host addSubview:button];+ UIView *micro = [[UIView alloc] initWithFrame:CGRectInset(button.bounds, -4, -4)];+ [micro addSubview:[[MockLottieView alloc] initWithFrame:micro.bounds]];+ [button addSubview:micro];+ MockAddToOwner *owner = [MockAddToOwner new];+ owner->uiButton = button;+ owner->uiView = button;+ owner->microInteractionView = micro;+ [button addTarget:owner action:@selector(performAction) forControlEvents:UIControlEventTouchUpInside];+ static char kOwnerKey;+ objc_setAssociatedObject(button, &kOwnerKey, owner, OBJC_ASSOCIATION_RETAIN_NONATOMIC);+ sgh_addToOwner = owner;+ setAddTo(0);+ return button;+}+static void at(NSTimeInterval seconds, void (^block)(void)) {dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(seconds * NSEC_PER_SEC)), dispatch_get_main_queue(), block);}diff --git a/tweak/Sources/Redesigned/Artist/ArtistHeader.x b/tweak/Sources/Redesigned/Artist/ArtistHeader.xindex 86a401ca..12b266f5 100644--- a/tweak/Sources/Redesigned/Artist/ArtistHeader.x+++ b/tweak/Sources/Redesigned/Artist/ArtistHeader.x@@ -271,8 +271,7 @@ static void applyHeader(UIView *header) {UIView *play = SGRFindByIdentifier(header, @"header-play-button", &kPlayKey);UIView *follow = SGRFindByIdentifier(header, @"Curation.FollowButtonElementKit.FollowButton", &kFollowKey);NSString *word = firstText(follow);- [info showShuffle:shuffle play:play trailing:follow trailingFallback:[UIImage systemImageNamed:@"person.badge.plus"]- playColor:SGRArtistFieldColor(container)];+ [info showShuffle:shuffle play:play trailing:follow trailingFallback:nil playColor:SGRArtistFieldColor(container)];// More, in the top trailing corner of the page itself rather than of the container, which scrolls away// with the photo: pinned there it is the same button in the same place on the album and the playlist,diff --git a/tweak/Sources/Redesigned/Kit/SGRActionRow.h b/tweak/Sources/Redesigned/Kit/SGRActionRow.hindex ca068583..39a5c12c 100644--- a/tweak/Sources/Redesigned/Kit/SGRActionRow.h+++ b/tweak/Sources/Redesigned/Kit/SGRActionRow.h@@ -46,8 +46,8 @@@property (nonatomic, copy) UIColor *onGlyphColor;// The word on Spotify's button instead of a glyph, in a glass capsule as wide as the word asks for: for a// text button such as the artist's Follow, whose word is its state ("Follow", "Following") in the app's-// language. Until Spotify's button has a word, the fallback glyph is drawn in the round shape instead. Set-// before the first -feedFrom:.+// language. Until Spotify's button has a word the button draws nothing and takes no touches. Set before the+// first -feedFrom:.@property (nonatomic) BOOL showsWord;// The width the button wants: SGRActionHeight for a glyph, the word and its padding for a word.- (CGFloat)sgr_width;@@ -57,7 +57,7 @@// Spotify's download button (SGRDownload.h) is drawn by Lottie, with nothing to copy: for it the button// draws the state Spotify reports instead -- the arrow, a ring filling up, the downloaded glyph -- and reads// it again while it is on screen, twice a second while a download runs, since nothing Spotify does then-// lays out anything the page hears.+// lays out anything the page hears. So is the add-to button, drawn as a plus or, saved, a checkmark.- (void)feedFrom:(UIView *)source;@enddiff --git a/tweak/Sources/Redesigned/Kit/SGRActionRow.m b/tweak/Sources/Redesigned/Kit/SGRActionRow.mindex 4a90ac16..fadbea61 100644--- a/tweak/Sources/Redesigned/Kit/SGRActionRow.m+++ b/tweak/Sources/Redesigned/Kit/SGRActionRow.m@@ -201,6 +201,7 @@ static NSString *labelTextIn(UIView *button) {UIImageView *_glyph;UILabel *_word;__weak UILabel *_watchedWord;+ __weak UIView *_watchedContent;__weak UIImageView *_watchedGlyph;// Spotify's own image, as it was taken: what the copy is compared against, since a copy re-rendered as// a template is no longer the same object.@@ -208,6 +209,8 @@ static NSString *labelTextIn(UIView *button) {// Standing in for Spotify's download button: its state drawn, and read again while on screen.SGRDownloadGlyph *_download;NSTimer *_downloadTimer;+ // Standing in for Spotify's add-to button: 1 saved, 0 not, -1 not one.+ NSInteger _addedShown;}- (instancetype)initWithFrame:(CGRect)frame {@@ -216,6 +219,7 @@ static NSString *labelTextIn(UIView *button) {_glyph.contentMode = UIViewContentModeScaleAspectFit;_glyph.userInteractionEnabled = NO;[self addSubview:_glyph];+ _addedShown = -1;self.isAccessibilityElement = YES;self.accessibilityTraits = UIAccessibilityTraitButton;@@ -234,9 +238,14 @@ static NSString *labelTextIn(UIView *button) {- (void)layoutSubviews {[super layoutSubviews];CGRect bounds = self.bounds;- // A word button waiting for its word draws the circle, and takes the capsule once the word is there:- // only one of the two shapes is asked for on a pass, so the other is put away rather than left under it.- if (self.showsWord && _word.text.length) {+ // A word button draws nothing until its word is there, then the capsule: a glyph in its place first+ // read as a different button. Only one shape is asked for on a pass, so the other is put away.+ if (self.showsWord && !_word.text.length) {+ ((UIView *)objc_getAssociatedObject(self, &kWordGlassKey)).hidden = YES;+ ((UIView *)objc_getAssociatedObject(self, &kMirrorGlassKey)).hidden = YES;+ return;+ }+ if (self.showsWord) {SGRGlassCapsuleInside(self, &kWordGlassKey, bounds.size, NO);((UIView *)objc_getAssociatedObject(self, &kMirrorGlassKey)).hidden = YES;[_word sizeToFit];@@ -257,8 +266,8 @@ static NSString *labelTextIn(UIView *button) {- (void)didMoveToWindow {[super didMoveToWindow];- if (!_download) return;- // Read again on the way in -- the download may have moved on while the page was away -- and not at all+ if (!_download && _addedShown < 0) return;+ // Read again on the way in -- the state may have moved on while the page was away -- and not at all// while out of the window.if (self.window && self.source) [self feedFrom:self.source];else [self sgr_followDownload:0];@@ -302,7 +311,35 @@ static NSString *labelTextIn(UIView *button) {return YES;}-// Reads the download again every `interval` seconds; 0 stops.+// Shows whether Spotify's add-to button has its album or playlist saved; YES when `source` is one.+- (BOOL)sgr_feedAddToFrom:(UIView *)source {+ BOOL added;+ if (!SGRReadAddTo(source, &added)) {+ _addedShown = -1;+ return NO;+ }+ if (_addedShown != added) {+ UIImage *image = [UIImage systemImageNamed:added ? @"checkmark" : @"plus"];+ BOOL animated = _addedShown >= 0 && self.window && !SGRReduceMotion();+ _addedShown = added;+ _takenGlyph = nil;+ if (@available(iOS 17.0, *)) {+ if (animated) [_glyph setSymbolImage:image withContentTransition:[NSSymbolReplaceContentTransition replaceDownUpTransition]];+ else _glyph.image = image;+ } else {+ _glyph.image = image;+ }+ // Saved is "on", in the accent colour, as downloaded is.+ _glyph.tintColor = added ? SGRAccent() : SGRPrimary();+ }+ NSString *word = source.accessibilityLabel ?: wordIn(source);+ if (word && ![self.accessibilityLabel isEqualToString:word]) self.accessibilityLabel = word;+ // Nothing is laid out when it is saved from elsewhere (the ⋯ sheet), so it is read again as download is.+ [self sgr_followDownload:self.window ? 2 : 0];+ return YES;+}++// Reads the state again every `interval` seconds; 0 stops.- (void)sgr_followDownload:(NSTimeInterval)interval {if (interval <= 0) {[_downloadTimer invalidate];@@ -348,31 +385,36 @@ static NSString *labelTextIn(UIView *button) {// Spotify's word is its state, which it fills in a moment after the button itself is there and// changes without laying out anything the page hears (the artist's Follow, issue #52). So the label// it puts the word in is watched, and the button hears it land.+ __weak SGRMirrorButton *weakSelf = self;+ __weak UIView *weakSource = source;UILabel *source_word = labelIn(source);if (source_word && source_word != _watchedWord) {_watchedWord = source_word;- __weak SGRMirrorButton *weakSelf = self;- __weak UIView *weakSource = source;SGRObserveText(source_word, ^(UILabel *label) {if (weakSelf && weakSource) [weakSelf feedFrom:weakSource];});}- // Until the word is there the button is the glyph it falls back to, which says what it does, rather- // than an empty capsule.+ // A label added or swapped in later has no watch on it; the view it lands in lays out then. Encore's+ // button is a Swift class, so its plain content view is watched when the button itself cannot be.+ if (source != _watchedContent && source.subviews.firstObject != _watchedContent) {+ void (^laidOut)(UIView *) = ^(UIView *view) {+ if (weakSelf && weakSource) [weakSelf feedFrom:weakSource];+ };+ if (SGRObserveLayout(source, laidOut)) _watchedContent = source;+ else if (SGRObserveLayout(source.subviews.firstObject, laidOut)) _watchedContent = source.subviews.firstObject;+ }BOOL hasWord = _word.text.length > 0;- if (_glyph.hidden != hasWord) {- _glyph.hidden = hasWord;+ if (!_glyph.hidden) _glyph.hidden = YES;+ if (self.userInteractionEnabled != hasWord) {+ self.userInteractionEnabled = hasWord;+ self.isAccessibilityElement = hasWord;[self setNeedsLayout];}- if (!hasWord && self.fallbackGlyph && _glyph.image != self.fallbackGlyph) {- _glyph.image = self.fallbackGlyph;- _glyph.tintColor = SGRPrimary();- }- if (!hasWord && !self.accessibilityLabel) self.accessibilityLabel = source.accessibilityLabel;return;}if ([self sgr_feedDownloadFrom:source]) return;+ if ([self sgr_feedAddToFrom:source]) return;UIImageView *glyph = glyphIn(source, 0);NSString *word = source.accessibilityLabel ?: wordIn(source);diff --git a/tweak/Sources/Redesigned/Kit/SGRDownload.h b/tweak/Sources/Redesigned/Kit/SGRDownload.hindex e621a257..e073f7b2 100644--- a/tweak/Sources/Redesigned/Kit/SGRDownload.h+++ b/tweak/Sources/Redesigned/Kit/SGRDownload.h@@ -25,6 +25,11 @@ typedef NS_ENUM(NSInteger, SGRDownloadState) {// is 0...1 while downloading, -1 when Spotify has none to give (an endless download, or not readable).BOOL SGRReadDownload(UIView *source, SGRDownloadState *state, CGFloat *progress);+// YES when `source`, or something under it, is Spotify's add-to button (Components.UI.AddToButton, saving an+// album or a playlist), with whether it is saved. Lottie draws that one too, so the state is read off its+// Encore owner's `currentStatus` (AddToButtonState: notAdded, added); NO when that cannot be read.+BOOL SGRReadAddTo(UIView *source, BOOL *added);+// The glyph, 24pt square, drawn in the middle of a round action row button. A change of state is animated// (symbol replaced in place, or the old shape shrinking away as the new one grows in), so the moment the// download starts or ends reads as a moment; progress eases along between the reads.diff --git a/tweak/Sources/Redesigned/Kit/SGRDownload.m b/tweak/Sources/Redesigned/Kit/SGRDownload.mindex 2991901f..1067816a 100644--- a/tweak/Sources/Redesigned/Kit/SGRDownload.m+++ b/tweak/Sources/Redesigned/Kit/SGRDownload.m@@ -1,4 +1,4 @@-// Reading Spotify's download button, and the glyph drawn for it.+// Reading Spotify's download and add-to buttons, and the glyph drawn for the download.#import <objc/runtime.h>#import "Core/SGCore.h"#import "SGRDownload.h"@@ -8,19 +8,19 @@static NSString *const kIdentifierPrefix = @"DownloadButton.Granular.";-static UIView *downloadButtonIn(UIView *source) {+static UIView *identifiedIn(UIView *source, NSString *prefix) {__block UIView *found = nil;SGForEachView(source, ^(UIView *v) {- if (!found && [v.accessibilityIdentifier hasPrefix:kIdentifierPrefix]) found = v;+ if (!found && [v.accessibilityIdentifier hasPrefix:prefix]) found = v;});return found;}// The Encore object behind the button: the target of the button's own action (-performAction).-static id ownerOf(UIView *button) {+static id ownerOf(UIView *button, NSString *kind) {if (![button isKindOfClass:UIControl.class]) return nil;for (id target in ((UIControl *)button).allTargets) {- if ([NSStringFromClass(object_getClass(target)) containsString:@"GranularDownloadButton"]) return target;+ if ([NSStringFromClass(object_getClass(target)) containsString:kind]) return target;}return nil;}@@ -49,7 +49,7 @@ static BOOL stateFromIdentifier(NSString *identifier, SGRDownloadState *state) {}BOOL SGRReadDownload(UIView *source, SGRDownloadState *state, CGFloat *progress) {- UIView *button = source ? downloadButtonIn(source) : nil;+ UIView *button = source ? identifiedIn(source, kIdentifierPrefix) : nil;if (!button) return NO;SGRDownloadState said = SGRDownloadNone;BOOL named = stateFromIdentifier(button.accessibilityIdentifier, &said);@@ -58,7 +58,7 @@ BOOL SGRReadDownload(UIView *source, SGRDownloadState *state, CGFloat *progress)// in the order they are declared; its progress an optional 8 byte number, the value then a byte that is// 1 when there is none. The identifier says the same state in words, and is what counts when the model// cannot be read.- id owner = ownerOf(button);+ id owner = ownerOf(button, @"GranularDownloadButton");const uint8_t *current = propertyBytes(owner, "currentState", 1);const uint8_t *stored = propertyBytes(owner, "progress", 9);BOOL modelled = current && *current <= 5;@@ -105,6 +105,22 @@ BOOL SGRReadDownload(UIView *source, SGRDownloadState *state, CGFloat *progress)return YES;}+BOOL SGRReadAddTo(UIView *source, BOOL *added) {+ UIView *button = source ? identifiedIn(source, @"Components.UI.AddToButton") : nil;+ if (!button) return NO;+ // A Swift enum without payloads, one byte: notAdded, added.+ const uint8_t *status = propertyBytes(ownerOf(button, @"AddToButton"), "currentStatus", 1);+ int raw = status ? *status : -1;+ static int logged = -2;+ if (raw != logged) {+ logged = raw;+ SGLog(@"redesign kit: add-to state %d, label \"%@\"", raw, button.accessibilityLabel);+ }+ if (!status || *status > 1) return NO;+ if (added) *added = *status == 1;+ return YES;+}+#pragma mark - the glyph// The ring inside the 24pt canvas, the width of the circle of a filled SF symbol at kSymbolSize.