Skip to content

Instantly share code, notes, and snippets.

@cmathew
Last active May 31, 2021 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmathew/bf73d3dd87276e29a97ffd9c3afe95d5 to your computer and use it in GitHub Desktop.
Save cmathew/bf73d3dd87276e29a97ffd9c3afe95d5 to your computer and use it in GitHub Desktop.
Defines the “glue” between a BottomSheetCallback and the Espresso IdlingResource framework.
abstract class BottomSheetResource(
private val bottomSheetBehavior: BottomSheetBehavior<View>
) : IdlingResource, BottomSheetBehavior.BottomSheetCallback() {
private var isIdle: Boolean = false
private var resourceCallback: IdlingResource.ResourceCallback? = null
override fun onSlide(bottomSheet: View, slideOffset: Float) {}
override fun onStateChanged(bottomSheet: View, newState: Int) {
val wasIdle = isIdle
isIdle = isDesiredState(newState)
if (!wasIdle && isIdle) {
bottomSheetBehavior.removeBottomSheetCallback(this)
resourceCallback?.onTransitionToIdle()
}
}
override fun isIdleNow(): Boolean {
return isIdle
}
override fun registerIdleTransitionCallback(callback: IdlingResource.ResourceCallback) {
resourceCallback = callback
val state = bottomSheetBehavior.state
isIdle = isDesiredState(state)
if (isIdle) {
resourceCallback!!.onTransitionToIdle()
} else {
bottomSheetBehavior.addBottomSheetCallback(this)
}
}
abstract fun isDesiredState(@BottomSheetBehavior.State state: Int): Boolean
}
@daniellAlgar
Copy link

Thank you kindest!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment