Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.
C
feat: Live Activity has a small layout on Apple Watch and CarPlay from iOS 18
My-Name-Is-Jeffb302e22f2103
diff --git a/extension/LiveActivity/LiveActivityWidget.swift b/extension/LiveActivity/LiveActivityWidget.swiftindex 4582d789..41161f0e 100644--- a/extension/LiveActivity/LiveActivityWidget.swift+++ b/extension/LiveActivity/LiveActivityWidget.swift@@ -20,6 +20,9 @@ private typealias Tab = SGLyricsAttributes.Tabstruct SGLiveActivityBundle: WidgetBundle {var body: some Widget {SGLyricsLiveActivity()+ if #available(iOS 18.0, *) {+ SGLyricsWatchLiveActivity()+ }}}@@ -83,41 +86,125 @@ extension SGLyricsAttributes.Tab {struct SGLyricsLiveActivity: Widget {var body: some WidgetConfiguration {- ActivityConfiguration(for: SGLyricsAttributes.self) { context in- FamilyView(state: context.state)- .foregroundStyle(.white)- .activityBackgroundTint(tint(context.state))- .activitySystemActionForegroundColor(.white)- .widgetURL(URL(string: "spotify:"))- } dynamicIsland: { context in- DynamicIsland {- DynamicIslandExpandedRegion(.bottom) {- Group {- if context.state.view == .panel {- Summary(state: context.state)- } else {- ContentView(state: context.state, upNext: 3)- }+ lyricsActivity(SGLyricsAttributes.self) { FamilyView(state: $0) }+ }+}++// The same activity from iOS 18, with its own small layout on Apple Watch and in CarPlay.+@available(iOS 18.0, *)+struct SGLyricsWatchLiveActivity: Widget {+ var body: some WidgetConfiguration {+ lyricsActivity(SGLyricsWatchAttributes.self) { FamilyAwareView(state: $0) }+ .supplementalActivityFamilies([.small])+ }+}++private func lyricsActivity<A: ActivityAttributes, Card: View>(+ _: A.Type, @ViewBuilder card: @escaping (State) -> Card+) -> ActivityConfiguration<A> where A.ContentState == State {+ ActivityConfiguration(for: A.self) { context in+ card(context.state)+ .foregroundStyle(.white)+ .activityBackgroundTint(tint(context.state))+ .activitySystemActionForegroundColor(.white)+ .widgetURL(URL(string: "spotify:"))+ } dynamicIsland: { context in+ DynamicIsland {+ // The row beside the camera: a note, and the sleep timer or whether it plays.+ DynamicIslandExpandedRegion(.leading) {+ Image(systemName: "music.note")+ .foregroundStyle(green)+ .padding(.leading, 6)+ }+ DynamicIslandExpandedRegion(.trailing) {+ Group {+ if let end = runningTimer(context.state) {+ Countdown(end: end)+ } else {+ PlayingSymbol(state: context.state)}- .frame(maxWidth: .infinity, alignment: .leading)- .padding(.horizontal, 4)}- } compactLeading: {- Badge(state: context.state)- } compactTrailing: {- if let end = context.state.timerEnd, end > Date() {- Text(timerInterval: Date()...end, countsDown: true)- .monospacedDigit()- .foregroundStyle(green)- .frame(maxWidth: 44)- } else {- ProgressRing(state: context.state)+ .padding(.trailing, 6)+ }+ DynamicIslandExpandedRegion(.bottom) {+ Group {+ if context.state.view == .panel {+ Summary(state: context.state)+ } else {+ ContentView(state: context.state, upNext: 3)+ }}- } minimal: {- Badge(state: context.state)+ .frame(maxWidth: .infinity, alignment: .leading)+ .padding(.horizontal, 4)+ // The island is always black, whatever the card's tint.+ .foregroundStyle(.white)}- .widgetURL(URL(string: "spotify:"))+ } compactLeading: {+ Badge(state: context.state)+ } compactTrailing: {+ if let end = runningTimer(context.state) {+ Countdown(end: end)+ .frame(maxWidth: 44)+ } else {+ ProgressRing(state: context.state)+ }+ } minimal: {+ Badge(state: context.state)+ }+ .widgetURL(URL(string: "spotify:"))+ }+}++@available(iOS 18.0, *)+private struct FamilyAwareView: View {+ let state: State+ @Environment(\.activityFamily) private var family++ var body: some View {+ switch family {+ case .small: SmallView(state: state)+ default: FamilyView(state: state)+ }+ }+}++// Apple Watch's Smart Stack and CarPlay: the cover and the line, or the track while the view is not+// the lyrics, and the bar under them. Nothing to tap, since a tap there opens Spotify on the iPhone.+private struct SmallView: View {+ let state: State++ private var lines: (String, String) {+ if state.view == .lyrics, !state.line.isEmpty {+ return (state.line, state.translation.flatMap { $0.isEmpty ? nil : $0 } ?? state.nextLine)+ }+ return (state.title, state.artist)+ }++ var body: some View {+ VStack(alignment: .leading, spacing: 6) {+ HStack(alignment: .center, spacing: 8) {+ if let image = coverImage(state) {+ Cover(image: image, side: 32)+ }+ VStack(alignment: .leading, spacing: 1) {+ Text(lines.0)+ .font(.headline)+ .lineLimit(2)+ .minimumScaleFactor(0.75)+ .direction(of: lines.0)+ if !lines.1.isEmpty {+ Text(lines.1)+ .font(.caption)+ .foregroundStyle(.white.opacity(0.6))+ .lineLimit(1)+ .direction(of: lines.1)+ }+ }+ }+ ProgressBar(state: state)}+ .frame(maxWidth: .infinity, alignment: .leading)+ .padding(10)}}@@ -142,6 +229,32 @@ private struct FamilyView: View {}}+// The sleep timer's end while it is still ahead; nil once it is up or when none is set, so no countdown+// is drawn from a range that runs backwards.+private func runningTimer(_ state: State) -> Date? {+ state.timerEnd.flatMap { $0 > Date() ? $0 : nil }+}++private struct Countdown: View {+ let end: Date++ var body: some View {+ Text(timerInterval: Date()...end, countsDown: true)+ .monospacedDigit()+ .foregroundStyle(green)+ }+}++// Paused or playing, where there is no progress or sleep timer to show.+private struct PlayingSymbol: View {+ let state: State++ var body: some View {+ Image(systemName: state.paused ? "pause.fill" : "waveform")+ .foregroundStyle(green)+ }+}+// The track's progress as a ring, for the compact Dynamic Island, which Apple Watch and CarPlay show too.private struct ProgressRing: View {let state: State@@ -159,8 +272,7 @@ private struct ProgressRing: View {.tint(green).frame(width: 18, height: 18)} else {- Image(systemName: state.paused ? "pause.fill" : "waveform")- .foregroundStyle(green)+ PlayingSymbol(state: state)}}}@@ -213,6 +325,29 @@ private struct LyricsView: View {}var body: some View {+ // No line being sung (before the first, in a break, without lyrics): the track holds the place.+ if state.line.isEmpty || state.line == "♪" {+ VStack(alignment: .center, spacing: 2) {+ Text(state.title.isEmpty ? "♪" : state.title)+ .font(lineFont.weight(.bold))+ .lineLimit(1)+ .frame(maxWidth: .infinity)+ .direction(of: state.title)+ if !state.title.isEmpty, !state.artist.isEmpty {+ Text(state.artist)+ .font(.subheadline.weight(.medium))+ .foregroundStyle(.white.opacity(0.6))+ .lineLimit(1)+ .frame(maxWidth: .infinity)+ .direction(of: state.artist)+ }+ }+ } else {+ line+ }+ }++ private var line: some View {VStack(alignment: .center, spacing: 4) {Text(state.line).font(lineFont.weight(.bold))diff --git a/tweak/Sources/Shared/LiveActivity/LiveActivity.x b/tweak/Sources/Shared/LiveActivity/LiveActivity.xindex 6ce13740..b8ec13f4 100644--- a/tweak/Sources/Shared/LiveActivity/LiveActivity.x+++ b/tweak/Sources/Shared/LiveActivity/LiveActivity.x@@ -283,7 +283,8 @@ static void tick(void) API_AVAILABLE(ios(17.0)) {}BOOL panel = view == SGLiveActivityPanel;- NSString *title = panel ? track.trackTitle : @"", *artist = panel ? track.artistName ?: @"" : @"";+ // Every view sends the track, which Apple Watch and CarPlay show beside the cover.+ NSString *title = track.trackTitle ?: @"", *artist = track.artistName ?: @"";BOOL shuffle = panel && state.options.shufflingContext;NSInteger repeatMode = panel ? repeatModeOf(state.options) : 0;NSInteger tab = panel ? sg_tab : 0;diff --git a/tweak/Sources/Shared/LiveActivity/LiveActivityBridge.swift b/tweak/Sources/Shared/LiveActivity/LiveActivityBridge.swiftindex 5d1fd04e..55a19ffe 100644--- a/tweak/Sources/Shared/LiveActivity/LiveActivityBridge.swift+++ b/tweak/Sources/Shared/LiveActivity/LiveActivityBridge.swift@@ -10,16 +10,23 @@ public final class SGLiveActivityBridge: NSObject {// 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 }+ private typealias State = SGLyricsAttributes.ContentState++ // Either type's activities: one left from before an update to iOS 18 carries on under its own.+ private static func live<A>(_ activities: [Activity<A>]) -> [String] {+ activities.filter { $0.activityState == .active || $0.activityState == .stale }.map(\.id)+ }++ private static var showing: [String] {+ live(Activity<SGLyricsAttributes>.activities) + live(Activity<SGLyricsWatchAttributes>.activities)}- @objc public static var isShowing: Bool { current != nil }+ @objc public static var isShowing: Bool { !showing.isEmpty }// Updates and ends go out one at a time in the order asked for, since ActivityKit applies// concurrent ones in whatever order they finish. An update still waiting gives way to a newer one.private enum Change: Sendable {- case update(String, ActivityContent<SGLyricsAttributes.ContentState>)+ case update(String, ActivityContent<State>)case end([String])}@@ -51,12 +58,15 @@ public final class SGLiveActivityBridge: NSObject {}return pending.changes.removeFirst()}) {- let activities = Activity<SGLyricsAttributes>.activitiesswitch change {case .update(let id, let content):- await activities.first { $0.id == id }?.update(content)+ await Activity<SGLyricsAttributes>.activities.first { $0.id == id }?.update(content)+ await Activity<SGLyricsWatchAttributes>.activities.first { $0.id == id }?.update(content)case .end(let ids):- for activity in activities where ids.contains(activity.id) {+ for activity in Activity<SGLyricsAttributes>.activities where ids.contains(activity.id) {+ await activity.end(nil, dismissalPolicy: .immediate)+ }+ for activity in Activity<SGLyricsWatchAttributes>.activities where ids.contains(activity.id) {await activity.end(nil, dismissalPolicy: .immediate)}}@@ -89,8 +99,8 @@ public final class SGLiveActivityBridge: NSObject {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))+ if let id = showing.first {+ queue(.update(id, content))return}guard ActivityAuthorizationInfo().areActivitiesEnabled else {@@ -98,8 +108,13 @@ public final class SGLiveActivityBridge: NSObject {return}do {- let activity = try Activity.request(attributes: SGLyricsAttributes(), content: content, pushType: nil)- log.notice("[spotifyglass] live activity: started \(activity.id, privacy: .public)")+ let id: String+ if #available(iOS 18.0, *) {+ id = try Activity.request(attributes: SGLyricsWatchAttributes(), content: content, pushType: nil).id+ } else {+ id = try Activity.request(attributes: SGLyricsAttributes(), content: content, pushType: nil).id+ }+ log.notice("[spotifyglass] live activity: started \(id, privacy: .public)")} catch {log.error("[spotifyglass] live activity: request failed: \(String(describing: error), privacy: .public)")}@@ -107,6 +122,6 @@ public final class SGLiveActivityBridge: NSObject {// Named now, so an activity requested after this call is not ended with them.@objc public static func end() {- queue(.end(Activity<SGLyricsAttributes>.activities.map(\.id)))+ queue(.end(Activity<SGLyricsAttributes>.activities.map(\.id) + Activity<SGLyricsWatchAttributes>.activities.map(\.id)))}}diff --git a/tweak/Sources/Shared/LiveActivity/LiveActivityShared.swift b/tweak/Sources/Shared/LiveActivity/LiveActivityShared.swiftindex fc121874..ba5bad29 100644--- a/tweak/Sources/Shared/LiveActivity/LiveActivityShared.swift+++ b/tweak/Sources/Shared/LiveActivity/LiveActivityShared.swift@@ -56,6 +56,15 @@ struct SGLyricsAttributes: ActivityAttributes {}}+// The same activity under a second name. Apple Watch and CarPlay show an activity's own small layout+// only when its widget declares that family, which only an iOS 18 widget can, and a widget built for+// iOS 17 still has to cover the old type. So from iOS 18 the tweak asks for this one, and the iOS 18+// widget is the one that answers it.+@available(iOS 16.1, *)+struct SGLyricsWatchAttributes: ActivityAttributes {+ typealias ContentState = SGLyricsAttributes.ContentState+}+// LiveActivityIntents run in the app's process, where the tweak acts on these notifications.let SGLiveActivityPlayNotification = Notification.Name("SGLiveActivityPlay")let SGLiveActivityActionNotification = Notification.Name("SGLiveActivityAction")