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: the Live Activity ends when Spotify is swiped away, and a launch ends one left over

My-Name-Is-Jeff8157d51383fd
diff --git a/docs/tweaks.md b/docs/tweaks.md
index 2bb0da48..c2565e94 100644
--- a/docs/tweaks.md
+++ b/docs/tweaks.md
@@ -207,7 +207,9 @@ Shared:
cover, and the cover is read only once the now playing title is the new track's. Taps are
LiveActivityIntents run inside Spotify and take a second or two to show on the card. The widget is extension/LiveActivity; ActivityKit pairs the two
by the attributes' type in LiveActivityShared.swift, compiled into both. It starts only with Spotify
- in front; its settings apply at once
+ in front; its settings apply at once. It ends when Spotify is swiped away while running; killed
+ while suspended, Spotify cannot end it, so each state goes stale a minute on (an unchanged card is
+ sent again every 20 s) and a launch ends any left over before it starts a new one
ListeningStats/ listening stats kept on the phone (ListeningStats.h lists its files): each music track timed while
it plays through PlayerState's observer and written to a text log in Application Support once it ran 30 s
diff --git a/tweak/Sources/Shared/LiveActivity/LiveActivity.x b/tweak/Sources/Shared/LiveActivity/LiveActivity.x
index b8ec13f4..a11f2692 100644
--- a/tweak/Sources/Shared/LiveActivity/LiveActivity.x
+++ b/tweak/Sources/Shared/LiveActivity/LiveActivity.x
@@ -22,6 +22,7 @@ API_AVAILABLE(ios(17.0))
tint:(NSString *)tint trackStart:(NSDate *)trackStart trackEnd:(NSDate *)trackEnd pausedAt:(NSNumber *)pausedAt
translation:(NSString *)translation cover:(NSData *)cover textSize:(NSInteger)textSize;
+ (void)end;
++ (void)endBeforeExit;
@end
// The control menu's tabs, SGLyricsAttributes.Tab's values.
@@ -40,6 +41,9 @@ static const NSTimeInterval kPausedTick = 1;
static const NSInteger kBreakMs = 4000;
// Seconds between attempts to start one, so a refused request (activities turned off) is not retried every tick.
static const NSTimeInterval kStartRetry = 10;
+// A card that has not changed for this long is sent again, so its stale date (the bridge's staleAfter,
+// three times this) keeps moving on while Spotify runs, and runs out only once Spotify is gone.
+static const NSTimeInterval kRefresh = 20;
// Tracks up next: the queue view shows four on the lock screen (three in the Dynamic Island), the
// control menu's queue tab three.
static const NSUInteger kUpNextQueue = 4;
@@ -63,6 +67,7 @@ static NSTimeInterval sg_tickEvery;
static NSString *sg_shown;
static NSString *sg_missingLyrics;
static NSDate *sg_lastStart;
+static NSDate *sg_lastSent;
static SGLiveActivityTab sg_tab;
// The sleep timer: an end, or the end of the track it was set on.
static NSDate *sg_sleepEnd;
@@ -78,17 +83,18 @@ static void startTimer(NSTimeInterval every) API_AVAILABLE(ios(17.0)) {
[NSRunLoop.mainRunLoop addTimer:sg_timer forMode:NSRunLoopCommonModes];
}
-// Sends `shown`, what the activity is to show as one string, unless it is showing that already; starts
-// the activity when there is none and the app is in front.
+// Sends `shown`, what the activity is to show as one string, unless it is showing that already and was
+// sent within kRefresh; starts the activity when there is none and the app is in front.
static void send(NSString *shown, void (^show)(void)) API_AVAILABLE(ios(17.0)) {
if (SGLiveActivityBridge.isShowing) {
- if ([shown isEqualToString:sg_shown]) return;
+ if ([shown isEqualToString:sg_shown] && -sg_lastSent.timeIntervalSinceNow < kRefresh) return;
} else {
if (UIApplication.sharedApplication.applicationState != UIApplicationStateActive) return;
if (sg_lastStart && -sg_lastStart.timeIntervalSinceNow < kStartRetry) return;
sg_lastStart = [NSDate date];
}
sg_shown = shown;
+ sg_lastSent = [NSDate date];
show();
}
@@ -392,6 +398,14 @@ void SGSetLiveActivityEnabled(BOOL on) {
runAction(note.object);
if (sg_timer) tick();
}];
+ // Swiped away while it plays, Spotify is told it is ending, and the card goes with it rather than
+ // staying up frozen over other apps' activities. No queue, so the block runs before the notification
+ // returns, and with it the process. Killed while suspended, Spotify is told nothing: the stale date
+ // and the %ctor's cleanup cover that.
+ [center addObserverForName:UIApplicationWillTerminateNotification object:nil queue:nil usingBlock:^(NSNotification *note) {
+ SGLog(@"live activity: app ending");
+ [SGLiveActivityBridge endBeforeExit];
+ }];
});
startTimer(kTick);
SGLog(@"live activity: on");
@@ -404,10 +418,12 @@ void SGSetLiveActivityEnabled(BOOL on) {
SGMigrateKey(SGKeyLiveActivityWas, SGKeyLiveActivity);
SGMigrateKey(SGKeyLiveActivityViewWas, SGKeyLiveActivityView);
BOOL on = SGFlag(SGKeyLiveActivity, NO);
- // Off, one left from a launch before the switch went off is ended.
+ // One left from the launch before is ended, on or off: Spotify may have been killed under it, so it
+ // shows a moment long gone. On, a fresh one starts once it is: until then the first ticks update
+ // the leftover, which is harmless, an update after the end being ignored.
dispatch_async(dispatch_get_main_queue(), ^{
+ [SGLiveActivityBridge end];
if (on) SGSetLiveActivityEnabled(YES);
- else [SGLiveActivityBridge end];
});
}
}
diff --git a/tweak/Sources/Shared/LiveActivity/LiveActivityBridge.swift b/tweak/Sources/Shared/LiveActivity/LiveActivityBridge.swift
index 55a19ffe..ec075daf 100644
--- a/tweak/Sources/Shared/LiveActivity/LiveActivityBridge.swift
+++ b/tweak/Sources/Shared/LiveActivity/LiveActivityBridge.swift
@@ -9,6 +9,9 @@ public final class SGLiveActivityBridge: NSObject {
private static let log = Logger(subsystem: "spotifyglass", category: "live activity")
// Under ActivityKit's 4096, with room for its own wrapping.
private static let maxStateBytes = 3800
+ // Each state is stale this long after it is sent: three times LiveActivity.x's kRefresh, which sends
+ // even an unchanged card again, so only a card Spotify can no longer feed goes stale.
+ private static let staleAfter: TimeInterval = 60
private typealias State = SGLyricsAttributes.ContentState
@@ -98,7 +101,7 @@ public final class SGLiveActivityBridge: NSObject {
state.cover = nil
log.notice("[spotifyglass] live activity: \(size, privacy: .public) bytes, sent without the cover")
}
- let content = ActivityContent(state: state, staleDate: nil)
+ let content = ActivityContent(state: state, staleDate: Date(timeIntervalSinceNow: staleAfter))
if let id = showing.first {
queue(.update(id, content))
return
@@ -124,4 +127,24 @@ public final class SGLiveActivityBridge: NSObject {
@objc public static func end() {
queue(.end(Activity<SGLyricsAttributes>.activities.map(\.id) + Activity<SGLyricsWatchAttributes>.activities.map(\.id)))
}
+
+ // At termination: ends them all, past the queue, and holds the calling thread until they are ended,
+ // two seconds at most, since the process is gone once the notification returns and an end still in
+ // flight goes with it. The cap also keeps an ActivityKit that waits on the main thread from hanging it.
+ @objc public static func endBeforeExit() {
+ if showing.isEmpty { return }
+ let done = DispatchSemaphore(value: 0)
+ Task.detached {
+ for activity in Activity<SGLyricsAttributes>.activities {
+ await activity.end(nil, dismissalPolicy: .immediate)
+ }
+ for activity in Activity<SGLyricsWatchAttributes>.activities {
+ await activity.end(nil, dismissalPolicy: .immediate)
+ }
+ done.signal()
+ }
+ if done.wait(timeout: .now() + 2) == .timedOut {
+ log.notice("[spotifyglass] live activity: not ended in time at exit")
+ }
+ }
}