feat: previous/scrub controls, mood mix and artwork for downloads
CI / build (push) Successful in 2m16s
CI / build (push) Successful in 2m16s
Playback: - Previous button: after two seconds it restarts the current track, otherwise it restores the last played track and puts the outgoing one back in front of the queue. History is persisted in the playback snapshot. - The progress slider is draggable on every platform. Desktop previously refused to seek; it now restarts the decoder at the requested position, asking the server for a timeOffset on remote streams and skipping PCM frames for local files. Artwork: - Client downloads store the 320px cover next to the MP3 and Android sets it as MediaMetadata artwork data, so the notification and offline playback keep the cover. Deleting a download removes the image too. Moods: - New mood setting and editor slider choose the seed share of each radio batch; the server generates the rest from recommendations. Library: - Delta refresh now seeds its song map from both cached music and cached longform. Longform used to disappear after the first delta, which is why the artist list and artist playlists were empty while search still worked.
This commit is contained in:
1 parent
c0ff586686
commit
17bda94bfc
18 files changed
+390
-27
No files matched your search
@@ -255,11 +255,16 @@ class ApiClient(private val http: HttpClient) {
|
||||
suspend fun cover(albumId: String, size: Int = 320): ByteArray? {
|
||||
val key = "$albumId:$size"
|
||||
if (coverCache.containsKey(key)) return coverCache[key]
|
||||
val bytes = runCatching {
|
||||
http.get("$baseUrl/api/v1/covers/$albumId?size=$size&token=${token.orEmpty().encodeURLParameter()}")
|
||||
.bodyAsBytes()
|
||||
val result = runCatching {
|
||||
val response = http.get(
|
||||
"$baseUrl/api/v1/covers/$albumId?size=$size&token=${token.orEmpty().encodeURLParameter()}",
|
||||
)
|
||||
if (response.status.isSuccess()) {
|
||||
response.bodyAsBytes().takeIf { it.isNotEmpty() }
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}.getOrNull()
|
||||
val result = bytes?.takeIf { it.isNotEmpty() }
|
||||
if (coverCache.size > MAX_CACHED_IMAGES) coverCache.clear()
|
||||
coverCache[key] = result
|
||||
return result
|
||||
|
||||
@@ -62,6 +62,8 @@ data class Mood(
|
||||
val query: String? = null,
|
||||
val genres: List<String> = emptyList(),
|
||||
val targetSize: Int,
|
||||
val seedPercent: Int = 50,
|
||||
val recommendationPercent: Int = 50,
|
||||
val refreshHours: Int,
|
||||
val ytEnabled: Boolean,
|
||||
val lastDiscoveryAt: String? = null,
|
||||
@@ -217,6 +219,7 @@ data class CreateMoodRequest(
|
||||
val refreshHours: Int = 24,
|
||||
val ytEnabled: Boolean = true,
|
||||
val seedSongIds: List<String> = emptyList(),
|
||||
val seedPercent: Int = 50,
|
||||
)
|
||||
|
||||
@Serializable
|
||||
@@ -230,6 +233,7 @@ data class UpdateMoodRequest(
|
||||
val targetSize: Int? = null,
|
||||
val refreshHours: Int? = null,
|
||||
val ytEnabled: Boolean? = null,
|
||||
val seedPercent: Int? = null,
|
||||
)
|
||||
|
||||
@Serializable
|
||||
|
||||
@@ -9,6 +9,10 @@ interface AppStorage {
|
||||
/** Returns a platform-specific handle for playback, or null when unsupported. */
|
||||
fun saveDownload(songId: String, bytes: ByteArray): String?
|
||||
fun downloadPath(songId: String): String?
|
||||
|
||||
/** Stores the cover next to the audio file so offline playback keeps artwork. */
|
||||
fun saveArtwork(songId: String, bytes: ByteArray): String?
|
||||
fun artworkBytes(songId: String): ByteArray?
|
||||
fun listDownloads(): Map<String, Long>
|
||||
fun deleteDownload(songId: String)
|
||||
fun storageUsedBytes(): Long
|
||||
|
||||
@@ -33,6 +33,7 @@ data class PlaybackSnapshot(
|
||||
val session: Session? = null,
|
||||
val queue: List<Song> = emptyList(),
|
||||
val current: Song? = null,
|
||||
val history: List<Song> = emptyList(),
|
||||
)
|
||||
|
||||
class LibraryCache(private val storage: AppStorage) {
|
||||
|
||||
+7
-1
@@ -9,7 +9,13 @@ import win.taptap.musicmanager.client.api.Song
|
||||
* store polls [isFinished] because some backends have no callback support.
|
||||
*/
|
||||
interface AudioPlayer {
|
||||
fun play(source: String, song: Song, local: Boolean, artworkUri: String? = null)
|
||||
fun play(
|
||||
source: String,
|
||||
song: Song,
|
||||
local: Boolean,
|
||||
artworkUri: String? = null,
|
||||
artworkBytes: ByteArray? = null,
|
||||
)
|
||||
fun pause()
|
||||
fun resume()
|
||||
fun stop()
|
||||
|
||||
@@ -49,6 +49,7 @@ data class AppState(
|
||||
val session: Session? = null,
|
||||
val queue: List<Song> = emptyList(),
|
||||
val current: Song? = null,
|
||||
val history: List<Song> = emptyList(),
|
||||
val playing: Boolean = false,
|
||||
val positionSeconds: Double = 0.0,
|
||||
val durationSeconds: Double = 0.0,
|
||||
@@ -114,6 +115,7 @@ class AppStore(
|
||||
session = playback?.session,
|
||||
queue = playback?.queue.orEmpty(),
|
||||
current = playback?.current,
|
||||
history = playback?.history.orEmpty(),
|
||||
durationSeconds = playback?.current?.duration?.toDouble() ?: 0.0,
|
||||
)
|
||||
}
|
||||
@@ -184,7 +186,11 @@ class AppStore(
|
||||
try {
|
||||
val moods = api.moods()
|
||||
val cached = cache.load()
|
||||
var songs = cached.songs.associateBy { it.id }.toMutableMap()
|
||||
// Both halves of the cache must feed the delta sync; otherwise the
|
||||
// long-form list disappears as soon as the first delta arrives.
|
||||
var songs = (cached.songs + cached.longform)
|
||||
.associateBy { it.id }
|
||||
.toMutableMap()
|
||||
var revision = cached.revision
|
||||
|
||||
if (revision == null) {
|
||||
@@ -309,6 +315,7 @@ class AppStore(
|
||||
targetSize: Int,
|
||||
refreshHours: Int,
|
||||
ytEnabled: Boolean,
|
||||
seedPercent: Int = 50,
|
||||
) {
|
||||
scope.launch {
|
||||
_state.update { it.copy(saving = true) }
|
||||
@@ -322,6 +329,7 @@ class AppStore(
|
||||
targetSize = targetSize,
|
||||
refreshHours = refreshHours,
|
||||
ytEnabled = ytEnabled,
|
||||
seedPercent = seedPercent,
|
||||
),
|
||||
)
|
||||
cacheDetail(detail)
|
||||
@@ -351,6 +359,7 @@ class AppStore(
|
||||
targetSize: Int,
|
||||
refreshHours: Int,
|
||||
ytEnabled: Boolean,
|
||||
seedPercent: Int = 50,
|
||||
) {
|
||||
scope.launch {
|
||||
_state.update { it.copy(saving = true) }
|
||||
@@ -367,6 +376,7 @@ class AppStore(
|
||||
targetSize = targetSize,
|
||||
refreshHours = refreshHours,
|
||||
ytEnabled = ytEnabled,
|
||||
seedPercent = seedPercent,
|
||||
),
|
||||
)
|
||||
cacheDetail(updated)
|
||||
@@ -526,6 +536,7 @@ class AppStore(
|
||||
session = null,
|
||||
queue = emptyList(),
|
||||
current = song,
|
||||
history = pushHistory(it.history, it.current),
|
||||
playing = true,
|
||||
positionSeconds = 0.0,
|
||||
durationSeconds = song.duration.toDouble(),
|
||||
@@ -592,6 +603,7 @@ class AppStore(
|
||||
session = null,
|
||||
queue = emptyList(),
|
||||
current = song,
|
||||
history = pushHistory(it.history, it.current),
|
||||
playing = true,
|
||||
positionSeconds = startAtSeconds,
|
||||
durationSeconds = song.duration.toDouble(),
|
||||
@@ -760,6 +772,52 @@ class AppStore(
|
||||
playNext()
|
||||
}
|
||||
|
||||
/**
|
||||
* Back button semantics: after two seconds of playback the current track
|
||||
* restarts, otherwise the previous track is restored. The outgoing track is
|
||||
* put back at the front of the queue so nothing is lost.
|
||||
*/
|
||||
fun previous() {
|
||||
val state = _state.value
|
||||
val song = state.current ?: return
|
||||
if (state.positionSeconds > 2.0) {
|
||||
restartCurrent(song)
|
||||
return
|
||||
}
|
||||
|
||||
val target = state.history.lastOrNull()
|
||||
if (target == null) {
|
||||
restartCurrent(song)
|
||||
return
|
||||
}
|
||||
|
||||
reportCurrentProgress(completed = false)
|
||||
_state.update {
|
||||
it.copy(
|
||||
history = it.history.dropLast(1),
|
||||
queue = listOf(song) + it.queue,
|
||||
current = target,
|
||||
playing = true,
|
||||
positionSeconds = 0.0,
|
||||
durationSeconds = target.duration.toDouble(),
|
||||
)
|
||||
}
|
||||
persistPlayback()
|
||||
startSong(target)
|
||||
report("started", target.id)
|
||||
}
|
||||
|
||||
private fun restartCurrent(song: Song) {
|
||||
reportCurrentProgress(completed = false)
|
||||
if (player.canSeek) {
|
||||
player.seekTo(0.0)
|
||||
_state.update { it.copy(positionSeconds = 0.0) }
|
||||
} else {
|
||||
_state.update { it.copy(playing = true, positionSeconds = 0.0) }
|
||||
startSong(song)
|
||||
}
|
||||
}
|
||||
|
||||
fun seekTo(seconds: Double) {
|
||||
if (!player.canSeek) return
|
||||
player.seekTo(seconds)
|
||||
@@ -840,6 +898,12 @@ class AppStore(
|
||||
try {
|
||||
val bytes = api.download(song.id)
|
||||
val path = storage.saveDownload(song.id, bytes)
|
||||
if (path != null) {
|
||||
runCatching { api.cover(song.albumId, 320) }
|
||||
.getOrNull()
|
||||
?.takeIf { it.isNotEmpty() }
|
||||
?.let { storage.saveArtwork(song.id, it) }
|
||||
}
|
||||
if (path == null) {
|
||||
_state.update {
|
||||
it.copy(
|
||||
@@ -953,6 +1017,7 @@ class AppStore(
|
||||
it.copy(
|
||||
queue = it.queue.drop(1),
|
||||
current = song,
|
||||
history = pushHistory(it.history, it.current),
|
||||
playing = true,
|
||||
positionSeconds = 0.0,
|
||||
durationSeconds = song.duration.toDouble(),
|
||||
@@ -990,6 +1055,7 @@ class AppStore(
|
||||
song = song,
|
||||
local = localPath != null,
|
||||
artworkUri = api.coverUrl(song.albumId),
|
||||
artworkBytes = storage.artworkBytes(song.id),
|
||||
)
|
||||
if (startAtSeconds > 0 && player.canSeek) {
|
||||
player.seekTo(startAtSeconds)
|
||||
@@ -1073,6 +1139,7 @@ class AppStore(
|
||||
_state.update {
|
||||
it.copy(
|
||||
current = null,
|
||||
history = emptyList(),
|
||||
playing = false,
|
||||
queue = emptyList(),
|
||||
session = null,
|
||||
@@ -1105,6 +1172,7 @@ class AppStore(
|
||||
session = state.session,
|
||||
queue = state.queue,
|
||||
current = state.current,
|
||||
history = state.history,
|
||||
),
|
||||
)
|
||||
}
|
||||
@@ -1123,6 +1191,10 @@ class AppStore(
|
||||
}
|
||||
}
|
||||
|
||||
/** History is bounded so the snapshot stays small. */
|
||||
private fun pushHistory(history: List<Song>, current: Song?): List<Song> =
|
||||
if (current == null) history else (history + current).takeLast(MAX_HISTORY)
|
||||
|
||||
private fun longformFolders(): Set<Int> =
|
||||
_state.value.user?.longformFolders?.toSet().orEmpty()
|
||||
|
||||
@@ -1179,6 +1251,7 @@ private const val RECONNECT_INITIAL_MS = 15_000L
|
||||
private const val RECONNECT_MAX_MS = 300_000L
|
||||
private const val DOWNLOAD_ATTEMPTS = 3
|
||||
private const val PROGRESS_POLL_TICKS = 12
|
||||
private const val MAX_HISTORY = 50
|
||||
|
||||
private fun LongformProgressItem.toProgress(): LongformProgress = LongformProgress(
|
||||
songId = song.id,
|
||||
|
||||
+26
@@ -29,6 +29,7 @@ import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.OutlinedButton
|
||||
import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.Slider
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Switch
|
||||
import androidx.compose.material3.Text
|
||||
@@ -44,6 +45,7 @@ import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.dp
|
||||
import kotlin.math.roundToInt
|
||||
import win.taptap.musicmanager.client.api.Recommendation
|
||||
import win.taptap.musicmanager.client.api.Song
|
||||
import win.taptap.musicmanager.client.store.AppState
|
||||
@@ -74,6 +76,7 @@ fun MoodDetailScreen(state: AppState, store: AppStore) {
|
||||
var target by remember(mood.id, mood.changedAt) { mutableStateOf(mood.targetSize.toString()) }
|
||||
var hours by remember(mood.id, mood.changedAt) { mutableStateOf(mood.refreshHours.toString()) }
|
||||
var yt by remember(mood.id, mood.changedAt) { mutableStateOf(mood.ytEnabled) }
|
||||
var seedPercent by remember(mood.id, mood.changedAt) { mutableStateOf(mood.seedPercent.toFloat()) }
|
||||
|
||||
val pendingDownloads = detail.recommendations.count {
|
||||
it.download?.state == "queued" || it.download?.state == "downloading"
|
||||
@@ -109,6 +112,11 @@ fun MoodDetailScreen(state: AppState, store: AppStore) {
|
||||
Text("${mood.seedCount} seeds", style = MaterialTheme.typography.labelMedium)
|
||||
Text("·", color = MaterialTheme.colorScheme.onSurfaceVariant)
|
||||
Text("${mood.targetSize} batch", style = MaterialTheme.typography.labelMedium)
|
||||
Text("·", color = MaterialTheme.colorScheme.onSurfaceVariant)
|
||||
Text(
|
||||
"${mood.seedPercent}% seeds",
|
||||
style = MaterialTheme.typography.labelMedium,
|
||||
)
|
||||
Spacer(Modifier.weight(1f))
|
||||
Button(onClick = { store.playMood(mood.id) }) {
|
||||
Icon(Icons.Rounded.PlayArrow, contentDescription = null, modifier = Modifier.size(18.dp))
|
||||
@@ -296,6 +304,23 @@ fun MoodDetailScreen(state: AppState, store: AppStore) {
|
||||
modifier = Modifier.weight(1f),
|
||||
)
|
||||
}
|
||||
Column(verticalArrangement = Arrangement.spacedBy(2.dp)) {
|
||||
Text(
|
||||
"Seed mix: ${seedPercent.roundToInt()}% seeds · ${100 - seedPercent.roundToInt()}% recommendations",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
)
|
||||
Slider(
|
||||
value = seedPercent,
|
||||
onValueChange = { seedPercent = it },
|
||||
valueRange = 0f..100f,
|
||||
steps = 9,
|
||||
)
|
||||
Text(
|
||||
"Each queued track rolls this share independently; if one side runs out, the other fills in.",
|
||||
style = MaterialTheme.typography.labelSmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
}
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
Text("YT Music discovery", Modifier.weight(1f))
|
||||
Switch(checked = yt, onCheckedChange = { yt = it })
|
||||
@@ -312,6 +337,7 @@ fun MoodDetailScreen(state: AppState, store: AppStore) {
|
||||
targetSize = target.toIntOrNull() ?: mood.targetSize,
|
||||
refreshHours = hours.toIntOrNull() ?: mood.refreshHours,
|
||||
ytEnabled = yt,
|
||||
seedPercent = seedPercent.roundToInt(),
|
||||
)
|
||||
},
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
|
||||
@@ -31,6 +31,7 @@ import androidx.compose.material3.FilledTonalButton
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.Slider
|
||||
import androidx.compose.material3.Switch
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextButton
|
||||
@@ -46,6 +47,7 @@ import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.dp
|
||||
import kotlin.math.roundToInt
|
||||
import win.taptap.musicmanager.client.api.Mood
|
||||
import win.taptap.musicmanager.client.store.AppState
|
||||
import win.taptap.musicmanager.client.store.AppStore
|
||||
@@ -114,9 +116,9 @@ fun MoodsScreen(state: AppState, store: AppStore) {
|
||||
CreateMoodDialog(
|
||||
saving = state.saving,
|
||||
onDismiss = { creating = false },
|
||||
onCreate = { name, description, genres, query, target, hours, yt ->
|
||||
onCreate = { name, description, genres, query, target, hours, yt, seedPercent ->
|
||||
creating = false
|
||||
store.createMood(name, description, genres, query, target, hours, yt)
|
||||
store.createMood(name, description, genres, query, target, hours, yt, seedPercent)
|
||||
},
|
||||
)
|
||||
}
|
||||
@@ -184,7 +186,7 @@ private fun MoodCard(mood: Mood, onOpen: () -> Unit, onPlay: () -> Unit) {
|
||||
private fun CreateMoodDialog(
|
||||
saving: Boolean,
|
||||
onDismiss: () -> Unit,
|
||||
onCreate: (String, String?, List<String>, String?, Int, Int, Boolean) -> Unit,
|
||||
onCreate: (String, String?, List<String>, String?, Int, Int, Boolean, Int) -> Unit,
|
||||
) {
|
||||
var name by remember { mutableStateOf("") }
|
||||
var description by remember { mutableStateOf("") }
|
||||
@@ -193,6 +195,7 @@ private fun CreateMoodDialog(
|
||||
var target by remember { mutableStateOf("30") }
|
||||
var hours by remember { mutableStateOf("24") }
|
||||
var yt by remember { mutableStateOf(true) }
|
||||
var seedPercent by remember { mutableStateOf(50f) }
|
||||
|
||||
AlertDialog(
|
||||
onDismissRequest = onDismiss,
|
||||
@@ -242,6 +245,18 @@ private fun CreateMoodDialog(
|
||||
modifier = Modifier.weight(1f),
|
||||
)
|
||||
}
|
||||
Column(verticalArrangement = Arrangement.spacedBy(2.dp)) {
|
||||
Text(
|
||||
"Seed mix: ${seedPercent.roundToInt()}% seeds · ${100 - seedPercent.roundToInt()}% recommendations",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
)
|
||||
Slider(
|
||||
value = seedPercent,
|
||||
onValueChange = { seedPercent = it },
|
||||
valueRange = 0f..100f,
|
||||
steps = 9,
|
||||
)
|
||||
}
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
Text("YT Music discovery", Modifier.weight(1f))
|
||||
Switch(checked = yt, onCheckedChange = { yt = it })
|
||||
@@ -260,6 +275,7 @@ private fun CreateMoodDialog(
|
||||
target.toIntOrNull() ?: 30,
|
||||
hours.toIntOrNull() ?: 24,
|
||||
yt,
|
||||
seedPercent.roundToInt(),
|
||||
)
|
||||
},
|
||||
) { Text("Create") }
|
||||
|
||||
@@ -24,6 +24,7 @@ import androidx.compose.material.icons.rounded.PlayArrow
|
||||
import androidx.compose.material.icons.rounded.PlaylistAdd
|
||||
import androidx.compose.material.icons.rounded.QueueMusic
|
||||
import androidx.compose.material.icons.rounded.SkipNext
|
||||
import androidx.compose.material.icons.rounded.SkipPrevious
|
||||
import androidx.compose.material3.DropdownMenu
|
||||
import androidx.compose.material3.DropdownMenuItem
|
||||
import androidx.compose.material3.HorizontalDivider
|
||||
@@ -74,10 +75,14 @@ fun PlayerBar(state: AppState, store: AppStore) {
|
||||
value = dragSeconds ?: progress,
|
||||
onValueChange = { dragSeconds = it },
|
||||
onValueChangeFinished = {
|
||||
dragSeconds?.let { store.seekTo(it * state.durationSeconds) }
|
||||
dragSeconds?.let { fraction ->
|
||||
if (state.durationSeconds > 0) {
|
||||
store.seekTo(fraction * state.durationSeconds)
|
||||
}
|
||||
}
|
||||
dragSeconds = null
|
||||
},
|
||||
enabled = store.canSeek(),
|
||||
enabled = store.canSeek() && state.durationSeconds > 0,
|
||||
modifier = Modifier.weight(1f),
|
||||
)
|
||||
IconButton(onClick = { queueOpen = true }) {
|
||||
@@ -165,6 +170,9 @@ fun PlayerBar(state: AppState, store: AppStore) {
|
||||
IconButton(onClick = { picker = true }) {
|
||||
Icon(Icons.Rounded.PlaylistAdd, contentDescription = "Add to another mood")
|
||||
}
|
||||
IconButton(onClick = { store.previous() }) {
|
||||
Icon(Icons.Rounded.SkipPrevious, contentDescription = "Previous")
|
||||
}
|
||||
IconButton(onClick = { store.skip() }) {
|
||||
Icon(Icons.Rounded.SkipNext, contentDescription = "Skip")
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user