From 37d5c7b7be9e3aba8757446a4e92d86c17bf14c8 Mon Sep 17 00:00:00 2001 From: Antoine POPINEAU Date: Wed, 8 Jul 2020 23:00:10 +0200 Subject: [PATCH] Load data from network on resume if cached data is empty. --- .../github/apognu/otter/fragments/FunkwhaleFragment.kt | 9 +++++---- .../com/github/apognu/otter/repositories/Repository.kt | 4 +++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/github/apognu/otter/fragments/FunkwhaleFragment.kt b/app/src/main/java/com/github/apognu/otter/fragments/FunkwhaleFragment.kt index 12f2b9b..2de5ab0 100644 --- a/app/src/main/java/com/github/apognu/otter/fragments/FunkwhaleFragment.kt +++ b/app/src/main/java/com/github/apognu/otter/fragments/FunkwhaleFragment.kt @@ -10,10 +10,7 @@ import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.github.apognu.otter.repositories.HttpUpstream import com.github.apognu.otter.repositories.Repository -import com.github.apognu.otter.utils.Cache -import com.github.apognu.otter.utils.Event -import com.github.apognu.otter.utils.EventBus -import com.github.apognu.otter.utils.untilNetwork +import com.github.apognu.otter.utils.* import com.google.gson.Gson import kotlinx.android.synthetic.main.fragment_artists.* import kotlinx.coroutines.Dispatchers.IO @@ -105,6 +102,10 @@ abstract class FunkwhaleFragment> : Fragment } repository.fetch(upstreams, size).untilNetwork(lifecycleScope, IO) { data, isCache, page, hasMore -> + if (isCache && data.isEmpty()) { + fetch(Repository.Origin.Network.origin) + } + lifecycleScope.launch(Main) { if (isCache) { adapter.data = data.toMutableList() diff --git a/app/src/main/java/com/github/apognu/otter/repositories/Repository.kt b/app/src/main/java/com/github/apognu/otter/repositories/Repository.kt index 9ce30a7..290a906 100644 --- a/app/src/main/java/com/github/apognu/otter/repositories/Repository.kt +++ b/app/src/main/java/com/github/apognu/otter/repositories/Repository.kt @@ -41,9 +41,11 @@ abstract class Repository> { cacheId?.let { cacheId -> Cache.get(context, cacheId)?.let { reader -> uncache(reader)?.let { cache -> - emit(Response(Origin.Cache, cache.data, ceil(cache.data.size / AppContext.PAGE_SIZE.toDouble()).toInt(), false)) + return@flow emit(Response(Origin.Cache, cache.data, ceil(cache.data.size / AppContext.PAGE_SIZE.toDouble()).toInt(), false)) } } + + return@flow emit(Response(Origin.Cache, listOf(), 1, false)) } }.flowOn(IO)