diff --git a/README.md b/README.md index 946c9b3..6a007d8 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,10 @@ a server-driven radio session and songs are added from Deezer or YouTube Music. - Add to mood; - Not interested (permanent for the mood); - Retry for failed downloads. + A pick is replaced automatically after `recommendationRotationPlays` plays + (default 3, configurable per mood), and **All new** on the mood page swaps the + whole list at once. **Refresh** only discovers and tops up empty slots, which + is why it usually kept the same songs. - **Radio** mixes the collection and ready recommendations using the mood's `collectionPercent`. The app keeps a small queue and asks for more from the same mood session. @@ -58,8 +62,9 @@ a server-driven radio session and songs are added from Deezer or YouTube Music. - **Scrubbing**: the progress slider can be dragged on every platform. Android and web seek natively; desktop restarts the decoder at the requested position. - **Mood settings** live on their own screen: name, description, discovery hint, - genres, source, collection share, recommendations, refresh interval, a - **Keep this mood offline** switch, and Delete at the bottom. + genres, source, collection share, recommendation count and replacement + threshold, refresh interval, a **Keep this mood offline** switch, and Delete + at the bottom. - **Downloads** screen for device-side copies: play, delete, clear, storage usage. - **Diagnostics**: warnings, errors and crashes are kept in a 500-entry local @@ -212,7 +217,7 @@ Tagged builds (`v*`) publish to internal testing from CI. The Gitea secrets are: | `ANDROID_KEY_PASSWORD` | key password | | `PLAY_SERVICE_ACCOUNT_JSON` | contents of the service account JSON | -Then `git tag v0.4.3 && git push --tags` builds, signs and uploads the bundle. +Then `git tag v0.5.0 && git push --tags` builds, signs and uploads the bundle. Gradle Play Publisher asks Play for the next free `versionCode` (`ResolutionStrategy.AUTO`), so only the tag/version name needs to change. diff --git a/composeApp/build.gradle.kts b/composeApp/build.gradle.kts index 6e8040f..02653a2 100644 --- a/composeApp/build.gradle.kts +++ b/composeApp/build.gradle.kts @@ -41,7 +41,7 @@ val hasUploadKey = uploadKeyStore?.exists() == true && val appVersionCode = (System.getenv("VERSION_CODE") ?: localProperties.getProperty("VERSION_CODE") ?: "1").toInt() val appVersionName = System.getenv("VERSION_NAME") ?: localProperties.getProperty("VERSION_NAME") - ?: "0.4.3" + ?: "0.5.0" kotlin { androidTarget { diff --git a/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/BuildInfo.kt b/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/BuildInfo.kt index 415b3b6..ab52c21 100644 --- a/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/BuildInfo.kt +++ b/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/BuildInfo.kt @@ -1,4 +1,4 @@ package win.taptap.musicmanager.client /** One place for the version shown in Settings and sent with diagnostics. */ -const val APP_VERSION = "0.4.3" +const val APP_VERSION = "0.5.0" diff --git a/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/api/ApiClient.kt b/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/api/ApiClient.kt index 9d25ec8..64c8f56 100644 --- a/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/api/ApiClient.kt +++ b/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/api/ApiClient.kt @@ -164,6 +164,19 @@ class ApiClient(private val http: HttpClient) { }, ) + /** Retires every active pick and asks the server for different ones. */ + suspend fun rotateRecommendations(moodId: String): RecommendationRotation = + parse( + http.post(url("/moods/$moodId/recommendations/rotate")) { + auth() + // The server searches and queues downloads for the new picks. + timeout { + requestTimeoutMillis = 120_000 + socketTimeoutMillis = 120_000 + } + }, + ) + suspend fun addRecommendation(moodId: String, externalId: String): RecommendationDecision = parse( http.post(url("/moods/$moodId/recommendations/$externalId/add")) { diff --git a/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/api/Models.kt b/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/api/Models.kt index f2d340c..2d7a831 100644 --- a/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/api/Models.kt +++ b/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/api/Models.kt @@ -98,6 +98,8 @@ data class Mood( val collectionPercent: Int = 50, val recommendationPercent: Int = 50, val recommendationCount: Int = 10, + /** Plays before a pick is replaced; 0 = never rotate. */ + val recommendationRotationPlays: Int = 3, val refreshHours: Int = 24, val lastDiscoveryAt: String? = null, val createdAt: String, @@ -139,6 +141,7 @@ data class Recommendation( val error: String? = null, val firstSeen: String = "", val lastSeen: String = "", + val playCount: Int = 0, ) @Serializable @@ -174,6 +177,14 @@ data class RecommendationDecision( val recommendation: Recommendation, ) +@Serializable +data class RecommendationRotation( + val detail: MoodDetail, + val rotated: Int = 0, + val active: Int = 0, + val error: String? = null, +) + @Serializable data class SearchResult( val source: String = "youtube", @@ -257,6 +268,7 @@ data class CreateMoodRequest( val source: String? = null, val collectionPercent: Int = 50, val recommendationCount: Int = 10, + val recommendationRotationPlays: Int = 3, val refreshHours: Int = 24, ) @@ -271,6 +283,7 @@ data class UpdateMoodRequest( val source: String? = null, val collectionPercent: Int? = null, val recommendationCount: Int? = null, + val recommendationRotationPlays: Int? = null, val refreshHours: Int? = null, ) diff --git a/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/store/AppStore.kt b/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/store/AppStore.kt index dbdda5c..b5892ca 100644 --- a/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/store/AppStore.kt +++ b/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/store/AppStore.kt @@ -94,6 +94,8 @@ data class AppState( val loading: Boolean = false, val saving: Boolean = false, val discovering: Boolean = false, + /** True while every recommendation is being replaced. */ + val rotatingPicks: Boolean = false, val message: String? = null, val messageIsError: Boolean = false, val messageId: Long = 0, @@ -847,6 +849,7 @@ class AppStore( source: String, collectionPercent: Int, recommendationCount: Int, + recommendationRotationPlays: Int, refreshHours: Int, ) { scope.launch { @@ -861,6 +864,7 @@ class AppStore( source = source, collectionPercent = collectionPercent, recommendationCount = recommendationCount, + recommendationRotationPlays = recommendationRotationPlays, refreshHours = refreshHours, ), ) @@ -928,6 +932,7 @@ class AppStore( source: String, collectionPercent: Int, recommendationCount: Int, + recommendationRotationPlays: Int, refreshHours: Int, ) { scope.launch { @@ -945,6 +950,7 @@ class AppStore( source = source, collectionPercent = collectionPercent, recommendationCount = recommendationCount, + recommendationRotationPlays = recommendationRotationPlays, refreshHours = refreshHours, ), ) @@ -1043,6 +1049,41 @@ class AppStore( } } + /** + * Asks the server to retire every active pick and fill the slots with other + * tracks. Plain refresh only tops up empty slots, which is why it usually + * returned the same list. + */ + fun rotateRecommendations() { + val moodId = _state.value.selectedMood?.mood?.id ?: return + if (_state.value.rotatingPicks || _state.value.discovering) return + scope.launch { + _state.update { it.copy(rotatingPicks = true) } + try { + val result = api.rotateRecommendations(moodId) + if (_state.value.selectedMood?.mood?.id == moodId) { + applyMoodDetail(result.detail) + } + _state.update { + it.copy( + rotatingPicks = false, + message = when { + result.error != null -> + "Swapped ${result.rotated} picks, but new ones failed: ${result.error}" + result.active > 0 -> "${result.active} new recommendations" + else -> "No different picks were available" + }, + messageIsError = result.error != null, + messageId = it.messageId + 1, + ) + } + } catch (t: Throwable) { + _state.update { it.copy(rotatingPicks = false) } + message(t) + } + } + } + fun addRecommendation(externalId: String) { val moodId = _state.value.selectedMood?.mood?.id ?: return if (_state.value.discovering) return diff --git a/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/ui/MoodDetailScreen.kt b/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/ui/MoodDetailScreen.kt index 1ee526b..f017028 100644 --- a/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/ui/MoodDetailScreen.kt +++ b/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/ui/MoodDetailScreen.kt @@ -25,6 +25,7 @@ import androidx.compose.material.icons.rounded.Refresh import androidx.compose.material.icons.rounded.RemoveCircleOutline import androidx.compose.material.icons.rounded.Search import androidx.compose.material.icons.rounded.Settings +import androidx.compose.material.icons.rounded.Shuffle import androidx.compose.material3.Button import androidx.compose.material3.CircularProgressIndicator import androidx.compose.material3.DropdownMenu @@ -39,7 +40,6 @@ import androidx.compose.material3.OutlinedTextField import androidx.compose.material3.Slider import androidx.compose.material3.Surface import androidx.compose.material3.Text -import androidx.compose.material3.TextButton import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue @@ -186,19 +186,41 @@ fun MoodDetailScreen(state: AppState, store: AppStore) { item { HorizontalDivider(Modifier.padding(vertical = 12.dp)) SectionTitle("Recommendations") { - Row(verticalAlignment = Alignment.CenterVertically) { - if (state.discovering) { + if (state.discovering || state.rotatingPicks) { + CircularProgressIndicator(Modifier.size(16.dp), strokeWidth = 2.dp) + } + } + } + + item { + val busy = state.discovering || state.rotatingPicks + Row( + modifier = Modifier.fillMaxWidth(), + horizontalArrangement = Arrangement.spacedBy(10.dp), + ) { + OutlinedButton( + onClick = { store.refreshRecommendations() }, + enabled = !busy && mood.recommendationCount > 0, + ) { + Icon(Icons.Rounded.Refresh, contentDescription = null, modifier = Modifier.size(16.dp)) + Spacer(Modifier.width(6.dp)) + Text("Refresh") + } + FilledTonalButton( + onClick = { store.rotateRecommendations() }, + enabled = !busy && mood.recommendationCount > 0, + ) { + if (state.rotatingPicks) { CircularProgressIndicator(Modifier.size(16.dp), strokeWidth = 2.dp) - Spacer(Modifier.width(8.dp)) - } - TextButton( - onClick = { store.refreshRecommendations() }, - enabled = !state.discovering && mood.recommendationCount > 0, - ) { - Icon(Icons.Rounded.Refresh, contentDescription = null, modifier = Modifier.size(16.dp)) - Spacer(Modifier.width(4.dp)) - Text("Refresh") + } else { + Icon( + Icons.Rounded.Shuffle, + contentDescription = null, + modifier = Modifier.size(16.dp), + ) } + Spacer(Modifier.width(6.dp)) + Text("All new") } } } diff --git a/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/ui/MoodSettingsScreen.kt b/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/ui/MoodSettingsScreen.kt index 5716f41..b32db1b 100644 --- a/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/ui/MoodSettingsScreen.kt +++ b/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/ui/MoodSettingsScreen.kt @@ -69,6 +69,9 @@ fun MoodSettingsScreen(state: AppState, store: AppStore) { var sourceMenu by remember { mutableStateOf(false) } var collectionPercent by remember(mood.id) { mutableStateOf(mood.collectionPercent.toFloat()) } var recommendations by remember(mood.id) { mutableStateOf(mood.recommendationCount.toFloat()) } + var rotationPlays by remember(mood.id) { + mutableStateOf(mood.recommendationRotationPlays.toFloat()) + } var hours by remember(mood.id) { mutableStateOf(mood.refreshHours.toString()) } var confirmDelete by remember { mutableStateOf(false) } val offline = mood.id in state.offlineMoods @@ -264,6 +267,32 @@ fun MoodSettingsScreen(state: AppState, store: AppStore) { } } + item { + val plays = rotationPlays.roundToInt() + Column(verticalArrangement = Arrangement.spacedBy(2.dp)) { + Text( + if (plays == 0) { + "Replace picks: never" + } else { + "Replace each pick after $plays ${if (plays == 1) "play" else "plays"}" + }, + style = MaterialTheme.typography.bodySmall, + ) + Slider( + value = rotationPlays, + onValueChange = { rotationPlays = it }, + valueRange = 0f..10f, + steps = 9, + ) + Text( + "A recommendation makes way for a different song once it has been played " + + "this often. Use All new on the mood page to swap them immediately.", + style = MaterialTheme.typography.labelSmall, + color = MaterialTheme.colorScheme.onSurfaceVariant, + ) + } + } + item { OutlinedTextField( value = hours, @@ -287,6 +316,7 @@ fun MoodSettingsScreen(state: AppState, store: AppStore) { source = source, collectionPercent = collectionPercent.roundToInt(), recommendationCount = recommendations.roundToInt(), + recommendationRotationPlays = rotationPlays.roundToInt(), refreshHours = hours.toIntOrNull() ?: mood.refreshHours, ) }, diff --git a/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/ui/MoodsScreen.kt b/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/ui/MoodsScreen.kt index e63c6b7..09fc9ba 100644 --- a/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/ui/MoodsScreen.kt +++ b/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/ui/MoodsScreen.kt @@ -129,7 +129,8 @@ fun MoodsScreen(state: AppState, store: AppStore) { source, collectionPercent, recommendations, - hours, + recommendationRotationPlays = 3, + refreshHours = hours, ) }, ) diff --git a/composeApp/src/commonTest/kotlin/win/taptap/musicmanager/client/AppStoreTest.kt b/composeApp/src/commonTest/kotlin/win/taptap/musicmanager/client/AppStoreTest.kt index fcc2a36..0485eef 100644 --- a/composeApp/src/commonTest/kotlin/win/taptap/musicmanager/client/AppStoreTest.kt +++ b/composeApp/src/commonTest/kotlin/win/taptap/musicmanager/client/AppStoreTest.kt @@ -583,6 +583,55 @@ class AppStoreTest { AppLog.clear() } + @Test + fun `all new replaces recommendations through the rotate endpoint`() = runTest { + val storage = MemoryStorage() + val cache = AppCache(storage) + cache.saveSession(SessionState("http://music", "token", User("alice", admin = false))) + val fresh = """{"source":"youtube","externalId":"v2","title":"Fresh Pick", + "artist":"Artist","duration":180,"state":"downloading","playCount":0, + "firstSeen":"2026-01-01T00:00:00Z","lastSeen":"2026-01-01T00:00:00Z"}""" + var rotateCalls = 0 + val engine = MockEngine { request -> + when (request.url.encodedPath) { + "/api/v1/me" -> respond( + """{"name":"alice","admin":false}""", + HttpStatusCode.OK, + jsonHeaders, + ) + "/api/v1/moods" -> respond("""{"moods":[${moodJson(1)}]}""", HttpStatusCode.OK, jsonHeaders) + "/api/v1/moods/mo-1" -> respond( + detailJson(listOf("sg-1"), recommendationJson("ready", "sg-1")), + HttpStatusCode.OK, + jsonHeaders, + ) + "/api/v1/moods/mo-1/recommendations/rotate" -> { + rotateCalls += 1 + respond( + """{"detail":${detailJson(listOf("sg-1"), fresh)}, + "rotated":1,"active":1}""", + HttpStatusCode.OK, + jsonHeaders, + ) + } + else -> respondError(HttpStatusCode.NotFound) + } + } + val store = store(storage, cache, FakePlayer(), engine) + store.openMood("mo-1") + awaitState { store.state.value.selectedMood != null } + + store.rotateRecommendations() + awaitState("rotate finished") { + rotateCalls == 1 && store.state.value.message?.contains("new recommendations") == true + } + + val detail = assertNotNull(store.state.value.selectedMood) + assertEquals(listOf("v2"), detail.recommendations.map { it.externalId }) + assertFalse(store.state.value.rotatingPicks) + store.release() + } + @Test fun `logout clears the session and the mood cache`() = runTest { val storage = MemoryStorage()