Skip to content

Commit

Permalink
Apply ViewModelUiState pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
abdrasulov committed Oct 23, 2024
1 parent 184dc0e commit da8f71a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ class GuidesFragment : BaseComposeFragment() {
fun GuidesScreen(navController: NavController) {
val viewModel = viewModel<GuidesViewModel>(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(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
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<GuidesUiState>() {
private var viewState: ViewState = ViewState.Loading
private var categories = listOf<GuideCategory>()
private var selectedCategory: GuideCategory? = null
private var expandedSections = setOf<String>()

var categories by mutableStateOf<List<GuideCategory>>(listOf())
private set
var selectedCategory by mutableStateOf<GuideCategory?>(null)
private set
var expandedSections by mutableStateOf(setOf<String>())
private set

var viewState by mutableStateOf<ViewState>(ViewState.Loading)
private set
override fun createState() = GuidesUiState(
viewState = viewState,
categories = categories,
selectedCategory = selectedCategory,
expandedSections = expandedSections
)

init {
viewModelScope.launch {
repository.guideCategories.asFlow().collect { dataState ->
viewModelScope.launch {
dataState.viewState?.let {
viewState = it
emitState()
}

if (dataState is DataState.Success) {
Expand All @@ -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<GuideCategory>) {
categories = guideCategories
onSelectCategory(guideCategories.first())
emitState()
}

fun toggleSection(sectionTitle: String, expanded: Boolean) {
Expand All @@ -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<GuideCategory>) {
categories = guideCategories
selectedCategory = guideCategories.first()

emitState()
}
}

data class GuidesUiState(
val viewState: ViewState,
val categories: List<GuideCategory>,
val selectedCategory: GuideCategory?,
val expandedSections: Set<String>
)

0 comments on commit da8f71a

Please sign in to comment.