feat: auth screen with auto-redirect, sync favorites/history with backend
This commit is contained in:
@@ -14,14 +14,21 @@ import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.graphics.Brush
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import android.util.Log
|
||||
import androidx.compose.ui.graphics.vector.ImageVector
|
||||
import androidx.compose.ui.layout.ContentScale
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.hilt.navigation.compose.hiltViewModel
|
||||
import coil.compose.AsyncImage
|
||||
import com.composables.icons.lucide.Heart
|
||||
import com.composables.icons.lucide.Lucide
|
||||
import com.composables.icons.lucide.Pause
|
||||
import com.composables.icons.lucide.Play
|
||||
import com.composables.icons.lucide.SkipBack
|
||||
import com.composables.icons.lucide.SkipForward
|
||||
import com.composables.icons.lucide.Circle
|
||||
import com.composables.icons.lucide.Square
|
||||
import com.radiola.domain.model.DeeplinkService
|
||||
import com.radiola.domain.model.Station
|
||||
import com.radiola.deeplink.DeeplinkNavigator
|
||||
@@ -33,6 +40,12 @@ fun PlayerBottomSheet(
|
||||
track: Track?,
|
||||
isPlaying: Boolean,
|
||||
onPlayPause: () -> Unit,
|
||||
onNext: () -> Unit,
|
||||
onPrevious: () -> Unit,
|
||||
isFavorite: Boolean,
|
||||
onToggleFavorite: () -> Unit,
|
||||
isRecording: Boolean,
|
||||
onToggleRecording: () -> Unit,
|
||||
modifier: Modifier = Modifier,
|
||||
viewModel: PlayerViewModel = hiltViewModel()
|
||||
) {
|
||||
@@ -50,12 +63,39 @@ fun PlayerBottomSheet(
|
||||
.padding(24.dp),
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
) {
|
||||
station?.let {
|
||||
Text(
|
||||
text = it.name,
|
||||
style = MaterialTheme.typography.labelMedium,
|
||||
color = Color(0xFF888888)
|
||||
)
|
||||
station?.let { s ->
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.spacedBy(8.dp)
|
||||
) {
|
||||
Text(
|
||||
text = s.name,
|
||||
style = MaterialTheme.typography.labelMedium,
|
||||
color = Color(0xFF888888)
|
||||
)
|
||||
IconButton(
|
||||
onClick = onToggleFavorite,
|
||||
modifier = Modifier.size(24.dp)
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Lucide.Heart,
|
||||
contentDescription = "Избранное",
|
||||
tint = if (isFavorite) Color(0xFFFF4081) else Color.White,
|
||||
modifier = Modifier.size(16.dp)
|
||||
)
|
||||
}
|
||||
IconButton(
|
||||
onClick = onToggleRecording,
|
||||
modifier = Modifier.size(24.dp)
|
||||
) {
|
||||
Icon(
|
||||
imageVector = if (isRecording) Lucide.Square else Lucide.Circle,
|
||||
contentDescription = if (isRecording) "Остановить запись" else "Запись",
|
||||
tint = if (isRecording) Color(0xFFFF5252) else Color(0xFFFF5252),
|
||||
modifier = Modifier.size(16.dp)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
Spacer(modifier = Modifier.height(24.dp))
|
||||
Box(
|
||||
@@ -96,8 +136,9 @@ fun PlayerBottomSheet(
|
||||
service = service,
|
||||
onClick = {
|
||||
track?.let { t ->
|
||||
Log.d("PlayerBottomSheet", "DeeplinkButton clicked, track=${t.artist} - ${t.song}")
|
||||
DeeplinkNavigator.openSearch(context, t, service)
|
||||
}
|
||||
} ?: Log.d("PlayerBottomSheet", "DeeplinkButton clicked but track is null")
|
||||
}
|
||||
)
|
||||
}
|
||||
@@ -107,14 +148,22 @@ fun PlayerBottomSheet(
|
||||
horizontalArrangement = Arrangement.spacedBy(24.dp),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
ControlButton(size = 56.dp, onClick = { })
|
||||
ControlButton(
|
||||
size = 56.dp,
|
||||
icon = Lucide.SkipBack,
|
||||
onClick = onPrevious
|
||||
)
|
||||
ControlButton(
|
||||
size = 72.dp,
|
||||
isPlay = true,
|
||||
isPlaying = isPlaying,
|
||||
onClick = onPlayPause
|
||||
)
|
||||
ControlButton(size = 56.dp, onClick = { })
|
||||
ControlButton(
|
||||
size = 56.dp,
|
||||
icon = Lucide.SkipForward,
|
||||
onClick = onNext
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -146,6 +195,7 @@ private fun ControlButton(
|
||||
size: androidx.compose.ui.unit.Dp,
|
||||
isPlay: Boolean = false,
|
||||
isPlaying: Boolean = false,
|
||||
icon: androidx.compose.ui.graphics.vector.ImageVector? = null,
|
||||
onClick: () -> Unit,
|
||||
modifier: Modifier = Modifier
|
||||
) {
|
||||
@@ -164,6 +214,13 @@ private fun ControlButton(
|
||||
tint = Color.Black,
|
||||
modifier = Modifier.size(size * 0.4f)
|
||||
)
|
||||
} else if (icon != null) {
|
||||
Icon(
|
||||
imageVector = icon,
|
||||
contentDescription = null,
|
||||
tint = Color.White,
|
||||
modifier = Modifier.size(size * 0.4f)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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