27 lines
845 B
Swift
27 lines
845 B
Swift
|
|
import ActivityKit
|
||
|
|
import Foundation
|
||
|
|
|
||
|
|
struct MusicActivityAttributes: ActivityAttributes {
|
||
|
|
struct ContentState: Codable, Hashable {
|
||
|
|
var title: String
|
||
|
|
var artist: String
|
||
|
|
var playing: Bool
|
||
|
|
var position: Double
|
||
|
|
var duration: Double
|
||
|
|
var updatedAt: Date
|
||
|
|
var connected: Bool
|
||
|
|
|
||
|
|
// Bound UTF-8, including pathological combining characters, below ActivityKit's 4 KB limit.
|
||
|
|
static func bounded(_ text: String) -> String {
|
||
|
|
var result = ""
|
||
|
|
for scalar in text.unicodeScalars {
|
||
|
|
guard result.utf8.count + scalar.utf8.count <= 256 else { break }
|
||
|
|
result.unicodeScalars.append(scalar)
|
||
|
|
}
|
||
|
|
return result
|
||
|
|
}
|
||
|
|
|
||
|
|
var progress: Double { duration > 0 ? min(1, max(0, position / duration)) : 0 }
|
||
|
|
}
|
||
|
|
}
|