kotlin-flow

Android Paging 3 library: how to change list with new param?

元气小坏坏 提交于 2021-01-29 12:36:35
问题 I have a search fragment that shows list of searched items. if user type something, I pass that string to url as new query parameter and get new list using paging 3 library. first solution is: //viewModel lateinit var postListUrl: String val postList: Flow<PagingData<Post>> = Pager(PagingConfig(pageSize = 20)) { PostPagingSource(postRepository, postListUrl) }.flow.cachedIn(viewModelScope) //fragment fun showPostList(url: String) { postListAdapter.submitData(lifecycle, PagingData.empty())

Android Paging 3 library: how to change list with new param?

こ雲淡風輕ζ 提交于 2021-01-29 10:49:29
问题 I have a search fragment that shows list of searched items. if user type something, I pass that string to url as new query parameter and get new list using paging 3 library. first solution is: //viewModel lateinit var postListUrl: String val postList: Flow<PagingData<Post>> = Pager(PagingConfig(pageSize = 20)) { PostPagingSource(postRepository, postListUrl) }.flow.cachedIn(viewModelScope) //fragment fun showPostList(url: String) { postListAdapter.submitData(lifecycle, PagingData.empty())

How To Test PagingData From Paging 3

痴心易碎 提交于 2021-01-29 09:11:11
问题 My ViewModel has a method which returns a flow of PagingData . In my app, the data is fetched from the remote server, which is then saved to Room (the single source of truth): fun getChocolates(): Flow<PagingData<Chocolate>> { val pagingSourceFactory = { dao().getChocolateListData() } return Pager( config = PagingConfig( pageSize = NETWORK_PAGE_SIZE, maxSize = MAX_MEMORY_SIZE, enablePlaceholders = false ), remoteMediator = ChocolateRemoteMediator( api, dao ), pagingSourceFactory =

Proper way to operate collections in StateFlow

旧巷老猫 提交于 2021-01-05 08:56:55
问题 I'm creating MutableStateFlow like this: val intSet = MutableStateFlow(HashSet<Int>()) And in some moment later I want to update collection in this flow: intSet.value.add(0) And this doesn't seem to work (the collection updates, but observers are not notified). The way that I found it working: val list = HashSet<Int>(intSet.value) list.add(0) intSet.value = list But it creates copy of the collection, so it doesn't look proper for me. Is there any simpler way to update collection in StateFlow?

Proper way to operate collections in StateFlow

▼魔方 西西 提交于 2021-01-05 08:56:39
问题 I'm creating MutableStateFlow like this: val intSet = MutableStateFlow(HashSet<Int>()) And in some moment later I want to update collection in this flow: intSet.value.add(0) And this doesn't seem to work (the collection updates, but observers are not notified). The way that I found it working: val list = HashSet<Int>(intSet.value) list.add(0) intSet.value = list But it creates copy of the collection, so it doesn't look proper for me. Is there any simpler way to update collection in StateFlow?

Combine multiple Kotlin flows in a list without waiting for a first value

余生颓废 提交于 2021-01-02 05:47:12
问题 I have a List<Flow<T>> , and would like to generate a Flow<List<T>> . This is almost what combine does - except that combine waits for each and every Flow to emit an initial value, which is not what I want. Take this code for example: val a = flow { repeat(3) { emit("a$it") delay(100) } } val b = flow { repeat(3) { delay(150) emit("b$it") } } val c = flow { delay(400) emit("c") } val flows = listOf(a, b, c) runBlocking { combine(flows) { it.toList() }.collect { println(it) } } With combine

Combine multiple Kotlin flows in a list without waiting for a first value

两盒软妹~` 提交于 2021-01-02 05:46:51
问题 I have a List<Flow<T>> , and would like to generate a Flow<List<T>> . This is almost what combine does - except that combine waits for each and every Flow to emit an initial value, which is not what I want. Take this code for example: val a = flow { repeat(3) { emit("a$it") delay(100) } } val b = flow { repeat(3) { delay(150) emit("b$it") } } val c = flow { delay(400) emit("c") } val flows = listOf(a, b, c) runBlocking { combine(flows) { it.toList() }.collect { println(it) } } With combine

Unit test the new Kotlin coroutine StateFlow

懵懂的女人 提交于 2020-12-05 06:55:49
问题 Recently was introduced the class StateFlow as part of Kotlin coroutine. I'm currently trying it and encounter an issue while trying to unit test my ViewModel. What I want to achieve: testing that my StateFlow is receiving all the state values in the correct order in my ViewModel. My code is as follow: ViewModel: class WalletViewModel(private val getUserWallets: GetUersWallets) : ViewModel() { val userWallet: StateFlow<State<UserWallets>> get() = _userWallets private val _userWallets:

Unit test the new Kotlin coroutine StateFlow

删除回忆录丶 提交于 2020-12-05 06:55:26
问题 Recently was introduced the class StateFlow as part of Kotlin coroutine. I'm currently trying it and encounter an issue while trying to unit test my ViewModel. What I want to achieve: testing that my StateFlow is receiving all the state values in the correct order in my ViewModel. My code is as follow: ViewModel: class WalletViewModel(private val getUserWallets: GetUersWallets) : ViewModel() { val userWallet: StateFlow<State<UserWallets>> get() = _userWallets private val _userWallets:

How to emit Flow value from different function? Kotlin Coroutines

牧云@^-^@ 提交于 2020-08-09 02:42:37
问题 I have a flow : val myflow = kotlinx.coroutines.flow.flow<Message>{} and want to emit values with function: override suspend fun sendMessage(chat: Chat, message: Message) { myflow.emit(message) } But compiler does not allow me to do this, is there any workarounds to solve this problem? 回答1: The answer of Animesh Sahu is pretty much correct. You can also return a Channel as a flow (see consumeAsFlow or asFlow on a BroadcastChannel). But there is also a thing called StateFlow currently in