mirror of
https://git.undefined-dev.fr/Blendman974/faceclaw.git
synced 2026-11-19 21:03:09 +00:00
112 lines
3.9 KiB
TypeScript
112 lines
3.9 KiB
TypeScript
import {
|
|
APPLE_MUSIC_APP_NAME,
|
|
APPLE_MUSIC_PACKAGE,
|
|
parseMediaBrowseItems,
|
|
type MediaBrowseItem,
|
|
type MediaBrowserApp,
|
|
type MediaBrowserBridge,
|
|
} from "./media-types";
|
|
import { spotifyService } from "./spotify.ios";
|
|
import type { SpotifyLibrary } from "./spotify-library";
|
|
import { SPOTIFY_PACKAGE } from "./spotify-player";
|
|
|
|
export type { MediaBrowseItem, MediaBrowserApp } from "./media-types";
|
|
|
|
declare const FaceclawAppleMusic: any;
|
|
|
|
const APPLE_MUSIC_APP: MediaBrowserApp = { packageName: APPLE_MUSIC_PACKAGE, serviceClass: "", appName: APPLE_MUSIC_APP_NAME };
|
|
|
|
/**
|
|
* The Apple Music library (playlists, artists, albums, songs in the
|
|
* on-device / iCloud music library) as a browse tree, through
|
|
* FaceclawAppleMusic's MediaPlayer queries. Playing an item queues its
|
|
* collection on the system Music player.
|
|
*/
|
|
export class AppleMusicBrowserBridge implements MediaBrowserBridge {
|
|
private connected = false;
|
|
/** Bumped on every connect/disconnect; answers to older requests are dropped. */
|
|
private generation = 0;
|
|
|
|
listBrowsableApps(): MediaBrowserApp[] {
|
|
return FaceclawAppleMusic.shared().hasPermission() ? [APPLE_MUSIC_APP] : [];
|
|
}
|
|
|
|
connect(app: MediaBrowserApp): Promise<string> {
|
|
this.disconnect();
|
|
if (app.packageName !== APPLE_MUSIC_PACKAGE) return Promise.reject(new Error("Not an Apple Music library"));
|
|
if (!FaceclawAppleMusic.shared().hasPermission()) return Promise.reject(new Error("Apple Music access is not allowed"));
|
|
this.connected = true;
|
|
return Promise.resolve("root");
|
|
}
|
|
|
|
browse(parentId: string): Promise<MediaBrowseItem[]> {
|
|
if (!this.connected) return Promise.reject(new Error("Not connected"));
|
|
const generation = this.generation;
|
|
return new Promise<MediaBrowseItem[]>((resolve, reject) => {
|
|
FaceclawAppleMusic.shared().browseCompletion(parentId, (json: string | null, error: string | null) => {
|
|
if (generation !== this.generation) reject(new Error("Disconnected"));
|
|
else if (error || json == null) reject(new Error(String(error || "Browse failed")));
|
|
else {
|
|
try {
|
|
resolve(parseMediaBrowseItems(String(json)));
|
|
} catch (parseError) {
|
|
reject(new Error(`Browse result parse failed: ${parseError}`));
|
|
}
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
playFromMediaId(mediaId: string): void {
|
|
if (!this.connected) return;
|
|
FaceclawAppleMusic.shared().playCompletion(mediaId, (error: string | null) => {
|
|
if (error) console.warn(`[apple-music] play ${mediaId} failed: ${error}`);
|
|
});
|
|
}
|
|
|
|
disconnect(): void {
|
|
this.connected = false;
|
|
this.generation++;
|
|
}
|
|
}
|
|
|
|
/** Apple Music's library, plus the Spotify library once signed in. */
|
|
export class IosMediaBrowserBridge implements MediaBrowserBridge {
|
|
private current: MediaBrowserBridge | null = null;
|
|
|
|
constructor(
|
|
private readonly appleMusic: AppleMusicBrowserBridge,
|
|
private readonly spotify: () => SpotifyLibrary | null,
|
|
) {}
|
|
|
|
listBrowsableApps(): MediaBrowserApp[] {
|
|
return [...this.appleMusic.listBrowsableApps(), ...(this.spotify()?.listBrowsableApps() ?? [])];
|
|
}
|
|
|
|
connect(app: MediaBrowserApp): Promise<string> {
|
|
this.disconnect();
|
|
const library = app.packageName === SPOTIFY_PACKAGE ? this.spotify() : this.appleMusic;
|
|
if (!library) return Promise.reject(new Error(`${app.appName} can't be browsed`));
|
|
this.current = library;
|
|
return library.connect(app);
|
|
}
|
|
|
|
browse(parentId: string): Promise<MediaBrowseItem[]> {
|
|
return this.current ? this.current.browse(parentId) : Promise.reject(new Error("Not connected"));
|
|
}
|
|
|
|
playFromMediaId(mediaId: string): void {
|
|
this.current?.playFromMediaId(mediaId);
|
|
}
|
|
|
|
disconnect(): void {
|
|
this.current?.disconnect();
|
|
this.current = null;
|
|
}
|
|
}
|
|
|
|
export const mediaBrowserBridge: MediaBrowserBridge = new IosMediaBrowserBridge(
|
|
new AppleMusicBrowserBridge(),
|
|
() => spotifyService()?.library ?? null,
|
|
);
|