mirror of
https://github.com/Boof2015/astra-mobile.git
synced 2026-08-19 04:06:43 +02:00
android auto more stuff
This commit is contained in:
@@ -2,6 +2,7 @@ import TrackPlayer, { State, type Track as RntpTrack } from 'react-native-track-
|
||||
|
||||
import type { PlaybackState, Track } from '@/types/audio';
|
||||
import { AstraCar } from '../../modules/astra-car';
|
||||
import { usePlaylistStore } from '@/stores/playlistStore';
|
||||
|
||||
function mapRntpState(state?: State): PlaybackState {
|
||||
switch (state) {
|
||||
@@ -20,7 +21,7 @@ function mapRntpState(state?: State): PlaybackState {
|
||||
|
||||
type CarNowPlayingTrack = Pick<
|
||||
Track,
|
||||
'title' | 'artist' | 'album' | 'artworkData' | 'duration' | 'sourceType' | 'sourceId' | 'artworkSourceId'
|
||||
'path' | 'title' | 'artist' | 'album' | 'artworkData' | 'duration' | 'sourceType' | 'sourceId' | 'artworkSourceId'
|
||||
>;
|
||||
|
||||
/** True when the track should use its remote server cover (no local cache to serve). */
|
||||
@@ -51,6 +52,7 @@ export function setCarNowPlaying(
|
||||
// Android Auto loads art only from content:// URIs, so we pass structured identity
|
||||
// (local hash or remote source+id) and let the native module build the content URI.
|
||||
const remote = isRemoteArt(track);
|
||||
const trackPath = track?.path ?? null;
|
||||
AstraCar.setNowPlaying({
|
||||
title: track?.title ?? null,
|
||||
artist: track?.artist ?? null,
|
||||
@@ -62,6 +64,8 @@ export function setCarNowPlaying(
|
||||
hasTrack: Boolean(track),
|
||||
duration: duration ?? track?.duration ?? null,
|
||||
position: position ?? null,
|
||||
trackPath,
|
||||
isFavorite: trackPath ? usePlaylistStore.getState().favoritePaths.has(trackPath) : false,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -74,6 +78,7 @@ export function setCarNowPlayingFromRntpTrack(
|
||||
setCarNowPlaying(
|
||||
track
|
||||
? {
|
||||
path: typeof track.astraPath === 'string' ? track.astraPath : String(track.url ?? track.id),
|
||||
title: track.title ?? 'Unknown title',
|
||||
artist: track.artist ?? 'Unknown artist',
|
||||
album: track.album ?? '',
|
||||
|
||||
@@ -15,9 +15,11 @@ import { buildArtistList, filterTracksByArtist } from '@/library/artistGrouping'
|
||||
import { dbTrackToTrack } from '@/library/trackAdapter';
|
||||
import { playForCar, playTracksForCar, pause, seekTo, skipToNext, skipToPrevious } from '@/audio/playbackController';
|
||||
import { syncCarNowPlayingFromTrackPlayer } from '@/audio/carSync';
|
||||
import TrackPlayer, { type Track as RntpTrack } from 'react-native-track-player';
|
||||
import { useAudioSettingsStore } from '@/stores/audioSettingsStore';
|
||||
import { useEQStore } from '@/stores/eqStore';
|
||||
import { useLibraryStore } from '@/stores/libraryStore';
|
||||
import { usePlaylistStore } from '@/stores/playlistStore';
|
||||
import { useRemoteSourcesStore } from '@/stores/remoteSourcesStore';
|
||||
import { useSettingsStore } from '@/stores/settingsStore';
|
||||
import type { DbTrack } from '@/types/library';
|
||||
@@ -53,6 +55,7 @@ async function initializeForCar(): Promise<void> {
|
||||
initPromise = (async () => {
|
||||
await useSettingsStore.getState().load();
|
||||
await useLibraryStore.getState().initialize();
|
||||
await usePlaylistStore.getState().refresh();
|
||||
await useRemoteSourcesStore.getState().init();
|
||||
await Promise.all([
|
||||
useEQStore.getState().load(),
|
||||
@@ -92,6 +95,9 @@ export async function handleAstraCarCommand(payload: CarCommandPayload): Promise
|
||||
case 'seek':
|
||||
if (typeof payload.position === 'number') await seekTo(payload.position);
|
||||
break;
|
||||
case 'toggleFavorite':
|
||||
await handleFavoriteCommand();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@@ -102,6 +108,19 @@ export async function handleAstraCarCommand(payload: CarCommandPayload): Promise
|
||||
}
|
||||
}
|
||||
|
||||
async function handleFavoriteCommand(): Promise<void> {
|
||||
const activeTrack = await TrackPlayer.getActiveTrack();
|
||||
const path = rntpTrackPath(activeTrack);
|
||||
if (!path) return;
|
||||
await usePlaylistStore.getState().toggleFavorite({ path });
|
||||
}
|
||||
|
||||
function rntpTrackPath(track: RntpTrack | null | undefined): string | null {
|
||||
if (!track) return null;
|
||||
if (typeof track.astraPath === 'string' && track.astraPath.length > 0) return track.astraPath;
|
||||
return typeof track.url === 'string' && track.url.length > 0 ? track.url : null;
|
||||
}
|
||||
|
||||
async function playMedia(media: CarMediaPayload): Promise<void> {
|
||||
const db = await openLibraryDb();
|
||||
const resolved = await resolveMediaTracks(db, media);
|
||||
|
||||
Reference in New Issue
Block a user