refactor: simplify PlayerViewModel initialization by loading record via DI
This commit is contained in:
@@ -29,18 +29,18 @@ import fr.ajaury.gwenedeg.player.ui.viewmodel.PlayerViewModel
|
|||||||
import fr.ajaury.gwenedeg.subtitle.domain.SubtitleLine
|
import fr.ajaury.gwenedeg.subtitle.domain.SubtitleLine
|
||||||
import fr.ajaury.gwenedeg.theme.GwenedegTheme
|
import fr.ajaury.gwenedeg.theme.GwenedegTheme
|
||||||
import org.koin.compose.viewmodel.koinViewModel
|
import org.koin.compose.viewmodel.koinViewModel
|
||||||
|
import org.koin.core.parameter.parametersOf
|
||||||
import kotlin.time.Duration.Companion.milliseconds
|
import kotlin.time.Duration.Companion.milliseconds
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun PlayerScreen(
|
fun PlayerScreen(
|
||||||
recordId: Int,
|
recordId: Int,
|
||||||
onBackClicked: () -> Unit = {},
|
onBackClicked: () -> Unit = {},
|
||||||
viewModel: PlayerViewModel = koinViewModel(),
|
viewModel: PlayerViewModel = koinViewModel(key = "$recordId") { parametersOf(recordId) },
|
||||||
) {
|
) {
|
||||||
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
|
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
|
||||||
|
|
||||||
DisposableEffect(recordId) {
|
DisposableEffect(Unit) {
|
||||||
viewModel.loadRecord(recordId = recordId)
|
|
||||||
onDispose {
|
onDispose {
|
||||||
viewModel.stop()
|
viewModel.stop()
|
||||||
}
|
}
|
||||||
|
|||||||
+19
-7
@@ -13,13 +13,14 @@ import kotlinx.coroutines.flow.SharingStarted
|
|||||||
import kotlinx.coroutines.flow.StateFlow
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
import kotlinx.coroutines.flow.WhileSubscribed
|
import kotlinx.coroutines.flow.WhileSubscribed
|
||||||
import kotlinx.coroutines.flow.combine
|
import kotlinx.coroutines.flow.combine
|
||||||
import kotlinx.coroutines.flow.map
|
import kotlinx.coroutines.flow.onStart
|
||||||
import kotlinx.coroutines.flow.stateIn
|
import kotlinx.coroutines.flow.stateIn
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlin.time.Duration.Companion.milliseconds
|
import kotlin.time.Duration.Companion.milliseconds
|
||||||
import kotlin.time.Duration.Companion.seconds
|
import kotlin.time.Duration.Companion.seconds
|
||||||
|
|
||||||
class PlayerViewModel(
|
class PlayerViewModel(
|
||||||
|
private val recordId: Int,
|
||||||
private val recordRepository: RecordRepository,
|
private val recordRepository: RecordRepository,
|
||||||
private val playbackRepository: PlaybackRepository,
|
private val playbackRepository: PlaybackRepository,
|
||||||
private val audioSessionManager: AudioSessionManager,
|
private val audioSessionManager: AudioSessionManager,
|
||||||
@@ -27,6 +28,8 @@ class PlayerViewModel(
|
|||||||
) : ViewModel() {
|
) : ViewModel() {
|
||||||
private val recordTitle = MutableStateFlow("")
|
private val recordTitle = MutableStateFlow("")
|
||||||
|
|
||||||
|
private var recordLoaded = false
|
||||||
|
|
||||||
val uiState: StateFlow<PlayerUiState> = combine(
|
val uiState: StateFlow<PlayerUiState> = combine(
|
||||||
recordTitle,
|
recordTitle,
|
||||||
playbackRepository.playbackState,
|
playbackRepository.playbackState,
|
||||||
@@ -41,17 +44,26 @@ class PlayerViewModel(
|
|||||||
subtitleLines = subtitle.lines,
|
subtitleLines = subtitle.lines,
|
||||||
currentSubtitleIndex = currentSubtitleIndex,
|
currentSubtitleIndex = currentSubtitleIndex,
|
||||||
)
|
)
|
||||||
}.stateIn(
|
}
|
||||||
scope = viewModelScope,
|
.onStart { loadRecord() }
|
||||||
started = SharingStarted.WhileSubscribed(5.seconds),
|
.stateIn(
|
||||||
initialValue = PlayerUiState(),
|
scope = viewModelScope,
|
||||||
)
|
started = SharingStarted.WhileSubscribed(5.seconds),
|
||||||
|
initialValue = PlayerUiState(),
|
||||||
|
)
|
||||||
|
|
||||||
init {
|
init {
|
||||||
audioSessionManager.activate()
|
audioSessionManager.activate()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun loadRecord(recordId: Int) {
|
private fun loadRecord() {
|
||||||
|
logger.debug("Loading record $recordId")
|
||||||
|
if (recordLoaded) {
|
||||||
|
logger.debug("Record $recordId already loaded")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
recordLoaded = true
|
||||||
|
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
val record = recordRepository.getRecord(id = recordId)
|
val record = recordRepository.getRecord(id = recordId)
|
||||||
if (record == null) {
|
if (record == null) {
|
||||||
|
|||||||
Reference in New Issue
Block a user