/
Stalnodg
/
RandomCoffee
Обзор
Документация
Войти
/
Stalnodg
/
RandomCoffee
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
develop
app/src/main/java/com/topit/randomcoffee/presentation/viewmodel/MenuViewModel.kt
97 строк
3 KB
Max
presentation: pagination moved from data to presentation, bug with broken reload fixed, ViewModel for shopping cart created
05 май 2026, 18:21
05 май 2026, 18:21
c0f75ae
Код
Авторство
О чём код?
package com.topit.randomcoffee.presentation.viewmodel import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import androidx.paging.PagingData import androidx.paging.cachedIn import com.topit.randomcoffee.domain.models.OperationResult import com.topit.randomcoffee.domain.models.Category import com.topit.randomcoffee.domain.models.Product import com.topit.randomcoffee.domain.usecase.GetCategoriesUseCase import com.topit.randomcoffee.presentation.paging.GetPagedProducts import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.CancellationException import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.flatMapLatest import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch import javax.inject.Inject @OptIn(ExperimentalCoroutinesApi::class) @HiltViewModel class MenuViewModel @Inject constructor( private val getCategoriesUseCase: GetCategoriesUseCase, private val getPagedProducts: GetPagedProducts ) : ViewModel() { private val _uiState = MutableStateFlow(MenuUiState()) val uiState = _uiState.asStateFlow() private val categoryIdForPaging = MutableStateFlow<Int?>(null) val products: Flow<PagingData<Product>> = categoryIdForPaging.flatMapLatest(getPagedProducts::invoke).cachedIn(viewModelScope) init { loadCategories() } private fun loadCategories() { viewModelScope.launch { try { when (val result = getCategoriesUseCase()) { is OperationResult.Success -> { _uiState.update { it.copy(categories = result.data, categoriesError = null) } syncPagingCategoryFromSelection() } is OperationResult.Failure -> { _uiState.update { it.copy(categoriesError = "Не удалось загрузить категории") } } } } catch (e: CancellationException) { throw e } catch (e: Exception) { _uiState.update { it.copy(categoriesError = e.message ?: "Не удалось загрузить категории") } } } } fun refreshCategories() { loadCategories() } fun dismissCategoriesError() { _uiState.update { it.copy(categoriesError = null) } } private fun syncPagingCategoryFromSelection() { val selection = _uiState.value.selectedTabIndexes val categoryId = if (selection.size == 1) { val index = selection.first() _uiState.value.categories.getOrNull(index)?.id } else { null } categoryIdForPaging.value = categoryId } fun updateSelectedTabIndexes(newSelection: Set<Int>) { _uiState.update { it.copy(selectedTabIndexes = newSelection) } syncPagingCategoryFromSelection() } } data class MenuUiState( val categories: List<Category> = emptyList(), val selectedTabIndexes: Set<Int> = emptySet(), val categoriesError: String? = null )