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

View file

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