Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workaround to fix play item by index #494

Merged
merged 1 commit into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,32 @@ import androidx.compose.ui.focus.FocusRequester
import androidx.wear.compose.material.ScalingLazyListState
import com.google.android.horologist.compose.layout.StateUtils
import com.google.android.horologist.media.ui.screens.entity.EntityScreen
import com.google.android.horologist.media.ui.state.model.DownloadMediaUiModel
import com.google.android.horologist.media.ui.state.model.PlaylistUiModel

@Composable
fun UampEntityScreen(
uampEntityScreenViewModel: UampEntityScreenViewModel,
onDownloadItemClick: (DownloadMediaUiModel) -> Unit,
onDownloadItemClick: () -> Unit,
onShuffleClick: (PlaylistUiModel) -> Unit,
onPlayClick: (PlaylistUiModel) -> Unit,
focusRequester: FocusRequester,
scalingLazyListState: ScalingLazyListState
) {
val uiState by StateUtils.rememberStateWithLifecycle(flow = uampEntityScreenViewModel.uiState)

// https://github.com/google/horologist/issues/496
val finishedPlayExecution by StateUtils.rememberStateWithLifecycle(flow = uampEntityScreenViewModel.finishedPlayExecution)
if (finishedPlayExecution) {
onDownloadItemClick()
}

EntityScreen(
entityScreenState = uiState,
onDownloadClick = {
uampEntityScreenViewModel.download()
},
onDownloadItemClick = {
uampEntityScreenViewModel.play(it.mediaUiModel.id)
onDownloadItemClick(it)
},
onShuffleClick = {
uampEntityScreenViewModel.shufflePlay()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ import com.google.android.horologist.mediasample.domain.model.PlaylistDownload
import com.google.android.horologist.mediasample.ui.mapper.DownloadMediaUiModelMapper
import com.google.android.horologist.mediasample.ui.mapper.PlaylistUiModelMapper
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
Expand Down Expand Up @@ -63,6 +66,9 @@ class UampEntityScreenViewModel @Inject constructor(
EntityScreenState.Loading(playlistName)
)

// https://github.com/google/horologist/issues/496
val finishedPlayExecution: MutableStateFlow<Boolean> = MutableStateFlow(false)

fun play(mediaId: String? = null) {
play(shuffled = false, mediaId = mediaId)
}
Expand All @@ -78,11 +84,18 @@ class UampEntityScreenViewModel @Inject constructor(
.coerceAtLeast(0)

playerRepository.setShuffleModeEnabled(shuffled)

playerRepository.setMediaList(playlistDownload.playlist.mediaList)
playerRepository.seekToDefaultPosition(index)
playerRepository.prepare()
playerRepository.play()

// https://github.com/google/horologist/issues/496
viewModelScope.launch {
delay(100)

playerRepository.seekToDefaultPosition(index)
playerRepository.prepare()
playerRepository.play()

finishedPlayExecution.value = true
}
}
}

Expand Down