Skip to content

Commit

Permalink
Merge pull request #545 from nimblehq/feature/535-add-asstateflow-to-…
Browse files Browse the repository at this point in the history
…viewmodel

[#535] Add asStateFlow to the StateFlow variables in ViewModel class
  • Loading branch information
ryan-conway authored Oct 23, 2023
2 parents 590332a + 1d51e5d commit b90cbe3
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package co.nimblehq.sample.compose.ui.base

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import co.nimblehq.sample.compose.lib.IsLoading
import co.nimblehq.sample.compose.ui.AppDestination
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
Expand All @@ -15,13 +14,13 @@ abstract class BaseViewModel : ViewModel() {
private var loadingCount: Int = 0

private val _isLoading = MutableStateFlow(false)
val isLoading: StateFlow<IsLoading> = _isLoading
val isLoading = _isLoading.asStateFlow()

protected val _error = MutableSharedFlow<Throwable>()
val error: SharedFlow<Throwable> = _error
val error = _error.asSharedFlow()

protected val _navigator = MutableSharedFlow<AppDestination>()
val navigator: SharedFlow<AppDestination> = _navigator
val navigator = _navigator.asSharedFlow()

/**
* To show loading manually, should call `hideLoading` after
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ class HomeViewModel @Inject constructor(
) : BaseViewModel() {

private val _uiModels = MutableStateFlow<List<UiModel>>(emptyList())
val uiModels: StateFlow<List<UiModel>> = _uiModels
val uiModels = _uiModels.asStateFlow()

private val _isFirstTimeLaunch = MutableStateFlow(false)
val isFirstTimeLaunch: StateFlow<Boolean> = _isFirstTimeLaunch
val isFirstTimeLaunch = _isFirstTimeLaunch.asStateFlow()

init {
getModelsUseCase()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package co.nimblehq.template.compose.ui.base

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import co.nimblehq.template.compose.lib.IsLoading
import co.nimblehq.template.compose.ui.AppDestination
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
Expand All @@ -15,13 +14,13 @@ abstract class BaseViewModel : ViewModel() {
private var loadingCount: Int = 0

private val _isLoading = MutableStateFlow(false)
val isLoading: StateFlow<IsLoading> = _isLoading
val isLoading = _isLoading.asStateFlow()

protected val _error = MutableSharedFlow<Throwable>()
val error: SharedFlow<Throwable> = _error
val error = _error.asSharedFlow()

protected val _navigator = MutableSharedFlow<AppDestination>()
val navigator: SharedFlow<AppDestination> = _navigator
val navigator = _navigator.asSharedFlow()

/**
* To show loading manually, should call `hideLoading` after
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class HomeViewModel @Inject constructor(
) : BaseViewModel() {

private val _uiModels = MutableStateFlow<List<UiModel>>(emptyList())
val uiModels: StateFlow<List<UiModel>> = _uiModels
val uiModels = _uiModels.asStateFlow()

init {
useCase()
Expand Down

0 comments on commit b90cbe3

Please sign in to comment.