## Summary - Add Siri and Shortcuts actions for music playback, queue management, playlists, favorites, and transport controls. - Bridge native App Intents to the mobile app’s playback and library data. ## Testing - Not run Reviewed-on: #40
60 lines
1.6 KiB
TypeScript
60 lines
1.6 KiB
TypeScript
import AsyncStorage from "@react-native-async-storage/async-storage"
|
|
import * as AppIntents from "expo-app-intents"
|
|
import { publishMusicCatalog } from "@/lib/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
|
|
}
|
|
const results = await Promise.allSettled([
|
|
AppIntents.deleteDonationsAsync({ intent: "playMusic" }),
|
|
AppIntents.clearPendingInvocationsAsync(),
|
|
...CATALOG_KINDS.map((kind) => publishMusicCatalog(kind, [])),
|
|
])
|
|
const failed = results.find((result) => result.status === "rejected")
|
|
if (failed?.status === "rejected") throw failed.reason
|
|
}
|
|
|
|
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))
|
|
}
|