streetsofboston
05/10/2020, 5:19 PMOrhan Tozan
05/10/2020, 5:22 PMliveData.map {} How does one let's say a StateFlow<Int> that maps it and multiplies the int value with 2, and creates a new StateFlow from it?Sinan Kozak
05/10/2020, 5:33 PMOrhan Tozan
05/10/2020, 5:34 PMstreetsofboston
05/10/2020, 6:03 PMStateFlow is a Flow. But map, flatMapXxx and such return a Flow. A StateFlow is the root source of a reactive stream, it contains the source, the state, whose changes are emitted downstream. A StateFlow is like an Rx BehaviorSubject this way.Orhan Tozan
05/10/2020, 6:03 PMSinan Kozak
05/10/2020, 6:13 PMOrhan Tozan
05/10/2020, 6:14 PMOrhan Tozan
05/10/2020, 6:14 PMursus
05/10/2020, 6:23 PMOrhan Tozan
05/10/2020, 6:24 PMprivate val chatContacts: StateFlow<List<ChatContact>> = MutableStateFlow(emptyList<ChatContact>())
.also { mutableFlow ->
viewModelEngineScope.launch {
Napier.d("seeChatContactsUseCase(): begin")
seeChatContactsUseCase().let { seeChatContactsResult ->
Napier.d("seeChatContactsUseCase(): end")
Napier.d("seeChatContactsResult: $seeChatContactsResult")
when (seeChatContactsResult) {
is Result.Failure -> _errors.offer(seeChatContactsResult.exception.toString())
is Result.Success -> mutableFlow.value = seeChatContactsResult.data
}
}
}
}
override val chatContactRows: StateFlow<List<ChatContactRow>> = MutableStateFlow(emptyList<ChatContactRow>())
.also { mutableFlow ->
viewModelEngineScope.launch {
chatContacts.collect { chatContacts ->
mutableFlow.value = chatContacts.map { chatContact ->
ChatContactRow(
id = chatContact.familyMembershipId,
title = chatContact.name,
avatar = chatContact.picture
?: ImageSource.Url("<https://cdn.pixabay.com/photo/2016/08/08/09/17/avatar-1577909_960_720.png>")
)
}
}
}
}Orhan Tozan
05/10/2020, 6:25 PMursus
05/10/2020, 6:26 PMOrhan Tozan
05/10/2020, 6:27 PMoverride val chatContactRows: StateFlow<List<ChatContactRow>> = MutableStateFlow(emptyList<ChatContactRow>())
.also { mutableFlow ->
viewModelEngineScope.launch {
chatContacts.collect { chatContacts ->
mutableFlow.value = chatContacts.map { chatContact ->
ChatContactRow(...)
)
}
}
}
}ursus
05/10/2020, 6:27 PMOrhan Tozan
05/10/2020, 6:27 PMOrhan Tozan
05/10/2020, 6:27 PMOrhan Tozan
05/10/2020, 6:27 PMOrhan Tozan
05/10/2020, 6:27 PMval name: StateFlow<String> = MutableStateFlow("Eric")
val greeting: StateFlow<String> = name.map { name -> "Hi $name!" }Sinan Kozak
05/10/2020, 6:29 PMursus
05/10/2020, 6:29 PMOrhan Tozan
05/10/2020, 6:29 PMSinan Kozak
05/10/2020, 6:30 PMOrhan Tozan
05/10/2020, 6:30 PMOrhan Tozan
05/10/2020, 6:30 PMSinan Kozak
05/10/2020, 6:32 PMOrhan Tozan
05/10/2020, 6:32 PMSinan Kozak
05/10/2020, 6:33 PMOrhan Tozan
05/10/2020, 6:34 PMursus
05/10/2020, 6:39 PMinit {
inputRelay
.map { }
.subscribe {
outputRelay.accept(it)
}
}Orhan Tozan
05/10/2020, 6:41 PMursus
05/10/2020, 6:53 PMstreetsofboston
05/10/2020, 6:54 PM.first() on the Flow you have (which is backed by a StateFlow).ursus
05/10/2020, 6:55 PMblockingFirst()Orhan Tozan
05/10/2020, 6:56 PMval value: Tstreetsofboston
05/10/2020, 6:56 PM.filter operation somewhere in the middle of the chain....Orhan Tozan
05/10/2020, 6:57 PMursus
05/10/2020, 6:58 PMstreetsofboston
05/10/2020, 6:59 PMOrhan Tozan
05/10/2020, 6:59 PMclass CounterModel {
private val _counter = MutableStateFlow(0) // private mutable state flow
val counter: StateFlow<Int> get() = _counter // publicly exposed as read-only state flow
fun inc() {
_counter.value++
}
}Orhan Tozan
05/10/2020, 6:59 PMOrhan Tozan
05/10/2020, 7:00 PMursus
05/10/2020, 7:01 PMstreetsofboston
05/10/2020, 7:01 PMfilter would return a StateFlow....? Filtered out values may cause the returned StateFlow to not have a current state....ursus
05/10/2020, 7:01 PMursus
05/10/2020, 7:02 PMursus
05/10/2020, 7:02 PMstreetsofboston
05/10/2020, 7:02 PMOrhan Tozan
05/10/2020, 7:04 PMOrhan Tozan
05/10/2020, 7:06 PMstreetsofboston
05/10/2020, 7:06 PMOrhan Tozan
05/10/2020, 7:07 PMursus
05/10/2020, 7:07 PMstreetsofboston
05/10/2020, 7:07 PM.value on it.ursus
05/10/2020, 7:08 PMOrhan Tozan
05/10/2020, 7:08 PMOrhan Tozan
05/10/2020, 7:10 PMursus
05/10/2020, 7:12 PMstreetsofboston
05/10/2020, 7:13 PMstreetsofboston
05/10/2020, 7:15 PMstreetsofboston
05/10/2020, 7:17 PM