-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #818 from Automattic/task/add-auto-scroll-discover…
…-carousal Add auto scroll to discover featured carousal
- Loading branch information
Showing
4 changed files
with
170 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...s/discover/src/main/java/au/com/shiftyjelly/pocketcasts/discover/util/AutoScrollHelper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package au.com.shiftyjelly.pocketcasts.discover.util | ||
|
||
import java.util.Timer | ||
import java.util.TimerTask | ||
|
||
private const val AUTO_SCROLL_INTERVAL = 5000L | ||
|
||
class AutoScrollHelper(private val onAutoScrollCompleted: () -> Unit) { | ||
private var autoScrollTimer: Timer? = null | ||
private var autoScrollTimerTask: TimerTask? = null | ||
private var skipAutoScroll = false | ||
|
||
fun startAutoScrollTimer(delay: Long = 0L) { | ||
if (autoScrollTimerTask != null) return | ||
autoScrollTimerTask = object : TimerTask() { | ||
override fun run() { | ||
if (!skipAutoScroll) onAutoScrollCompleted() | ||
skipAutoScroll = false | ||
} | ||
} | ||
autoScrollTimer = Timer().apply { | ||
schedule(autoScrollTimerTask, delay, AUTO_SCROLL_INTERVAL) | ||
} | ||
} | ||
|
||
fun stopAutoScrollTimer() { | ||
autoScrollTimer?.cancel() | ||
autoScrollTimerTask?.cancel() | ||
autoScrollTimer = null | ||
autoScrollTimerTask = null | ||
} | ||
|
||
fun skipAutoScroll() { | ||
skipAutoScroll = true | ||
} | ||
|
||
companion object { | ||
const val AUTO_SCROLL_DELAY = 5000L | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...rc/main/java/au/com/shiftyjelly/pocketcasts/discover/util/ScrollingLinearLayoutManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
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 | ||
|
||
/* | ||
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, | ||
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) | ||
} | ||
|
||
/* 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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters