Last active
May 31, 2021 14:28
-
-
Save cmathew/bf73d3dd87276e29a97ffd9c3afe95d5 to your computer and use it in GitHub Desktop.
Defines the “glue” between a BottomSheetCallback and the Espresso IdlingResource framework.
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
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 | |
} |
Hello!
May I ask what theBottomSheetBehaviourWrapper
looks like?
Oh, that's a simple wrapper class we use for Mock-ability in unit tests. But the standard BottomSheetBehavior will suffice for this example - I updated the gist to use that.
Thank you kindest!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello!
May I ask what the
BottomSheetBehaviourWrapper
looks like?