From bc1e911b4144d014ffefcaa7b91eae9a7628cf80 Mon Sep 17 00:00:00 2001 From: Antoine POPINEAU Date: Fri, 10 Jul 2020 20:28:44 +0200 Subject: [PATCH] Globalize the use of caching for main sections data, improved handling of loading more pages. --- .../otter/fragments/AlbumsGridFragment.kt | 1 + .../otter/fragments/FunkwhaleFragment.kt | 51 ++++++++++--------- .../otter/fragments/PlaylistsFragment.kt | 1 + .../apognu/otter/fragments/RadiosFragment.kt | 1 + .../apognu/otter/playback/RadioPlayer.kt | 5 -- .../com/github/apognu/otter/utils/Util.kt | 8 +-- 6 files changed, 33 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/com/github/apognu/otter/fragments/AlbumsGridFragment.kt b/app/src/main/java/com/github/apognu/otter/fragments/AlbumsGridFragment.kt index 592853a..d260806 100644 --- a/app/src/main/java/com/github/apognu/otter/fragments/AlbumsGridFragment.kt +++ b/app/src/main/java/com/github/apognu/otter/fragments/AlbumsGridFragment.kt @@ -19,6 +19,7 @@ class AlbumsGridFragment : FunkwhaleFragment() { override val viewRes = R.layout.fragment_albums_grid override val recycler: RecyclerView get() = albums override val layoutManager get() = GridLayoutManager(context, 3) + override val alwaysRefresh = false override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) 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 5a1f9ba..84e8d8b 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 @@ -28,8 +25,7 @@ abstract class FunkwhaleAdapter : RecyclerView. } abstract class FunkwhaleFragment> : Fragment() { - val INITIAL_PAGES = 5 - val OFFSCREEN_PAGES = 10 + val OFFSCREEN_PAGES = 20 abstract val viewRes: Int abstract val recycler: RecyclerView @@ -39,7 +35,6 @@ abstract class FunkwhaleFragment> : Fragment lateinit var repository: Repository lateinit var adapter: A - private var initialFetched = false private var moreLoading = false private var listener: Job? = null @@ -57,10 +52,10 @@ abstract class FunkwhaleFragment> : Fragment if (upstream.behavior == HttpUpstream.Behavior.Progressive) { recycler.setOnScrollChangeListener { _, _, _, _, _ -> val offset = recycler.computeVerticalScrollOffset() - val left = recycler.computeVerticalScrollRange() - recycler.height - offset - if (initialFetched && !moreLoading && offset > 0 && left < (recycler.height * OFFSCREEN_PAGES)) { + if (!moreLoading && offset > 0 && needsMoreOffscreenPages()) { moreLoading = true + fetch(Repository.Origin.Network.origin, adapter.data.size) } } @@ -106,13 +101,17 @@ abstract class FunkwhaleFragment> : Fragment } } - repository.fetch(upstreams, size).untilNetwork(lifecycleScope, IO) { data, isCache, page, hasMore -> + moreLoading = true + + repository.fetch(upstreams, size).untilNetwork(lifecycleScope, IO) { data, isCache, _, hasMore -> if (isCache && data.isEmpty()) { return@untilNetwork fetch(Repository.Origin.Network.origin) } lifecycleScope.launch(Main) { if (isCache) { + moreLoading = false + adapter.data = data.toMutableList() adapter.notifyDataSetChanged() @@ -127,14 +126,6 @@ abstract class FunkwhaleFragment> : Fragment adapter.data.addAll(data) - (repository.upstream as? HttpUpstream<*, *>)?.let { upstream -> - when (upstream.behavior) { - HttpUpstream.Behavior.Progressive -> if (!hasMore || page >= INITIAL_PAGES) swiper?.isRefreshing = false - HttpUpstream.Behavior.AtOnce -> if (!hasMore) swiper?.isRefreshing = false - HttpUpstream.Behavior.Single -> if (!hasMore) swiper?.isRefreshing = false - } - } - withContext(IO) { if (adapter.data.isNotEmpty()) { try { @@ -151,20 +142,27 @@ abstract class FunkwhaleFragment> : Fragment } if (hasMore) { - moreLoading = false - (repository.upstream as? HttpUpstream<*, *>)?.let { upstream -> if (!isCache && upstream.behavior == HttpUpstream.Behavior.Progressive) { - if (page < INITIAL_PAGES) { - moreLoading = true + if (first || needsMoreOffscreenPages()) { fetch(Repository.Origin.Network.origin, adapter.data.size) } else { - initialFetched = true + moreLoading = false } + } else { + moreLoading = false } } } + (repository.upstream as? HttpUpstream<*, *>)?.let { upstream -> + when (upstream.behavior) { + HttpUpstream.Behavior.Progressive -> if (!hasMore || !moreLoading) swiper?.isRefreshing = false + HttpUpstream.Behavior.AtOnce -> if (!hasMore) swiper?.isRefreshing = false + HttpUpstream.Behavior.Single -> if (!hasMore) swiper?.isRefreshing = false + } + } + when (first) { true -> { adapter.notifyDataSetChanged() @@ -176,4 +174,11 @@ abstract class FunkwhaleFragment> : Fragment } } } + + private fun needsMoreOffscreenPages(): Boolean { + val offset = recycler.computeVerticalScrollOffset() + val left = recycler.computeVerticalScrollRange() - recycler.height - offset + + return left < (recycler.height * OFFSCREEN_PAGES) + } } diff --git a/app/src/main/java/com/github/apognu/otter/fragments/PlaylistsFragment.kt b/app/src/main/java/com/github/apognu/otter/fragments/PlaylistsFragment.kt index b24e925..82d4a11 100644 --- a/app/src/main/java/com/github/apognu/otter/fragments/PlaylistsFragment.kt +++ b/app/src/main/java/com/github/apognu/otter/fragments/PlaylistsFragment.kt @@ -17,6 +17,7 @@ import kotlinx.android.synthetic.main.fragment_playlists.* class PlaylistsFragment : FunkwhaleFragment() { override val viewRes = R.layout.fragment_playlists override val recycler: RecyclerView get() = playlists + override val alwaysRefresh = false override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) diff --git a/app/src/main/java/com/github/apognu/otter/fragments/RadiosFragment.kt b/app/src/main/java/com/github/apognu/otter/fragments/RadiosFragment.kt index e251aaf..36fdcbb 100644 --- a/app/src/main/java/com/github/apognu/otter/fragments/RadiosFragment.kt +++ b/app/src/main/java/com/github/apognu/otter/fragments/RadiosFragment.kt @@ -16,6 +16,7 @@ import kotlinx.coroutines.launch class RadiosFragment : FunkwhaleFragment() { override val viewRes = R.layout.fragment_radios override val recycler: RecyclerView get() = radios + override val alwaysRefresh = false override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) diff --git a/app/src/main/java/com/github/apognu/otter/playback/RadioPlayer.kt b/app/src/main/java/com/github/apognu/otter/playback/RadioPlayer.kt index f40a016..7ca7cfc 100644 --- a/app/src/main/java/com/github/apognu/otter/playback/RadioPlayer.kt +++ b/app/src/main/java/com/github/apognu/otter/playback/RadioPlayer.kt @@ -35,11 +35,6 @@ class RadioPlayer(val context: Context, val scope: CoroutineScope) { private val favoritedRepository = FavoritedRepository(context) init { - Cache.get(context, "radio_type")?.readLine()?.log() - Cache.get(context, "radio_id")?.readLine()?.log() - Cache.get(context, "radio_session")?.readLine()?.log() - Cache.get(context, "radio_cookie")?.readLine()?.log() - Cache.get(context, "radio_type")?.readLine()?.let { radio_type -> Cache.get(context, "radio_id")?.readLine()?.toInt()?.let { radio_id -> Cache.get(context, "radio_session")?.readLine()?.toInt()?.let { radio_session -> diff --git a/app/src/main/java/com/github/apognu/otter/utils/Util.kt b/app/src/main/java/com/github/apognu/otter/utils/Util.kt index 9d3a3f4..13edf41 100644 --- a/app/src/main/java/com/github/apognu/otter/utils/Util.kt +++ b/app/src/main/java/com/github/apognu/otter/utils/Util.kt @@ -12,12 +12,8 @@ fun Context?.toast(message: String, length: Int = Toast.LENGTH_SHORT) { } } -fun Any.log(message: String) { - Log.d("FUNKWHALE", "${this.javaClass.simpleName}: $message") -} - -fun Any.log() { - Log.d("FUNKWHALE", this.toString()) +fun Any.log(message: Any) { + Log.d("OTTER", "${javaClass.simpleName}: $message") } fun maybeNormalizeUrl(rawUrl: String?): String? {