Skip to content

Commit

Permalink
Replace SharedFlow by Channel for simplicity and have the same behavi…
Browse files Browse the repository at this point in the history
…our with less code
  • Loading branch information
andremion committed Mar 12, 2024
1 parent c6ab8d9 commit c4282b9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ package io.github.andremion.musicplayer.presentation.discovery

import io.github.andremion.musicplayer.domain.MusicRepository
import io.github.andremion.musicplayer.presentation.AsyncContent
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.retryWhen
import kotlinx.coroutines.flow.shareIn
import kotlinx.coroutines.flow.stateIn
import moe.tlaster.precompose.viewmodel.ViewModel
import moe.tlaster.precompose.viewmodel.viewModelScope
Expand Down Expand Up @@ -54,17 +55,13 @@ class DiscoveryViewModel(
initialValue = DiscoveryUiState()
)

private val mutableUiEffect = MutableSharedFlow<DiscoveryUiEffect>(extraBufferCapacity = 1)
val uiEffect: SharedFlow<DiscoveryUiEffect> = mutableUiEffect
.shareIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(5_000),
)
private val uiEffectChannel = Channel<DiscoveryUiEffect>()
val uiEffect: Flow<DiscoveryUiEffect> = uiEffectChannel.consumeAsFlow()

fun onUiEvent(event: DiscoveryUiEvent) {
when (event) {
is DiscoveryUiEvent.PlaylistClick -> {
mutableUiEffect.tryEmit(DiscoveryUiEffect.NavigateToPlayer(event.playlistId))
uiEffectChannel.trySend(DiscoveryUiEffect.NavigateToPlayer(event.playlistId))
}

DiscoveryUiEvent.RetryClick -> retry.tryEmit(Unit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@ import io.github.andremion.musicplayer.domain.entity.Music
import io.github.andremion.musicplayer.domain.entity.Playlist
import io.github.andremion.musicplayer.presentation.AsyncContent
import kotlinx.coroutines.Job
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.retryWhen
import kotlinx.coroutines.flow.shareIn
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import moe.tlaster.precompose.viewmodel.ViewModel
Expand Down Expand Up @@ -84,12 +85,8 @@ class PlayerViewModel(
initialValue = PlayerUiState()
)

private val mutableUiEffect = MutableSharedFlow<PlayerUiEffect>(extraBufferCapacity = 1)
val uiEffect: SharedFlow<PlayerUiEffect> = mutableUiEffect
.shareIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(5_000),
)
private val uiEffectChannel = Channel<PlayerUiEffect>()
val uiEffect: Flow<PlayerUiEffect> = uiEffectChannel.consumeAsFlow()

fun onUiEvent(event: PlayerUiEvent) {
when (event) {
Expand Down Expand Up @@ -130,7 +127,7 @@ class PlayerViewModel(
}

PlayerUiEvent.ClearPlaylistClick -> {
mutableUiEffect.tryEmit(PlayerUiEffect.NavigateToDiscovery)
uiEffectChannel.trySend(PlayerUiEffect.NavigateToDiscovery)
}
}
}
Expand Down

0 comments on commit c4282b9

Please sign in to comment.