AkshayChordiya / News

A sample News 🗞 app built using Modern Android Development [Architecture Components, Coroutines, Retrofit, Room, Kotlin, Dagger]
MIT License
848 stars 166 forks source link

Loading progress won't show for the first run of the app. #37

Closed droidchef closed 4 years ago

droidchef commented 4 years ago

Based on my understanding of the NetworkBoundResource it looks like when the app will start for the first time, loading progress won't appear and the user will see a blank screen.

Here is why:

    fun asFlow() = flow {
        emit(ViewState.loading())

        val dbValue = loadFromDb().first()
        if (shouldFetch(dbValue)) {
            emit(ViewState.success(dbValue))
            try {
                val apiResponse = fetchFromNetwork()
                when {
                    apiResponse.isSuccessful && apiResponse.body() != null -> {
                        apiResponse.body()?.let { saveNetworkResult(it) }
                        emitAll(loadFromDb().map { ViewState.success(it) })
                    }
                    else -> {
                        emit(ViewState.error(apiResponse.message()))
                    }
                }
            } catch (e: Exception) {
                emit(ViewState.error("Unable to fetch from network"))
            }
        } else {
            emitAll(loadFromDb().map { ViewState.success(it) })
        }
    }

The shouldFetch() doesn't respect dbValue's actual value.

At the first start, this will be empty. However, code will immediately emit an Empty success state whereas it is actually still loading items from the network.

AkshayChordiya commented 4 years ago

Yep you are right. I have solved this, will be pushing a new update soon. Thanks 🙏