Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Defines the “glue” between a BottomSheetCallback and the Espresso IdlingResource framework.
abstract class BottomSheetResource(
private val bottomSheetBehavior: BottomSheetBehaviorWrapper
) : 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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment