Created
January 23, 2023 16:25
-
-
Save alexzaitsev/41afe68c9313eff991ca0f0064711f81 to your computer and use it in GitHub Desktop.
Song Details screen
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.view.screen.song.details | |
data class SongDetailsViewState( | |
val author: Author, | |
val song: Song, | |
val loading: Boolean | |
) | |
sealed class SongDetailsSideEffect { | |
object ShowNetworkError : SongDetailsSideEffect() | |
} | |
sealed class SongDetailsUserAction { | |
object Play: SongDetailsUserAction() | |
object Pause: SongDetailsUserAction() | |
object Like: SongDetailsUserAction() | |
object Dislike: SongDetailsUserAction() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.view.screen.song.details | |
import androidx.compose.runtime.Composable | |
import org.koin.androidx.compose.get | |
import org.orbitmvi.orbit.compose.collectAsState | |
import org.orbitmvi.orbit.compose.collectSideEffect | |
@Composable | |
fun SongDetailsScreen( | |
onBackClicked: () -> Unit, | |
onShowNetworkError: () -> Unit | |
) { | |
val viewModel = get<SongDetailsViewModel>() | |
SideEffect( | |
viewModel = viewModel, | |
onShowNetworkError = onShowNetworkError | |
) | |
ViewState( | |
viewModel = viewModel, | |
onBackClicked = onBackClicked | |
) | |
} | |
@Composable | |
private fun SideEffect( | |
viewModel: SongDetailsViewModel, | |
onShowNetworkError: () -> Unit | |
) = viewModel.collectSideEffect { effect -> | |
return@collectSideEffect when (effect) { | |
SongDetailsSideEffect.ShowNetworkError -> onShowNetworkError() | |
} | |
} | |
@Composable | |
private fun ViewState( | |
viewModel: SongDetailsViewModel, | |
onBackClicked: () -> Unit | |
) = SongDetailsViewState( | |
state = viewModel.collectAsState().value, | |
onBackClicked = onBackClicked, | |
onPlayClicked = { viewModel.sendAction(SongDetailsUserAction.Play) }, | |
onPauseClicked = { viewModel.sendAction(SongDetailsUserAction.Pause) }, | |
onLikeClicked = { viewModel.sendAction(SongDetailsUserAction.Like) }, | |
onDislikeClicked = { viewModel.sendAction(SongDetailsUserAction.Dislike) } | |
) | |
@Composable | |
private fun SongDetailsViewState( | |
viewState: SongDetailsViewState, | |
onBackClicked: () -> Unit, | |
onPlayClicked: () -> Unit, | |
onPauseClicked: () -> Unit, | |
onLikeClicked: () -> Unit, | |
onDislikeClicked: () -> Unit | |
) { | |
// draw your UI here | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.view.screen.song.details | |
import com.example.view.screen.BaseViewModel | |
class SongDetailsViewModel : | |
BaseViewModel<SongDetailsViewState, SongDetailsSideEffect, SongDetailsUserAction> | |
(initialViewState = SongDetailsViewState(null, null, true)) { | |
override fun processUserAction(action: SongDetailsUserAction) = when (action) { | |
SongDetailsUserAction.Dislike -> dislike() | |
SongDetailsUserAction.Like -> like() | |
SongDetailsUserAction.Pause -> pause() | |
SongDetailsUserAction.Play -> play() | |
} | |
private fun dislike() { | |
// TODO call a UseCase here and change ViewState | |
} | |
private fun like() { | |
// TODO call a UseCase here and change ViewState | |
} | |
private fun pause() { | |
// TODO call a UseCase here and change ViewState | |
} | |
private fun play() { | |
// TODO call a UseCase here and change ViewState | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment