84 lines
3.3 KiB
Swift
84 lines
3.3 KiB
Swift
import UIKit
|
|
import WidgetKit
|
|
|
|
/// Publishes the now-playing snapshot for the Home/Lock Screen widgets (Android Widgets.updateAll).
|
|
@MainActor
|
|
enum WidgetBridge {
|
|
private static var last = NowSnapshot()
|
|
private static var artVersion = 0
|
|
private static var lastArt: UIImage?
|
|
private static var lastReload = Date.distantPast
|
|
private static var reloadPending = false
|
|
|
|
static func publish(song: Song?, playing: Bool, position: Double, duration: Double, art: UIImage?,
|
|
palette: Palette, lyrics: Lyrics.Result?, shuffle: Bool, repeatMode: Int) {
|
|
var s = NowSnapshot()
|
|
if let song {
|
|
s.title = song.displayTitle
|
|
s.artist = song.displayArtist
|
|
s.album = song.album
|
|
s.favourite = PlayStats.shared.fav(song)
|
|
}
|
|
s.playing = playing
|
|
s.position = position
|
|
s.duration = duration
|
|
s.at = Date().timeIntervalSince1970
|
|
s.shuffle = shuffle
|
|
s.repeatMode = repeatMode
|
|
s.dark = palette.dark
|
|
s.widgetFPS = Prefs.shared.widgetAnim
|
|
s.palette = [palette.primary, palette.onPrimary, palette.primaryContainer, palette.onPrimaryContainer,
|
|
palette.secondaryContainer, palette.onSecondaryContainer, palette.surface, palette.surfaceHigh,
|
|
palette.onSurface, palette.onSurfaceVariant]
|
|
if let lr = lyrics {
|
|
if lr.synced {
|
|
s.lyrics = lr.lines.map(\.t)
|
|
s.lyricText = lr.lines.map(\.text)
|
|
} else {
|
|
s.plainLyric = lr.lines.prefix(4).map(\.text).joined(separator: "\n")
|
|
}
|
|
}
|
|
if art !== lastArt {
|
|
lastArt = art
|
|
artVersion += 1
|
|
if let art, let d = squareThumb(art, 400)?.jpegData(compressionQuality: 0.85) {
|
|
try? d.write(to: NowSnapshot.artFile, options: .atomic)
|
|
} else {
|
|
try? FileManager.default.removeItem(at: NowSnapshot.artFile)
|
|
}
|
|
}
|
|
s.hasArt = art != nil
|
|
s.artVersion = artVersion
|
|
// Only the position drifting is not worth a reload: the widget extrapolates it.
|
|
var cmp = s
|
|
cmp.position = last.position; cmp.at = last.at
|
|
let drifted = abs(s.position(at: Date()) - last.position(at: Date())) > 2
|
|
guard cmp != last || drifted else { return }
|
|
last = s
|
|
s.save()
|
|
reload()
|
|
}
|
|
|
|
/// WidgetKit throttles reloads; coalesce bursts (track change + art + lyrics) into one.
|
|
private static func reload() {
|
|
guard !reloadPending else { return }
|
|
let wait = max(0, 0.4 - Date().timeIntervalSince(lastReload))
|
|
reloadPending = true
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + wait) {
|
|
reloadPending = false
|
|
lastReload = Date()
|
|
WidgetCenter.shared.reloadAllTimelines()
|
|
}
|
|
}
|
|
|
|
static func squareThumb(_ img: UIImage, _ px: CGFloat) -> UIImage? {
|
|
let side = min(img.size.width, img.size.height)
|
|
guard side > 0 else { return nil }
|
|
let r = UIGraphicsImageRenderer(size: CGSize(width: px, height: px))
|
|
return r.image { _ in
|
|
let scale = px / side
|
|
let w = img.size.width * scale, h = img.size.height * scale
|
|
img.draw(in: CGRect(x: (px - w) / 2, y: (px - h) / 2, width: w, height: h))
|
|
}
|
|
}
|
|
}
|