Continue removing Outcome class in favor of Result (search suggestions)

This commit is contained in:
vfsfitvnm 2022-07-01 19:41:52 +02:00
parent 14f46429ef
commit 17cf2454c7
2 changed files with 23 additions and 22 deletions

View file

@ -34,13 +34,12 @@ import it.vfsfitvnm.route.RouteHandler
import it.vfsfitvnm.vimusic.Database
import it.vfsfitvnm.vimusic.R
import it.vfsfitvnm.vimusic.query
import it.vfsfitvnm.vimusic.ui.components.OutcomeItem
import it.vfsfitvnm.vimusic.ui.components.TopAppBar
import it.vfsfitvnm.vimusic.ui.components.themed.LoadingOrError
import it.vfsfitvnm.vimusic.ui.styling.LocalColorPalette
import it.vfsfitvnm.vimusic.ui.styling.LocalTypography
import it.vfsfitvnm.vimusic.utils.medium
import it.vfsfitvnm.vimusic.utils.secondary
import it.vfsfitvnm.youtubemusic.Outcome
import it.vfsfitvnm.youtubemusic.YouTube
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
@ -68,8 +67,8 @@ fun SearchScreen(
FocusRequester()
}
val searchSuggestions by produceState<Outcome<List<String>?>>(
initialValue = Outcome.Initial,
val searchSuggestionsResult by produceState<Result<List<String>?>?>(
initialValue = null,
key1 = textFieldValue
) {
value = if (textFieldValue.text.isNotEmpty()) {
@ -77,7 +76,7 @@ fun SearchScreen(
YouTube.getSearchSuggestions(textFieldValue.text)
}
} else {
Outcome.Initial
null
}
}
@ -304,10 +303,8 @@ fun SearchScreen(
}
}
OutcomeItem(
outcome = searchSuggestions
) { suggestions ->
suggestions?.forEach { suggestion ->
searchSuggestionsResult?.getOrNull()?.let { suggestions ->
suggestions.forEach { suggestion ->
Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier
@ -352,6 +349,8 @@ fun SearchScreen(
)
}
}
} ?: searchSuggestionsResult?.exceptionOrNull()?.let { throwable ->
LoadingOrError(errorMessage = throwable.javaClass.canonicalName) {}
}
}
}

View file

@ -420,19 +420,21 @@ object YouTube {
}.recoverIfCancelled()
}
suspend fun getSearchSuggestions(input: String): Outcome<List<String>?> {
return client.postCatching("/youtubei/v1/music/get_search_suggestions") {
contentType(ContentType.Application.Json)
setBody(
GetSearchSuggestionsBody(
context = Context.DefaultWeb,
input = input
suspend fun getSearchSuggestions(input: String): Result<List<String>?>? {
return runCatching {
val body = client.post("/youtubei/v1/music/get_search_suggestions") {
contentType(ContentType.Application.Json)
setBody(
GetSearchSuggestionsBody(
context = Context.DefaultWeb,
input = input
)
)
)
parameter("key", Key)
parameter("prettyPrint", false)
}.bodyCatching<GetSearchSuggestionsResponse>().map { response ->
response
parameter("key", Key)
parameter("prettyPrint", false)
}.body<GetSearchSuggestionsResponse>()
body
.contents
?.flatMap { content ->
content
@ -445,7 +447,7 @@ object YouTube {
?.query
}
}
}
}.recoverIfCancelled()
}
suspend fun player(videoId: String, playlistId: String? = null): Outcome<PlayerResponse> {