Files
otunes/apps/mobile/lib/native-playlist-window.ts
joshuafhigginsandremhelper d66e26d0f4 feat(mobile): use native playlists for queued playback (#17)
- Add native playlist windowing and lock-screen controls
- Sync playback queues through a batched Convex song query

---------

Co-authored-by: Rem Helper <remhelper@abunchofknowitalls.com>
Reviewed-on: https://git.abunchofknowitalls.com/joshuafhiggins/otunes/pulls/17
2026-08-19 11:44:29 -04:00

57 lines
1.4 KiB
TypeScript

import type { Id } from "@otunes/convex/_generated/dataModel"
type RepeatMode = "none" | "once" | "always"
type PlaylistLoopMode = "none" | "single" | "all"
export const NATIVE_UPCOMING_TRACKS = 30
export function nativePlaylistSongIds(
historySongIds: Id<"songs">[],
queueSongIds: Id<"songs">[]
) {
return [
...historySongIds,
...queueSongIds.slice(0, 1 + NATIVE_UPCOMING_TRACKS),
]
}
export function repeatToPlaylistLoop(repeat: RepeatMode): PlaylistLoopMode {
if (repeat === "once") {
return "single"
}
return "none"
}
export function nativePlaylistNeedsRebuild(
loadedSongIds: Id<"songs">[],
historySongIds: Id<"songs">[],
queueSongIds: Id<"songs">[]
) {
if (loadedSongIds.length === 0 || queueSongIds.length === 0) {
return true
}
const currentIndex = historySongIds.length
if (currentIndex >= loadedSongIds.length) {
return true
}
if (loadedSongIds[currentIndex] !== queueSongIds[0]) {
return true
}
const remainingNative = loadedSongIds.length - currentIndex - 1
const remainingQueue = queueSongIds.length - 1
if (remainingQueue > remainingNative && remainingNative < 5) {
return true
}
const compareCount = Math.min(remainingNative, remainingQueue)
for (let i = 0; i < compareCount; i++) {
if (loadedSongIds[currentIndex + 1 + i] !== queueSongIds[1 + i]) {
return true
}
}
return false
}