Mulberry/Shared/WidgetShared.swift

121 lines
4.6 KiB
Swift

import Foundation
import AppIntents
/// What the widget shows; written by the app into the shared app-group container.
struct NowSnapshot: Codable, Equatable {
var title = ""
var artist = ""
var album = ""
var playing = false
var position: Double = 0
var duration: Double = 0
var at: Double = 0 // when `position` was sampled (epoch seconds)
var hasArt = false
var artVersion = 0
var favourite = false
var shuffle = false
var repeatMode = 0
var palette: [UInt32] = [] // primary, onPrimary, primaryC, onPrimaryC, secondaryC, onSecondaryC, surface, surfaceHigh, onSurface, onSurfaceVar
var dark = true
var lyrics: [Double] = [] // line start times (synced only)
var lyricText: [String] = []
var plainLyric = "" // first unsynced lines when lyrics aren't timed
var widgetFPS = 15
static let group = "group.com.mulberry.player"
static let kind = "MulberryPlayer"
static let lyricsKind = "MulberryLyrics"
static let shapeKind = "MulberryShape"
static var container: URL {
if let u = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: group) { return u }
// Jailbreak installs without a group container: fall back to a shared world-readable folder.
let u = URL(fileURLWithPath: "/var/mobile/Library/Mulberry", isDirectory: true)
try? FileManager.default.createDirectory(at: u, withIntermediateDirectories: true)
return u
}
static var file: URL { container.appendingPathComponent("now.json") }
static var artFile: URL { container.appendingPathComponent("art.jpg") }
static func load() -> NowSnapshot {
guard let d = try? Data(contentsOf: file), let s = try? JSONDecoder().decode(NowSnapshot.self, from: d) else { return NowSnapshot() }
return s
}
func save() {
if let d = try? JSONEncoder().encode(self) { try? d.write(to: NowSnapshot.file, options: .atomic) }
}
/// Position at a given time, extrapolated while playing.
func position(at date: Date) -> Double {
guard playing else { return position }
return min(duration > 0 ? duration : .infinity, position + date.timeIntervalSince1970 - at)
}
func lyricIndex(at date: Date) -> Int {
let t = position(at: date)
var ans = -1
for (i, s) in lyrics.enumerated() where s <= t + 0.15 { ans = i }
return ans
}
}
// MARK: Widget buttons (iOS 17 interactive widgets)
// One parameterless AudioPlaybackIntent per button, like FlacBox. AudioPlaybackIntent runs in the app's process.
// build.sh generates Metadata.appintents for these (tools/gen-appintents.py); without it iOS drops the widget.
enum MulberryCommand: String {
case toggle, next, previous, shuffle, repeatMode, favourite
}
/// The app installs a handler at launch; inside the widget extension it stays nil.
enum IntentRouter {
@MainActor static var handler: ((MulberryCommand) -> Void)?
static func run(_ c: MulberryCommand) async {
await MainActor.run { IntentRouter.handler?(c) }
}
}
@available(iOS 17.0, *)
struct PlayPauseIntent: AudioPlaybackIntent {
static var title: LocalizedStringResource = "Play or pause"
init() {}
func perform() async throws -> some IntentResult { await IntentRouter.run(.toggle); return .result() }
}
@available(iOS 17.0, *)
struct NextTrackIntent: AudioPlaybackIntent {
static var title: LocalizedStringResource = "Next track"
init() {}
func perform() async throws -> some IntentResult { await IntentRouter.run(.next); return .result() }
}
@available(iOS 17.0, *)
struct PreviousTrackIntent: AudioPlaybackIntent {
static var title: LocalizedStringResource = "Previous track"
init() {}
func perform() async throws -> some IntentResult { await IntentRouter.run(.previous); return .result() }
}
@available(iOS 17.0, *)
struct ShuffleIntent: AudioPlaybackIntent {
static var title: LocalizedStringResource = "Shuffle"
init() {}
func perform() async throws -> some IntentResult { await IntentRouter.run(.shuffle); return .result() }
}
@available(iOS 17.0, *)
struct RepeatIntent: AudioPlaybackIntent {
static var title: LocalizedStringResource = "Repeat"
init() {}
func perform() async throws -> some IntentResult { await IntentRouter.run(.repeatMode); return .result() }
}
@available(iOS 17.0, *)
struct FavouriteIntent: AudioPlaybackIntent {
static var title: LocalizedStringResource = "Favourite"
init() {}
func perform() async throws -> some IntentResult { await IntentRouter.run(.favourite); return .result() }
}