myanmarking
06/03/2020, 11:44 AMsealed class Updates{
data class LiveUpdate(val progress: Int): Updates()
data class History(val list: List<Int>): Updates()
}
private fun fetchUpdates(): Flow<Updates> {
TODO()
}
private fun save(list: List<Updates.History>){
TODO()
}
fun dispatchUpdates(): Flow<Updates.LiveUpdate>{
val history = mutableListOf<Updates.History>()
return fetchUpdates()
.onEach {
if(it is Updates.History){
history.add(it)
}
}
.onCompletion { if(it != null) save(history) }
.filterIsInstance<Updates.History>()
}
Is there any way to improve this code so that the save part doesn't end up a side-effect? Something like an inner flow. I'm not sure how to approach thisgildor
06/03/2020, 11:48 AMfun historyUpdates(): Flow<Updates.History> {
return fetchUpdates().filterIsInstance<Updates.History>()
}
suspend fun saveHistory() {
save(historyUpdates().toList())
}
//and some other method which want to get updates on every update, but without mixing with side effect
myanmarking
06/03/2020, 11:53 AMgildor
06/03/2020, 11:53 AMmyanmarking
06/03/2020, 11:54 AMgildor
06/03/2020, 11:54 AMcannot have two calls to the same parent flowBut why?
myanmarking
06/03/2020, 11:56 AMgildor
06/03/2020, 11:56 AMmyanmarking
06/03/2020, 11:57 AMgildor
06/03/2020, 11:57 AMahulyk
06/03/2020, 2:28 PMStateFlow
. I did some simple sample -> https://pl.kotl.in/CIFp67rcrmyanmarking
06/03/2020, 2:31 PMgildor
06/03/2020, 11:53 PM