From 434dec23af9e52f9c6e04d3a385e99b7b8920741 Mon Sep 17 00:00:00 2001 From: ashiagr Date: Tue, 7 Mar 2023 18:08:37 +0530 Subject: [PATCH 1/8] Auto scroll discover carousal --- .../discover/util/AutoScrollHelper.kt | 36 +++++++++++ .../discover/view/DiscoverAdapter.kt | 62 ++++++++++++++++++- 2 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/util/AutoScrollHelper.kt diff --git a/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/util/AutoScrollHelper.kt b/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/util/AutoScrollHelper.kt new file mode 100644 index 00000000000..4f3ec6d4aa5 --- /dev/null +++ b/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/util/AutoScrollHelper.kt @@ -0,0 +1,36 @@ +package au.com.shiftyjelly.pocketcasts.discover.util + +import java.util.Timer +import java.util.TimerTask + +private const val AUTO_SCROLL_INTERVAL = 3000L + +class AutoScrollHelper(private val onAutoScrollCompleted: () -> Unit) { + private var autoScrollTimer: Timer? = null + private var autoScrollTimerTask: TimerTask? = null + private var skipAutoScroll = false + + fun startAutoScrollTimer() { + if (autoScrollTimerTask != null) return + autoScrollTimerTask = object : TimerTask() { + override fun run() { + if (!skipAutoScroll) onAutoScrollCompleted() + skipAutoScroll = false + } + } + autoScrollTimer = Timer().apply { + schedule(autoScrollTimerTask, 0, AUTO_SCROLL_INTERVAL) + } + } + + fun stopAutoScrollTimer() { + autoScrollTimer?.cancel() + autoScrollTimerTask?.cancel() + autoScrollTimer = null + autoScrollTimerTask = null + } + + fun skipAutoScroll() { + skipAutoScroll = true + } +} diff --git a/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/view/DiscoverAdapter.kt b/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/view/DiscoverAdapter.kt index 8bafc44a05c..9d441894772 100644 --- a/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/view/DiscoverAdapter.kt +++ b/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/view/DiscoverAdapter.kt @@ -13,6 +13,7 @@ import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.ListAdapter import androidx.recyclerview.widget.RecyclerView +import androidx.recyclerview.widget.RecyclerView.OnScrollListener import au.com.shiftyjelly.pocketcasts.analytics.AnalyticsEvent import au.com.shiftyjelly.pocketcasts.analytics.AnalyticsTrackerWrapper import au.com.shiftyjelly.pocketcasts.analytics.FirebaseAnalyticsTracker @@ -27,6 +28,7 @@ import au.com.shiftyjelly.pocketcasts.discover.databinding.RowPodcastSmallListBi import au.com.shiftyjelly.pocketcasts.discover.databinding.RowSingleEpisodeBinding import au.com.shiftyjelly.pocketcasts.discover.databinding.RowSinglePodcastBinding import au.com.shiftyjelly.pocketcasts.discover.extensions.updateSubscribeButtonIcon +import au.com.shiftyjelly.pocketcasts.discover.util.AutoScrollHelper import au.com.shiftyjelly.pocketcasts.discover.view.DiscoverFragment.Companion.EPISODE_UUID_KEY import au.com.shiftyjelly.pocketcasts.discover.view.DiscoverFragment.Companion.LIST_ID_KEY import au.com.shiftyjelly.pocketcasts.discover.view.DiscoverFragment.Companion.PODCAST_UUID_KEY @@ -163,6 +165,28 @@ internal class DiscoverAdapter( } inner class CarouselListViewHolder(var binding: RowCarouselListBinding) : NetworkLoadableViewHolder(binding.root) { + private var autoScrollHelper: AutoScrollHelper? = null + private val scrollListener = object : OnScrollListener() { + private var draggingStarted: Boolean = false + + override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) { + super.onScrollStateChanged(recyclerView, newState) + when (newState) { + RecyclerView.SCROLL_STATE_SETTLING -> Unit // Do nothing + RecyclerView.SCROLL_STATE_DRAGGING -> { + draggingStarted = true + autoScrollHelper?.stopAutoScrollTimer() + } + RecyclerView.SCROLL_STATE_IDLE -> { + if (draggingStarted) { + autoScrollHelper?.startAutoScrollTimer() + draggingStarted = false + } + } + } + } + } + val adapter = CarouselListRowAdapter(null, theme, listener::onPodcastClicked, listener::onPodcastSubscribe, analyticsTracker) private val linearLayoutManager = @@ -173,23 +197,59 @@ internal class DiscoverAdapter( init { recyclerView?.layoutManager = linearLayoutManager recyclerView?.itemAnimator = null + recyclerView?.addOnScrollListener(scrollListener) + + autoScrollHelper = AutoScrollHelper { + if (adapter.itemCount == 0) return@AutoScrollHelper + val nextPosition = (binding.pageIndicatorView.position + 1) + .takeIf { it < adapter.itemCount } ?: 0 + recyclerView?.smoothScrollToPosition(nextPosition) + binding.pageIndicatorView.position = nextPosition + trackPageChanged(nextPosition) + } + val snapHelper = HorizontalPeekSnapHelper(0) snapHelper.attachToRecyclerView(recyclerView) snapHelper.onSnapPositionChanged = { position -> + /* Page just snapped, skip auto scroll */ + autoScrollHelper?.skipAutoScroll() binding.pageIndicatorView.position = position - analyticsTracker.track(AnalyticsEvent.DISCOVER_FEATURED_PAGE_CHANGED, mapOf(CURRENT_PAGE to position, TOTAL_PAGES to adapter.itemCount)) + trackPageChanged(position) } recyclerView?.adapter = adapter adapter.submitList(listOf(LoadingItem())) + + itemView.viewTreeObserver?.apply { + /* Stop auto scroll when app is backgrounded */ + addOnWindowFocusChangeListener { hasFocus -> + if (!hasFocus) autoScrollHelper?.stopAutoScrollTimer() + } + /* Manage auto scroll when itemView's visibility changes on going to next screen */ + addOnGlobalLayoutListener { + if (itemView.isShown) { + autoScrollHelper?.startAutoScrollTimer() + } else { + autoScrollHelper?.stopAutoScrollTimer() + } + } + } } override fun onRestoreInstanceState(state: Parcelable?) { super.onRestoreInstanceState(state) recyclerView?.post { binding.pageIndicatorView.position = linearLayoutManager.findFirstVisibleItemPosition() + recyclerView.scrollToPosition(binding.pageIndicatorView.position) } } + + private fun trackPageChanged(position: Int) { + analyticsTracker.track( + AnalyticsEvent.DISCOVER_FEATURED_PAGE_CHANGED, + mapOf(CURRENT_PAGE to position, TOTAL_PAGES to adapter.itemCount) + ) + } } inner class SmallListViewHolder(val binding: RowPodcastSmallListBinding) : NetworkLoadableViewHolder(binding.root), ShowAllRow { From c3f6fb31114eefa6ac3ea5279416342cc2319507 Mon Sep 17 00:00:00 2001 From: ashiagr Date: Tue, 7 Mar 2023 18:11:19 +0530 Subject: [PATCH 2/8] Add feature flag --- base.gradle | 1 + .../discover/view/DiscoverAdapter.kt | 17 ++++++++++------- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/base.gradle b/base.gradle index d593d89154e..2c7433ccdbe 100644 --- a/base.gradle +++ b/base.gradle @@ -31,6 +31,7 @@ android { // Feature Flags buildConfigField "boolean", "END_OF_YEAR_ENABLED", "false" buildConfigField "boolean", "SINGLE_SIGN_ON_ENABLED", "false" + buildConfigField "boolean", "DISCOVER_FEATURED_AUTO_SCROLL", "false" testInstrumentationRunner project.testInstrumentationRunner testApplicationId "au.com.shiftyjelly.pocketcasts.test" + project.name.replace("-", "_") diff --git a/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/view/DiscoverAdapter.kt b/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/view/DiscoverAdapter.kt index 9d441894772..59d4d36d8e6 100644 --- a/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/view/DiscoverAdapter.kt +++ b/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/view/DiscoverAdapter.kt @@ -36,6 +36,7 @@ import au.com.shiftyjelly.pocketcasts.discover.viewmodel.PodcastList import au.com.shiftyjelly.pocketcasts.localization.helper.TimeHelper import au.com.shiftyjelly.pocketcasts.localization.helper.tryToLocalise import au.com.shiftyjelly.pocketcasts.models.entity.Episode +import au.com.shiftyjelly.pocketcasts.preferences.BuildConfig import au.com.shiftyjelly.pocketcasts.repositories.images.into import au.com.shiftyjelly.pocketcasts.servers.cdn.ArtworkColors import au.com.shiftyjelly.pocketcasts.servers.cdn.StaticServerManagerImpl @@ -199,13 +200,15 @@ internal class DiscoverAdapter( recyclerView?.itemAnimator = null recyclerView?.addOnScrollListener(scrollListener) - autoScrollHelper = AutoScrollHelper { - if (adapter.itemCount == 0) return@AutoScrollHelper - val nextPosition = (binding.pageIndicatorView.position + 1) - .takeIf { it < adapter.itemCount } ?: 0 - recyclerView?.smoothScrollToPosition(nextPosition) - binding.pageIndicatorView.position = nextPosition - trackPageChanged(nextPosition) + if (BuildConfig.DISCOVER_FEATURED_AUTO_SCROLL) { + autoScrollHelper = AutoScrollHelper { + if (adapter.itemCount == 0) return@AutoScrollHelper + val nextPosition = (binding.pageIndicatorView.position + 1) + .takeIf { it < adapter.itemCount } ?: 0 + recyclerView?.smoothScrollToPosition(nextPosition) + binding.pageIndicatorView.position = nextPosition + trackPageChanged(nextPosition) + } } val snapHelper = HorizontalPeekSnapHelper(0) From 57442c89e56f392880b086fddbf76d630c0a4916 Mon Sep 17 00:00:00 2001 From: ashiagr Date: Wed, 8 Mar 2023 15:57:25 +0530 Subject: [PATCH 3/8] Increase prefetch count --- .../shiftyjelly/pocketcasts/discover/view/DiscoverAdapter.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/view/DiscoverAdapter.kt b/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/view/DiscoverAdapter.kt index 59d4d36d8e6..462f43e953c 100644 --- a/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/view/DiscoverAdapter.kt +++ b/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/view/DiscoverAdapter.kt @@ -76,6 +76,7 @@ import au.com.shiftyjelly.pocketcasts.ui.R as UR private const val MAX_ROWS_SMALL_LIST = 20 private const val CURRENT_PAGE = "current_page" private const val TOTAL_PAGES = "total_pages" +private const val INITIAL_PREFETCH_COUNT = 3 internal data class ChangeRegionRow(val region: DiscoverRegion) @@ -192,7 +193,7 @@ internal class DiscoverAdapter( private val linearLayoutManager = LinearLayoutManager(itemView.context, RecyclerView.HORIZONTAL, false).apply { - initialPrefetchItemCount = 1 + initialPrefetchItemCount = INITIAL_PREFETCH_COUNT } init { From 328943807fd3c1ce2bae63f7acd9f7c7c0694027 Mon Sep 17 00:00:00 2001 From: ashiagr Date: Wed, 8 Mar 2023 16:17:18 +0530 Subject: [PATCH 4/8] Slow down scroll animation --- .../util/ScrollingLinearLayoutManager.kt | 33 +++++++++++++++++++ .../discover/view/DiscoverAdapter.kt | 13 +++++--- 2 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/util/ScrollingLinearLayoutManager.kt diff --git a/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/util/ScrollingLinearLayoutManager.kt b/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/util/ScrollingLinearLayoutManager.kt new file mode 100644 index 00000000000..38e1e7b643e --- /dev/null +++ b/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/util/ScrollingLinearLayoutManager.kt @@ -0,0 +1,33 @@ +package au.com.shiftyjelly.pocketcasts.discover.util + +import android.content.Context +import android.util.DisplayMetrics +import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.LinearSmoothScroller +import androidx.recyclerview.widget.RecyclerView + +/* Increases scrolling speed of recyclerView.smoothScrollToPosition(position) */ +class ScrollingLinearLayoutManager( + context: Context?, + orientation: Int, + reverseLayout: Boolean, +) : LinearLayoutManager(context, orientation, reverseLayout) { + override fun smoothScrollToPosition( + recyclerView: RecyclerView, + state: RecyclerView.State, + position: Int, + ) { + val linearSmoothScroller: LinearSmoothScroller = + object : LinearSmoothScroller(recyclerView.context) { + override fun calculateSpeedPerPixel(displayMetrics: DisplayMetrics): Float { + return MILLISECONDS_PER_INCH / displayMetrics.densityDpi + } + } + linearSmoothScroller.targetPosition = position + startSmoothScroll(linearSmoothScroller) + } + + companion object { + private const val MILLISECONDS_PER_INCH = 100f // default is 25f (bigger = slower) + } +} diff --git a/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/view/DiscoverAdapter.kt b/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/view/DiscoverAdapter.kt index 462f43e953c..03d95d043c1 100644 --- a/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/view/DiscoverAdapter.kt +++ b/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/view/DiscoverAdapter.kt @@ -29,6 +29,7 @@ import au.com.shiftyjelly.pocketcasts.discover.databinding.RowSingleEpisodeBindi import au.com.shiftyjelly.pocketcasts.discover.databinding.RowSinglePodcastBinding import au.com.shiftyjelly.pocketcasts.discover.extensions.updateSubscribeButtonIcon import au.com.shiftyjelly.pocketcasts.discover.util.AutoScrollHelper +import au.com.shiftyjelly.pocketcasts.discover.util.ScrollingLinearLayoutManager import au.com.shiftyjelly.pocketcasts.discover.view.DiscoverFragment.Companion.EPISODE_UUID_KEY import au.com.shiftyjelly.pocketcasts.discover.view.DiscoverFragment.Companion.LIST_ID_KEY import au.com.shiftyjelly.pocketcasts.discover.view.DiscoverFragment.Companion.PODCAST_UUID_KEY @@ -191,13 +192,17 @@ internal class DiscoverAdapter( val adapter = CarouselListRowAdapter(null, theme, listener::onPodcastClicked, listener::onPodcastSubscribe, analyticsTracker) - private val linearLayoutManager = - LinearLayoutManager(itemView.context, RecyclerView.HORIZONTAL, false).apply { + private val scrollingLayoutManager = + ScrollingLinearLayoutManager( + itemView.context, + RecyclerView.HORIZONTAL, + false + ).apply { initialPrefetchItemCount = INITIAL_PREFETCH_COUNT } init { - recyclerView?.layoutManager = linearLayoutManager + recyclerView?.layoutManager = scrollingLayoutManager recyclerView?.itemAnimator = null recyclerView?.addOnScrollListener(scrollListener) @@ -243,7 +248,7 @@ internal class DiscoverAdapter( override fun onRestoreInstanceState(state: Parcelable?) { super.onRestoreInstanceState(state) recyclerView?.post { - binding.pageIndicatorView.position = linearLayoutManager.findFirstVisibleItemPosition() + binding.pageIndicatorView.position = scrollingLayoutManager.findFirstVisibleItemPosition() recyclerView.scrollToPosition(binding.pageIndicatorView.position) } } From 74d7ac0dc1a4d9b7da3b202b7d8044bb19b1736f Mon Sep 17 00:00:00 2001 From: ashiagr Date: Wed, 8 Mar 2023 16:18:26 +0530 Subject: [PATCH 5/8] Start auto scroll timer with a delay after manual swipe --- .../pocketcasts/discover/util/AutoScrollHelper.kt | 8 ++++++-- .../pocketcasts/discover/view/DiscoverAdapter.kt | 3 ++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/util/AutoScrollHelper.kt b/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/util/AutoScrollHelper.kt index 4f3ec6d4aa5..2717ff29a9c 100644 --- a/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/util/AutoScrollHelper.kt +++ b/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/util/AutoScrollHelper.kt @@ -10,7 +10,7 @@ class AutoScrollHelper(private val onAutoScrollCompleted: () -> Unit) { private var autoScrollTimerTask: TimerTask? = null private var skipAutoScroll = false - fun startAutoScrollTimer() { + fun startAutoScrollTimer(delay: Long = 0L) { if (autoScrollTimerTask != null) return autoScrollTimerTask = object : TimerTask() { override fun run() { @@ -19,7 +19,7 @@ class AutoScrollHelper(private val onAutoScrollCompleted: () -> Unit) { } } autoScrollTimer = Timer().apply { - schedule(autoScrollTimerTask, 0, AUTO_SCROLL_INTERVAL) + schedule(autoScrollTimerTask, delay, AUTO_SCROLL_INTERVAL) } } @@ -33,4 +33,8 @@ class AutoScrollHelper(private val onAutoScrollCompleted: () -> Unit) { fun skipAutoScroll() { skipAutoScroll = true } + + companion object { + const val AUTO_SCROLL_DELAY = 1000L + } } diff --git a/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/view/DiscoverAdapter.kt b/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/view/DiscoverAdapter.kt index 03d95d043c1..e2040321ea9 100644 --- a/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/view/DiscoverAdapter.kt +++ b/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/view/DiscoverAdapter.kt @@ -182,7 +182,8 @@ internal class DiscoverAdapter( } RecyclerView.SCROLL_STATE_IDLE -> { if (draggingStarted) { - autoScrollHelper?.startAutoScrollTimer() + /* Start auto scroll with a delay after a manual swipe */ + autoScrollHelper?.startAutoScrollTimer(delay = AutoScrollHelper.AUTO_SCROLL_DELAY) draggingStarted = false } } From 0bfa26c4dc01520a60413c1a8a9840b67db68047 Mon Sep 17 00:00:00 2001 From: ashiagr Date: Wed, 8 Mar 2023 16:19:28 +0530 Subject: [PATCH 6/8] Avoid back scroll animation on repeat --- .../discover/view/DiscoverAdapter.kt | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/view/DiscoverAdapter.kt b/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/view/DiscoverAdapter.kt index e2040321ea9..e52dad5c170 100644 --- a/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/view/DiscoverAdapter.kt +++ b/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/view/DiscoverAdapter.kt @@ -69,6 +69,8 @@ import io.reactivex.disposables.Disposable import io.reactivex.functions.BiFunction import io.reactivex.rxkotlin.subscribeBy import io.reactivex.schedulers.Schedulers +import kotlinx.coroutines.MainScope +import kotlinx.coroutines.launch import timber.log.Timber import java.util.Locale import au.com.shiftyjelly.pocketcasts.localization.R as LR @@ -210,11 +212,19 @@ internal class DiscoverAdapter( if (BuildConfig.DISCOVER_FEATURED_AUTO_SCROLL) { autoScrollHelper = AutoScrollHelper { if (adapter.itemCount == 0) return@AutoScrollHelper - val nextPosition = (binding.pageIndicatorView.position + 1) + val currentPosition = binding.pageIndicatorView.position + val nextPosition = (currentPosition + 1) .takeIf { it < adapter.itemCount } ?: 0 - recyclerView?.smoothScrollToPosition(nextPosition) - binding.pageIndicatorView.position = nextPosition - trackPageChanged(nextPosition) + MainScope().launch { + if (nextPosition > currentPosition) { + recyclerView?.smoothScrollToPosition(nextPosition) + } else { + /* Jump to the beginning to avoid a backward scroll animation */ + recyclerView?.scrollToPosition(nextPosition) + } + binding.pageIndicatorView.position = nextPosition + trackPageChanged(nextPosition) + } } } From 59b48795c02fb05818dcf84d0c0b555fa5a51332 Mon Sep 17 00:00:00 2001 From: ashiagr Date: Fri, 10 Mar 2023 13:27:47 +0530 Subject: [PATCH 7/8] Increase scroll and delay interval to 5 secs --- .../shiftyjelly/pocketcasts/discover/util/AutoScrollHelper.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/util/AutoScrollHelper.kt b/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/util/AutoScrollHelper.kt index 2717ff29a9c..7c15d38262d 100644 --- a/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/util/AutoScrollHelper.kt +++ b/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/util/AutoScrollHelper.kt @@ -3,7 +3,7 @@ package au.com.shiftyjelly.pocketcasts.discover.util import java.util.Timer import java.util.TimerTask -private const val AUTO_SCROLL_INTERVAL = 3000L +private const val AUTO_SCROLL_INTERVAL = 5000L class AutoScrollHelper(private val onAutoScrollCompleted: () -> Unit) { private var autoScrollTimer: Timer? = null @@ -35,6 +35,6 @@ class AutoScrollHelper(private val onAutoScrollCompleted: () -> Unit) { } companion object { - const val AUTO_SCROLL_DELAY = 1000L + const val AUTO_SCROLL_DELAY = 5000L } } From 0fd13a53c245222a5aa83adee92fc322175f78b1 Mon Sep 17 00:00:00 2001 From: ashiagr Date: Fri, 10 Mar 2023 13:32:53 +0530 Subject: [PATCH 8/8] Pre fetch next page --- .../discover/util/ScrollingLinearLayoutManager.kt | 12 +++++++++++- .../pocketcasts/discover/view/DiscoverAdapter.kt | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/util/ScrollingLinearLayoutManager.kt b/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/util/ScrollingLinearLayoutManager.kt index 38e1e7b643e..262ca19bf0f 100644 --- a/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/util/ScrollingLinearLayoutManager.kt +++ b/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/util/ScrollingLinearLayoutManager.kt @@ -6,7 +6,10 @@ import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearSmoothScroller import androidx.recyclerview.widget.RecyclerView -/* Increases scrolling speed of recyclerView.smoothScrollToPosition(position) */ +/* +1. Increases scrolling speed of recyclerView.smoothScrollToPosition(position) +2. Sets custom extra layout space for pre caching an extra page +*/ class ScrollingLinearLayoutManager( context: Context?, orientation: Int, @@ -27,6 +30,13 @@ class ScrollingLinearLayoutManager( startSmoothScroll(linearSmoothScroller) } + /* By default, LinearLayoutManager lays out 1 extra page of items while smooth scrolling, in the direction of the scroll. + This behavior is overridden to lay out an extra page even on a manual swipe. */ + override fun calculateExtraLayoutSpace(state: RecyclerView.State, extraLayoutSpace: IntArray) { + extraLayoutSpace[0] = 0 + extraLayoutSpace[1] = this.width + } + companion object { private const val MILLISECONDS_PER_INCH = 100f // default is 25f (bigger = slower) } diff --git a/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/view/DiscoverAdapter.kt b/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/view/DiscoverAdapter.kt index e52dad5c170..237ba041dbd 100644 --- a/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/view/DiscoverAdapter.kt +++ b/modules/features/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/view/DiscoverAdapter.kt @@ -79,7 +79,7 @@ import au.com.shiftyjelly.pocketcasts.ui.R as UR private const val MAX_ROWS_SMALL_LIST = 20 private const val CURRENT_PAGE = "current_page" private const val TOTAL_PAGES = "total_pages" -private const val INITIAL_PREFETCH_COUNT = 3 +private const val INITIAL_PREFETCH_COUNT = 1 internal data class ChangeRegionRow(val region: DiscoverRegion)