96 lines
3.6 KiB
Swift
96 lines
3.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). AudioPlaybackIntent runs in the app's process.
|
|
|
|
enum MulberryCommand: String, AppEnum {
|
|
case toggle, next, previous, shuffle, repeatMode, favourite
|
|
|
|
static var typeDisplayRepresentation: TypeDisplayRepresentation { "Mulberry command" }
|
|
static var caseDisplayRepresentations: [MulberryCommand: DisplayRepresentation] {
|
|
[.toggle: "Play/Pause", .next: "Next", .previous: "Previous", .shuffle: "Shuffle", .repeatMode: "Repeat",
|
|
.favourite: "Favourite"]
|
|
}
|
|
}
|
|
|
|
@available(iOS 17.0, *)
|
|
struct MulberryControlIntent: AudioPlaybackIntent {
|
|
static var title: LocalizedStringResource = "Control Mulberry"
|
|
static var isDiscoverable = false
|
|
|
|
@Parameter(title: "Command") var command: MulberryCommand
|
|
|
|
init() { command = .toggle }
|
|
init(_ c: MulberryCommand) { command = c }
|
|
|
|
func perform() async throws -> some IntentResult {
|
|
let c = command
|
|
await MainActor.run { IntentRouter.handler?(c) }
|
|
return .result()
|
|
}
|
|
}
|
|
|
|
/// The app installs a handler at launch; inside the widget extension it stays nil.
|
|
enum IntentRouter {
|
|
@MainActor static var handler: ((MulberryCommand) -> Void)?
|
|
}
|