Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#535] Add asStateFlow to the StateFlow variables in ViewModel class #545

Merged
merged 2 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
luongvo marked this conversation as resolved.
Show resolved Hide resolved
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
Loading