Start removing Outcome class in favor of Result
This commit is contained in:
@@ -17,17 +17,16 @@ import androidx.compose.ui.unit.dp
|
||||
import com.valentinilk.shimmer.shimmer
|
||||
import it.vfsfitvnm.vimusic.R
|
||||
import it.vfsfitvnm.vimusic.ui.components.Message
|
||||
import it.vfsfitvnm.vimusic.ui.components.OutcomeItem
|
||||
import it.vfsfitvnm.vimusic.ui.components.themed.TextFieldDialog
|
||||
import it.vfsfitvnm.vimusic.ui.components.themed.TextPlaceholder
|
||||
import it.vfsfitvnm.vimusic.ui.styling.LocalTypography
|
||||
import it.vfsfitvnm.vimusic.utils.center
|
||||
import it.vfsfitvnm.vimusic.utils.secondary
|
||||
import it.vfsfitvnm.youtubemusic.Outcome
|
||||
|
||||
|
||||
@Composable
|
||||
fun LyricsView(
|
||||
lyricsOutcome: Outcome<String>,
|
||||
lyrics: String?,
|
||||
onInitialize: () -> Unit,
|
||||
onSearchOnline: () -> Unit,
|
||||
onLyricsUpdate: (String) -> Unit,
|
||||
@@ -42,7 +41,7 @@ fun LyricsView(
|
||||
if (isEditingLyrics) {
|
||||
TextFieldDialog(
|
||||
hintText = "Enter the lyrics",
|
||||
initialTextInput = lyricsOutcome.valueOrNull ?: "",
|
||||
initialTextInput = lyrics ?: "",
|
||||
singleLine = false,
|
||||
maxLines = 10,
|
||||
isTextInputValid = { true },
|
||||
@@ -53,26 +52,7 @@ fun LyricsView(
|
||||
)
|
||||
}
|
||||
|
||||
OutcomeItem(
|
||||
outcome = lyricsOutcome,
|
||||
onInitialize = onInitialize,
|
||||
onLoading = {
|
||||
Column(
|
||||
verticalArrangement = Arrangement.Center,
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.shimmer()
|
||||
) {
|
||||
repeat(16) { index ->
|
||||
TextPlaceholder(
|
||||
modifier = Modifier
|
||||
.alpha(1f - index * 0.05f)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
) { lyrics ->
|
||||
if (lyrics != null ) {
|
||||
Column(
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
modifier = Modifier
|
||||
@@ -125,5 +105,22 @@ fun LyricsView(
|
||||
)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
SideEffect(onInitialize)
|
||||
|
||||
Column(
|
||||
verticalArrangement = Arrangement.Center,
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.shimmer()
|
||||
) {
|
||||
repeat(16) { index ->
|
||||
TextPlaceholder(
|
||||
modifier = Modifier
|
||||
.alpha(1f - index * 0.05f)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,11 +34,11 @@ import it.vfsfitvnm.vimusic.ui.components.BottomSheetState
|
||||
import it.vfsfitvnm.vimusic.ui.screens.rememberLyricsRoute
|
||||
import it.vfsfitvnm.vimusic.ui.styling.LocalColorPalette
|
||||
import it.vfsfitvnm.vimusic.ui.styling.LocalTypography
|
||||
import it.vfsfitvnm.vimusic.utils.*
|
||||
import it.vfsfitvnm.youtubemusic.Outcome
|
||||
import it.vfsfitvnm.vimusic.utils.PlayerState
|
||||
import it.vfsfitvnm.vimusic.utils.center
|
||||
import it.vfsfitvnm.vimusic.utils.color
|
||||
import it.vfsfitvnm.vimusic.utils.medium
|
||||
import it.vfsfitvnm.youtubemusic.YouTube
|
||||
import it.vfsfitvnm.youtubemusic.isEvaluable
|
||||
import it.vfsfitvnm.youtubemusic.toNotNull
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
@@ -62,8 +62,8 @@ fun PlayerBottomSheet(
|
||||
|
||||
var route by rememberRoute()
|
||||
|
||||
var nextOutcome by remember(playerState?.mediaItem?.mediaId) {
|
||||
mutableStateOf<Outcome<YouTube.NextResult>>(Outcome.Initial)
|
||||
var nextResult by remember(playerState?.mediaItem?.mediaId) {
|
||||
mutableStateOf<Result<YouTube.NextResult>?>(null)
|
||||
}
|
||||
|
||||
BottomSheet(
|
||||
@@ -150,8 +150,8 @@ fun PlayerBottomSheet(
|
||||
}
|
||||
}
|
||||
) {
|
||||
var lyricsOutcome by remember(song) {
|
||||
mutableStateOf(song?.lyrics?.let { Outcome.Success(it) } ?: Outcome.Initial)
|
||||
var lyricsResult by remember(song) {
|
||||
mutableStateOf(song?.lyrics?.let { Result.success(it) })
|
||||
}
|
||||
|
||||
RouteHandler(
|
||||
@@ -181,21 +181,16 @@ fun PlayerBottomSheet(
|
||||
val context = LocalContext.current
|
||||
|
||||
LyricsView(
|
||||
lyricsOutcome = lyricsOutcome,
|
||||
lyrics = lyricsResult?.getOrNull(),
|
||||
nestedScrollConnectionProvider = layoutState::nestedScrollConnection,
|
||||
onInitialize = {
|
||||
coroutineScope.launch(Dispatchers.Main) {
|
||||
lyricsOutcome = Outcome.Loading
|
||||
|
||||
val mediaItem = player?.currentMediaItem!!
|
||||
|
||||
if (nextOutcome.isEvaluable) {
|
||||
nextOutcome = Outcome.Loading
|
||||
|
||||
|
||||
if (nextResult == null) {
|
||||
val mediaItemIndex = player.currentMediaItemIndex
|
||||
|
||||
nextOutcome = withContext(Dispatchers.IO) {
|
||||
nextResult = withContext(Dispatchers.IO) {
|
||||
YouTube.next(
|
||||
mediaItem.mediaId,
|
||||
mediaItem.mediaMetadata.extras?.getString("playlistId"),
|
||||
@@ -204,11 +199,9 @@ fun PlayerBottomSheet(
|
||||
}
|
||||
}
|
||||
|
||||
lyricsOutcome = nextOutcome.flatMap {
|
||||
it.lyrics?.text().toNotNull()
|
||||
}.map { lyrics ->
|
||||
lyrics ?: ""
|
||||
}.map { lyrics ->
|
||||
lyricsResult = nextResult?.map { nextResult ->
|
||||
nextResult.lyrics?.text()?.getOrNull() ?: ""
|
||||
}?.map { lyrics ->
|
||||
query {
|
||||
song?.let {
|
||||
Database.update(song.copy(lyrics = lyrics))
|
||||
|
||||
@@ -5,6 +5,7 @@ import it.vfsfitvnm.youtubemusic.YouTube
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
|
||||
data class YouTubeRadio(
|
||||
private val videoId: String? = null,
|
||||
private val playlistId: String? = null,
|
||||
@@ -23,11 +24,11 @@ data class YouTubeRadio(
|
||||
params = parameters,
|
||||
playlistSetVideoId = playlistSetVideoId,
|
||||
continuation = nextContinuation
|
||||
)
|
||||
}.map { nextResult ->
|
||||
mediaItems = nextResult.items?.map(YouTube.Item.Song::asMediaItem)
|
||||
nextResult.continuation?.takeUnless { nextContinuation == nextResult.continuation }
|
||||
}.recoverWith(nextContinuation).valueOrNull
|
||||
)?.getOrNull()?.let { nextResult ->
|
||||
mediaItems = nextResult.items?.map(YouTube.Item.Song::asMediaItem)
|
||||
nextResult.continuation?.takeUnless { nextContinuation == nextResult.continuation }
|
||||
}
|
||||
}
|
||||
|
||||
return mediaItems ?: emptyList()
|
||||
}
|
||||
Reference in New Issue
Block a user