private suspend fun observeChannels(searchQuery: String) { logger.d { "ViewModel is observing channels. When state is available, it will be notified" }queryChannelsState.filterNotNull().collectLatest { queryChannelsState ->channelMutes.combine(queryChannelsState.channelsStateData, ::Pair).map { (channelMutes, state) ->when (state) {ChannelsStateData.NoQueryActive,ChannelsStateData.Loading,-> channelsState.copy(isLoading = true,searchQuery = searchQuery).also {logger.d { "Loading state for query" }}ChannelsStateData.OfflineNoResults -> {logger.d { "No offline results. Channels are empty" }channelsState.copy(isLoading = false,channelItems = emptyList(),searchQuery = searchQuery)}is ChannelsStateData.Result -> {logger.d { "Received result for state of channels" }channelsState.copy(isLoading = false,channelItems = createChannelItems(state.channels, channelMutes),isLoadingMore = false,endOfChannels = queryChannelsState.endOfChannels.value,searchQuery = searchQuery)}}}.collectLatest { newState -> channelsState = newState }}}
Kicks off operations required to combine and build the [ChannelsState] object for the UI. t connects the 'loadingMore', 'channelsState' and 'endOfChannels' properties from the [queryChannelsState]. @param searchQuery The search query string used to search channels.