feat(ui): add PlayerBottomSheet and PlayerViewModel
This commit is contained in:
66
app/src/main/java/com/radiola/ui/player/PlayerViewModel.kt
Normal file
66
app/src/main/java/com/radiola/ui/player/PlayerViewModel.kt
Normal file
@@ -0,0 +1,66 @@
|
||||
package com.radiola.ui.player
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.radiola.domain.model.DeeplinkService
|
||||
import com.radiola.domain.model.Station
|
||||
import com.radiola.domain.model.Track
|
||||
import com.radiola.domain.repository.SettingsRepository
|
||||
import com.radiola.domain.usecase.GetNowPlayingUseCase
|
||||
import com.radiola.domain.usecase.SearchTrackInServiceUseCase
|
||||
import com.radiola.service.PlayerController
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.flow.*
|
||||
import kotlinx.coroutines.launch
|
||||
import javax.inject.Inject
|
||||
|
||||
@HiltViewModel
|
||||
class PlayerViewModel @Inject constructor(
|
||||
private val playerController: PlayerController,
|
||||
private val getNowPlayingUseCase: GetNowPlayingUseCase,
|
||||
private val searchTrackInServiceUseCase: SearchTrackInServiceUseCase,
|
||||
private val settingsRepository: SettingsRepository
|
||||
) : ViewModel() {
|
||||
|
||||
val isPlaying: StateFlow<Boolean> = playerController.isPlaying
|
||||
val currentStationPrefix: StateFlow<String?> = playerController.currentStationPrefix
|
||||
|
||||
private val _currentTrack = MutableStateFlow<Track?>(null)
|
||||
val currentTrack: StateFlow<Track?> = _currentTrack.asStateFlow()
|
||||
|
||||
private val _enabledServices = MutableStateFlow<List<DeeplinkService>>(emptyList())
|
||||
val enabledServices: StateFlow<List<DeeplinkService>> = _enabledServices.asStateFlow()
|
||||
|
||||
init {
|
||||
viewModelScope.launch {
|
||||
settingsRepository.getEnabledDeeplinkServices().collect { ids ->
|
||||
_enabledServices.value = DeeplinkService.entries.filter { it.serviceId in ids }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun play(station: Station) {
|
||||
playerController.play(station.streamUrl, station.prefix)
|
||||
viewModelScope.launch {
|
||||
getNowPlayingUseCase(station.prefix).collect { track ->
|
||||
_currentTrack.value = track
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun pause() {
|
||||
playerController.pause()
|
||||
}
|
||||
|
||||
fun resume(station: Station) {
|
||||
if (playerController.currentStationPrefix.value == station.prefix) {
|
||||
playerController.exoPlayer.play()
|
||||
} else {
|
||||
play(station)
|
||||
}
|
||||
}
|
||||
|
||||
fun getDeeplinkUrl(track: Track, service: DeeplinkService): String {
|
||||
return searchTrackInServiceUseCase(track, service)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user