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

feat: the Live Activity shows the cover, and its lyrics get a Translations switch and a Text size of their own

My-Name-Is-Jeffcee7b963d985
diff --git a/docs/tweaks.md b/docs/tweaks.md
index 6c909a50..9dd3012e 100644
--- a/docs/tweaks.md
+++ b/docs/tweaks.md
@@ -165,10 +165,14 @@ Shared:
sung with the next one under it, the tracks up next (a tap on one skipping ahead to it), or a control
menu of tabs, Controls (previous, play and pause, next, shuffle, repeat), Queue and a sleep Timer of
the mod's own that pauses Spotify (LiveActivity.h lists its files): a timer polls the player and
- sends a new state only when what the view shows changes, local updates only, no push. Taps are
+ sends a new state only when what the view shows changes, local updates only, no push. The card
+ is tinted with the cover's colour and shows the cover itself, a JPEG of a few dozen pixels inside
+ the state, which ActivityKit caps at 4 KB (the bridge sends it without the cover when it would
+ not fit); the colour is darkened to a luminance of 0.04 at most, so the white text keeps 4.5:1 on any
+ 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 switch and its view apply at once
+ in front; its settings apply at once
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
@@ -311,8 +315,8 @@ Controls into its Strength (10 to 100%, a tap at the new strength with each step
Strength (20 to 200%, 100% being how it first shipped) and Follows, Everything (a tap on each kick and
snare and a rumble under the bass), Beat (the taps without the rumble) or Bass (the kicks' taps and the
rumble), all applying straight away. Live Activity, on iOS 17 and up under either look: its switch and which view it
-shows, Lyrics, Queue or Control menu, both
-applying straight away, the row reading out the view or Off. Audio effects, in either look (Shared/AudioEffects/AudioEffectsPage.m):
+shows, Lyrics, Queue or Control menu, and for Lyrics its Translations (off until switched on) and Text
+size (Small, Medium or Large), all applying straight away, the row reading out the view or Off. Audio effects, in either look (Shared/AudioEffects/AudioEffectsPage.m):
the effects' switch with what the engine is doing under it, Presets (built-in ones and your own, saved, loaded
and deleted there) and Headphones (AutoEq's corrections, searched and applied to the Graphic EQ), either of which
turns the effects on, then a card per effect, each opening out into its sliders, choices, curve or file library
diff --git a/extension/LiveActivity/LiveActivityWidget.swift b/extension/LiveActivity/LiveActivityWidget.swift
index 01a23fc4..4582d789 100644
--- a/extension/LiveActivity/LiveActivityWidget.swift
+++ b/extension/LiveActivity/LiveActivityWidget.swift
@@ -7,6 +7,7 @@
import ActivityKit
import AppIntents
import SwiftUI
+import UIKit
import WidgetKit
private let green = Color(red: 0.12, green: 0.84, blue: 0.38)
@@ -29,6 +30,39 @@ private func tint(_ state: State) -> Color {
blue: Double(value & 0xFF) / 255)
}
+private func coverImage(_ state: State) -> UIImage? {
+ state.cover.flatMap(UIImage.init(data:))
+}
+
+// The cover, a few dozen pixels a side and drawn larger, so it reads as the cover and not as a picture of it.
+private struct Cover: View {
+ let image: UIImage
+ let side: CGFloat
+
+ var body: some View {
+ Image(uiImage: image)
+ .resizable()
+ .aspectRatio(contentMode: .fill)
+ .frame(width: side, height: side)
+ .clipShape(RoundedRectangle(cornerRadius: side / 6, style: .continuous))
+ }
+}
+
+// The compact and minimal Dynamic Island's leading view, which Apple Watch and CarPlay show too: the
+// cover, or a note before it is read.
+private struct Badge: View {
+ let state: State
+
+ var body: some View {
+ if let image = coverImage(state) {
+ Cover(image: image, side: 22)
+ } else {
+ Image(systemName: "music.note")
+ .foregroundStyle(green)
+ }
+ }
+}
+
extension SGLyricsAttributes.Tab {
var symbol: String {
switch self {
@@ -69,8 +103,7 @@ struct SGLyricsLiveActivity: Widget {
.padding(.horizontal, 4)
}
} compactLeading: {
- Image(systemName: "music.note")
- .foregroundStyle(green)
+ Badge(state: context.state)
} compactTrailing: {
if let end = context.state.timerEnd, end > Date() {
Text(timerInterval: Date()...end, countsDown: true)
@@ -81,21 +114,26 @@ struct SGLyricsLiveActivity: Widget {
ProgressRing(state: context.state)
}
} minimal: {
- Image(systemName: "music.note")
- .foregroundStyle(green)
+ Badge(state: context.state)
}
.widgetURL(URL(string: "spotify:"))
}
}
}
-// The lock screen's card, and StandBy's: what the view shows, and the bar under it.
+// The lock screen's card, and StandBy's: the cover beside what the view shows, and the bar under both.
+// The control menu's five chips need the whole width, so it goes without the cover.
private struct FamilyView: View {
let state: State
var body: some View {
VStack(alignment: .leading, spacing: 10) {
- ContentView(state: state, upNext: 4)
+ HStack(alignment: .center, spacing: 12) {
+ if state.view != .panel, let image = coverImage(state) {
+ Cover(image: image, side: 52)
+ }
+ ContentView(state: state, upNext: 4)
+ }
ProgressBar(state: state)
}
.frame(maxWidth: .infinity, alignment: .leading)
@@ -165,10 +203,19 @@ private struct ContentView: View {
private struct LyricsView: View {
let state: State
+ // The page's Text size; the scale factor below still keeps a long line inside the 160 point clip.
+ private var lineFont: Font {
+ switch state.textSize {
+ case 0: .headline
+ case 2: .title2
+ default: .title3
+ }
+ }
+
var body: some View {
VStack(alignment: .center, spacing: 4) {
Text(state.line)
- .font(.title3.weight(.bold))
+ .font(lineFont.weight(.bold))
.lineLimit(2)
.minimumScaleFactor(0.7)
.multilineTextAlignment(.center)
@@ -186,7 +233,7 @@ private struct LyricsView: View {
if !state.nextLine.isEmpty {
Text(state.nextLine)
.font(.subheadline.weight(.semibold))
- .foregroundStyle(.white.opacity(0.45))
+ .foregroundStyle(.white.opacity(0.6))
.lineLimit(1)
.direction(of: state.nextLine)
}
@@ -232,7 +279,7 @@ private struct QueueView: View {
VStack(alignment: .leading, spacing: 3) {
Text("Up next")
.font(.caption.weight(.semibold))
- .foregroundStyle(.white.opacity(0.45))
+ .foregroundStyle(.white.opacity(0.6))
if state.tracks.isEmpty {
Text("Nothing up next")
.font(.subheadline)
@@ -241,7 +288,7 @@ private struct QueueView: View {
ForEach(Array(state.tracks.prefix(upNext).enumerated()), id: \.offset) { _, track in
// A tap skips ahead to the track.
Button(intent: SGPlayQueuedTrackIntent(track.uri)) {
- (Text(track.title).fontWeight(.semibold) + Text(" " + track.artist).foregroundColor(.white.opacity(0.5)))
+ (Text(track.title).fontWeight(.semibold) + Text(" " + track.artist).foregroundColor(.white.opacity(0.6)))
.font(.subheadline)
.lineLimit(1)
.frame(maxWidth: .infinity, alignment: .leading)
@@ -385,7 +432,7 @@ private struct ControlsPage: View {
.font(.subheadline.weight(.semibold))
Text(state.artist)
.font(.subheadline)
- .foregroundStyle(.white.opacity(0.55))
+ .foregroundStyle(.white.opacity(0.6))
}
.lineLimit(1)
.invalidatableContent()
@@ -413,7 +460,7 @@ private struct QueuePage: View {
if state.tracks.isEmpty {
Text("Nothing up next")
.font(.footnote)
- .foregroundStyle(.white.opacity(0.55))
+ .foregroundStyle(.white.opacity(0.6))
.frame(maxWidth: .infinity, minHeight: 60)
}
ForEach(Array(state.tracks.prefix(3).enumerated()), id: \.offset) { _, track in
@@ -426,7 +473,7 @@ private struct QueuePage: View {
.font(.footnote.weight(.semibold))
Text(track.artist)
.font(.footnote)
- .foregroundStyle(.white.opacity(0.55))
+ .foregroundStyle(.white.opacity(0.6))
Spacer(minLength: 0)
}
.lineLimit(1)
@@ -451,7 +498,7 @@ private struct TimerPage: View {
VStack(alignment: .leading, spacing: 0) {
Text("Music stops")
.font(.caption)
- .foregroundStyle(.white.opacity(0.55))
+ .foregroundStyle(.white.opacity(0.6))
if let end = state.timerEnd {
Text(timerInterval: Date()...end, countsDown: true)
.font(.system(size: 34, weight: .bold).monospacedDigit())
diff --git a/tweak/Sources/Shared/LiveActivity/LiveActivity.h b/tweak/Sources/Shared/LiveActivity/LiveActivity.h
index a4a0d933..b75506a0 100644
--- a/tweak/Sources/Shared/LiveActivity/LiveActivity.h
+++ b/tweak/Sources/Shared/LiveActivity/LiveActivity.h
@@ -24,6 +24,10 @@
#define SGKeyLiveActivity @"spotifyglass.liveActivity"
// Which view it shows, the index into the page's list.
#define SGKeyLiveActivityView @"spotifyglass.liveActivity.view"
+// The lyrics view's: the line's translation under it (off until switched on), and the line's size, an
+// SGLiveActivityTextSize.
+#define SGKeyLiveActivityTranslation @"spotifyglass.liveActivity.translation"
+#define SGKeyLiveActivityTextSize @"spotifyglass.liveActivity.textSize"
// What the keys were called while this was the redesign's alone; LiveActivity.x's %ctor moves them over.
#define SGKeyLiveActivityWas @"spotifyglass.redesign.liveActivity"
#define SGKeyLiveActivityViewWas @"spotifyglass.redesign.liveActivity.view"
@@ -34,11 +38,18 @@ typedef NS_ENUM(NSInteger, SGLiveActivityView) {
SGLiveActivityPanel, // the control menu
};
+// SGLyricsAttributes.ContentState's textSize; the widget gives each a text style.
+typedef NS_ENUM(NSInteger, SGLiveActivityTextSize) {
+ SGLiveActivityTextSmall = 0,
+ SGLiveActivityTextMedium,
+ SGLiveActivityTextLarge,
+};
+
// From the switch: starts following the player (and the activity, the app being in front) or ends both.
void SGSetLiveActivityEnabled(BOOL on);
@class UIViewController;
-// The Live Activity page: its switch and which view it shows.
+// The Live Activity page: its switch, which view it shows, and the lyrics' translation and size.
UIViewController *SGLiveActivitySettingsPage(void);
// What the root's row reads out beside its chevron: the view shown, or Off.
NSString *SGLiveActivitySummary(void);
diff --git a/tweak/Sources/Shared/LiveActivity/LiveActivity.x b/tweak/Sources/Shared/LiveActivity/LiveActivity.x
index 6b330f2d..6ce13740 100644
--- a/tweak/Sources/Shared/LiveActivity/LiveActivity.x
+++ b/tweak/Sources/Shared/LiveActivity/LiveActivity.x
@@ -7,6 +7,7 @@
#import <objc/runtime.h>
#import <MediaPlayer/MediaPlayer.h>
#import "Core/SGCore.h"
+#import "Shared/Player/PlayerState.h"
#import "Headers/SPTPlayer.h"
#import "Shared/Lyrics/Lyrics.h"
#import "LiveActivity.h"
@@ -19,7 +20,7 @@ API_AVAILABLE(ios(17.0))
tab:(NSInteger)tab title:(NSString *)title artist:(NSString *)artist shuffle:(BOOL)shuffle repeatMode:(NSInteger)repeatMode
timerEnd:(NSDate *)timerEnd timerEndOfTrack:(BOOL)timerEndOfTrack
tint:(NSString *)tint trackStart:(NSDate *)trackStart trackEnd:(NSDate *)trackEnd pausedAt:(NSNumber *)pausedAt
- translation:(NSString *)translation;
+ translation:(NSString *)translation cover:(NSData *)cover textSize:(NSInteger)textSize;
+ (void)end;
@end
@@ -45,6 +46,17 @@ static const NSUInteger kUpNextQueue = 4;
static const NSUInteger kUpNextPanel = 3;
// The end of track timer pauses this close to the end, so the next track never starts.
static const NSInteger kEndOfTrackMs = 500;
+// The cover goes to the card in the state itself, which ActivityKit caps at 4 KB with everything else in
+// it and encodes as JSON, the cover in base64: this many bytes of JPEG come to about 1.9 KB of it.
+static const NSUInteger kCoverBytes = 1400;
+// A new state takes about this long to show on the card, so the line is picked this far ahead and
+// lands as it is sung rather than a beat after.
+static const NSInteger kRenderLeadMs = 800;
+// A track start this close to the one sent is the same start, rounded the other way.
+static const NSTimeInterval kStartSlack = 1.5;
+// The card's background is at most this light, as relative luminance (about #383838), so every white on
+// it keeps 4.5:1: the dimmest, at 0.6, comes to about 5.3:1 there, the line itself over 11:1.
+static const double kTintLuminance = 0.04;
static NSTimer *sg_timer;
static NSTimeInterval sg_tickEvery;
@@ -118,37 +130,85 @@ static NSString *lyricsLine(NSString *trackID, NSString **next, NSString **trans
// Plain text has no line being sung: the note, as for a track with no lyrics.
if (lines && SGKaraokeLinesTiming(lines) == SGKaraokeTimingNone) lines = nil;
NSInteger position = SGKaraokePositionMs();
+ if (position >= 0 && !SGPlayerState().isPaused) position += kRenderLeadMs;
// With two voices at once, the one that came in first, and the one singing over it as the next.
NSInteger index = SGKaraokeLeadLine(lines, position);
*next = index + 1 < (NSInteger)lines.count ? SGKaraokeLineText(lines[index + 1]) : @"";
if (index < 0) return @"♪";
SGKaraokeLine *current = lines[index];
- *translation = current.translation;
BOOL nextFarOff = index + 1 == (NSInteger)lines.count || lines[index + 1].start - position > kBreakMs;
- return position > current.end + kBreakMs && nextFarOff ? @"♪" : SGKaraokeLineText(current);
+ if (position > current.end + kBreakMs && nextFarOff) return @"♪";
+ *translation = current.translation;
+ return SGKaraokeLineText(current);
}
-// The cover's colour for the card: Spotify's now playing artwork drawn into one pixel, darkened so white
-// text keeps its contrast. Read once a track.
-static NSString *sg_tintTrack, *sg_tint;
+// The cover for the card, read once a track from Spotify's now playing artwork: its colour, darkened so
+// white text keeps its contrast, and a small JPEG of it. At a track change the player moves a moment
+// before Spotify hands over the new artwork, so it is read only once the now playing title is the
+// track's (as Shared/LockScreenLyrics/LyricsArtwork.x does); until then the last track's stays. Spotify
+// hands over a placeholder first and the cover after it, a new artwork object each time, so a new
+// object for the same track is read again.
+static NSString *sg_artTrack, *sg_tint;
+static NSData *sg_cover;
+static __weak id sg_artObject;
+
+// The cover at the largest of a few sizes whose JPEG fits kCoverBytes, drawn in device RGB so no colour
+// profile rides along; nil when none fits.
+static NSData *coverJPEG(CGImageRef image) {
+ for (NSNumber *side in @[@56, @48, @40, @32]) {
+ size_t pixels = side.unsignedIntegerValue;
+ CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
+ CGContextRef context = CGBitmapContextCreate(NULL, pixels, pixels, 8, 0, space, (CGBitmapInfo)kCGImageAlphaNoneSkipLast);
+ CGColorSpaceRelease(space);
+ if (!context) return nil;
+ CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
+ CGContextDrawImage(context, CGRectMake(0, 0, pixels, pixels), image);
+ CGImageRef small = CGBitmapContextCreateImage(context);
+ CGContextRelease(context);
+ NSData *data = small ? UIImageJPEGRepresentation([UIImage imageWithCGImage:small], 0.5) : nil;
+ CGImageRelease(small);
+ if (data.length && data.length <= kCoverBytes) return data;
+ }
+ return nil;
+}
-static NSString *tintFor(NSString *trackID) {
- if (!trackID || [trackID isEqualToString:sg_tintTrack]) return sg_tint;
- MPMediaItemArtwork *artwork = MPNowPlayingInfoCenter.defaultCenter.nowPlayingInfo[MPMediaItemPropertyArtwork];
- UIImage *image = [artwork isKindOfClass:MPMediaItemArtwork.class] ? [artwork imageWithSize:CGSizeMake(32, 32)] : nil;
- if (!image.CGImage) return sg_tint;
+// A channel of 0 to 255 as light, the sRGB curve undone.
+static double linear(double channel) {
+ channel /= 255;
+ return channel <= 0.04045 ? channel / 12.92 : pow((channel + 0.055) / 1.055, 2.4);
+}
+
+// The card's background, RRGGBB: at most 0.55 of the cover's colour, the rest black, and darker still
+// until it is no lighter than kTintLuminance. At a fixed 0.55 a white or yellow cover came out #8C8C8C.
+static NSString *tintOf(double red, double green, double blue) {
+ double keep = 0.55;
+ while (keep > 0 && 0.2126 * linear(red * keep) + 0.7152 * linear(green * keep) + 0.0722 * linear(blue * keep) > kTintLuminance) keep -= 0.01;
+ keep = MAX(keep, 0);
+ return [NSString stringWithFormat:@"%02X%02X%02X", (int)(red * keep), (int)(green * keep), (int)(blue * keep)];
+}
+
+static void readArtwork(SPTPlayerState *state, NSString *trackID) {
+ if (!trackID) return;
+ NSDictionary *info = MPNowPlayingInfoCenter.defaultCenter.nowPlayingInfo;
+ if (![info[MPMediaItemPropertyTitle] isEqual:state.track.trackTitle]) return;
+ MPMediaItemArtwork *artwork = info[MPMediaItemPropertyArtwork];
+ if ([trackID isEqualToString:sg_artTrack] && artwork == sg_artObject) return;
+ sg_artObject = artwork;
+ UIImage *image = [artwork isKindOfClass:MPMediaItemArtwork.class] ? [artwork imageWithSize:CGSizeMake(64, 64)] : nil;
+ if (!image.CGImage) return;
unsigned char pixel[4] = {0};
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(pixel, 1, 1, 8, 4, space, (CGBitmapInfo)kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(space);
- if (!context) return sg_tint;
+ if (!context) return;
CGContextSetInterpolationQuality(context, kCGInterpolationMedium);
CGContextDrawImage(context, CGRectMake(0, 0, 1, 1), image.CGImage);
CGContextRelease(context);
- const double keep = 0.55; // of the colour, the rest black
- sg_tintTrack = trackID;
- sg_tint = [NSString stringWithFormat:@"%02X%02X%02X", (int)(pixel[0] * keep), (int)(pixel[1] * keep), (int)(pixel[2] * keep)];
- return sg_tint;
+ sg_artTrack = trackID;
+ sg_tint = tintOf(pixel[0], pixel[1], pixel[2]);
+ sg_cover = coverJPEG(image.CGImage);
+ static int logged;
+ if (logged++ < 6) SGLog(@"live activity: cover of %@, %lu bytes", trackID, (unsigned long)sg_cover.length);
}
static void clearSleepTimer(void) {
@@ -188,20 +248,27 @@ static void tick(void) API_AVAILABLE(ios(17.0)) {
if (sg_timer && sg_tickEvery != every) startTimer(every);
NSString *line = @"", *next = @"", *translation = nil;
if (view == SGLiveActivityLyrics) line = lyricsLine(trackID, &next, &translation);
- // The redesign's switch for translations (SGRKeyLyricsTranslation, Redesigned/Lyrics/LyricsText.h).
- if (!SGFlag(@"spotifyglass.redesign.lyricsTranslation", NO)) translation = nil;
- NSString *tint = tintFor(trackID);
+ if (!SGFlag(SGKeyLiveActivityTranslation, NO)) translation = nil;
+ NSInteger textSize = view == SGLiveActivityLyrics ? SGInt(SGKeyLiveActivityTextSize, SGLiveActivityTextMedium) : SGLiveActivityTextMedium;
+ readArtwork(state, trackID);
+ NSString *tint = sg_tint;
+ NSData *cover = sg_cover;
// The bar runs by itself from where the track started; paused, it holds at the share played.
double duration = [state respondsToSelector:@selector(duration)] ? state.duration : 0;
NSInteger position = SGKaraokePositionMs();
NSDate *trackStart = nil, *trackEnd = nil;
NSNumber *pausedAt = nil;
+ static NSDate *sentStart;
if (duration > 0 && position >= 0) {
double played = position / 1000.0;
trackStart = [NSDate dateWithTimeIntervalSinceNow:-played];
trackStart = [NSDate dateWithTimeIntervalSince1970:round(trackStart.timeIntervalSince1970)];
- trackEnd = [trackStart dateByAddingTimeInterval:duration];
+ // Playing, the start only moves on a seek or a new track; paused, it creeps on with the clock and
+ // the bar is drawn from pausedAt, so it is left out of what tells a new state.
if (paused) pausedAt = @(MIN(1, played / duration));
+ else if (sentStart && fabs([trackStart timeIntervalSinceDate:sentStart]) < kStartSlack) trackStart = sentStart;
+ else sentStart = trackStart;
+ trackEnd = [trackStart dateByAddingTimeInterval:duration];
}
NSMutableArray<NSString *> *titles = [NSMutableArray array], *artists = [NSMutableArray array], *uris = [NSMutableArray array];
@@ -226,13 +293,15 @@ static void tick(void) API_AVAILABLE(ios(17.0)) {
NSMutableArray<NSString *> *parts = [NSMutableArray arrayWithObjects:@(view).stringValue, paused ? @"1" : @"0", line, next,
@(tab).stringValue, title, artist, shuffle ? @"1" : @"0", @(repeatMode).stringValue,
@((long long)sleepEnd.timeIntervalSince1970).stringValue, endOfTrack ? @"1" : @"0", tint ?: @"", translation ?: @"",
- @((long long)trackStart.timeIntervalSince1970).stringValue, paused ? @(round(pausedAt.doubleValue * 100)).stringValue : @"", nil];
+ paused ? @"" : @((long long)trackStart.timeIntervalSince1970).stringValue, paused ? @(round(pausedAt.doubleValue * 100)).stringValue : @"",
+ cover ? sg_artTrack : @"", @(textSize).stringValue, nil];
for (NSUInteger i = 0; i < titles.count; i++) [parts addObject:[NSString stringWithFormat:@"%@\t%@\t%@", titles[i], artists[i], uris[i]]];
send([parts componentsJoinedByString:@"\n"], ^{
[SGLiveActivityBridge showWithView:view paused:paused line:line nextLine:next titles:titles artists:artists uris:uris
tab:tab title:title artist:artist shuffle:shuffle repeatMode:repeatMode
timerEnd:sleepEnd timerEndOfTrack:endOfTrack
- tint:tint trackStart:trackStart trackEnd:trackEnd pausedAt:pausedAt translation:translation];
+ tint:tint trackStart:trackStart trackEnd:trackEnd pausedAt:pausedAt translation:translation
+ cover:cover textSize:textSize];
});
}
diff --git a/tweak/Sources/Shared/LiveActivity/LiveActivityBridge.swift b/tweak/Sources/Shared/LiveActivity/LiveActivityBridge.swift
index 2625875c..5d1fd04e 100644
--- a/tweak/Sources/Shared/LiveActivity/LiveActivityBridge.swift
+++ b/tweak/Sources/Shared/LiveActivity/LiveActivityBridge.swift
@@ -7,6 +7,8 @@ import os
@objc(SGLiveActivityBridge)
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
private static var current: Activity<SGLyricsAttributes>? {
Activity<SGLyricsAttributes>.activities.first { $0.activityState == .active || $0.activityState == .stale }
@@ -68,17 +70,24 @@ public final class SGLiveActivityBridge: NSObject {
titles: [String], artists: [String], uris: [String],
tab: Int, title: String, artist: String, shuffle: Bool, repeatMode: Int,
timerEnd: Date?, timerEndOfTrack: Bool,
- tint: String?, trackStart: Date?, trackEnd: Date?, pausedAt: NSNumber?, translation: String?) {
+ tint: String?, trackStart: Date?, trackEnd: Date?, pausedAt: NSNumber?, translation: String?,
+ cover: Data?, textSize: Int) {
let tracks = titles.indices.map {
SGLyricsAttributes.Track(title: titles[$0], artist: artists[$0], uri: uris[$0])
}
- let state = SGLyricsAttributes.ContentState(
+ var state = SGLyricsAttributes.ContentState(
view: SGLyricsAttributes.View(rawValue: view) ?? .lyrics, paused: paused,
line: line, nextLine: nextLine, tracks: tracks,
tab: SGLyricsAttributes.Tab(rawValue: tab) ?? .controls, title: title, artist: artist,
shuffle: shuffle, repeatMode: repeatMode, timerEnd: timerEnd, timerEndOfTrack: timerEndOfTrack,
tint: tint, trackStart: trackStart, trackEnd: trackEnd, pausedAt: pausedAt?.doubleValue,
- translation: translation)
+ translation: translation, cover: cover, textSize: textSize)
+ // ActivityKit drops a state over 4 KB without a word and the card freezes, so with long lines and
+ // a busy cover the cover gives way.
+ if cover != nil, let size = try? JSONEncoder().encode(state).count, size > maxStateBytes {
+ state.cover = nil
+ log.notice("[spotifyglass] live activity: \(size, privacy: .public) bytes, sent without the cover")
+ }
let content = ActivityContent(state: state, staleDate: nil)
if let activity = current {
queue(.update(activity.id, content))
diff --git a/tweak/Sources/Shared/LiveActivity/LiveActivitySettings.m b/tweak/Sources/Shared/LiveActivity/LiveActivitySettings.m
index 636de9a3..ac46e26c 100644
--- a/tweak/Sources/Shared/LiveActivity/LiveActivitySettings.m
+++ b/tweak/Sources/Shared/LiveActivity/LiveActivitySettings.m
@@ -11,8 +11,12 @@ UIViewController *SGLiveActivitySettingsPage(void) {
SGModRow *on = SGOptionRow(@"Live Activity", nil, SGKeyLiveActivity);
on.changed = ^(BOOL value) { SGSetLiveActivityEnabled(value); };
SGModRow *view = SGChoiceRow(@"Shows", nil, SGKeyLiveActivityView, viewNames(), SGLiveActivityLyrics);
+ // The lyrics view's own rows, shown while it is picked; the tick reads them, so they apply at once.
+ SGModRow *translation = SGOptionRow(@"Translations", @"Under the line, when the lyrics have one", SGKeyLiveActivityTranslation);
+ SGModRow *size = SGChoiceRow(@"Text size", nil, SGKeyLiveActivityTextSize, @[@"Small", @"Medium", @"Large"], SGLiveActivityTextMedium);
+ for (SGModRow *row in @[translation, size]) row.visible = ^BOOL { return SGInt(SGKeyLiveActivityView, SGLiveActivityLyrics) == SGLiveActivityLyrics; };
return [[SGModPage alloc] initWithTitle:@"Live Activity" intro:nil sections:@[
- SGSection(nil, @[on, view]),
+ SGSection(nil, @[on, view, translation, size]),
] footer:nil];
}
diff --git a/tweak/Sources/Shared/LiveActivity/LiveActivityShared.swift b/tweak/Sources/Shared/LiveActivity/LiveActivityShared.swift
index 9ea49aac..fc121874 100644
--- a/tweak/Sources/Shared/LiveActivity/LiveActivityShared.swift
+++ b/tweak/Sources/Shared/LiveActivity/LiveActivityShared.swift
@@ -48,6 +48,11 @@ struct SGLyricsAttributes: ActivityAttributes {
var pausedAt: Double?
// The line's translation, when the lyrics have one and translations are shown.
var translation: String?
+ // The cover as a JPEG of a few dozen pixels a side, nil before it is read or when it does not fit.
+ // Optional, as everything added later is, so an activity left by the build before still decodes.
+ var cover: Data?
+ // The lyrics' size, SGLiveActivityTextSize's values: 0 small, 1 medium, 2 large.
+ var textSize: Int?
}
}