From f3bbca9c276b78941651051076b48c2d19700663 Mon Sep 17 00:00:00 2001 From: Antoine POPINEAU Date: Wed, 8 Jul 2020 23:21:47 +0200 Subject: [PATCH] Fixed caching of metadata. --- .../otter/fragments/FunkwhaleFragment.kt | 49 ++++++++++--------- 1 file changed, 25 insertions(+), 24 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 2de5ab0..c168e89 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,7 +10,10 @@ 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.* +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.google.gson.Gson import kotlinx.android.synthetic.main.fragment_artists.* import kotlinx.coroutines.Dispatchers.IO @@ -103,7 +106,7 @@ abstract class FunkwhaleFragment> : Fragment repository.fetch(upstreams, size).untilNetwork(lifecycleScope, IO) { data, isCache, page, hasMore -> if (isCache && data.isEmpty()) { - fetch(Repository.Origin.Network.origin) + return@untilNetwork fetch(Repository.Origin.Network.origin) } lifecycleScope.launch(Main) { @@ -130,33 +133,31 @@ abstract class FunkwhaleFragment> : Fragment } } - when (hasMore) { - false -> withContext(IO) { - if (adapter.data.isNotEmpty()) { - try { - repository.cacheId?.let { cacheId -> - Cache.set( - context, - cacheId, - Gson().toJson(repository.cache(adapter.data)).toByteArray() - ) - } - } catch (e: ConcurrentModificationException) { + withContext(IO) { + if (adapter.data.isNotEmpty()) { + try { + repository.cacheId?.let { cacheId -> + Cache.set( + context, + cacheId, + Gson().toJson(repository.cache(adapter.data)).toByteArray() + ) } + } catch (e: ConcurrentModificationException) { } } + } - true -> { - moreLoading = false + if (hasMore) { + moreLoading = false - (repository.upstream as? HttpUpstream<*, *>)?.let { upstream -> - if (upstream.behavior == HttpUpstream.Behavior.Progressive) { - if (page < INITIAL_PAGES) { - moreLoading = true - fetch(Repository.Origin.Network.origin, adapter.data.size) - } else { - initialFetched = true - } + (repository.upstream as? HttpUpstream<*, *>)?.let { upstream -> + if (!isCache && upstream.behavior == HttpUpstream.Behavior.Progressive) { + if (page < INITIAL_PAGES) { + moreLoading = true + fetch(Repository.Origin.Network.origin, adapter.data.size) + } else { + initialFetched = true } } }