StateFlow not emitting update like LiveData

22 views Asked by At

I was implementing pagination with concatadapter, first adapter content, second is the loading progess while paginating. From the paging codelab https://github.com/android/codelab-android-paging/pull/46/files#diff-27062d7e2623b9c23539c4686febbc6d37ac66c7b388612fde38b2bdca5bc87f

When I use LiveData my retry option works

private val _repoLoadStatus = MutableLiveData<LoadState>()
    val repoLoadStatus: LiveData<LoadState>
        get() = _repoLoadStatus.distinctUntilChanged()

While trying with flow nothing happens. Basically just stuck on this screen without refreshing data. enter image description here

private val _homeFeedResponse = MutableStateFlow<State>(State.Empty)
val homeFeedResponse = _homeFeedResponse.asStateFlow()

This is the VM function

fun getHomeFeed(
        page: String,
        size: String
    ) = viewModelScope.launch {
        _homeFeedResponse.value = State.Loading
        when (val response = homeFeedRepository.getHomeFeed(page, size)) {
            is ResultWrapper.GenericError -> {
                _homeFeedResponse.value = State.Failed("${response.code} ${response.error}")
            }

            ResultWrapper.NetworkError -> {
                _homeFeedResponse.value = State.Failed("Network Error")
            }

            is ResultWrapper.Success -> {
                lastPageReached = response.value.next == null
                if (response.value.next != null) pageNumber += 1
                if (_homeFeedUiState.value.homeFeedPosts.isNotEmpty() && page == "1") {
                    _homeFeedUiState.update {
                        it.copy(homeFeedPosts = response.value.homeFeedPosts)
                    }
                } else {
                    _homeFeedUiState.update {
                        it.copy(homeFeedPosts = it.homeFeedPosts + response.value.homeFeedPosts)
                    }
                }
                _homeFeedResponse.value = State.Success(response.value)
            }
        }
    }
0

There are 0 answers