ViewModel
private val _wordPressPostsState = Channel<WordPressPostsState>()
val wordPressPostList: List<WordPressPostDataDomain>
field = mutableListOf<WordPressPostDataDomain>()
Inside a function that is calling a use case which returns a Flow
when (it) {
is RequestStatus.Success -> {
if (it.data.postList.isNotEmpty()) {
wordPressPostList.addAll(it.data.postList)
}
_wordPressPostsState.send(WordPressPostsState.FetchSuccess(wordPressPostList.toList()))
}
}
Fragment
val newsAdapter: ListAdapter
newsAdapter.submitList(state.postList)
ListAdapter won't work without calling .toList() on either state.postList or wordPressPostList when passing it to state WordPressPostsState.FetchSuccess in ViewModel. This means that wordPressPostList is still a MutableList.
One thing I noticed is that the data type was shown to be both List inside and outside but actually behaves like mutable in ViewModel and immutable in Fragment similar of what is stated in the documentation.
state,newsAdapter,_wordPressPostsStateand others. Also, property definition forwordPressPostListisn't at all a valid Kotlin code.stateis your typical sealed class,newsAdapteris Android's ListAdapter,_wordPressPostsStateis Kotlin's Channel.