feat: auth screen with auto-redirect, sync favorites/history with backend
This commit is contained in:
@@ -3,8 +3,11 @@ package com.radiola.ui.stations
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.radiola.domain.model.Station
|
||||
import com.radiola.domain.repository.FavoritesRepository
|
||||
import com.radiola.domain.repository.StationRepository
|
||||
import com.radiola.domain.usecase.GetStationsUseCase
|
||||
import com.radiola.domain.usecase.PlayStationUseCase
|
||||
import com.radiola.domain.usecase.RefreshStationsUseCase
|
||||
import com.radiola.domain.usecase.ToggleFavoriteUseCase
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.flow.*
|
||||
@@ -14,8 +17,11 @@ import javax.inject.Inject
|
||||
@HiltViewModel
|
||||
class StationsViewModel @Inject constructor(
|
||||
private val getStationsUseCase: GetStationsUseCase,
|
||||
private val refreshStationsUseCase: RefreshStationsUseCase,
|
||||
private val playStationUseCase: PlayStationUseCase,
|
||||
private val toggleFavoriteUseCase: ToggleFavoriteUseCase
|
||||
private val toggleFavoriteUseCase: ToggleFavoriteUseCase,
|
||||
private val favoritesRepository: FavoritesRepository,
|
||||
private val stationRepository: StationRepository
|
||||
) : ViewModel() {
|
||||
|
||||
private val _searchQuery = MutableStateFlow("")
|
||||
@@ -37,7 +43,9 @@ class StationsViewModel @Inject constructor(
|
||||
) { allStations, query, tag ->
|
||||
allStations
|
||||
.filter { station ->
|
||||
tag == null || station.tags.contains(tag) || station.genre.equals(tag, ignoreCase = true)
|
||||
tag == null ||
|
||||
station.genre.equals(tag, ignoreCase = true) ||
|
||||
station.tags.any { it.equals(tag, ignoreCase = true) }
|
||||
}
|
||||
.filter { station ->
|
||||
query.isBlank() ||
|
||||
@@ -46,10 +54,22 @@ class StationsViewModel @Inject constructor(
|
||||
}
|
||||
}.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), emptyList())
|
||||
|
||||
val tags: StateFlow<List<String>> = getStationsUseCase()
|
||||
.map { stations -> stations.flatMap { it.tags }.distinct().sorted() }
|
||||
val tags: StateFlow<List<String>> = stationRepository.getTags()
|
||||
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), emptyList())
|
||||
|
||||
val favoriteIds: StateFlow<Set<Int>> = favoritesRepository.getFavorites()
|
||||
.map { list -> list.map { it.id }.toSet() }
|
||||
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), emptySet())
|
||||
|
||||
init {
|
||||
viewModelScope.launch {
|
||||
_isLoading.value = true
|
||||
refreshStationsUseCase()
|
||||
.onFailure { _error.value = it.localizedMessage ?: "Ошибка загрузки" }
|
||||
_isLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
fun onSearchQueryChange(query: String) {
|
||||
_searchQuery.value = query
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user