feat: auth screen with auto-redirect, sync favorites/history with backend
This commit is contained in:
@@ -7,10 +7,18 @@ import com.radiola.domain.model.Station
|
||||
import com.radiola.domain.model.Track
|
||||
import com.radiola.domain.repository.SettingsRepository
|
||||
import com.radiola.domain.repository.StationRepository
|
||||
import com.radiola.domain.repository.NowPlayingRepository
|
||||
import com.radiola.domain.repository.RecordingRepository
|
||||
import com.radiola.domain.usecase.GetNowPlayingUseCase
|
||||
import com.radiola.domain.usecase.GetStationsUseCase
|
||||
import com.radiola.domain.usecase.SearchTrackInServiceUseCase
|
||||
import com.radiola.domain.repository.TrackHistoryRepository
|
||||
import com.radiola.domain.usecase.ToggleFavoriteUseCase
|
||||
import com.radiola.domain.usecase.auth.PushHistoryUseCase
|
||||
import com.radiola.service.PlayerController
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.*
|
||||
import kotlinx.coroutines.launch
|
||||
import javax.inject.Inject
|
||||
@@ -19,9 +27,15 @@ import javax.inject.Inject
|
||||
class PlayerViewModel @Inject constructor(
|
||||
private val playerController: PlayerController,
|
||||
private val stationRepository: StationRepository,
|
||||
private val nowPlayingRepository: NowPlayingRepository,
|
||||
private val getStationsUseCase: GetStationsUseCase,
|
||||
private val getNowPlayingUseCase: GetNowPlayingUseCase,
|
||||
private val searchTrackInServiceUseCase: SearchTrackInServiceUseCase,
|
||||
private val settingsRepository: SettingsRepository
|
||||
private val toggleFavoriteUseCase: ToggleFavoriteUseCase,
|
||||
private val trackHistoryRepository: TrackHistoryRepository,
|
||||
private val settingsRepository: SettingsRepository,
|
||||
private val recordingRepository: RecordingRepository,
|
||||
private val pushHistoryUseCase: PushHistoryUseCase
|
||||
) : ViewModel() {
|
||||
|
||||
val isPlaying: StateFlow<Boolean> = playerController.isPlaying
|
||||
@@ -36,31 +50,113 @@ class PlayerViewModel @Inject constructor(
|
||||
private val _enabledServices = MutableStateFlow<List<DeeplinkService>>(emptyList())
|
||||
val enabledServices: StateFlow<List<DeeplinkService>> = _enabledServices.asStateFlow()
|
||||
|
||||
private val _stations = MutableStateFlow<List<Station>>(emptyList())
|
||||
val stations: StateFlow<List<Station>> = _stations.asStateFlow()
|
||||
|
||||
private val _playlist = MutableStateFlow<List<Station>>(emptyList())
|
||||
val playlist: StateFlow<List<Station>> = _playlist.asStateFlow()
|
||||
|
||||
val isRecording: StateFlow<Boolean> = recordingRepository.isRecording
|
||||
|
||||
private var nowPlayingJob: Job? = null
|
||||
|
||||
init {
|
||||
playerController.onSkipToNext = { playNext() }
|
||||
playerController.onSkipToPrevious = { playPrevious() }
|
||||
viewModelScope.launch {
|
||||
getStationsUseCase().collect { _stations.value = it }
|
||||
}
|
||||
viewModelScope.launch {
|
||||
settingsRepository.getEnabledDeeplinkServices().collect { ids ->
|
||||
_enabledServices.value = DeeplinkService.entries.filter { it.serviceId in ids }
|
||||
}
|
||||
}
|
||||
viewModelScope.launch {
|
||||
currentStationPrefix.collect { prefix ->
|
||||
prefix?.let { p ->
|
||||
// Find station by prefix from repository
|
||||
// Note: repository only has getStationById; we use a workaround
|
||||
// In real implementation, add getStationByPrefix to repository
|
||||
_currentTrack
|
||||
.filterNotNull()
|
||||
.distinctUntilChanged()
|
||||
.collect { track ->
|
||||
trackHistoryRepository.addTrack(track)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun play(station: Station, playlist: List<Station>? = null) {
|
||||
_currentStation.value = station
|
||||
_currentTrack.value = null
|
||||
_playlist.value = playlist ?: _stations.value
|
||||
playerController.play(station.streamUrl, station.prefix, station.name)
|
||||
viewModelScope.launch { pushHistoryUseCase(station.id) }
|
||||
nowPlayingJob?.cancel()
|
||||
nowPlayingJob = viewModelScope.launch {
|
||||
// Polling loop for Record API now playing
|
||||
launch {
|
||||
while (true) {
|
||||
nowPlayingRepository.refreshNowPlaying()
|
||||
delay(10_000)
|
||||
}
|
||||
}
|
||||
// Collect now playing for this station (API has priority: covers + accurate metadata)
|
||||
launch {
|
||||
getNowPlayingUseCase(station.id)
|
||||
.distinctUntilChanged()
|
||||
.collect { track ->
|
||||
if (track != null) {
|
||||
_currentTrack.value = track
|
||||
playerController.updateMetadata(
|
||||
track.song,
|
||||
track.artist,
|
||||
track.coverUrl ?: "",
|
||||
station.name
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
// Fallback: Icy metadata from stream for stations not in Record API
|
||||
launch {
|
||||
playerController.icyTitle
|
||||
.filterNotNull()
|
||||
.distinctUntilChanged()
|
||||
.collect { icyTitle ->
|
||||
// Only use Icy if no API track is currently active
|
||||
if (_currentTrack.value == null) {
|
||||
val track = parseIcyTitle(icyTitle)
|
||||
if (track != null) {
|
||||
_currentTrack.value = track
|
||||
playerController.updateMetadata(
|
||||
track.song,
|
||||
track.artist,
|
||||
"",
|
||||
station.name
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun play(station: Station) {
|
||||
_currentStation.value = station
|
||||
playerController.play(station.streamUrl, station.prefix)
|
||||
viewModelScope.launch {
|
||||
getNowPlayingUseCase(station.prefix).collect { track ->
|
||||
_currentTrack.value = track
|
||||
private fun parseIcyTitle(title: String?): Track? {
|
||||
if (title.isNullOrBlank()) return null
|
||||
val separators = listOf(" - ", " — ", " – ")
|
||||
for (sep in separators) {
|
||||
val parts = title.split(sep, limit = 2)
|
||||
if (parts.size == 2) {
|
||||
return Track(
|
||||
artist = parts[0].trim(),
|
||||
song = parts[1].trim(),
|
||||
coverUrl = null,
|
||||
stationName = _currentStation.value?.name ?: ""
|
||||
)
|
||||
}
|
||||
}
|
||||
// No separator found: treat entire string as song title
|
||||
return Track(
|
||||
artist = "",
|
||||
song = title.trim(),
|
||||
coverUrl = null,
|
||||
stationName = _currentStation.value?.name ?: ""
|
||||
)
|
||||
}
|
||||
|
||||
fun pause() {
|
||||
@@ -68,14 +164,49 @@ class PlayerViewModel @Inject constructor(
|
||||
}
|
||||
|
||||
fun resume() {
|
||||
playerController.exoPlayer.play()
|
||||
playerController.play()
|
||||
}
|
||||
|
||||
fun togglePlayPause() {
|
||||
if (isPlaying.value) pause() else resume()
|
||||
}
|
||||
|
||||
fun playNext() {
|
||||
val current = _currentStation.value ?: return
|
||||
val list = _playlist.value
|
||||
if (list.isEmpty()) return
|
||||
val index = list.indexOfFirst { it.id == current.id }
|
||||
val next = list.getOrNull((index + 1).mod(list.size))
|
||||
next?.let { play(it, list) }
|
||||
}
|
||||
|
||||
fun playPrevious() {
|
||||
val current = _currentStation.value ?: return
|
||||
val list = _playlist.value
|
||||
if (list.isEmpty()) return
|
||||
val index = list.indexOfFirst { it.id == current.id }
|
||||
val prev = list.getOrNull((index - 1).mod(list.size))
|
||||
prev?.let { play(it, list) }
|
||||
}
|
||||
|
||||
fun getDeeplinkUrl(track: Track, service: DeeplinkService): String {
|
||||
return searchTrackInServiceUseCase(track, service)
|
||||
}
|
||||
|
||||
fun toggleFavorite(station: Station) {
|
||||
viewModelScope.launch {
|
||||
toggleFavoriteUseCase(station)
|
||||
}
|
||||
}
|
||||
|
||||
fun toggleRecording() {
|
||||
viewModelScope.launch {
|
||||
if (recordingRepository.isRecording.value) {
|
||||
recordingRepository.stopRecording()
|
||||
} else {
|
||||
val station = _currentStation.value ?: return@launch
|
||||
recordingRepository.startRecording(station, _currentTrack.value)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user