This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val flow1: Flow<Int> = ... | |
val flow2: Flow<Int> = ... | |
val result = combine(flow1, flow2) { a, b -> a + b } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val liveData1: LiveData<Int> = ... | |
val liveData2: LiveData<Int> = ... | |
val result = MediatorLiveData<Int>() | |
result.addSource(liveData1) { value -> | |
result.setValue(liveData1.value ?: 0 + (liveData2.value ?: 0)) | |
} | |
result.addSource(liveData2) { value -> | |
result.setValue(liveData1.value ?: 0 + (liveData2.value ?: 0)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyViewModel(authManager..., repository...) : ViewModel() { | |
private val userId: Flow<String?> = | |
authManager.observeUser().map { user -> user?.id } | |
val result: StateFlow<Result<Item>> = userId.flatMapLatest { newUserId -> | |
repository.observeItem(newUserId) | |
}.stateIn( | |
scope = viewModelScope, | |
started = WhileSubscribed(5000), | |
initialValue = Result.LoadingUser |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyViewModel(authManager..., repository...) : ViewModel() { | |
private val userId: Flow<String?> = | |
authManager.observeUser().map { user -> user?.id } | |
val result: LiveData<Result<Item>> = userId.flatMapLatest { newUserId -> | |
repository.observeItem(newUserId) | |
}.asLiveData() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyViewModel(authManager..., repository...) : ViewModel() { | |
private val userId: LiveData<String?> = | |
authManager.observeUser().map { user -> user.id }.asLiveData() | |
val result = userId.switchMap { newUserId -> | |
repository.observeItem(newUserId).asLiveData() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyViewModel(authManager..., repository...) : ViewModel() { | |
private val userId: LiveData<String?> = | |
authManager.observeUser().map { user -> user.id }.asLiveData() | |
val result = userId.switchMap { newUserId -> | |
repository.observeItem(newUserId).asLiveData() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val result = userId.transformLatest { newUserId -> | |
emit(Result.LoadingData) | |
emit(repository.fetchItem(newUserId)) | |
}.stateIn( | |
scope = viewModelScope, | |
started = WhileSubscribed(5000), | |
initialValue = Result.LoadingUser // Note the different Loading states | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyViewModel(authManager..., repository...) : ViewModel() { | |
private val userId: Flow<UserId> = authManager.observeUser().map { user -> user.id } | |
val result: StateFlow<Result<Item>> = userId.mapLatest { newUserId -> | |
repository.fetchItem(newUserId) | |
}.stateIn( | |
scope = viewModelScope, | |
started = WhileSubscribed(5000), | |
initialValue = Result.Loading | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyViewModel(authManager..., repository...) : ViewModel() { | |
private val userId: Flow<UserId> = authManager.observeUser().map { user -> user.id } | |
val result: LiveData<Result<Item>> = userId.mapLatest { newUserId -> | |
repository.fetchItem(newUserId) | |
}.asLiveData() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyViewModel(authManager..., repository...) : ViewModel() { | |
private val userId: LiveData<String?> = | |
authManager.observeUser().map { user -> user.id }.asLiveData() | |
val result: LiveData<Result<Item>> = userId.switchMap { newUserId -> | |
liveData { emit(repository.fetchItem(newUserId)) } | |
} | |
} |