feat(mobile): add Siri and Shortcuts music intents
- Add native App Intents for playback, library actions, and music search - Bridge intents to the mobile player and Convex library
This commit is contained in:
17 files changed
+2988
-4
No files matched your search
@@ -0,0 +1,28 @@
|
||||
internal import ExpoAppIntents
|
||||
internal import ExpoModulesCore
|
||||
|
||||
/// Other files in this directory use a `.intent.swift` suffix. Expo's inline-module
|
||||
/// scan treats every plain `.swift` basename as a Module subclass, so only this file
|
||||
/// is registered. The rest are still compiled into the app target.
|
||||
enum MusicAppIntents {
|
||||
static func register() {
|
||||
Task {
|
||||
await AppIntentDispatcher.shared.setShortcutsRefreshHandler {
|
||||
AppShortcuts.updateAppShortcutParameters()
|
||||
}
|
||||
AppShortcuts.updateAppShortcutParameters()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Registered Expo inline module that wires app-target App Intents code to expo-app-intents.
|
||||
/// Do not change the name of this class.
|
||||
final class AppIntentsSetup: Module {
|
||||
public func definition() -> ExpoModulesCore.ModuleDefinition {
|
||||
Name("AppIntentsSetup")
|
||||
|
||||
OnCreate {
|
||||
MusicAppIntents.register()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
import AppIntents
|
||||
|
||||
/// Phrases compiled into the app. Each one includes the app name, and each
|
||||
/// phrase fills at most one parameter. Siri asks for anything a phrase leaves out.
|
||||
struct AppShortcuts: AppShortcutsProvider {
|
||||
static var appShortcuts: [AppShortcut] {
|
||||
AppShortcut(
|
||||
intent: PlayMusicIntent(),
|
||||
phrases: [
|
||||
"Play music in \(.applicationName)",
|
||||
"Play \(\.$song) in \(.applicationName)",
|
||||
"Play the album \(\.$album) in \(.applicationName)",
|
||||
"Play \(\.$artist) in \(.applicationName)",
|
||||
"Play the playlist \(\.$playlist) in \(.applicationName)",
|
||||
],
|
||||
shortTitle: "Play Music",
|
||||
systemImageName: "play.fill"
|
||||
)
|
||||
AppShortcut(
|
||||
intent: AddToPlaylistIntent(),
|
||||
phrases: [
|
||||
"Add to a playlist in \(.applicationName)",
|
||||
"Add \(\.$song) to a playlist in \(.applicationName)",
|
||||
],
|
||||
shortTitle: "Add to Playlist",
|
||||
systemImageName: "text.badge.plus"
|
||||
)
|
||||
AppShortcut(
|
||||
intent: AddToUpNextIntent(),
|
||||
phrases: [
|
||||
"Play \(\.$song) next in \(.applicationName)",
|
||||
"Add to Up Next in \(.applicationName)",
|
||||
],
|
||||
shortTitle: "Add to Up Next",
|
||||
systemImageName: "text.line.first.and.arrowtriangle.forward"
|
||||
)
|
||||
AppShortcut(
|
||||
intent: ClearUpNextIntent(),
|
||||
phrases: [
|
||||
"Clear Up Next in \(.applicationName)",
|
||||
],
|
||||
shortTitle: "Clear Up Next",
|
||||
systemImageName: "clear"
|
||||
)
|
||||
AppShortcut(
|
||||
intent: CreatePlaylistIntent(),
|
||||
phrases: [
|
||||
"Create a playlist in \(.applicationName)",
|
||||
],
|
||||
shortTitle: "Create Playlist",
|
||||
systemImageName: "music.note.list"
|
||||
)
|
||||
AppShortcut(
|
||||
intent: GetCurrentSongIntent(),
|
||||
phrases: [
|
||||
"What's playing in \(.applicationName)",
|
||||
],
|
||||
shortTitle: "Get Current Song",
|
||||
systemImageName: "music.note"
|
||||
)
|
||||
AppShortcut(
|
||||
intent: LoveSongIntent(),
|
||||
phrases: [
|
||||
"Love this song in \(.applicationName)",
|
||||
"Favorite this song in \(.applicationName)",
|
||||
"Favorite song in \(.applicationName)",
|
||||
],
|
||||
shortTitle: "Love Song",
|
||||
systemImageName: "heart.fill"
|
||||
)
|
||||
AppShortcut(
|
||||
intent: PauseMusicIntent(),
|
||||
phrases: [
|
||||
"Pause \(.applicationName)",
|
||||
],
|
||||
shortTitle: "Pause",
|
||||
systemImageName: "pause.fill"
|
||||
)
|
||||
AppShortcut(
|
||||
intent: ResumeMusicIntent(),
|
||||
phrases: [
|
||||
"Resume \(.applicationName)",
|
||||
],
|
||||
shortTitle: "Resume",
|
||||
systemImageName: "play.fill"
|
||||
)
|
||||
AppShortcut(
|
||||
intent: SkipMusicIntent(),
|
||||
phrases: [
|
||||
"Skip this song in \(.applicationName)",
|
||||
],
|
||||
shortTitle: "Skip",
|
||||
systemImageName: "forward.fill"
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,625 @@
|
||||
#if compiler(>=6.4)
|
||||
import AppIntents
|
||||
internal import ExpoAppIntents
|
||||
import Foundation
|
||||
import MediaIntents
|
||||
|
||||
/// Apple's audio schemas are what Siri and Shortcuts use for Music.app actions
|
||||
/// such as play, add to library, add to playlist, love, and create station.
|
||||
/// They require the iOS 27 SDK. Older compilers keep the custom intents in
|
||||
/// `MusicIntents.swift`.
|
||||
|
||||
@available(iOS 27.0, *)
|
||||
@AppEnum(schema: .audio.playbackAttributes)
|
||||
enum PlaybackAttributes: String {
|
||||
case shuffle
|
||||
case `repeat`
|
||||
|
||||
static let caseDisplayRepresentations: [PlaybackAttributes: DisplayRepresentation] = [
|
||||
.shuffle: "Shuffle",
|
||||
.`repeat`: "Repeat",
|
||||
]
|
||||
}
|
||||
|
||||
@available(iOS 27.0, *)
|
||||
@AppEnum(schema: .audio.queueInsertionLocation)
|
||||
enum QueueInsertionLocation: String {
|
||||
case next
|
||||
case tail
|
||||
|
||||
static let caseDisplayRepresentations: [QueueInsertionLocation: DisplayRepresentation] = [
|
||||
.next: "Play Next",
|
||||
.tail: "Play Last",
|
||||
]
|
||||
}
|
||||
|
||||
@available(iOS 27.0, *)
|
||||
@AppEnum(schema: .audio.affinityState)
|
||||
enum AffinityState: String {
|
||||
case like
|
||||
case dislike
|
||||
case unset
|
||||
|
||||
static let caseDisplayRepresentations: [AffinityState: DisplayRepresentation] = [
|
||||
.like: "Like",
|
||||
.dislike: "Dislike",
|
||||
.unset: "Unset",
|
||||
]
|
||||
}
|
||||
|
||||
@available(iOS 27.0, *)
|
||||
@UnionValue
|
||||
enum PlaylistOwner {
|
||||
case name(String)
|
||||
}
|
||||
|
||||
@available(iOS 27.0, *)
|
||||
@AppEntity(schema: .audio.artist)
|
||||
struct AudioArtistEntity {
|
||||
static let defaultQuery = AudioArtistQuery()
|
||||
|
||||
var id: String
|
||||
var name: String
|
||||
|
||||
var displayRepresentation: DisplayRepresentation {
|
||||
DisplayRepresentation(title: "\(name)")
|
||||
}
|
||||
|
||||
init(record: AppIntentEntityRecord) {
|
||||
id = record.id
|
||||
name = record.title
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 27.0, *)
|
||||
struct AudioArtistQuery: EntityStringQuery {
|
||||
func entities(for identifiers: [AudioArtistEntity.ID]) async throws -> [AudioArtistEntity] {
|
||||
try await MusicCatalog.records(ofKind: MusicCatalog.artistKind, matching: identifiers)
|
||||
.map(AudioArtistEntity.init(record:))
|
||||
}
|
||||
|
||||
func suggestedEntities() async throws -> [AudioArtistEntity] {
|
||||
let records = try await MusicCatalog.records(ofKind: MusicCatalog.artistKind)
|
||||
return MusicCatalog.suggested(records, limit: 25, favoritesFirst: true)
|
||||
.map(AudioArtistEntity.init(record:))
|
||||
}
|
||||
|
||||
func entities(matching string: String) async throws -> [AudioArtistEntity] {
|
||||
let records = try await MusicCatalog.records(ofKind: MusicCatalog.artistKind)
|
||||
if string.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
|
||||
return MusicCatalog.suggested(records, limit: 25, favoritesFirst: true)
|
||||
.map(AudioArtistEntity.init(record:))
|
||||
}
|
||||
return MusicCatalog.rank(records, query: string, limit: 50, score: MusicCatalog.nameScore)
|
||||
.map(AudioArtistEntity.init(record:))
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 27.0, *)
|
||||
extension AudioArtistQuery: IntentValueQuery {
|
||||
func values(for audioSearch: AudioSearch) async throws -> [AudioArtistEntity] {
|
||||
switch audioSearch.criteria {
|
||||
case .searchQuery(let query):
|
||||
return try await entities(matching: query)
|
||||
case .unspecified:
|
||||
return try await suggestedEntities()
|
||||
case .url(let urls):
|
||||
return try await artists(for: urls)
|
||||
@unknown default:
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
private func artists(for urls: [URL]) async throws -> [AudioArtistEntity] {
|
||||
let identifiers = urls.compactMap { url -> String? in
|
||||
guard case .artist(let id) = MusicCatalog.linkTarget(from: url) else {
|
||||
return nil
|
||||
}
|
||||
return id
|
||||
}
|
||||
return try await entities(for: identifiers)
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 27.0, *)
|
||||
@AppEntity(schema: .audio.album)
|
||||
struct AudioAlbumEntity {
|
||||
static let defaultQuery = AudioAlbumQuery()
|
||||
|
||||
var id: String
|
||||
var title: String
|
||||
var artistName: String
|
||||
var artists: [AudioArtistEntity]
|
||||
var universalProductCode: String?
|
||||
|
||||
var displayRepresentation: DisplayRepresentation {
|
||||
DisplayRepresentation(title: "\(title)", subtitle: "\(artistName)")
|
||||
}
|
||||
|
||||
init(record: AppIntentEntityRecord, artistsById: [String: AudioArtistEntity]) {
|
||||
id = record.id
|
||||
title = record.title
|
||||
artistName = record.metadata["artistName"] ?? ""
|
||||
universalProductCode = nil
|
||||
if let artist = artistsById[record.metadata["artistId"] ?? ""] {
|
||||
artists = [artist]
|
||||
} else {
|
||||
artists = []
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 27.0, *)
|
||||
struct AudioAlbumQuery: EntityStringQuery {
|
||||
func entities(for identifiers: [AudioAlbumEntity.ID]) async throws -> [AudioAlbumEntity] {
|
||||
let artistsById = try await Self.artistsById()
|
||||
return try await MusicCatalog.records(ofKind: MusicCatalog.albumKind, matching: identifiers)
|
||||
.map { AudioAlbumEntity(record: $0, artistsById: artistsById) }
|
||||
}
|
||||
|
||||
func suggestedEntities() async throws -> [AudioAlbumEntity] {
|
||||
let artistsById = try await Self.artistsById()
|
||||
let records = try await MusicCatalog.records(ofKind: MusicCatalog.albumKind)
|
||||
return MusicCatalog.suggested(records, limit: 25, favoritesFirst: true)
|
||||
.map { AudioAlbumEntity(record: $0, artistsById: artistsById) }
|
||||
}
|
||||
|
||||
func entities(matching string: String) async throws -> [AudioAlbumEntity] {
|
||||
let artistsById = try await Self.artistsById()
|
||||
let records = try await MusicCatalog.records(ofKind: MusicCatalog.albumKind)
|
||||
let matches = string.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
|
||||
? MusicCatalog.suggested(records, limit: 25, favoritesFirst: true)
|
||||
: MusicCatalog.rank(records, query: string, limit: 50, score: MusicCatalog.albumScore)
|
||||
return matches.map { AudioAlbumEntity(record: $0, artistsById: artistsById) }
|
||||
}
|
||||
|
||||
fileprivate static func artistsById() async throws -> [String: AudioArtistEntity] {
|
||||
let artists = try await MusicCatalog.records(ofKind: MusicCatalog.artistKind)
|
||||
.map(AudioArtistEntity.init(record:))
|
||||
return Dictionary(uniqueKeysWithValues: artists.map { ($0.id, $0) })
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 27.0, *)
|
||||
extension AudioAlbumQuery: IntentValueQuery {
|
||||
func values(for audioSearch: AudioSearch) async throws -> [AudioAlbumEntity] {
|
||||
switch audioSearch.criteria {
|
||||
case .searchQuery(let query):
|
||||
return try await entities(matching: query)
|
||||
case .unspecified:
|
||||
return try await suggestedEntities()
|
||||
case .url(let urls):
|
||||
let identifiers = urls.compactMap { url -> String? in
|
||||
guard case .album(let id) = MusicCatalog.linkTarget(from: url) else {
|
||||
return nil
|
||||
}
|
||||
return id
|
||||
}
|
||||
return try await entities(for: identifiers)
|
||||
@unknown default:
|
||||
return []
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 27.0, *)
|
||||
@AppEntity(schema: .audio.song)
|
||||
struct AudioSongEntity {
|
||||
static let defaultQuery = AudioSongQuery()
|
||||
|
||||
var id: String
|
||||
var title: String
|
||||
var artistName: String
|
||||
var composerName: String?
|
||||
var albumTitle: String?
|
||||
var artists: [AudioArtistEntity]
|
||||
var album: AudioAlbumEntity?
|
||||
var composers: [AudioArtistEntity]
|
||||
var internationalStandardRecordingCode: String?
|
||||
|
||||
var displayRepresentation: DisplayRepresentation {
|
||||
DisplayRepresentation(title: "\(title)", subtitle: "\(artistName)")
|
||||
}
|
||||
|
||||
init(
|
||||
record: AppIntentEntityRecord,
|
||||
artistsById: [String: AudioArtistEntity],
|
||||
albumsById: [String: AudioAlbumEntity]
|
||||
) {
|
||||
id = record.id
|
||||
title = record.title
|
||||
artistName = record.metadata["artistName"] ?? ""
|
||||
composerName = nil
|
||||
albumTitle = record.metadata["albumTitle"]
|
||||
composers = []
|
||||
internationalStandardRecordingCode = nil
|
||||
if let artist = artistsById[record.metadata["artistId"] ?? ""] {
|
||||
artists = [artist]
|
||||
} else {
|
||||
artists = []
|
||||
}
|
||||
album = albumsById[record.metadata["albumId"] ?? ""]
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 27.0, *)
|
||||
struct AudioSongQuery: EntityStringQuery {
|
||||
func entities(for identifiers: [AudioSongEntity.ID]) async throws -> [AudioSongEntity] {
|
||||
let context = try await AudioLibraryContext.load()
|
||||
return try await MusicCatalog.records(ofKind: MusicCatalog.songKind, matching: identifiers)
|
||||
.map { context.song(from: $0) }
|
||||
}
|
||||
|
||||
func suggestedEntities() async throws -> [AudioSongEntity] {
|
||||
let context = try await AudioLibraryContext.load()
|
||||
let records = try await MusicCatalog.records(ofKind: MusicCatalog.songKind)
|
||||
return MusicCatalog.suggested(records, limit: 25, favoritesFirst: true).map {
|
||||
context.song(from: $0)
|
||||
}
|
||||
}
|
||||
|
||||
func entities(matching string: String) async throws -> [AudioSongEntity] {
|
||||
let context = try await AudioLibraryContext.load()
|
||||
let records = try await MusicCatalog.records(ofKind: MusicCatalog.songKind)
|
||||
let matches = string.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
|
||||
? MusicCatalog.suggested(records, limit: 25, favoritesFirst: true)
|
||||
: MusicCatalog.rank(records, query: string, limit: 50, score: MusicCatalog.songScore)
|
||||
return matches.map { context.song(from: $0) }
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 27.0, *)
|
||||
extension AudioSongQuery: IntentValueQuery {
|
||||
func values(for audioSearch: AudioSearch) async throws -> [AudioSongEntity] {
|
||||
switch audioSearch.criteria {
|
||||
case .searchQuery(let query):
|
||||
return try await entities(matching: query)
|
||||
case .unspecified:
|
||||
return try await suggestedEntities()
|
||||
case .url(let urls):
|
||||
let context = try await AudioLibraryContext.load()
|
||||
var matches: [AppIntentEntityRecord] = []
|
||||
for url in urls {
|
||||
guard let target = MusicCatalog.linkTarget(from: url) else {
|
||||
continue
|
||||
}
|
||||
matches.append(contentsOf: try await MusicCatalog.songs(for: target))
|
||||
}
|
||||
return matches.map { context.song(from: $0) }
|
||||
@unknown default:
|
||||
return []
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 27.0, *)
|
||||
@AppEntity(schema: .audio.playlist)
|
||||
struct AudioPlaylistEntity {
|
||||
static let defaultQuery = AudioPlaylistQuery()
|
||||
|
||||
var id: String
|
||||
var title: String
|
||||
var owner: PlaylistOwner?
|
||||
var createdByMe: Bool?
|
||||
var curatedForMe: Bool?
|
||||
|
||||
var displayRepresentation: DisplayRepresentation {
|
||||
DisplayRepresentation(title: "\(title)")
|
||||
}
|
||||
|
||||
init(record: AppIntentEntityRecord) {
|
||||
id = record.id
|
||||
title = record.title
|
||||
let ownerName = record.metadata["ownerName"] ?? ""
|
||||
owner = ownerName.isEmpty ? nil : .name(ownerName)
|
||||
createdByMe = record.metadata["createdByMe"] == "1"
|
||||
curatedForMe = nil
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 27.0, *)
|
||||
struct AudioPlaylistQuery: EntityStringQuery {
|
||||
func entities(for identifiers: [AudioPlaylistEntity.ID]) async throws -> [AudioPlaylistEntity] {
|
||||
try await MusicCatalog.records(ofKind: MusicCatalog.playlistKind, matching: identifiers)
|
||||
.map(AudioPlaylistEntity.init(record:))
|
||||
}
|
||||
|
||||
func suggestedEntities() async throws -> [AudioPlaylistEntity] {
|
||||
try await MusicCatalog.records(ofKind: MusicCatalog.playlistKind)
|
||||
.map(AudioPlaylistEntity.init(record:))
|
||||
}
|
||||
|
||||
func entities(matching string: String) async throws -> [AudioPlaylistEntity] {
|
||||
let records = try await MusicCatalog.records(ofKind: MusicCatalog.playlistKind)
|
||||
if string.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
|
||||
return records.map(AudioPlaylistEntity.init(record:))
|
||||
}
|
||||
return MusicCatalog.rank(records, query: string, limit: 50, score: MusicCatalog.nameScore)
|
||||
.map(AudioPlaylistEntity.init(record:))
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 27.0, *)
|
||||
extension AudioPlaylistQuery: IntentValueQuery {
|
||||
func values(for audioSearch: AudioSearch) async throws -> [AudioPlaylistEntity] {
|
||||
switch audioSearch.criteria {
|
||||
case .searchQuery(let query):
|
||||
return try await entities(matching: query)
|
||||
case .unspecified:
|
||||
return try await suggestedEntities()
|
||||
case .url(let urls):
|
||||
let identifiers = urls.compactMap { url -> String? in
|
||||
guard case .playlist(let id) = MusicCatalog.linkTarget(from: url) else {
|
||||
return nil
|
||||
}
|
||||
return id
|
||||
}
|
||||
return try await entities(for: identifiers)
|
||||
@unknown default:
|
||||
return []
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 27.0, *)
|
||||
@AppEntity(schema: .audio.warmupAudioQueueResult)
|
||||
struct WarmupAudioQueueResultEntity {
|
||||
static let defaultQuery = WarmupAudioQueueResultQuery()
|
||||
|
||||
var id: String
|
||||
var title: String
|
||||
|
||||
var displayRepresentation: DisplayRepresentation {
|
||||
DisplayRepresentation(title: "\(title)")
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 27.0, *)
|
||||
struct WarmupAudioQueueResultQuery: EntityStringQuery {
|
||||
func entities(for identifiers: [WarmupAudioQueueResultEntity.ID]) async throws -> [WarmupAudioQueueResultEntity] {
|
||||
identifiers.compactMap { id in
|
||||
guard let entry = WarmupAudioStore.entry(id: id) else {
|
||||
return nil
|
||||
}
|
||||
return WarmupAudioQueueResultEntity(id: entry.id, title: entry.title)
|
||||
}
|
||||
}
|
||||
|
||||
func suggestedEntities() async throws -> [WarmupAudioQueueResultEntity] {
|
||||
WarmupAudioStore.all().map { WarmupAudioQueueResultEntity(id: $0.id, title: $0.title) }
|
||||
}
|
||||
|
||||
func entities(matching string: String) async throws -> [WarmupAudioQueueResultEntity] {
|
||||
let query = MusicCatalog.normalized(string)
|
||||
return WarmupAudioStore.all().compactMap { entry in
|
||||
if query.isEmpty || MusicCatalog.normalized(entry.title).contains(query) {
|
||||
return WarmupAudioQueueResultEntity(id: entry.id, title: entry.title)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 27.0, *)
|
||||
enum WarmupAudioStore {
|
||||
private static let defaultsKey = "otunes.warmupAudioQueue"
|
||||
|
||||
struct Entry: Codable {
|
||||
var id: String
|
||||
var title: String
|
||||
}
|
||||
|
||||
static func remember(id: String, title: String) {
|
||||
var entries = all()
|
||||
entries.removeAll { $0.id == id }
|
||||
entries.append(Entry(id: id, title: title))
|
||||
if entries.count > 20 {
|
||||
entries = Array(entries.suffix(20))
|
||||
}
|
||||
if let data = try? JSONEncoder().encode(entries) {
|
||||
UserDefaults.standard.set(data, forKey: defaultsKey)
|
||||
}
|
||||
}
|
||||
|
||||
static func entry(id: String) -> Entry? {
|
||||
all().first { $0.id == id }
|
||||
}
|
||||
|
||||
static func all() -> [Entry] {
|
||||
guard let data = UserDefaults.standard.data(forKey: defaultsKey),
|
||||
let entries = try? JSONDecoder().decode([Entry].self, from: data)
|
||||
else {
|
||||
return []
|
||||
}
|
||||
return entries
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 27.0, *)
|
||||
@UnionValue
|
||||
enum AudioItem {
|
||||
case song(AudioSongEntity)
|
||||
case album(AudioAlbumEntity)
|
||||
case artist(AudioArtistEntity)
|
||||
case playlist(AudioPlaylistEntity)
|
||||
|
||||
var kind: String {
|
||||
switch self {
|
||||
case .song:
|
||||
return "song"
|
||||
case .album:
|
||||
return "album"
|
||||
case .artist:
|
||||
return "artist"
|
||||
case .playlist:
|
||||
return "playlist"
|
||||
}
|
||||
}
|
||||
|
||||
var itemId: String {
|
||||
switch self {
|
||||
case .song(let song):
|
||||
return song.id
|
||||
case .album(let album):
|
||||
return album.id
|
||||
case .artist(let artist):
|
||||
return artist.id
|
||||
case .playlist(let playlist):
|
||||
return playlist.id
|
||||
}
|
||||
}
|
||||
|
||||
var itemTitle: String {
|
||||
switch self {
|
||||
case .song(let song):
|
||||
return song.title
|
||||
case .album(let album):
|
||||
return album.title
|
||||
case .artist(let artist):
|
||||
return artist.name
|
||||
case .playlist(let playlist):
|
||||
return playlist.title
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 27.0, *)
|
||||
private struct AudioLibraryContext {
|
||||
var artistsById: [String: AudioArtistEntity]
|
||||
var albumsById: [String: AudioAlbumEntity]
|
||||
|
||||
static func load() async throws -> AudioLibraryContext {
|
||||
let artistsById = try await AudioAlbumQuery.artistsById()
|
||||
let albums = try await MusicCatalog.records(ofKind: MusicCatalog.albumKind).map {
|
||||
AudioAlbumEntity(record: $0, artistsById: artistsById)
|
||||
}
|
||||
return AudioLibraryContext(
|
||||
artistsById: artistsById,
|
||||
albumsById: Dictionary(uniqueKeysWithValues: albums.map { ($0.id, $0) })
|
||||
)
|
||||
}
|
||||
|
||||
func song(from record: AppIntentEntityRecord) -> AudioSongEntity {
|
||||
AudioSongEntity(record: record, artistsById: artistsById, albumsById: albumsById)
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 27.0, *)
|
||||
@AppIntent(schema: .audio.playAudio)
|
||||
struct PlayAudioIntent: AudioStartingIntent {
|
||||
static let openAppWhenRun = false
|
||||
|
||||
var audioEntity: AudioItem
|
||||
var playbackAttributes: Set<PlaybackAttributes>
|
||||
var warmupAudioQueueResult: WarmupAudioQueueResultEntity?
|
||||
var queueLocation: QueueInsertionLocation?
|
||||
|
||||
@MainActor
|
||||
func perform() async throws -> some IntentResult & ProvidesDialog {
|
||||
let queue: String
|
||||
switch queueLocation {
|
||||
case .next:
|
||||
queue = "next"
|
||||
case .tail:
|
||||
queue = "tail"
|
||||
case nil:
|
||||
queue = "now"
|
||||
}
|
||||
await MusicCommands.play(
|
||||
kind: audioEntity.kind,
|
||||
id: audioEntity.itemId,
|
||||
title: audioEntity.itemTitle,
|
||||
shuffle: playbackAttributes.contains(.shuffle),
|
||||
repeatMode: playbackAttributes.contains(.`repeat`) ? "all" : "none",
|
||||
queue: queue,
|
||||
warmupId: warmupAudioQueueResult?.id
|
||||
)
|
||||
return .result(dialog: "Playing \(audioEntity.itemTitle).")
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 27.0, *)
|
||||
@AppIntent(schema: .audio.warmupAudioQueue)
|
||||
struct WarmupAudioQueueIntent {
|
||||
static let openAppWhenRun = false
|
||||
|
||||
var audioEntity: AudioItem
|
||||
var playbackAttributes: Set<PlaybackAttributes>
|
||||
|
||||
@MainActor
|
||||
func perform() async throws -> some IntentResult & ReturnsValue<WarmupAudioQueueResultEntity> {
|
||||
let id = await MusicCommands.warmup(
|
||||
kind: audioEntity.kind,
|
||||
id: audioEntity.itemId,
|
||||
title: audioEntity.itemTitle,
|
||||
shuffle: playbackAttributes.contains(.shuffle)
|
||||
)
|
||||
WarmupAudioStore.remember(id: id, title: audioEntity.itemTitle)
|
||||
let result = WarmupAudioQueueResultEntity(id: id, title: audioEntity.itemTitle)
|
||||
return .result(value: result)
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 27.0, *)
|
||||
@AppIntent(schema: .audio.addToPlaylist)
|
||||
struct AddAudioToPlaylistIntent {
|
||||
static let openAppWhenRun = false
|
||||
|
||||
var audioEntity: AudioItem
|
||||
var playlist: AudioPlaylistEntity
|
||||
|
||||
@MainActor
|
||||
func perform() async throws -> some IntentResult & ProvidesDialog {
|
||||
if audioEntity.kind == "playlist" && audioEntity.itemId == playlist.id {
|
||||
throw MusicIntentFailure("Choose a different playlist.")
|
||||
}
|
||||
await MusicCommands.addToPlaylist(
|
||||
playlistId: playlist.id,
|
||||
sources: [(kind: audioEntity.kind, id: audioEntity.itemId)]
|
||||
)
|
||||
return .result(dialog: "Adding to \(playlist.title).")
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 27.0, *)
|
||||
@AppIntent(schema: .audio.updateAudioAffinity)
|
||||
struct UpdateAudioAffinityIntent {
|
||||
static let openAppWhenRun = false
|
||||
|
||||
var affinityState: AffinityState
|
||||
var target: AudioItem
|
||||
|
||||
@MainActor
|
||||
func perform() async throws -> some IntentResult & ProvidesDialog {
|
||||
if affinityState == .dislike {
|
||||
throw MusicIntentFailure("oTunes doesn't keep dislikes. You can unlike it instead.")
|
||||
}
|
||||
if target.kind == "playlist" {
|
||||
throw MusicIntentFailure("oTunes can love songs, albums, and artists.")
|
||||
}
|
||||
let state = affinityState == .like ? "like" : "unset"
|
||||
await MusicCommands.updateAffinity(kind: target.kind, id: target.itemId, state: state)
|
||||
let verb = affinityState == .like ? "Loving" : "Unliking"
|
||||
return .result(dialog: "\(verb) \(target.itemTitle).")
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 27.0, *)
|
||||
@AppIntent(schema: .audio.createStation)
|
||||
struct CreateAudioStationIntent {
|
||||
static let openAppWhenRun = false
|
||||
|
||||
@MainActor
|
||||
func perform() async throws -> some IntentResult & ProvidesDialog {
|
||||
guard let song = try await MusicCatalog.currentSong() else {
|
||||
throw MusicIntentFailure("Nothing is playing in oTunes.")
|
||||
}
|
||||
if song.artistId.isEmpty {
|
||||
throw MusicIntentFailure("oTunes can't find an artist for this song.")
|
||||
}
|
||||
await MusicCommands.createStation(artistId: song.artistId, artistName: song.artistName)
|
||||
let artist = song.artistName.isEmpty ? "this artist" : song.artistName
|
||||
return .result(dialog: "Playing more by \(artist).")
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,345 @@
|
||||
import AppIntents
|
||||
internal import ExpoAppIntents
|
||||
import Foundation
|
||||
|
||||
struct MusicIntentFailure: Error, CustomLocalizedStringResourceConvertible {
|
||||
let localizedStringResource: LocalizedStringResource
|
||||
|
||||
init(_ message: String) {
|
||||
localizedStringResource = LocalizedStringResource(stringLiteral: message)
|
||||
}
|
||||
}
|
||||
|
||||
enum MusicLinkTarget {
|
||||
case song(String)
|
||||
case album(String)
|
||||
case artist(String)
|
||||
case playlist(String)
|
||||
}
|
||||
|
||||
/// Reads the catalogs JavaScript publishes with `setEntityCatalogAsync` and turns
|
||||
/// spoken requests into those records. The same kinds back Shortcuts pickers and,
|
||||
/// on iOS 27, the audio schemas Siri uses for Apple Music-style requests.
|
||||
enum MusicCatalog {
|
||||
static let songKind = "song"
|
||||
static let albumKind = "album"
|
||||
static let artistKind = "artist"
|
||||
static let playlistKind = "playlist"
|
||||
static let nowPlayingKind = "nowPlaying"
|
||||
/// Kept in sync with the catalog query and the JavaScript bridge.
|
||||
static let favoritesPlaylistId = "otunes:favorites"
|
||||
|
||||
static func records(ofKind kind: String) async throws -> [AppIntentEntityRecord] {
|
||||
try await AppIntentEntityStore.shared.entities(ofKind: kind)
|
||||
}
|
||||
|
||||
static func records(ofKind kind: String, matching identifiers: [String]) async throws -> [AppIntentEntityRecord] {
|
||||
try await AppIntentEntityStore.shared.entities(ofKind: kind, matching: identifiers)
|
||||
}
|
||||
|
||||
static func currentSong() async throws -> SongEntity? {
|
||||
guard let current = try await records(ofKind: nowPlayingKind).first else {
|
||||
return nil
|
||||
}
|
||||
if let song = try await SongQuery().entities(for: [current.id]).first {
|
||||
return song
|
||||
}
|
||||
return SongEntity(record: current)
|
||||
}
|
||||
|
||||
static func normalized(_ value: String) -> String {
|
||||
value
|
||||
.folding(options: [.caseInsensitive, .diacriticInsensitive], locale: .current)
|
||||
.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
}
|
||||
|
||||
static func cleanedQuery(_ query: String) -> String {
|
||||
var text = normalized(query)
|
||||
let prefixes = [
|
||||
"the song ", "the album ", "the artist ", "the playlist ",
|
||||
"songs by ", "music by ", "song ", "album ", "artist ", "playlist ", "some ",
|
||||
]
|
||||
var didStrip = true
|
||||
while didStrip {
|
||||
didStrip = false
|
||||
for prefix in prefixes where text.hasPrefix(prefix) {
|
||||
text = String(text.dropFirst(prefix.count))
|
||||
.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
didStrip = true
|
||||
}
|
||||
}
|
||||
return text
|
||||
}
|
||||
|
||||
static func rank(
|
||||
_ records: [AppIntentEntityRecord],
|
||||
query: String,
|
||||
limit: Int,
|
||||
score: (AppIntentEntityRecord, String) -> Int
|
||||
) -> [AppIntentEntityRecord] {
|
||||
let cleaned = cleanedQuery(query)
|
||||
guard !cleaned.isEmpty else {
|
||||
return []
|
||||
}
|
||||
return records
|
||||
.compactMap { record -> (AppIntentEntityRecord, Int)? in
|
||||
let value = score(record, cleaned)
|
||||
return value > 0 ? (record, value) : nil
|
||||
}
|
||||
.sorted { lhs, rhs in
|
||||
if lhs.1 != rhs.1 {
|
||||
return lhs.1 > rhs.1
|
||||
}
|
||||
return lhs.0.title.localizedCaseInsensitiveCompare(rhs.0.title) == .orderedAscending
|
||||
}
|
||||
.prefix(limit)
|
||||
.map(\.0)
|
||||
}
|
||||
|
||||
static func songScore(_ record: AppIntentEntityRecord, _ query: String) -> Int {
|
||||
let title = normalized(record.title)
|
||||
let artist = normalized(record.metadata["artistName"] ?? "")
|
||||
if title == query {
|
||||
return 100
|
||||
}
|
||||
if let range = query.range(of: " by ") {
|
||||
let titlePart = normalized(String(query[..<range.lowerBound]))
|
||||
let artistPart = normalized(String(query[range.upperBound...]))
|
||||
var score = 0
|
||||
if !titlePart.isEmpty && (title == titlePart || title.contains(titlePart)) {
|
||||
score += title == titlePart ? 70 : 50
|
||||
}
|
||||
if !artistPart.isEmpty && (artist == artistPart || artist.contains(artistPart)) {
|
||||
score += artist == artistPart ? 30 : 20
|
||||
}
|
||||
return score >= 50 ? score : 0
|
||||
}
|
||||
if title.contains(query) || (query.contains(title) && title.count > 2) {
|
||||
return title.contains(query) ? 80 : 60
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
static func albumScore(_ record: AppIntentEntityRecord, _ query: String) -> Int {
|
||||
let title = normalized(record.title)
|
||||
let artist = normalized(record.metadata["artistName"] ?? "")
|
||||
if title == query {
|
||||
return 100
|
||||
}
|
||||
if title.contains(query) || (query.contains(title) && title.count > 2) {
|
||||
return 80
|
||||
}
|
||||
if artist == query {
|
||||
return 45
|
||||
}
|
||||
if artist.contains(query) && query.count > 2 {
|
||||
return 30
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
static func nameScore(_ record: AppIntentEntityRecord, _ query: String) -> Int {
|
||||
let name = normalized(record.title)
|
||||
let synonyms = record.synonyms.map(normalized)
|
||||
if name == query || synonyms.contains(query) {
|
||||
return 100
|
||||
}
|
||||
if name.contains(query) || synonyms.contains(where: { $0.contains(query) }) {
|
||||
return 80
|
||||
}
|
||||
if query.contains(name) && name.count > 2 {
|
||||
return 60
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
static func suggested(
|
||||
_ records: [AppIntentEntityRecord],
|
||||
limit: Int,
|
||||
favoritesFirst: Bool
|
||||
) -> [AppIntentEntityRecord] {
|
||||
let pool: [AppIntentEntityRecord]
|
||||
if favoritesFirst {
|
||||
let favorites = records.filter { $0.metadata["favorite"] == "1" }
|
||||
pool = favorites.isEmpty ? records : favorites
|
||||
} else {
|
||||
pool = records
|
||||
}
|
||||
return Array(pool.prefix(limit))
|
||||
}
|
||||
|
||||
static func linkTarget(from url: URL) -> MusicLinkTarget? {
|
||||
let parts = url.path.split(separator: "/").map(String.init).filter { !$0.isEmpty }
|
||||
guard let kind = parts.first, parts.count >= 2 else {
|
||||
return nil
|
||||
}
|
||||
let id = parts[1]
|
||||
switch kind {
|
||||
case "album":
|
||||
if let songId = URLComponents(url: url, resolvingAgainstBaseURL: false)?
|
||||
.queryItems?
|
||||
.first(where: { $0.name == "s" })?
|
||||
.value,
|
||||
!songId.isEmpty
|
||||
{
|
||||
return .song(songId)
|
||||
}
|
||||
return .album(id)
|
||||
case "artist":
|
||||
return .artist(id)
|
||||
case "playlist":
|
||||
return .playlist(id == "favorites" ? favoritesPlaylistId : id)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
static func songs(for target: MusicLinkTarget) async throws -> [AppIntentEntityRecord] {
|
||||
let songs = try await records(ofKind: songKind)
|
||||
switch target {
|
||||
case .song(let id):
|
||||
return songs.filter { $0.id == id }
|
||||
case .album(let id):
|
||||
return Array(songs.filter { $0.metadata["albumId"] == id }.prefix(25))
|
||||
case .artist(let id):
|
||||
return Array(songs.filter { $0.metadata["artistId"] == id }.prefix(25))
|
||||
case .playlist:
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
static func formatDuration(_ seconds: Int) -> String {
|
||||
guard seconds > 0 else {
|
||||
return ""
|
||||
}
|
||||
return String(format: "%d:%02d", seconds / 60, seconds % 60)
|
||||
}
|
||||
}
|
||||
|
||||
enum MusicCommands {
|
||||
static func play(
|
||||
kind: String,
|
||||
id: String,
|
||||
title: String,
|
||||
shuffle: Bool?,
|
||||
repeatMode: String?,
|
||||
queue: String,
|
||||
warmupId: String?
|
||||
) async -> String {
|
||||
let params: AppIntentParams = [
|
||||
"kind": .string(kind),
|
||||
"id": .string(id),
|
||||
"title": .string(title),
|
||||
"queue": .string(queue),
|
||||
"shuffle": shuffle.map { .string($0 ? "songs" : "off") } ?? .null,
|
||||
"repeat": repeatMode.map(AppIntentValue.string) ?? .null,
|
||||
"warmupId": warmupId.map(AppIntentValue.string) ?? .null,
|
||||
]
|
||||
return await AppIntentDispatcher.shared.dispatch(name: "playMusic", params: params)
|
||||
}
|
||||
|
||||
static func warmup(kind: String, id: String, title: String, shuffle: Bool) async -> String {
|
||||
await AppIntentDispatcher.shared.dispatch(
|
||||
name: "warmupAudio",
|
||||
params: [
|
||||
"kind": .string(kind),
|
||||
"id": .string(id),
|
||||
"title": .string(title),
|
||||
"shuffle": .string(shuffle ? "songs" : "off"),
|
||||
]
|
||||
)
|
||||
}
|
||||
|
||||
static func addToPlaylist(playlistId: String, sources: [(kind: String, id: String)]) async {
|
||||
await AppIntentDispatcher.shared.dispatch(
|
||||
name: "addToPlaylist",
|
||||
params: [
|
||||
"playlistId": .string(playlistId),
|
||||
"sources": .array(sources.map { source in
|
||||
.object([
|
||||
"kind": .string(source.kind),
|
||||
"id": .string(source.id),
|
||||
])
|
||||
}),
|
||||
]
|
||||
)
|
||||
}
|
||||
|
||||
static func addToUpNext(
|
||||
position: String,
|
||||
sources: [(kind: String, id: String)]
|
||||
) async {
|
||||
await AppIntentDispatcher.shared.dispatch(
|
||||
name: "addToUpNext",
|
||||
params: [
|
||||
"position": .string(position),
|
||||
"sources": .array(sources.map { source in
|
||||
.object([
|
||||
"kind": .string(source.kind),
|
||||
"id": .string(source.id),
|
||||
])
|
||||
}),
|
||||
]
|
||||
)
|
||||
}
|
||||
|
||||
static func createPlaylist(name: String, sources: [(kind: String, id: String)]) async {
|
||||
await AppIntentDispatcher.shared.dispatch(
|
||||
name: "createPlaylist",
|
||||
params: [
|
||||
"name": .string(name),
|
||||
"sources": .array(sources.map { source in
|
||||
.object([
|
||||
"kind": .string(source.kind),
|
||||
"id": .string(source.id),
|
||||
])
|
||||
}),
|
||||
]
|
||||
)
|
||||
}
|
||||
|
||||
static func clearUpNext() async {
|
||||
await AppIntentDispatcher.shared.dispatch(name: "clearUpNext")
|
||||
}
|
||||
|
||||
static func createStation(artistId: String, artistName: String) async {
|
||||
await AppIntentDispatcher.shared.dispatch(
|
||||
name: "createStation",
|
||||
params: [
|
||||
"artistId": .string(artistId),
|
||||
"title": .string(artistName),
|
||||
]
|
||||
)
|
||||
}
|
||||
|
||||
static func updateAffinity(kind: String, id: String, state: String) async {
|
||||
await AppIntentDispatcher.shared.dispatch(
|
||||
name: "updateAffinity",
|
||||
params: [
|
||||
"kind": .string(kind),
|
||||
"id": .string(id),
|
||||
"state": .string(state),
|
||||
]
|
||||
)
|
||||
}
|
||||
|
||||
static func transport(_ command: String) async {
|
||||
await AppIntentDispatcher.shared.dispatch(
|
||||
name: "transport",
|
||||
params: ["command": .string(command)]
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
enum MusicIntentDiscovery {
|
||||
/// Schema actions replace these Shortcuts entries on iOS 27. Older systems,
|
||||
/// and builds made without the iOS 27 SDK, keep the custom actions visible.
|
||||
static var hidesSchemaDuplicate: Bool {
|
||||
#if compiler(>=6.4)
|
||||
if #available(iOS 27.0, *) {
|
||||
return true
|
||||
}
|
||||
#endif
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,301 @@
|
||||
import AppIntents
|
||||
internal import ExpoAppIntents
|
||||
import Foundation
|
||||
|
||||
struct SongEntity: AppEntity {
|
||||
static let typeDisplayRepresentation = TypeDisplayRepresentation(name: "Song")
|
||||
static let defaultQuery = SongQuery()
|
||||
|
||||
var id: String
|
||||
var title: String
|
||||
var artistName: String
|
||||
var artistId: String
|
||||
var albumTitle: String
|
||||
var albumId: String
|
||||
var durationSeconds: Int
|
||||
var genres: String
|
||||
var year: String
|
||||
var trackNumber: String
|
||||
var discNumber: String
|
||||
|
||||
var displayRepresentation: DisplayRepresentation {
|
||||
DisplayRepresentation(title: "\(title)", subtitle: "\(artistName)")
|
||||
}
|
||||
|
||||
init(record: AppIntentEntityRecord) {
|
||||
id = record.id
|
||||
title = record.title
|
||||
artistName = record.metadata["artistName"] ?? record.subtitle ?? ""
|
||||
artistId = record.metadata["artistId"] ?? ""
|
||||
albumTitle = record.metadata["albumTitle"] ?? ""
|
||||
albumId = record.metadata["albumId"] ?? ""
|
||||
durationSeconds = Int(record.metadata["duration"] ?? "") ?? 0
|
||||
genres = record.metadata["genres"] ?? ""
|
||||
year = record.metadata["year"] ?? ""
|
||||
trackNumber = record.metadata["track"] ?? ""
|
||||
discNumber = record.metadata["disk"] ?? ""
|
||||
}
|
||||
}
|
||||
|
||||
struct SongQuery: EntityStringQuery {
|
||||
func entities(for identifiers: [String]) async throws -> [SongEntity] {
|
||||
try await MusicCatalog.records(ofKind: MusicCatalog.songKind, matching: identifiers)
|
||||
.map(SongEntity.init(record:))
|
||||
}
|
||||
|
||||
func suggestedEntities() async throws -> [SongEntity] {
|
||||
let records = try await MusicCatalog.records(ofKind: MusicCatalog.songKind)
|
||||
return MusicCatalog.suggested(records, limit: 25, favoritesFirst: true)
|
||||
.map(SongEntity.init(record:))
|
||||
}
|
||||
|
||||
func entities(matching string: String) async throws -> [SongEntity] {
|
||||
let records = try await MusicCatalog.records(ofKind: MusicCatalog.songKind)
|
||||
let query = string.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
if query.isEmpty {
|
||||
return MusicCatalog.suggested(records, limit: 25, favoritesFirst: true)
|
||||
.map(SongEntity.init(record:))
|
||||
}
|
||||
return MusicCatalog.rank(records, query: query, limit: 50, score: MusicCatalog.songScore)
|
||||
.map(SongEntity.init(record:))
|
||||
}
|
||||
}
|
||||
|
||||
struct AlbumEntity: AppEntity {
|
||||
static let typeDisplayRepresentation = TypeDisplayRepresentation(name: "Album")
|
||||
static let defaultQuery = AlbumQuery()
|
||||
|
||||
var id: String
|
||||
var title: String
|
||||
var artistName: String
|
||||
var artistId: String
|
||||
var year: String
|
||||
|
||||
var displayRepresentation: DisplayRepresentation {
|
||||
DisplayRepresentation(title: "\(title)", subtitle: "\(artistName)")
|
||||
}
|
||||
|
||||
init(record: AppIntentEntityRecord) {
|
||||
id = record.id
|
||||
title = record.title
|
||||
artistName = record.metadata["artistName"] ?? record.subtitle ?? ""
|
||||
artistId = record.metadata["artistId"] ?? ""
|
||||
year = record.metadata["year"] ?? ""
|
||||
}
|
||||
}
|
||||
|
||||
struct AlbumQuery: EntityStringQuery {
|
||||
func entities(for identifiers: [String]) async throws -> [AlbumEntity] {
|
||||
try await MusicCatalog.records(ofKind: MusicCatalog.albumKind, matching: identifiers)
|
||||
.map(AlbumEntity.init(record:))
|
||||
}
|
||||
|
||||
func suggestedEntities() async throws -> [AlbumEntity] {
|
||||
let records = try await MusicCatalog.records(ofKind: MusicCatalog.albumKind)
|
||||
return MusicCatalog.suggested(records, limit: 25, favoritesFirst: true)
|
||||
.map(AlbumEntity.init(record:))
|
||||
}
|
||||
|
||||
func entities(matching string: String) async throws -> [AlbumEntity] {
|
||||
let records = try await MusicCatalog.records(ofKind: MusicCatalog.albumKind)
|
||||
let query = string.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
if query.isEmpty {
|
||||
return MusicCatalog.suggested(records, limit: 25, favoritesFirst: true)
|
||||
.map(AlbumEntity.init(record:))
|
||||
}
|
||||
return MusicCatalog.rank(records, query: query, limit: 50, score: MusicCatalog.albumScore)
|
||||
.map(AlbumEntity.init(record:))
|
||||
}
|
||||
}
|
||||
|
||||
struct ArtistEntity: AppEntity {
|
||||
static let typeDisplayRepresentation = TypeDisplayRepresentation(name: "Artist")
|
||||
static let defaultQuery = ArtistQuery()
|
||||
|
||||
var id: String
|
||||
var name: String
|
||||
|
||||
var displayRepresentation: DisplayRepresentation {
|
||||
DisplayRepresentation(title: "\(name)")
|
||||
}
|
||||
|
||||
init(record: AppIntentEntityRecord) {
|
||||
id = record.id
|
||||
name = record.title
|
||||
}
|
||||
}
|
||||
|
||||
struct ArtistQuery: EntityStringQuery {
|
||||
func entities(for identifiers: [String]) async throws -> [ArtistEntity] {
|
||||
try await MusicCatalog.records(ofKind: MusicCatalog.artistKind, matching: identifiers)
|
||||
.map(ArtistEntity.init(record:))
|
||||
}
|
||||
|
||||
func suggestedEntities() async throws -> [ArtistEntity] {
|
||||
let records = try await MusicCatalog.records(ofKind: MusicCatalog.artistKind)
|
||||
return MusicCatalog.suggested(records, limit: 25, favoritesFirst: true)
|
||||
.map(ArtistEntity.init(record:))
|
||||
}
|
||||
|
||||
func entities(matching string: String) async throws -> [ArtistEntity] {
|
||||
let records = try await MusicCatalog.records(ofKind: MusicCatalog.artistKind)
|
||||
let query = string.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
if query.isEmpty {
|
||||
return MusicCatalog.suggested(records, limit: 25, favoritesFirst: true)
|
||||
.map(ArtistEntity.init(record:))
|
||||
}
|
||||
return MusicCatalog.rank(records, query: query, limit: 50, score: MusicCatalog.nameScore)
|
||||
.map(ArtistEntity.init(record:))
|
||||
}
|
||||
}
|
||||
|
||||
struct PlaylistEntity: AppEntity {
|
||||
static let typeDisplayRepresentation = TypeDisplayRepresentation(name: "Playlist")
|
||||
static let defaultQuery = PlaylistQuery()
|
||||
|
||||
var id: String
|
||||
var title: String
|
||||
var ownerName: String
|
||||
var createdByMe: Bool
|
||||
|
||||
var displayRepresentation: DisplayRepresentation {
|
||||
if ownerName.isEmpty {
|
||||
return DisplayRepresentation(title: "\(title)")
|
||||
}
|
||||
return DisplayRepresentation(title: "\(title)", subtitle: "\(ownerName)")
|
||||
}
|
||||
|
||||
init(record: AppIntentEntityRecord) {
|
||||
id = record.id
|
||||
title = record.title
|
||||
ownerName = record.metadata["ownerName"] ?? record.subtitle ?? ""
|
||||
createdByMe = record.metadata["createdByMe"] == "1"
|
||||
}
|
||||
}
|
||||
|
||||
struct PlaylistQuery: EntityStringQuery {
|
||||
func entities(for identifiers: [String]) async throws -> [PlaylistEntity] {
|
||||
try await MusicCatalog.records(ofKind: MusicCatalog.playlistKind, matching: identifiers)
|
||||
.map(PlaylistEntity.init(record:))
|
||||
}
|
||||
|
||||
func suggestedEntities() async throws -> [PlaylistEntity] {
|
||||
try await MusicCatalog.records(ofKind: MusicCatalog.playlistKind)
|
||||
.map(PlaylistEntity.init(record:))
|
||||
}
|
||||
|
||||
func entities(matching string: String) async throws -> [PlaylistEntity] {
|
||||
let records = try await MusicCatalog.records(ofKind: MusicCatalog.playlistKind)
|
||||
let query = string.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
if query.isEmpty {
|
||||
return records.map(PlaylistEntity.init(record:))
|
||||
}
|
||||
return MusicCatalog.rank(records, query: query, limit: 50, score: MusicCatalog.nameScore)
|
||||
.map(PlaylistEntity.init(record:))
|
||||
}
|
||||
}
|
||||
|
||||
enum MusicShuffle: String, AppEnum {
|
||||
case off
|
||||
case songs
|
||||
|
||||
static let typeDisplayRepresentation = TypeDisplayRepresentation(name: "Shuffle")
|
||||
static let caseDisplayRepresentations: [MusicShuffle: DisplayRepresentation] = [
|
||||
.off: "Off",
|
||||
.songs: "Songs",
|
||||
]
|
||||
}
|
||||
|
||||
enum MusicRepeat: String, AppEnum {
|
||||
case none
|
||||
case one
|
||||
case all
|
||||
|
||||
static let typeDisplayRepresentation = TypeDisplayRepresentation(name: "Repeat")
|
||||
static let caseDisplayRepresentations: [MusicRepeat: DisplayRepresentation] = [
|
||||
.none: "None",
|
||||
.one: "One",
|
||||
.all: "All",
|
||||
]
|
||||
}
|
||||
|
||||
enum UpNextPosition: String, AppEnum {
|
||||
case next
|
||||
case last
|
||||
|
||||
static let typeDisplayRepresentation = TypeDisplayRepresentation(name: "Position")
|
||||
static let caseDisplayRepresentations: [UpNextPosition: DisplayRepresentation] = [
|
||||
.next: "Play Next",
|
||||
.last: "Play Last",
|
||||
]
|
||||
}
|
||||
|
||||
enum MusicDetail: String, AppEnum {
|
||||
case title
|
||||
case artist
|
||||
case album
|
||||
case albumArtist
|
||||
case genre
|
||||
case duration
|
||||
case year
|
||||
case trackNumber
|
||||
case discNumber
|
||||
|
||||
static let typeDisplayRepresentation = TypeDisplayRepresentation(name: "Detail")
|
||||
static let caseDisplayRepresentations: [MusicDetail: DisplayRepresentation] = [
|
||||
.title: "Title",
|
||||
.artist: "Artist",
|
||||
.album: "Album",
|
||||
.albumArtist: "Album Artist",
|
||||
.genre: "Genre",
|
||||
.duration: "Duration",
|
||||
.year: "Year",
|
||||
.trackNumber: "Track Number",
|
||||
.discNumber: "Disc Number",
|
||||
]
|
||||
}
|
||||
|
||||
enum MusicSelection {
|
||||
static func sources(
|
||||
song: SongEntity?,
|
||||
album: AlbumEntity?,
|
||||
artist: ArtistEntity?,
|
||||
playlist: PlaylistEntity?
|
||||
) throws -> [(kind: String, id: String, title: String)] {
|
||||
var sources: [(kind: String, id: String, title: String)] = []
|
||||
if let song {
|
||||
sources.append(("song", song.id, song.title))
|
||||
}
|
||||
if let album {
|
||||
sources.append(("album", album.id, album.title))
|
||||
}
|
||||
if let artist {
|
||||
sources.append(("artist", artist.id, artist.name))
|
||||
}
|
||||
if let playlist {
|
||||
sources.append(("playlist", playlist.id, playlist.title))
|
||||
}
|
||||
if sources.isEmpty {
|
||||
throw MusicIntentFailure("Choose a song, album, artist, or playlist.")
|
||||
}
|
||||
return sources
|
||||
}
|
||||
|
||||
static func singleTarget(
|
||||
song: SongEntity?,
|
||||
album: AlbumEntity?,
|
||||
artist: ArtistEntity?,
|
||||
playlist: PlaylistEntity?
|
||||
) throws -> (kind: String, id: String, title: String)? {
|
||||
let sources = [
|
||||
song.map { (kind: "song", id: $0.id, title: $0.title) },
|
||||
album.map { (kind: "album", id: $0.id, title: $0.title) },
|
||||
artist.map { (kind: "artist", id: $0.id, title: $0.name) },
|
||||
playlist.map { (kind: "playlist", id: $0.id, title: $0.title) },
|
||||
].compactMap { $0 }
|
||||
if sources.count > 1 {
|
||||
throw MusicIntentFailure("Choose one song, album, artist, or playlist.")
|
||||
}
|
||||
return sources.first
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,428 @@
|
||||
import AppIntents
|
||||
import Foundation
|
||||
|
||||
// Apple Music runs these actions in the background. Leaving openAppWhenRun false
|
||||
// keeps oTunes behind Siri and Shortcuts instead of taking the screen.
|
||||
|
||||
struct PlayMusicIntent: AppIntent {
|
||||
static let title: LocalizedStringResource = "Play Music"
|
||||
static let description = IntentDescription("Plays music using oTunes.")
|
||||
static let openAppWhenRun = false
|
||||
static var isDiscoverable: Bool { !MusicIntentDiscovery.hidesSchemaDuplicate }
|
||||
|
||||
@Parameter(title: "Song")
|
||||
var song: SongEntity?
|
||||
|
||||
@Parameter(title: "Album")
|
||||
var album: AlbumEntity?
|
||||
|
||||
@Parameter(title: "Artist")
|
||||
var artist: ArtistEntity?
|
||||
|
||||
@Parameter(title: "Playlist")
|
||||
var playlist: PlaylistEntity?
|
||||
|
||||
@Parameter(title: "Shuffle")
|
||||
var shuffle: MusicShuffle?
|
||||
|
||||
@Parameter(title: "Repeat")
|
||||
var repeatMode: MusicRepeat?
|
||||
|
||||
@MainActor
|
||||
func perform() async throws -> some IntentResult & ProvidesDialog {
|
||||
let target = try MusicSelection.singleTarget(
|
||||
song: song,
|
||||
album: album,
|
||||
artist: artist,
|
||||
playlist: playlist
|
||||
)
|
||||
let kind = target?.0 ?? "resume"
|
||||
let id = target?.1 ?? ""
|
||||
let name = target?.2 ?? "your music"
|
||||
await MusicCommands.play(
|
||||
kind: kind,
|
||||
id: id,
|
||||
title: name,
|
||||
shuffle: shuffle.map { $0 == .songs },
|
||||
repeatMode: repeatMode?.rawValue,
|
||||
queue: "now",
|
||||
warmupId: nil
|
||||
)
|
||||
return .result(dialog: "Playing \(name).")
|
||||
}
|
||||
}
|
||||
|
||||
struct AddToPlaylistIntent: AppIntent {
|
||||
static let title: LocalizedStringResource = "Add to Playlist"
|
||||
static let description = IntentDescription(
|
||||
"Adds the items passed as input to the specified playlist."
|
||||
)
|
||||
static let openAppWhenRun = false
|
||||
static var isDiscoverable: Bool { !MusicIntentDiscovery.hidesSchemaDuplicate }
|
||||
|
||||
@Parameter(title: "Song")
|
||||
var song: SongEntity?
|
||||
|
||||
@Parameter(title: "Album")
|
||||
var album: AlbumEntity?
|
||||
|
||||
@Parameter(title: "Artist")
|
||||
var artist: ArtistEntity?
|
||||
|
||||
@Parameter(title: "Music")
|
||||
var sourcePlaylist: PlaylistEntity?
|
||||
|
||||
@Parameter(title: "Playlist")
|
||||
var playlist: PlaylistEntity
|
||||
|
||||
@MainActor
|
||||
func perform() async throws -> some IntentResult & ProvidesDialog {
|
||||
let sources = try MusicSelection.sources(
|
||||
song: song,
|
||||
album: album,
|
||||
artist: artist,
|
||||
playlist: sourcePlaylist
|
||||
)
|
||||
if sources.contains(where: { $0.id == playlist.id && $0.kind == "playlist" }) {
|
||||
throw MusicIntentFailure("Choose a different playlist.")
|
||||
}
|
||||
await MusicCommands.addToPlaylist(
|
||||
playlistId: playlist.id,
|
||||
sources: sources.map { (kind: $0.kind, id: $0.id) }
|
||||
)
|
||||
return .result(dialog: "Adding to \(playlist.title).")
|
||||
}
|
||||
}
|
||||
|
||||
struct AddToUpNextIntent: AppIntent {
|
||||
static let title: LocalizedStringResource = "Add to Up Next"
|
||||
static let description = IntentDescription(
|
||||
"Adds the music passed as input to your Up Next queue."
|
||||
)
|
||||
static let openAppWhenRun = false
|
||||
|
||||
@Parameter(title: "Song")
|
||||
var song: SongEntity?
|
||||
|
||||
@Parameter(title: "Album")
|
||||
var album: AlbumEntity?
|
||||
|
||||
@Parameter(title: "Artist")
|
||||
var artist: ArtistEntity?
|
||||
|
||||
@Parameter(title: "Playlist")
|
||||
var playlist: PlaylistEntity?
|
||||
|
||||
@Parameter(title: "Position", default: .next)
|
||||
var position: UpNextPosition
|
||||
|
||||
@MainActor
|
||||
func perform() async throws -> some IntentResult & ProvidesDialog {
|
||||
let sources = try MusicSelection.sources(
|
||||
song: song,
|
||||
album: album,
|
||||
artist: artist,
|
||||
playlist: playlist
|
||||
)
|
||||
await MusicCommands.addToUpNext(
|
||||
position: position.rawValue,
|
||||
sources: sources.map { (kind: $0.kind, id: $0.id) }
|
||||
)
|
||||
let placement = position == .next ? "Playing this next." : "Adding this to the end of Up Next."
|
||||
return .result(dialog: "\(placement)")
|
||||
}
|
||||
}
|
||||
|
||||
struct ClearUpNextIntent: AppIntent {
|
||||
static let title: LocalizedStringResource = "Clear Up Next"
|
||||
static let description = IntentDescription("Clears the Up Next queue.")
|
||||
static let openAppWhenRun = false
|
||||
|
||||
@MainActor
|
||||
func perform() async throws -> some IntentResult & ProvidesDialog {
|
||||
await MusicCommands.clearUpNext()
|
||||
return .result(dialog: "Clearing Up Next.")
|
||||
}
|
||||
}
|
||||
|
||||
struct CreatePlaylistIntent: AppIntent {
|
||||
static let title: LocalizedStringResource = "Create Playlist"
|
||||
static let description = IntentDescription(
|
||||
"Creates a new playlist, adding any items passed as input."
|
||||
)
|
||||
static let openAppWhenRun = false
|
||||
|
||||
@Parameter(title: "Name")
|
||||
var name: String
|
||||
|
||||
@Parameter(title: "Song")
|
||||
var song: SongEntity?
|
||||
|
||||
@Parameter(title: "Album")
|
||||
var album: AlbumEntity?
|
||||
|
||||
@Parameter(title: "Artist")
|
||||
var artist: ArtistEntity?
|
||||
|
||||
@Parameter(title: "Playlist")
|
||||
var playlist: PlaylistEntity?
|
||||
|
||||
@MainActor
|
||||
func perform() async throws -> some IntentResult & ProvidesDialog {
|
||||
let trimmed = name.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
if trimmed.isEmpty {
|
||||
throw MusicIntentFailure("A playlist needs a name.")
|
||||
}
|
||||
let sources = [
|
||||
song.map { (kind: "song", id: $0.id) },
|
||||
album.map { (kind: "album", id: $0.id) },
|
||||
artist.map { (kind: "artist", id: $0.id) },
|
||||
playlist.map { (kind: "playlist", id: $0.id) },
|
||||
].compactMap { $0 }
|
||||
await MusicCommands.createPlaylist(name: trimmed, sources: sources)
|
||||
return .result(dialog: "Creating \(trimmed).")
|
||||
}
|
||||
}
|
||||
|
||||
struct GetCurrentSongIntent: AppIntent {
|
||||
static let title: LocalizedStringResource = "Get Current Song"
|
||||
static let description = IntentDescription(
|
||||
"Returns the song that is currently playing in oTunes, if any."
|
||||
)
|
||||
static let openAppWhenRun = false
|
||||
|
||||
@MainActor
|
||||
func perform() async throws -> some IntentResult & ReturnsValue<SongEntity> & ProvidesDialog {
|
||||
guard let song = try await MusicCatalog.currentSong() else {
|
||||
throw MusicIntentFailure("Nothing is playing in oTunes.")
|
||||
}
|
||||
return .result(value: song, dialog: "\(song.title)")
|
||||
}
|
||||
}
|
||||
|
||||
struct GetDetailsOfMusicIntent: AppIntent {
|
||||
static let title: LocalizedStringResource = "Get Details of Music"
|
||||
static let description = IntentDescription(
|
||||
"Gets a specific piece of information from the music passed into the action."
|
||||
)
|
||||
static let openAppWhenRun = false
|
||||
|
||||
@Parameter(title: "Song")
|
||||
var song: SongEntity?
|
||||
|
||||
@Parameter(title: "Album")
|
||||
var album: AlbumEntity?
|
||||
|
||||
@Parameter(title: "Artist")
|
||||
var artist: ArtistEntity?
|
||||
|
||||
@Parameter(title: "Playlist")
|
||||
var playlist: PlaylistEntity?
|
||||
|
||||
@Parameter(title: "Detail")
|
||||
var detail: MusicDetail
|
||||
|
||||
@MainActor
|
||||
func perform() async throws -> some IntentResult & ReturnsValue<String> & ProvidesDialog {
|
||||
let value = try await detailValue()
|
||||
return .result(value: value, dialog: value.isEmpty ? "Not available." : "\(value)")
|
||||
}
|
||||
|
||||
private func detailValue() async throws -> String {
|
||||
if let target = try MusicSelection.singleTarget(
|
||||
song: song,
|
||||
album: album,
|
||||
artist: artist,
|
||||
playlist: playlist
|
||||
) {
|
||||
return try value(for: target)
|
||||
}
|
||||
guard let current = try await MusicCatalog.currentSong() else {
|
||||
throw MusicIntentFailure("Choose music, or play a song in oTunes first.")
|
||||
}
|
||||
return Self.value(detail, song: current)
|
||||
}
|
||||
|
||||
private func value(for target: (kind: String, id: String, title: String)) throws -> String {
|
||||
switch target.kind {
|
||||
case "song":
|
||||
guard let song else {
|
||||
throw MusicIntentFailure("That song is no longer in your library.")
|
||||
}
|
||||
return Self.value(detail, song: song)
|
||||
case "album":
|
||||
guard let album else {
|
||||
throw MusicIntentFailure("That album is no longer in your library.")
|
||||
}
|
||||
return Self.value(detail, album: album)
|
||||
case "artist":
|
||||
guard let artist else {
|
||||
throw MusicIntentFailure("That artist is no longer in your library.")
|
||||
}
|
||||
return Self.value(detail, artist: artist)
|
||||
case "playlist":
|
||||
guard let playlist else {
|
||||
throw MusicIntentFailure("That playlist is no longer in your library.")
|
||||
}
|
||||
return Self.value(detail, playlist: playlist)
|
||||
default:
|
||||
throw MusicIntentFailure("oTunes can't read that.")
|
||||
}
|
||||
}
|
||||
|
||||
private static func value(_ detail: MusicDetail, song: SongEntity) -> String {
|
||||
switch detail {
|
||||
case .title:
|
||||
return song.title
|
||||
case .artist, .albumArtist:
|
||||
return song.artistName
|
||||
case .album:
|
||||
return song.albumTitle
|
||||
case .genre:
|
||||
return song.genres
|
||||
case .duration:
|
||||
return MusicCatalog.formatDuration(song.durationSeconds)
|
||||
case .year:
|
||||
return song.year == "0" ? "" : song.year
|
||||
case .trackNumber:
|
||||
return song.trackNumber
|
||||
case .discNumber:
|
||||
return song.discNumber
|
||||
}
|
||||
}
|
||||
|
||||
private static func value(_ detail: MusicDetail, album: AlbumEntity) -> String {
|
||||
switch detail {
|
||||
case .title, .album:
|
||||
return album.title
|
||||
case .artist, .albumArtist:
|
||||
return album.artistName
|
||||
case .year:
|
||||
return album.year == "0" ? "" : album.year
|
||||
case .genre, .duration, .trackNumber, .discNumber:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
private static func value(_ detail: MusicDetail, artist: ArtistEntity) -> String {
|
||||
switch detail {
|
||||
case .title, .artist, .albumArtist:
|
||||
return artist.name
|
||||
case .album, .genre, .duration, .year, .trackNumber, .discNumber:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
private static func value(_ detail: MusicDetail, playlist: PlaylistEntity) -> String {
|
||||
switch detail {
|
||||
case .title:
|
||||
return playlist.title
|
||||
case .artist, .albumArtist:
|
||||
return playlist.ownerName
|
||||
case .album, .genre, .duration, .year, .trackNumber, .discNumber:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct LoveSongIntent: AppIntent {
|
||||
static let title: LocalizedStringResource = "Love Song"
|
||||
static let description = IntentDescription(
|
||||
"Loves the song that is playing in oTunes. Also used for Favorite Song."
|
||||
)
|
||||
static let openAppWhenRun = false
|
||||
|
||||
@MainActor
|
||||
func perform() async throws -> some IntentResult & ProvidesDialog {
|
||||
guard let song = try await MusicCatalog.currentSong() else {
|
||||
throw MusicIntentFailure("Nothing is playing in oTunes.")
|
||||
}
|
||||
await MusicCommands.updateAffinity(kind: "song", id: song.id, state: "like")
|
||||
return .result(dialog: "Loving \(song.title).")
|
||||
}
|
||||
}
|
||||
|
||||
struct UnlikeSongIntent: AppIntent {
|
||||
static let title: LocalizedStringResource = "Unlike Song"
|
||||
static let description = IntentDescription("Removes the love from the song that is playing.")
|
||||
static let openAppWhenRun = false
|
||||
|
||||
@MainActor
|
||||
func perform() async throws -> some IntentResult & ProvidesDialog {
|
||||
guard let song = try await MusicCatalog.currentSong() else {
|
||||
throw MusicIntentFailure("Nothing is playing in oTunes.")
|
||||
}
|
||||
await MusicCommands.updateAffinity(kind: "song", id: song.id, state: "unset")
|
||||
return .result(dialog: "Unliking \(song.title).")
|
||||
}
|
||||
}
|
||||
|
||||
struct CreateStationIntent: AppIntent {
|
||||
static let title: LocalizedStringResource = "Create Station"
|
||||
static let description = IntentDescription(
|
||||
"Plays more music by the artist of the song that is playing."
|
||||
)
|
||||
static let openAppWhenRun = false
|
||||
static var isDiscoverable: Bool { !MusicIntentDiscovery.hidesSchemaDuplicate }
|
||||
|
||||
@MainActor
|
||||
func perform() async throws -> some IntentResult & ProvidesDialog {
|
||||
guard let song = try await MusicCatalog.currentSong() else {
|
||||
throw MusicIntentFailure("Nothing is playing in oTunes.")
|
||||
}
|
||||
if song.artistId.isEmpty {
|
||||
throw MusicIntentFailure("oTunes can't find an artist for this song.")
|
||||
}
|
||||
await MusicCommands.createStation(artistId: song.artistId, artistName: song.artistName)
|
||||
let artist = song.artistName.isEmpty ? "this artist" : song.artistName
|
||||
return .result(dialog: "Playing more by \(artist).")
|
||||
}
|
||||
}
|
||||
|
||||
struct PauseMusicIntent: AppIntent {
|
||||
static let title: LocalizedStringResource = "Pause Music"
|
||||
static let description = IntentDescription("Pauses playback in oTunes.")
|
||||
static let openAppWhenRun = false
|
||||
|
||||
@MainActor
|
||||
func perform() async throws -> some IntentResult & ProvidesDialog {
|
||||
await MusicCommands.transport("pause")
|
||||
return .result(dialog: "Pausing.")
|
||||
}
|
||||
}
|
||||
|
||||
struct ResumeMusicIntent: AppIntent {
|
||||
static let title: LocalizedStringResource = "Resume Music"
|
||||
static let description = IntentDescription("Resumes playback in oTunes.")
|
||||
static let openAppWhenRun = false
|
||||
|
||||
@MainActor
|
||||
func perform() async throws -> some IntentResult & ProvidesDialog {
|
||||
await MusicCommands.transport("resume")
|
||||
return .result(dialog: "Resuming.")
|
||||
}
|
||||
}
|
||||
|
||||
struct SkipMusicIntent: AppIntent {
|
||||
static let title: LocalizedStringResource = "Skip Song"
|
||||
static let description = IntentDescription("Skips to the next song in oTunes.")
|
||||
static let openAppWhenRun = false
|
||||
|
||||
@MainActor
|
||||
func perform() async throws -> some IntentResult & ProvidesDialog {
|
||||
await MusicCommands.transport("next")
|
||||
return .result(dialog: "Skipping.")
|
||||
}
|
||||
}
|
||||
|
||||
struct PreviousTrackIntent: AppIntent {
|
||||
static let title: LocalizedStringResource = "Previous Song"
|
||||
static let description = IntentDescription("Goes back to the previous song in oTunes.")
|
||||
static let openAppWhenRun = false
|
||||
|
||||
@MainActor
|
||||
func perform() async throws -> some IntentResult & ProvidesDialog {
|
||||
await MusicCommands.transport("previous")
|
||||
return .result(dialog: "Going back.")
|
||||
}
|
||||
}
|
||||
@@ -65,10 +65,15 @@
|
||||
}
|
||||
],
|
||||
"expo-image",
|
||||
"expo-status-bar"
|
||||
"expo-status-bar",
|
||||
"expo-app-intents",
|
||||
"./plugins/with-app-intent-sources.js"
|
||||
],
|
||||
"experiments": {
|
||||
"typedRoutes": true
|
||||
"typedRoutes": true,
|
||||
"inlineModules": {
|
||||
"watchedDirectories": ["app-intents"]
|
||||
}
|
||||
},
|
||||
"extra": {
|
||||
"router": {},
|
||||
|
||||
@@ -7,6 +7,7 @@ import { Redirect, Stack } from "expo-router"
|
||||
import { Platform } from "react-native"
|
||||
import { useEffect } from "react"
|
||||
|
||||
import { AppIntentBridge } from "@/components/app-intent-bridge"
|
||||
import { ConvexAuthRecovery } from "@/components/convex-auth-recovery"
|
||||
import { LoadingView } from "@/components/ui/primitives"
|
||||
import { AddToPlaylistProvider } from "@/contexts/add-to-playlist"
|
||||
@@ -68,6 +69,7 @@ export default function AppLayout() {
|
||||
return (
|
||||
<ConvexAuthRecovery onInvalidate={invalidateSession}>
|
||||
<PlaybackProvider>
|
||||
<AppIntentBridge />
|
||||
<AppNavigationSync />
|
||||
<AddToPlaylistProvider>
|
||||
<FavoritesProvider>
|
||||
@@ -77,7 +79,8 @@ export default function AppLayout() {
|
||||
name="settings"
|
||||
options={{
|
||||
headerShown: false,
|
||||
presentation: Platform.OS === "ios" ? "modal" : "fullScreenModal",
|
||||
presentation:
|
||||
Platform.OS === "ios" ? "modal" : "fullScreenModal",
|
||||
animation: "slide_from_bottom",
|
||||
gestureEnabled: true,
|
||||
gestureDirection: "vertical",
|
||||
|
||||
@@ -0,0 +1,708 @@
|
||||
import { useConvex, useQuery } from "convex/react"
|
||||
import * as AppIntents from "expo-app-intents"
|
||||
import type { AppIntentEntity, AppIntentInvocation } from "expo-app-intents"
|
||||
import { useEffect, useMemo, useRef, useSyncExternalStore } from "react"
|
||||
|
||||
import {
|
||||
ANONYMOUS_CACHE_SCOPE,
|
||||
useCacheScope,
|
||||
} from "@/contexts/persistent-cache"
|
||||
import { usePlayback } from "@/contexts/playback"
|
||||
import {
|
||||
FAVORITES_PLAYLIST_ID,
|
||||
loadHandledIntentIds,
|
||||
rememberHandledIntentId,
|
||||
} from "@/lib/app-intent-handled"
|
||||
import { showError } from "@/lib/toast"
|
||||
import { fetchCachedQuery } from "@otunes/convex/client/cached-query"
|
||||
import { getPlaybackStore } from "@otunes/convex/client/playback-store"
|
||||
import type { RepeatMode } from "@otunes/convex/client/playback-queue"
|
||||
import { api } from "@otunes/convex/_generated/api"
|
||||
import type { Id } from "@otunes/convex/_generated/dataModel"
|
||||
|
||||
const warmedAudioIds = new Set<string>()
|
||||
|
||||
class IntentRejected extends Error {}
|
||||
|
||||
const KNOWN_INTENTS = new Set([
|
||||
"playMusic",
|
||||
"warmupAudio",
|
||||
"addToPlaylist",
|
||||
"addToUpNext",
|
||||
"clearUpNext",
|
||||
"createPlaylist",
|
||||
"createStation",
|
||||
"updateAffinity",
|
||||
"transport",
|
||||
])
|
||||
|
||||
type MusicSource = {
|
||||
kind: string
|
||||
id: string
|
||||
}
|
||||
|
||||
function readString(value: unknown) {
|
||||
return typeof value === "string" && value.trim() ? value : null
|
||||
}
|
||||
|
||||
function readSources(value: unknown): MusicSource[] {
|
||||
if (!Array.isArray(value)) {
|
||||
return []
|
||||
}
|
||||
return value.flatMap((entry) => {
|
||||
if (!entry || typeof entry !== "object") {
|
||||
return []
|
||||
}
|
||||
const source = entry as { kind?: unknown; id?: unknown }
|
||||
if (typeof source.kind !== "string" || typeof source.id !== "string") {
|
||||
return []
|
||||
}
|
||||
if (!source.kind || !source.id) {
|
||||
return []
|
||||
}
|
||||
return [{ kind: source.kind, id: source.id }]
|
||||
})
|
||||
}
|
||||
|
||||
function shuffleChoice(value: unknown, fallback: boolean) {
|
||||
if (value === "songs") return true
|
||||
if (value === "off") return false
|
||||
if (value == null) return fallback
|
||||
throw new IntentRejected("Shuffle must be Off or Songs.")
|
||||
}
|
||||
|
||||
function repeatChoice(value: unknown): RepeatMode | null {
|
||||
if (value == null) return null
|
||||
if (value === "none") return "none"
|
||||
if (value === "one") return "once"
|
||||
if (value === "all") return "always"
|
||||
throw new IntentRejected("Repeat must be None, One, or All.")
|
||||
}
|
||||
|
||||
function isTransient(error: unknown) {
|
||||
const message = (
|
||||
error instanceof Error ? error.message : String(error)
|
||||
).toLowerCase()
|
||||
return (
|
||||
message.includes("not authenticated") ||
|
||||
message.includes("network") ||
|
||||
message.includes("fetch") ||
|
||||
message.includes("connection") ||
|
||||
message.includes("timed out") ||
|
||||
message.includes("timeout")
|
||||
)
|
||||
}
|
||||
|
||||
function songEntity(song: {
|
||||
id: string
|
||||
title: string
|
||||
artistName: string
|
||||
artistId: string
|
||||
albumTitle: string
|
||||
albumId: string
|
||||
durationSeconds: number
|
||||
genres?: string
|
||||
year?: number
|
||||
track?: number
|
||||
diskNumber?: number | null
|
||||
favorite?: boolean
|
||||
}): AppIntentEntity {
|
||||
return {
|
||||
id: song.id,
|
||||
title: song.title,
|
||||
subtitle: song.artistName,
|
||||
synonyms: song.artistName ? [song.artistName] : undefined,
|
||||
metadata: {
|
||||
artistName: song.artistName,
|
||||
artistId: song.artistId,
|
||||
albumTitle: song.albumTitle,
|
||||
albumId: song.albumId,
|
||||
duration: String(song.durationSeconds),
|
||||
genres: song.genres ?? "",
|
||||
year: song.year == null ? "" : String(song.year),
|
||||
track: song.track == null ? "" : String(song.track),
|
||||
disk: song.diskNumber == null ? "" : String(song.diskNumber),
|
||||
favorite: song.favorite ? "1" : "0",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
export function AppIntentBridge() {
|
||||
const convex = useConvex()
|
||||
const cacheScope = useCacheScope()
|
||||
const playback = usePlayback()
|
||||
const catalog = useQuery(api.library.intentCatalog, {})
|
||||
const store = useMemo(
|
||||
() =>
|
||||
getPlaybackStore(
|
||||
cacheScope === ANONYMOUS_CACHE_SCOPE ? null : cacheScope
|
||||
),
|
||||
[cacheScope]
|
||||
)
|
||||
const snapshot = useSyncExternalStore(store.subscribe, store.getSnapshot)
|
||||
const handledIds = useRef<Set<string> | null>(null)
|
||||
const publishChain = useRef(Promise.resolve())
|
||||
const publishGeneration = useRef(0)
|
||||
const playbackRef = useRef(playback)
|
||||
playbackRef.current = playback
|
||||
|
||||
useEffect(() => {
|
||||
if (!catalog || !AppIntents.isAvailable()) {
|
||||
return
|
||||
}
|
||||
const generation = ++publishGeneration.current
|
||||
const snapshotCatalog = catalog
|
||||
publishChain.current = publishChain.current
|
||||
.catch(() => undefined)
|
||||
.then(async () => {
|
||||
if (generation !== publishGeneration.current) {
|
||||
return
|
||||
}
|
||||
const writes = [
|
||||
AppIntents.setEntityCatalogAsync(
|
||||
"song",
|
||||
snapshotCatalog.songs.map((song) =>
|
||||
songEntity({
|
||||
...song,
|
||||
artistName: song.artistName,
|
||||
})
|
||||
)
|
||||
),
|
||||
AppIntents.setEntityCatalogAsync(
|
||||
"album",
|
||||
snapshotCatalog.albums.map((album) => ({
|
||||
id: album.id,
|
||||
title: album.title,
|
||||
subtitle: album.artistName,
|
||||
synonyms: album.artistName ? [album.artistName] : undefined,
|
||||
metadata: {
|
||||
artistName: album.artistName,
|
||||
artistId: album.artistId,
|
||||
year: String(album.year),
|
||||
favorite: album.favorite ? "1" : "0",
|
||||
},
|
||||
}))
|
||||
),
|
||||
AppIntents.setEntityCatalogAsync(
|
||||
"artist",
|
||||
snapshotCatalog.artists.map((artist) => ({
|
||||
id: artist.id,
|
||||
title: artist.name,
|
||||
metadata: { favorite: artist.favorite ? "1" : "0" },
|
||||
}))
|
||||
),
|
||||
]
|
||||
if (snapshotCatalog.authenticated) {
|
||||
writes.push(
|
||||
AppIntents.setEntityCatalogAsync(
|
||||
"playlist",
|
||||
snapshotCatalog.playlists.map((playlist) => ({
|
||||
id: playlist.id,
|
||||
title: playlist.title,
|
||||
subtitle: playlist.ownerName,
|
||||
synonyms:
|
||||
playlist.id === FAVORITES_PLAYLIST_ID
|
||||
? [
|
||||
"favorites",
|
||||
"favourite songs",
|
||||
"liked songs",
|
||||
"loved songs",
|
||||
]
|
||||
: undefined,
|
||||
metadata: {
|
||||
ownerName: playlist.ownerName,
|
||||
createdByMe: playlist.createdByMe ? "1" : "0",
|
||||
},
|
||||
}))
|
||||
)
|
||||
)
|
||||
}
|
||||
await Promise.all(writes)
|
||||
})
|
||||
.catch((error) => {
|
||||
console.warn("Could not publish the oTunes music catalog.", error)
|
||||
})
|
||||
}, [catalog])
|
||||
|
||||
const currentSongId = playback.queueSongIds[0] ?? null
|
||||
const currentSong = playback.currentSong
|
||||
|
||||
useEffect(() => {
|
||||
if (!snapshot.hydrated || !AppIntents.isAvailable()) {
|
||||
return
|
||||
}
|
||||
if (!currentSongId) {
|
||||
void AppIntents.setEntityCatalogAsync("nowPlaying", []).catch((error) => {
|
||||
console.warn("Could not clear the current song.", error)
|
||||
})
|
||||
return
|
||||
}
|
||||
if (!currentSong || currentSong.id !== currentSongId) {
|
||||
return
|
||||
}
|
||||
void AppIntents.setEntityCatalogAsync("nowPlaying", [
|
||||
songEntity({
|
||||
id: currentSong.id,
|
||||
title: currentSong.title,
|
||||
artistName: currentSong.artist.name,
|
||||
artistId: currentSong.artist.id,
|
||||
albumTitle: currentSong.album.title,
|
||||
albumId: currentSong.album.id,
|
||||
durationSeconds: currentSong.durationSeconds,
|
||||
}),
|
||||
]).catch((error) => {
|
||||
console.warn("Could not publish the current song.", error)
|
||||
})
|
||||
}, [currentSong, currentSongId, snapshot.hydrated])
|
||||
|
||||
AppIntents.useAppIntents(async (pendingIntents) => {
|
||||
if (!handledIds.current) {
|
||||
handledIds.current = await loadHandledIntentIds()
|
||||
}
|
||||
const handled = handledIds.current
|
||||
|
||||
for (const invocation of pendingIntents) {
|
||||
if (handled.has(invocation.id)) {
|
||||
await AppIntents.removePendingInvocationAsync(invocation.id)
|
||||
continue
|
||||
}
|
||||
if (!KNOWN_INTENTS.has(invocation.name)) {
|
||||
await AppIntents.removePendingInvocationAsync(invocation.id)
|
||||
continue
|
||||
}
|
||||
try {
|
||||
await performInvocation(invocation)
|
||||
await rememberHandledIntentId(handled, invocation.id)
|
||||
await AppIntents.removePendingInvocationAsync(invocation.id)
|
||||
} catch (error) {
|
||||
if (error instanceof IntentRejected || !isTransient(error)) {
|
||||
const message =
|
||||
error instanceof Error ? error.message : "oTunes couldn't do that."
|
||||
if (!(error instanceof IntentRejected) || message) {
|
||||
showError(message)
|
||||
}
|
||||
try {
|
||||
await rememberHandledIntentId(handled, invocation.id)
|
||||
await AppIntents.removePendingInvocationAsync(invocation.id)
|
||||
} catch (removeError) {
|
||||
console.warn(
|
||||
"App Intent remains pending",
|
||||
invocation.id,
|
||||
removeError
|
||||
)
|
||||
}
|
||||
continue
|
||||
}
|
||||
console.warn("App Intent remains pending", invocation.id, error)
|
||||
}
|
||||
}
|
||||
|
||||
async function songIdsFor(kind: string, id: string) {
|
||||
if (kind === "song") {
|
||||
return [id as Id<"songs">]
|
||||
}
|
||||
if (kind === "album") {
|
||||
return fetchCachedQuery(
|
||||
convex,
|
||||
cacheScope,
|
||||
api.playlists.getAlbumSongIds,
|
||||
{
|
||||
albumId: id as Id<"albums">,
|
||||
}
|
||||
)
|
||||
}
|
||||
if (kind === "artist") {
|
||||
return fetchCachedQuery(
|
||||
convex,
|
||||
cacheScope,
|
||||
api.playlists.getOrderedArtistSongIds,
|
||||
{ artistId: id as Id<"artists"> }
|
||||
)
|
||||
}
|
||||
if (kind === "playlist") {
|
||||
if (id === FAVORITES_PLAYLIST_ID) {
|
||||
const favorites = await fetchCachedQuery(
|
||||
convex,
|
||||
cacheScope,
|
||||
api.favorites.getUserFavorites,
|
||||
{}
|
||||
)
|
||||
return favorites.favoriteSongs
|
||||
}
|
||||
return fetchCachedQuery(
|
||||
convex,
|
||||
cacheScope,
|
||||
api.playlists.getPlaylistSongIds,
|
||||
{ playlistId: id as Id<"playlists"> }
|
||||
)
|
||||
}
|
||||
throw new IntentRejected("Choose a song, album, artist, or playlist.")
|
||||
}
|
||||
|
||||
async function uniqueSongIds(sources: MusicSource[]) {
|
||||
const ids: Id<"songs">[] = []
|
||||
for (const source of sources) {
|
||||
ids.push(...(await songIdsFor(source.kind, source.id)))
|
||||
}
|
||||
return [...new Set(ids)]
|
||||
}
|
||||
|
||||
async function performInvocation(invocation: AppIntentInvocation) {
|
||||
const actions = playbackRef.current
|
||||
switch (invocation.name) {
|
||||
case "playMusic":
|
||||
await playMusic(invocation)
|
||||
return
|
||||
case "warmupAudio":
|
||||
await warmup(invocation)
|
||||
return
|
||||
case "addToPlaylist":
|
||||
await addToPlaylist(invocation)
|
||||
return
|
||||
case "addToUpNext":
|
||||
await addToUpNext(invocation)
|
||||
return
|
||||
case "clearUpNext":
|
||||
await actions.clearUpNext()
|
||||
return
|
||||
case "createPlaylist":
|
||||
await createPlaylist(invocation)
|
||||
return
|
||||
case "createStation":
|
||||
await createStation(invocation)
|
||||
return
|
||||
case "updateAffinity":
|
||||
await updateAffinity(invocation)
|
||||
return
|
||||
case "transport":
|
||||
await transport(invocation)
|
||||
return
|
||||
default:
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
async function playMusic(invocation: AppIntentInvocation) {
|
||||
const actions = playbackRef.current
|
||||
const queue = readString(invocation.params.queue) ?? "now"
|
||||
const warmupId = readString(invocation.params.warmupId)
|
||||
const repeat = repeatChoice(invocation.params.repeat)
|
||||
if (repeat) {
|
||||
await actions.setRepeatMode(repeat)
|
||||
}
|
||||
|
||||
if (queue === "next" || queue === "tail") {
|
||||
const kind = readString(invocation.params.kind)
|
||||
const id = readString(invocation.params.id)
|
||||
if (!kind || !id) {
|
||||
throw new IntentRejected("Choose music to add to Up Next.")
|
||||
}
|
||||
const shuffled = shuffleChoice(
|
||||
invocation.params.shuffle,
|
||||
actions.shuffleEnabled
|
||||
)
|
||||
if (shuffled !== actions.shuffleEnabled) {
|
||||
await actions.setShuffleEnabled(shuffled)
|
||||
}
|
||||
const ids = await songIdsFor(kind, id)
|
||||
if (ids.length === 0) {
|
||||
throw new IntentRejected("Nothing to add to Up Next.")
|
||||
}
|
||||
if (queue === "next") {
|
||||
await actions.addToQueueNext(ids)
|
||||
} else {
|
||||
await actions.addToQueueLast(ids)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if (warmupId && warmedAudioIds.has(warmupId)) {
|
||||
const shuffled = shuffleChoice(
|
||||
invocation.params.shuffle,
|
||||
actions.shuffleEnabled
|
||||
)
|
||||
if (shuffled !== actions.shuffleEnabled) {
|
||||
await actions.setShuffleEnabled(shuffled)
|
||||
}
|
||||
actions.setIsPlaying(true)
|
||||
return
|
||||
}
|
||||
|
||||
const kind = readString(invocation.params.kind) ?? "resume"
|
||||
if (kind === "resume") {
|
||||
const shuffled = shuffleChoice(
|
||||
invocation.params.shuffle,
|
||||
actions.shuffleEnabled
|
||||
)
|
||||
if (shuffled !== actions.shuffleEnabled) {
|
||||
await actions.setShuffleEnabled(shuffled)
|
||||
}
|
||||
if (actions.queueSongIds.length === 0) {
|
||||
const favorites = await fetchCachedQuery(
|
||||
convex,
|
||||
cacheScope,
|
||||
api.favorites.getUserFavorites,
|
||||
{}
|
||||
)
|
||||
if (favorites.favoriteSongs.length === 0) {
|
||||
throw new IntentRejected(
|
||||
"Nothing is queued, and your favorites are empty."
|
||||
)
|
||||
}
|
||||
if (shuffled) {
|
||||
await actions.playShuffled(favorites.favoriteSongs)
|
||||
} else {
|
||||
await actions.playInOrder(favorites.favoriteSongs)
|
||||
}
|
||||
return
|
||||
}
|
||||
actions.setIsPlaying(true)
|
||||
return
|
||||
}
|
||||
|
||||
const id = readString(invocation.params.id)
|
||||
if (!id) {
|
||||
throw new IntentRejected("Choose music to play.")
|
||||
}
|
||||
const shuffled = shuffleChoice(
|
||||
invocation.params.shuffle,
|
||||
actions.shuffleEnabled
|
||||
)
|
||||
if (kind === "song") {
|
||||
const song = await fetchCachedQuery(
|
||||
convex,
|
||||
cacheScope,
|
||||
api.library.getSong,
|
||||
{
|
||||
id: id as Id<"songs">,
|
||||
}
|
||||
)
|
||||
if (!song) {
|
||||
throw new IntentRejected("That song is no longer in your library.")
|
||||
}
|
||||
const albumSongIds = await fetchCachedQuery(
|
||||
convex,
|
||||
cacheScope,
|
||||
api.playlists.getAlbumSongIds,
|
||||
{ albumId: song.album }
|
||||
)
|
||||
await actions.startFromSong(
|
||||
song._id,
|
||||
albumSongIds.length > 0 ? albumSongIds : [song._id],
|
||||
shuffled,
|
||||
true
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
const ids = await songIdsFor(kind, id)
|
||||
if (ids.length === 0) {
|
||||
throw new IntentRejected("Nothing to play.")
|
||||
}
|
||||
if (shuffled) {
|
||||
await actions.playShuffled(ids)
|
||||
} else {
|
||||
await actions.playInOrder(ids)
|
||||
}
|
||||
}
|
||||
|
||||
async function warmup(invocation: AppIntentInvocation) {
|
||||
const kind = readString(invocation.params.kind)
|
||||
const id = readString(invocation.params.id)
|
||||
if (!kind || !id) {
|
||||
throw new IntentRejected("Choose music to get ready.")
|
||||
}
|
||||
const shuffled = shuffleChoice(invocation.params.shuffle, false)
|
||||
if (kind === "song") {
|
||||
const song = await fetchCachedQuery(
|
||||
convex,
|
||||
cacheScope,
|
||||
api.library.getSong,
|
||||
{
|
||||
id: id as Id<"songs">,
|
||||
}
|
||||
)
|
||||
if (!song) {
|
||||
throw new IntentRejected("That song is no longer in your library.")
|
||||
}
|
||||
const albumSongIds = await fetchCachedQuery(
|
||||
convex,
|
||||
cacheScope,
|
||||
api.playlists.getAlbumSongIds,
|
||||
{ albumId: song.album }
|
||||
)
|
||||
await playbackRef.current.startFromSong(
|
||||
song._id,
|
||||
albumSongIds.length > 0 ? albumSongIds : [song._id],
|
||||
shuffled,
|
||||
false
|
||||
)
|
||||
} else {
|
||||
const ids = await songIdsFor(kind, id)
|
||||
if (ids.length === 0) {
|
||||
throw new IntentRejected("Nothing to get ready.")
|
||||
}
|
||||
await playbackRef.current.prepareQueue(ids, shuffled)
|
||||
}
|
||||
warmedAudioIds.add(invocation.id)
|
||||
}
|
||||
|
||||
async function addToPlaylist(invocation: AppIntentInvocation) {
|
||||
const playlistId = readString(invocation.params.playlistId)
|
||||
const sources = readSources(invocation.params.sources)
|
||||
if (!playlistId || sources.length === 0) {
|
||||
throw new IntentRejected("Choose music and a playlist.")
|
||||
}
|
||||
const songIds = await uniqueSongIds(sources)
|
||||
if (songIds.length === 0) {
|
||||
throw new IntentRejected("Those items have no songs to add.")
|
||||
}
|
||||
if (playlistId === FAVORITES_PLAYLIST_ID) {
|
||||
for (const source of sources) {
|
||||
if (source.kind === "song") {
|
||||
await convex.mutation(api.favorites.favoriteSong, {
|
||||
songId: source.id as Id<"songs">,
|
||||
})
|
||||
} else if (source.kind === "album") {
|
||||
await convex.mutation(api.favorites.favoriteAlbum, {
|
||||
albumId: source.id as Id<"albums">,
|
||||
})
|
||||
} else if (source.kind === "artist") {
|
||||
await convex.mutation(api.favorites.favoriteArtist, {
|
||||
artistId: source.id as Id<"artists">,
|
||||
})
|
||||
} else if (source.kind === "playlist") {
|
||||
const ids = await songIdsFor(source.kind, source.id)
|
||||
if (ids.length > 0) {
|
||||
await convex.mutation(api.favorites.favoriteSongs, {
|
||||
songIds: ids,
|
||||
})
|
||||
}
|
||||
} else {
|
||||
throw new IntentRejected(
|
||||
"Choose a song, album, artist, or playlist."
|
||||
)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
await convex.mutation(api.playlists.addSongsToPlaylist, {
|
||||
playlistId: playlistId as Id<"playlists">,
|
||||
songIds,
|
||||
})
|
||||
}
|
||||
|
||||
async function addToUpNext(invocation: AppIntentInvocation) {
|
||||
const sources = readSources(invocation.params.sources)
|
||||
if (sources.length === 0) {
|
||||
throw new IntentRejected("Choose music to add to Up Next.")
|
||||
}
|
||||
const ids = await uniqueSongIds(sources)
|
||||
if (ids.length === 0) {
|
||||
throw new IntentRejected("Nothing to add to Up Next.")
|
||||
}
|
||||
const position =
|
||||
readString(invocation.params.position) === "last" ? "last" : "next"
|
||||
if (position === "last") {
|
||||
await playbackRef.current.addToQueueLast(ids)
|
||||
} else {
|
||||
await playbackRef.current.addToQueueNext(ids)
|
||||
}
|
||||
}
|
||||
|
||||
async function createPlaylist(invocation: AppIntentInvocation) {
|
||||
const name = readString(invocation.params.name)
|
||||
if (!name) {
|
||||
throw new IntentRejected("A playlist needs a name.")
|
||||
}
|
||||
const songIds = await uniqueSongIds(
|
||||
readSources(invocation.params.sources)
|
||||
)
|
||||
await convex.mutation(api.playlists.createPlaylist, { name, songIds })
|
||||
}
|
||||
|
||||
async function createStation(invocation: AppIntentInvocation) {
|
||||
const artistId = readString(invocation.params.artistId)
|
||||
if (!artistId) {
|
||||
throw new IntentRejected("Nothing is playing in oTunes.")
|
||||
}
|
||||
await playbackRef.current.playArtistShuffled(artistId as Id<"artists">)
|
||||
}
|
||||
|
||||
async function updateAffinity(invocation: AppIntentInvocation) {
|
||||
const kind = readString(invocation.params.kind)
|
||||
const id = readString(invocation.params.id)
|
||||
const state = readString(invocation.params.state)
|
||||
if (!kind || !id || (state !== "like" && state !== "unset")) {
|
||||
throw new IntentRejected(
|
||||
"oTunes can love or unlike a song, album, or artist."
|
||||
)
|
||||
}
|
||||
if (kind === "playlist") {
|
||||
throw new IntentRejected("oTunes can love songs, albums, and artists.")
|
||||
}
|
||||
if (state === "like") {
|
||||
if (kind === "song") {
|
||||
await convex.mutation(api.favorites.favoriteSong, {
|
||||
songId: id as Id<"songs">,
|
||||
})
|
||||
} else if (kind === "album") {
|
||||
await convex.mutation(api.favorites.favoriteAlbum, {
|
||||
albumId: id as Id<"albums">,
|
||||
})
|
||||
} else if (kind === "artist") {
|
||||
await convex.mutation(api.favorites.favoriteArtist, {
|
||||
artistId: id as Id<"artists">,
|
||||
})
|
||||
} else {
|
||||
throw new IntentRejected(
|
||||
"oTunes can love songs, albums, and artists."
|
||||
)
|
||||
}
|
||||
return
|
||||
}
|
||||
if (kind === "song") {
|
||||
await convex.mutation(api.favorites.unfavoriteSong, {
|
||||
songId: id as Id<"songs">,
|
||||
})
|
||||
} else if (kind === "album") {
|
||||
await convex.mutation(api.favorites.unfavoriteAlbum, {
|
||||
albumId: id as Id<"albums">,
|
||||
})
|
||||
} else if (kind === "artist") {
|
||||
await convex.mutation(api.favorites.unfavoriteArtist, {
|
||||
artistId: id as Id<"artists">,
|
||||
})
|
||||
} else {
|
||||
throw new IntentRejected("oTunes can love songs, albums, and artists.")
|
||||
}
|
||||
}
|
||||
|
||||
async function transport(invocation: AppIntentInvocation) {
|
||||
const actions = playbackRef.current
|
||||
const command = readString(invocation.params.command)
|
||||
if (command === "pause") {
|
||||
actions.setIsPlaying(false)
|
||||
return
|
||||
}
|
||||
if (command === "resume") {
|
||||
if (actions.queueSongIds.length === 0) {
|
||||
throw new IntentRejected("Nothing is queued in oTunes.")
|
||||
}
|
||||
actions.setIsPlaying(true)
|
||||
return
|
||||
}
|
||||
if (command === "next") {
|
||||
await actions.skipNext()
|
||||
return
|
||||
}
|
||||
if (command === "previous") {
|
||||
await actions.skipPrevious()
|
||||
return
|
||||
}
|
||||
throw new IntentRejected("oTunes doesn't know that playback control.")
|
||||
}
|
||||
})
|
||||
|
||||
return null
|
||||
}
|
||||
@@ -112,8 +112,18 @@ type PlaybackContextValue = {
|
||||
songIds?: Id<"songs">[]
|
||||
) => Promise<void>
|
||||
toggleShuffle: () => Promise<void>
|
||||
setShuffleEnabled: (enabled: boolean) => Promise<void>
|
||||
cycleRepeat: () => Promise<void>
|
||||
setRepeatMode: (mode: RepeatMode) => Promise<void>
|
||||
prepareQueue: (songIds: Id<"songs">[], shuffled: boolean) => Promise<void>
|
||||
startFromSong: (
|
||||
songId: Id<"songs">,
|
||||
songIds: Id<"songs">[],
|
||||
shuffled: boolean,
|
||||
play: boolean
|
||||
) => Promise<void>
|
||||
clearQueue: () => Promise<void>
|
||||
clearUpNext: () => Promise<void>
|
||||
clearHistory: () => Promise<void>
|
||||
reorderQueue: (songIds: Id<"songs">[]) => Promise<void>
|
||||
skipToQueueIndex: (index: number) => Promise<void>
|
||||
@@ -138,8 +148,13 @@ type PlaybackActionsValue = Pick<
|
||||
| "playPlaylist"
|
||||
| "playPlaylistShuffled"
|
||||
| "toggleShuffle"
|
||||
| "setShuffleEnabled"
|
||||
| "cycleRepeat"
|
||||
| "setRepeatMode"
|
||||
| "prepareQueue"
|
||||
| "startFromSong"
|
||||
| "clearQueue"
|
||||
| "clearUpNext"
|
||||
| "clearHistory"
|
||||
| "reorderQueue"
|
||||
| "skipToQueueIndex"
|
||||
@@ -1060,16 +1075,63 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
||||
store.update(playbackQueue.toggleShuffle)
|
||||
}, [store])
|
||||
|
||||
const setShuffleEnabled = useCallback(
|
||||
async (enabled: boolean) => {
|
||||
store.update((state) => playbackQueue.setShuffleEnabled(state, enabled))
|
||||
},
|
||||
[store]
|
||||
)
|
||||
|
||||
const cycleRepeat = useCallback(async () => {
|
||||
store.update(playbackQueue.cycleRepeat)
|
||||
}, [store])
|
||||
|
||||
const setRepeatMode = useCallback(
|
||||
async (mode: RepeatMode) => {
|
||||
store.update((state) => playbackQueue.setRepeatMode(state, mode))
|
||||
},
|
||||
[store]
|
||||
)
|
||||
|
||||
const prepareQueue = useCallback(
|
||||
async (songIds: Id<"songs">[], shuffled: boolean) => {
|
||||
if (songIds.length === 0) return
|
||||
playRequestRef.current += 1
|
||||
store.update((state) =>
|
||||
playbackQueue.startQueue(state, songIds, shuffled)
|
||||
)
|
||||
setIsPlaying(false)
|
||||
},
|
||||
[store, setIsPlaying]
|
||||
)
|
||||
|
||||
const startFromSong = useCallback(
|
||||
async (
|
||||
songId: Id<"songs">,
|
||||
songIds: Id<"songs">[],
|
||||
shuffled: boolean,
|
||||
play: boolean
|
||||
) => {
|
||||
playRequestRef.current += 1
|
||||
store.update((state) => playbackQueue.setShuffleEnabled(state, shuffled))
|
||||
store.update((state) =>
|
||||
playbackQueue.startQueueFromSong(state, songId, songIds)
|
||||
)
|
||||
setIsPlaying(play)
|
||||
},
|
||||
[store, setIsPlaying]
|
||||
)
|
||||
|
||||
const clearQueue = useCallback(async () => {
|
||||
playRequestRef.current += 1
|
||||
store.update(playbackQueue.clearQueue)
|
||||
setIsPlaying(false)
|
||||
}, [store, setIsPlaying])
|
||||
|
||||
const clearUpNext = useCallback(async () => {
|
||||
store.update(playbackQueue.clearUpNext)
|
||||
}, [store])
|
||||
|
||||
const clearHistory = useCallback(async () => {
|
||||
store.update(playbackQueue.clearHistory)
|
||||
}, [store])
|
||||
@@ -1147,8 +1209,13 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
||||
playPlaylist,
|
||||
playPlaylistShuffled,
|
||||
toggleShuffle,
|
||||
setShuffleEnabled,
|
||||
cycleRepeat,
|
||||
setRepeatMode,
|
||||
prepareQueue,
|
||||
startFromSong,
|
||||
clearQueue,
|
||||
clearUpNext,
|
||||
clearHistory,
|
||||
reorderQueue,
|
||||
skipToQueueIndex,
|
||||
@@ -1161,7 +1228,12 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
||||
addToQueueLast,
|
||||
clearHistory,
|
||||
clearQueue,
|
||||
clearUpNext,
|
||||
cycleRepeat,
|
||||
prepareQueue,
|
||||
startFromSong,
|
||||
setRepeatMode,
|
||||
setShuffleEnabled,
|
||||
playAlbum,
|
||||
playAlbumShuffled,
|
||||
playArtist,
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
import AsyncStorage from "@react-native-async-storage/async-storage"
|
||||
import * as AppIntents from "expo-app-intents"
|
||||
|
||||
export const FAVORITES_PLAYLIST_ID = "otunes:favorites"
|
||||
|
||||
const HANDLED_KEY = "otunes:handled-app-intents"
|
||||
const MAX_HANDLED = 200
|
||||
|
||||
const CATALOG_KINDS = [
|
||||
"song",
|
||||
"album",
|
||||
"artist",
|
||||
"playlist",
|
||||
"nowPlaying",
|
||||
] as const
|
||||
|
||||
export async function clearAppIntentCatalogs() {
|
||||
if (!AppIntents.isAvailable()) {
|
||||
return
|
||||
}
|
||||
await Promise.all(
|
||||
CATALOG_KINDS.map((kind) => AppIntents.setEntityCatalogAsync(kind, []))
|
||||
)
|
||||
}
|
||||
|
||||
export async function loadHandledIntentIds() {
|
||||
const raw = await AsyncStorage.getItem(HANDLED_KEY)
|
||||
if (!raw) {
|
||||
return new Set<string>()
|
||||
}
|
||||
try {
|
||||
const parsed = JSON.parse(raw) as unknown
|
||||
if (!Array.isArray(parsed)) {
|
||||
return new Set<string>()
|
||||
}
|
||||
return new Set(parsed.filter((id) => typeof id === "string"))
|
||||
} catch {
|
||||
return new Set<string>()
|
||||
}
|
||||
}
|
||||
|
||||
export async function rememberHandledIntentId(ids: Set<string>, id: string) {
|
||||
ids.add(id)
|
||||
const list = [...ids]
|
||||
const trimmed =
|
||||
list.length > MAX_HANDLED ? list.slice(list.length - MAX_HANDLED) : list
|
||||
if (trimmed.length !== list.length) {
|
||||
ids.clear()
|
||||
for (const item of trimmed) {
|
||||
ids.add(item)
|
||||
}
|
||||
}
|
||||
await AsyncStorage.setItem(HANDLED_KEY, JSON.stringify(trimmed))
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import type { useRouter } from "expo-router"
|
||||
|
||||
import { clearAppIntentCatalogs } from "@/lib/app-intent-handled"
|
||||
import { resetPlaybackStores } from "@otunes/convex/client/playback-store"
|
||||
import { clearPersistentCache } from "@/lib/persistent-cache"
|
||||
|
||||
@@ -8,7 +9,7 @@ type AppRouter = ReturnType<typeof useRouter>
|
||||
/** Forgets this device's cached library and playback state. */
|
||||
export async function clearLocalData() {
|
||||
resetPlaybackStores()
|
||||
await clearPersistentCache()
|
||||
await Promise.all([clearPersistentCache(), clearAppIntentCatalogs()])
|
||||
}
|
||||
|
||||
export async function signOutToSignIn(
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
"@react-native/metro-config": "0.88.0",
|
||||
"convex": "^1.40.0",
|
||||
"expo": "~58.0.16",
|
||||
"expo-app-intents": "0.5.10",
|
||||
"expo-asset": "~58.0.17",
|
||||
"expo-audio": "^58.0.9",
|
||||
"expo-blur": "~58.0.5",
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
const { createRequire } = require("module");
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
|
||||
const expoRequire = createRequire(require.resolve("expo/package.json"));
|
||||
const { withAppDelegate, withXcodeProject } = expoRequire("@expo/config-plugins");
|
||||
const { createBuildSourceFile } = expoRequire(
|
||||
"@expo/config-plugins/build/ios/XcodeProjectFile",
|
||||
);
|
||||
const { getProjectName } = expoRequire(
|
||||
"@expo/config-plugins/build/ios/utils/Xcodeproj",
|
||||
);
|
||||
|
||||
const REGISTER_CALL = "MusicAppIntents.register()";
|
||||
|
||||
/**
|
||||
* Current Expo prebuild adds `experiments.inlineModules.watchedDirectories`
|
||||
* to the app target. Copying those Swift files again would compile them twice.
|
||||
* Older toolchains that do not do this still need the files in the app target,
|
||||
* plus an AppDelegate call, because the setup module is not registered for them.
|
||||
*/
|
||||
function prebuildCompilesWatchedDirectories() {
|
||||
try {
|
||||
const cliRequire = createRequire(expoRequire.resolve("@expo/cli/package.json"));
|
||||
cliRequire.resolve("@expo/inline-modules/package.json");
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function swiftSources(projectRoot) {
|
||||
const sourceDir = path.join(projectRoot, "app-intents");
|
||||
return fs
|
||||
.readdirSync(sourceDir)
|
||||
.filter((file) => file.endsWith(".swift"))
|
||||
.sort()
|
||||
.map((file) => ({
|
||||
file,
|
||||
contents: fs.readFileSync(path.join(sourceDir, file), "utf8"),
|
||||
}));
|
||||
}
|
||||
|
||||
function withCopiedAppIntentSources(config) {
|
||||
return withXcodeProject(config, (mod) => {
|
||||
const projectRoot = mod.modRequest.projectRoot;
|
||||
const projectName = getProjectName(projectRoot);
|
||||
fs.mkdirSync(path.join(mod.modRequest.platformProjectRoot, projectName), {
|
||||
recursive: true,
|
||||
});
|
||||
|
||||
for (const source of swiftSources(projectRoot)) {
|
||||
mod.modResults = createBuildSourceFile({
|
||||
project: mod.modResults,
|
||||
nativeProjectRoot: mod.modRequest.platformProjectRoot,
|
||||
filePath: path.join(projectName, source.file),
|
||||
fileContents: source.contents,
|
||||
overwrite: true,
|
||||
});
|
||||
}
|
||||
|
||||
return mod;
|
||||
});
|
||||
}
|
||||
|
||||
function withAppIntentRegistration(config) {
|
||||
return withAppDelegate(config, (mod) => {
|
||||
if (mod.modResults.language !== "swift") {
|
||||
throw new Error(
|
||||
"oTunes App Intents need the Swift AppDelegate so Siri shortcuts can refresh.",
|
||||
);
|
||||
}
|
||||
if (!mod.modResults.contents.includes(REGISTER_CALL)) {
|
||||
const anchor = "let delegate = ReactNativeDelegate()";
|
||||
if (!mod.modResults.contents.includes(anchor)) {
|
||||
throw new Error(
|
||||
"AppDelegate.swift is missing its React Native startup, so App Intents could not be registered.",
|
||||
);
|
||||
}
|
||||
mod.modResults.contents = mod.modResults.contents.replace(
|
||||
anchor,
|
||||
`${REGISTER_CALL}\n ${anchor}`,
|
||||
);
|
||||
}
|
||||
return mod;
|
||||
});
|
||||
}
|
||||
|
||||
function withAppIntentSources(config) {
|
||||
if (prebuildCompilesWatchedDirectories()) {
|
||||
return config;
|
||||
}
|
||||
return withAppIntentRegistration(withCopiedAppIntentSources(config));
|
||||
}
|
||||
|
||||
module.exports = withAppIntentSources;
|
||||
@@ -132,6 +132,34 @@ export function cycleRepeat(state: PlaybackQueueState): PlaybackQueueState {
|
||||
return { ...state, repeatEnabled }
|
||||
}
|
||||
|
||||
export function setShuffleEnabled(
|
||||
state: PlaybackQueueState,
|
||||
enabled: boolean
|
||||
): PlaybackQueueState {
|
||||
if (state.shuffleEnabled === enabled) {
|
||||
return state
|
||||
}
|
||||
return toggleShuffle(state)
|
||||
}
|
||||
|
||||
export function setRepeatMode(
|
||||
state: PlaybackQueueState,
|
||||
repeatEnabled: RepeatMode
|
||||
): PlaybackQueueState {
|
||||
if (state.repeatEnabled === repeatEnabled) {
|
||||
return state
|
||||
}
|
||||
return { ...state, repeatEnabled }
|
||||
}
|
||||
|
||||
/** Drops everything after the current song. The song that is playing stays. */
|
||||
export function clearUpNext(state: PlaybackQueueState): PlaybackQueueState {
|
||||
if (state.queue.length <= 1) {
|
||||
return state
|
||||
}
|
||||
return { ...state, queue: [state.queue[0]] }
|
||||
}
|
||||
|
||||
export function clearQueue(state: PlaybackQueueState): PlaybackQueueState {
|
||||
return { ...state, queue: [], history: [], progressSeconds: 0 }
|
||||
}
|
||||
|
||||
@@ -1047,6 +1047,175 @@ export const getSongsForPlayback = query({
|
||||
},
|
||||
})
|
||||
|
||||
const FAVORITES_PLAYLIST_ID = "otunes:favorites"
|
||||
|
||||
/**
|
||||
* Compact library snapshot for Siri and Shortcuts. Playback still loads songs
|
||||
* through the existing queries; this only carries the names those pickers match.
|
||||
*/
|
||||
export const intentCatalog = query({
|
||||
args: {},
|
||||
handler: async (ctx) => {
|
||||
const userId = await getAuthUserId(ctx)
|
||||
const user = userId ? await ctx.db.get("users", userId) : null
|
||||
const favoriteSongs = new Set(user?.favoriteSongs ?? [])
|
||||
const favoriteAlbums = new Set(user?.favoriteAlbums ?? [])
|
||||
const favoriteArtists = new Set(user?.favoriteArtists ?? [])
|
||||
|
||||
const artistDocs = await ctx.db.query("artists").collect()
|
||||
const albumDocs = await ctx.db.query("albums").collect()
|
||||
const songDocs = await ctx.db.query("songs").collect()
|
||||
const artistMap = new Map(artistDocs.map((artist) => [artist._id, artist]))
|
||||
const albumMap = new Map(albumDocs.map((album) => [album._id, album]))
|
||||
|
||||
const artists = artistDocs
|
||||
.flatMap((artist) => {
|
||||
const name = artist.name.trim()
|
||||
if (!name) {
|
||||
return []
|
||||
}
|
||||
return [
|
||||
{
|
||||
id: artist._id,
|
||||
name,
|
||||
favorite: favoriteArtists.has(artist._id),
|
||||
},
|
||||
]
|
||||
})
|
||||
.sort((a, b) => a.name.localeCompare(b.name))
|
||||
|
||||
const albums = albumDocs
|
||||
.flatMap((album) => {
|
||||
const artist = artistMap.get(album.artist)
|
||||
const title = album.name.trim()
|
||||
if (!artist || !title) {
|
||||
return []
|
||||
}
|
||||
return [
|
||||
{
|
||||
id: album._id,
|
||||
title,
|
||||
artistName: artist.name,
|
||||
artistId: artist._id,
|
||||
year: album.year,
|
||||
favorite: favoriteAlbums.has(album._id),
|
||||
},
|
||||
]
|
||||
})
|
||||
.sort((a, b) => a.title.localeCompare(b.title))
|
||||
|
||||
const songs = songDocs
|
||||
.flatMap((song) => {
|
||||
const album = albumMap.get(song.album)
|
||||
const artist = artistMap.get(song.albumArtist)
|
||||
const title = song.name.trim()
|
||||
if (!album || !artist || !title) {
|
||||
return []
|
||||
}
|
||||
const credits = getSongArtistNames(song.artist)
|
||||
const artistName = songArtistsMatchAlbum(credits, artist.name)
|
||||
? artist.name
|
||||
: credits.join(", ") || artist.name
|
||||
return [
|
||||
{
|
||||
id: song._id,
|
||||
title,
|
||||
artistName,
|
||||
artistId: artist._id,
|
||||
albumTitle: album.name,
|
||||
albumId: album._id,
|
||||
durationSeconds: Math.max(0, Math.round(song.length)),
|
||||
genres: song.genres
|
||||
.map((genre) => genre.trim())
|
||||
.filter((genre) => genre.length > 0)
|
||||
.join(", "),
|
||||
year: song.year,
|
||||
track: song.track,
|
||||
diskNumber: song.diskNumber,
|
||||
favorite: favoriteSongs.has(song._id),
|
||||
},
|
||||
]
|
||||
})
|
||||
.sort((a, b) => a.title.localeCompare(b.title))
|
||||
|
||||
const playlists: {
|
||||
id: string
|
||||
title: string
|
||||
ownerName: string
|
||||
createdByMe: boolean
|
||||
songCount: number
|
||||
}[] = []
|
||||
|
||||
if (user) {
|
||||
const owned = await ctx.db
|
||||
.query("playlists")
|
||||
.withIndex("by_owner", (q) => q.eq("owner", user._id))
|
||||
.collect()
|
||||
const ownedIds = new Set(owned.map((playlist) => playlist._id))
|
||||
const subscribed = (
|
||||
await Promise.all(
|
||||
(user.subscribedPlaylists ?? [])
|
||||
.filter((playlistId) => !ownedIds.has(playlistId))
|
||||
.map((playlistId) => ctx.db.get("playlists", playlistId))
|
||||
)
|
||||
).flatMap((playlist) => (playlist ? [playlist] : []))
|
||||
const subscribedOwners = await Promise.all(
|
||||
[...new Set(subscribed.map((playlist) => playlist.owner))].map(
|
||||
(ownerId) => ctx.db.get("users", ownerId)
|
||||
)
|
||||
)
|
||||
const ownerNames = new Map(
|
||||
subscribedOwners.flatMap((owner) =>
|
||||
owner ? [[owner._id, owner.name] as const] : []
|
||||
)
|
||||
)
|
||||
|
||||
playlists.push({
|
||||
id: FAVORITES_PLAYLIST_ID,
|
||||
title: "Favorites",
|
||||
ownerName: user.name,
|
||||
createdByMe: true,
|
||||
songCount: user.favoriteSongs?.length ?? 0,
|
||||
})
|
||||
|
||||
const listed = [
|
||||
...owned.map((playlist) => ({
|
||||
playlist,
|
||||
createdByMe: true,
|
||||
ownerName: user.name,
|
||||
})),
|
||||
...subscribed.map((playlist) => ({
|
||||
playlist,
|
||||
createdByMe: false,
|
||||
ownerName: ownerNames.get(playlist.owner) ?? "",
|
||||
})),
|
||||
].sort((a, b) => a.playlist.name.localeCompare(b.playlist.name))
|
||||
|
||||
for (const entry of listed) {
|
||||
const title = entry.playlist.name.trim()
|
||||
if (!title) {
|
||||
continue
|
||||
}
|
||||
playlists.push({
|
||||
id: entry.playlist._id,
|
||||
title,
|
||||
ownerName: entry.ownerName,
|
||||
createdByMe: entry.createdByMe,
|
||||
songCount: entry.playlist.songs.length,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
authenticated: user !== null,
|
||||
songs,
|
||||
albums,
|
||||
artists,
|
||||
playlists,
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
export const getSongByName = internalQuery({
|
||||
args: { name: v.string() },
|
||||
handler: async (ctx, args) => {
|
||||
|
||||
Generated
+24
@@ -55,6 +55,9 @@ importers:
|
||||
expo:
|
||||
specifier: ~58.0.16
|
||||
version: 58.0.16(@babel/core@7.29.7)(@expo/dom-webview@58.0.2)(@expo/metro-runtime@58.0.14)(expo-router@58.0.24)(react-dom@19.3.0(react@19.3.0))(react-native-worklets@0.13.0(@babel/core@7.29.7)(@react-native/metro-config@0.88.0(@babel/core@7.29.7))(react-native@0.88.0(@babel/core@7.29.7)(@react-native/metro-config@0.88.0(@babel/core@7.29.7))(@types/react@19.3.0)(react@19.3.0))(react@19.3.0))(react-native@0.88.0(@babel/core@7.29.7)(@react-native/metro-config@0.88.0(@babel/core@7.29.7))(@types/react@19.3.0)(react@19.3.0))(react@19.3.0)(typescript@6.0.3)
|
||||
expo-app-intents:
|
||||
specifier: 0.5.10
|
||||
version: 0.5.10(@babel/core@7.29.7)(expo@58.0.16)(react-dom@19.3.0(react@19.3.0))(react-native-worklets@0.13.0(@babel/core@7.29.7)(@react-native/metro-config@0.88.0(@babel/core@7.29.7))(react-native@0.88.0(@babel/core@7.29.7)(@react-native/metro-config@0.88.0(@babel/core@7.29.7))(@types/react@19.3.0)(react@19.3.0))(react@19.3.0))(react-native@0.88.0(@babel/core@7.29.7)(@react-native/metro-config@0.88.0(@babel/core@7.29.7))(@types/react@19.3.0)(react@19.3.0))(react@19.3.0)
|
||||
expo-asset:
|
||||
specifier: ~58.0.17
|
||||
version: 58.0.17(expo@58.0.16)(react-native@0.88.0(@babel/core@7.29.7)(@react-native/metro-config@0.88.0(@babel/core@7.29.7))(@types/react@19.3.0)(react@19.3.0))(react@19.3.0)(typescript@6.0.3)
|
||||
@@ -3950,6 +3953,14 @@ packages:
|
||||
exif-parser@0.1.12:
|
||||
resolution: {integrity: sha512-c2bQfLNbMzLPmzQuOr8fy0csy84WmwnER81W88DzTp9CYNPJ6yzOj2EZAh9pywYpqHnshVLHQJ8WzldAyfY+Iw==}
|
||||
|
||||
expo-app-intents@0.5.10:
|
||||
resolution: {integrity: sha512-PNxAbIaNjLDeajnVJGClDXnB1CFkGScuAYcEORFsmaLmABMjlxq/jNC708cgWVyDgX2XUvDBXifq2rqvQAsPmQ==}
|
||||
hasBin: true
|
||||
peerDependencies:
|
||||
expo: '*'
|
||||
react: '*'
|
||||
react-native: '*'
|
||||
|
||||
expo-asset@58.0.17:
|
||||
resolution: {integrity: sha512-CDYXjwNoTQh4s00pR99mkbwh6A5/eUDMqKXPoLnuTW6kYKzuQEVxLpOPTj5zg0uYpCA6KAHI294li0roYGMqHQ==}
|
||||
peerDependencies:
|
||||
@@ -10953,6 +10964,19 @@ snapshots:
|
||||
|
||||
exif-parser@0.1.12: {}
|
||||
|
||||
expo-app-intents@0.5.10(@babel/core@7.29.7)(expo@58.0.16)(react-dom@19.3.0(react@19.3.0))(react-native-worklets@0.13.0(@babel/core@7.29.7)(@react-native/metro-config@0.88.0(@babel/core@7.29.7))(react-native@0.88.0(@babel/core@7.29.7)(@react-native/metro-config@0.88.0(@babel/core@7.29.7))(@types/react@19.3.0)(react@19.3.0))(react@19.3.0))(react-native@0.88.0(@babel/core@7.29.7)(@react-native/metro-config@0.88.0(@babel/core@7.29.7))(@types/react@19.3.0)(react@19.3.0))(react@19.3.0):
|
||||
dependencies:
|
||||
'@expo/ui': 58.0.21(@babel/core@7.29.7)(expo@58.0.16)(react-dom@19.3.0(react@19.3.0))(react-native-worklets@0.13.0(@babel/core@7.29.7)(@react-native/metro-config@0.88.0(@babel/core@7.29.7))(react-native@0.88.0(@babel/core@7.29.7)(@react-native/metro-config@0.88.0(@babel/core@7.29.7))(@types/react@19.3.0)(react@19.3.0))(react@19.3.0))(react-native@0.88.0(@babel/core@7.29.7)(@react-native/metro-config@0.88.0(@babel/core@7.29.7))(@types/react@19.3.0)(react@19.3.0))(react@19.3.0)
|
||||
commander: 12.1.0
|
||||
expo: 58.0.16(@babel/core@7.29.7)(@expo/dom-webview@58.0.2)(@expo/metro-runtime@58.0.14)(expo-router@58.0.24)(react-dom@19.3.0(react@19.3.0))(react-native-worklets@0.13.0(@babel/core@7.29.7)(@react-native/metro-config@0.88.0(@babel/core@7.29.7))(react-native@0.88.0(@babel/core@7.29.7)(@react-native/metro-config@0.88.0(@babel/core@7.29.7))(@types/react@19.3.0)(react@19.3.0))(react@19.3.0))(react-native@0.88.0(@babel/core@7.29.7)(@react-native/metro-config@0.88.0(@babel/core@7.29.7))(@types/react@19.3.0)(react@19.3.0))(react@19.3.0)(typescript@6.0.3)
|
||||
prompts: 2.4.2
|
||||
react: 19.3.0
|
||||
react-native: 0.88.0(@babel/core@7.29.7)(@react-native/metro-config@0.88.0(@babel/core@7.29.7))(@types/react@19.3.0)(react@19.3.0)
|
||||
transitivePeerDependencies:
|
||||
- '@babel/core'
|
||||
- react-dom
|
||||
- react-native-worklets
|
||||
|
||||
expo-asset@58.0.17(expo@58.0.16)(react-native@0.88.0(@babel/core@7.29.7)(@react-native/metro-config@0.88.0(@babel/core@7.29.7))(@types/react@19.3.0)(react@19.3.0))(react@19.3.0)(typescript@6.0.3):
|
||||
dependencies:
|
||||
'@expo/image-utils': 0.12.5(typescript@6.0.3)
|
||||
|
||||
Reference in new issue
Block a user