feat: search YouTube Music and poll for the first recommendation batch
- label and document the YouTube Music search source - allow up to 60s for a search and request ten results - keep refreshing a new mood until its first recommendations arrive
This commit is contained in:
1 parent
c896f81306
commit
96ab25a91d
6 files changed
+32
-15
No files matched your search
@@ -179,9 +179,19 @@ class ApiClient(private val http: HttpClient) {
|
||||
suspend fun preview(moodId: String, limit: Int = 20): List<Song> =
|
||||
get<TracksResponse>("/moods/$moodId/preview?limit=$limit").songs
|
||||
|
||||
suspend fun search(query: String, limit: Int = 20, moodId: String? = null): List<SearchResult> {
|
||||
suspend fun search(query: String, limit: Int = 10, moodId: String? = null): List<SearchResult> {
|
||||
val mood = moodId?.let { "&moodId=${it.encodeURLParameter()}" }.orEmpty()
|
||||
return get<SearchResponse>("/search?q=${query.encodeURLParameter()}&limit=$limit$mood").results
|
||||
return parse<SearchResponse>(
|
||||
http.get(url("/search?q=${query.encodeURLParameter()}&limit=$limit$mood")) {
|
||||
auth()
|
||||
// yt-dlp resolves every YouTube Music hit to get artist, album
|
||||
// and duration, so a search takes longer than a plain API call.
|
||||
timeout {
|
||||
requestTimeoutMillis = 60_000
|
||||
socketTimeoutMillis = 60_000
|
||||
}
|
||||
},
|
||||
).results
|
||||
}
|
||||
|
||||
suspend fun startSession(moodId: String, limit: Int = 12): Session =
|
||||
|
||||
@@ -363,7 +363,7 @@ class AppStore(
|
||||
delay(SEARCH_DEBOUNCE_MS)
|
||||
_state.update { it.copy(searching = true) }
|
||||
try {
|
||||
val results = api.search(query, 20, _state.value.searchTargetMoodId)
|
||||
val results = api.search(query, SEARCH_LIMIT, _state.value.searchTargetMoodId)
|
||||
_state.update { it.copy(searchResults = results) }
|
||||
} catch (t: CancellationException) {
|
||||
throw t
|
||||
@@ -1357,5 +1357,6 @@ private const val DOWNLOAD_ATTEMPTS = 3
|
||||
private const val PROGRESS_POLL_TICKS = 12
|
||||
private const val MAX_HISTORY = 50
|
||||
private const val SEARCH_DEBOUNCE_MS = 300L
|
||||
private const val SEARCH_LIMIT = 10
|
||||
private const val DOWNLOAD_POLL_MS = 4_000L
|
||||
private const val DOWNLOAD_POLLS = 60
|
||||
+5
-2
@@ -84,9 +84,12 @@ fun MoodDetailScreen(state: AppState, store: AppStore) {
|
||||
detail.recommendations.count {
|
||||
it.download?.state == "queued" || it.download?.state == "downloading"
|
||||
}
|
||||
LaunchedEffect(mood.id, inFlight) {
|
||||
// A new mood gets its first batch from a background discovery run, which
|
||||
// resolves the YouTube Music search. Keep refreshing until it lands.
|
||||
val waitingForFirstBatch = mood.recommendationCount > 0 && detail.recommendations.isEmpty()
|
||||
LaunchedEffect(mood.id, inFlight, waitingForFirstBatch) {
|
||||
var polls = 0
|
||||
while (inFlight > 0 && polls < 60) {
|
||||
while ((inFlight > 0 || waitingForFirstBatch) && polls < 60) {
|
||||
kotlinx.coroutines.delay(5_000)
|
||||
store.refreshSelectedMood()
|
||||
polls += 1
|
||||
|
||||
@@ -99,7 +99,7 @@ fun SearchScreen(state: AppState, store: AppStore) {
|
||||
OutlinedTextField(
|
||||
value = state.searchQuery,
|
||||
onValueChange = store::setSearchQuery,
|
||||
label = { Text("Search YouTube") },
|
||||
label = { Text("Search YouTube Music") },
|
||||
singleLine = true,
|
||||
leadingIcon = { Icon(Icons.Rounded.Search, contentDescription = null) },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
@@ -137,7 +137,7 @@ fun SearchScreen(state: AppState, store: AppStore) {
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
CircularProgressIndicator(Modifier.size(18.dp), strokeWidth = 2.dp)
|
||||
Spacer(Modifier.width(10.dp))
|
||||
Text("Searching YouTube…", style = MaterialTheme.typography.bodySmall)
|
||||
Text("Searching YouTube Music…", style = MaterialTheme.typography.bodySmall)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user