Files
otunes/apps/mobile/lib/native-playlist-window.ts
T

119 lines
3.2 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 type NativePlaylistEdit =
| { type: "remove"; index: number }
| { type: "insert"; index: number; songId: Id<"songs"> }
| { type: "add"; songId: Id<"songs"> }
/**
* Edits that turn the loaded native playlist into `desiredSongIds` without
* touching the playing track, applied in order. `null` when the playing track
* itself differs, which needs a rebuild.
*/
export function nativePlaylistEdits(
loadedSongIds: Id<"songs">[],
loadedCurrentIndex: number,
desiredSongIds: Id<"songs">[],
desiredCurrentIndex: number
): NativePlaylistEdit[] | null {
const current = loadedSongIds[loadedCurrentIndex]
if (
current === undefined ||
current !== desiredSongIds[desiredCurrentIndex]
) {
return null
}
const edits: NativePlaylistEdit[] = []
const desiredUpcoming = desiredSongIds.slice(desiredCurrentIndex + 1)
const keptUpcoming = commonPrefixLength(
loadedSongIds.slice(loadedCurrentIndex + 1),
desiredUpcoming
)
for (
let index = loadedSongIds.length - 1;
index > loadedCurrentIndex + keptUpcoming;
index--
) {
edits.push({ type: "remove", index })
}
for (const songId of desiredUpcoming.slice(keptUpcoming)) {
edits.push({ type: "add", songId })
}
const keptHistory = commonPrefixLength(
loadedSongIds.slice(0, loadedCurrentIndex),
desiredSongIds.slice(0, desiredCurrentIndex)
)
for (let index = loadedCurrentIndex - 1; index >= keptHistory; index--) {
edits.push({ type: "remove", index })
}
for (let index = keptHistory; index < desiredCurrentIndex; index++) {
edits.push({ type: "insert", index, songId: desiredSongIds[index] })
}
return edits
}
function commonPrefixLength(a: Id<"songs">[], b: Id<"songs">[]) {
let length = 0
while (length < a.length && length < b.length && a[length] === b[length]) {
length++
}
return length
}
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
}