diff --git a/app/src/main/java/io/horizontalsystems/bankwallet/modules/settings/guides/GuidesFragment.kt b/app/src/main/java/io/horizontalsystems/bankwallet/modules/settings/guides/GuidesFragment.kt index 544da7b2b6..356978dd0e 100644 --- a/app/src/main/java/io/horizontalsystems/bankwallet/modules/settings/guides/GuidesFragment.kt +++ b/app/src/main/java/io/horizontalsystems/bankwallet/modules/settings/guides/GuidesFragment.kt @@ -52,10 +52,12 @@ class GuidesFragment : BaseComposeFragment() { fun GuidesScreen(navController: NavController) { val viewModel = viewModel(factory = GuidesModule.Factory()) - val viewState = viewModel.viewState - val categories = viewModel.categories - val selectedCategory = viewModel.selectedCategory - val expandedSections = viewModel.expandedSections + val uiState = viewModel.uiState + + val viewState = uiState.viewState + val categories = uiState.categories + val selectedCategory = uiState.selectedCategory + val expandedSections = uiState.expandedSections Column(modifier = Modifier.background(color = ComposeAppTheme.colors.tyler)) { AppBar( diff --git a/app/src/main/java/io/horizontalsystems/bankwallet/modules/settings/guides/GuidesViewModel.kt b/app/src/main/java/io/horizontalsystems/bankwallet/modules/settings/guides/GuidesViewModel.kt index 1667b81f8e..48ff19a9e3 100644 --- a/app/src/main/java/io/horizontalsystems/bankwallet/modules/settings/guides/GuidesViewModel.kt +++ b/app/src/main/java/io/horizontalsystems/bankwallet/modules/settings/guides/GuidesViewModel.kt @@ -1,27 +1,25 @@ package io.horizontalsystems.bankwallet.modules.settings.guides -import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.setValue -import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope +import io.horizontalsystems.bankwallet.core.ViewModelUiState import io.horizontalsystems.bankwallet.entities.DataState import io.horizontalsystems.bankwallet.entities.GuideCategory import io.horizontalsystems.bankwallet.entities.ViewState import kotlinx.coroutines.launch import kotlinx.coroutines.rx2.asFlow -class GuidesViewModel(private val repository: GuidesRepository) : ViewModel() { +class GuidesViewModel(private val repository: GuidesRepository) : ViewModelUiState() { + private var viewState: ViewState = ViewState.Loading + private var categories = listOf() + private var selectedCategory: GuideCategory? = null + private var expandedSections = setOf() - var categories by mutableStateOf>(listOf()) - private set - var selectedCategory by mutableStateOf(null) - private set - var expandedSections by mutableStateOf(setOf()) - private set - - var viewState by mutableStateOf(ViewState.Loading) - private set + override fun createState() = GuidesUiState( + viewState = viewState, + categories = categories, + selectedCategory = selectedCategory, + expandedSections = expandedSections + ) init { viewModelScope.launch { @@ -29,6 +27,7 @@ class GuidesViewModel(private val repository: GuidesRepository) : ViewModel() { viewModelScope.launch { dataState.viewState?.let { viewState = it + emitState() } if (dataState is DataState.Success) { @@ -41,15 +40,7 @@ class GuidesViewModel(private val repository: GuidesRepository) : ViewModel() { fun onSelectCategory(category: GuideCategory) { selectedCategory = category - } - - override fun onCleared() { - repository.clear() - } - - private fun didFetchGuideCategories(guideCategories: List) { - categories = guideCategories - onSelectCategory(guideCategories.first()) + emitState() } fun toggleSection(sectionTitle: String, expanded: Boolean) { @@ -58,6 +49,25 @@ class GuidesViewModel(private val repository: GuidesRepository) : ViewModel() { } else { expandedSections.plus(sectionTitle) } + + emitState() } + override fun onCleared() { + repository.clear() + } + + private fun didFetchGuideCategories(guideCategories: List) { + categories = guideCategories + selectedCategory = guideCategories.first() + + emitState() + } } + +data class GuidesUiState( + val viewState: ViewState, + val categories: List, + val selectedCategory: GuideCategory?, + val expandedSections: Set +)