-
Notifications
You must be signed in to change notification settings - Fork 9
Description
We should be able to set the unlocked status programmatically. This is important when persisting state between screen rotations and/or app recycling. Also important when it reflects some "unlocked" state coming from a database field or somewhere else. Currently I'm using reflection and a delay hack to achieve that with the code bellow:
private fun setSliderConfirmed() {
/**
* This is a HACKY way of keeping the slider's state since it doesn't have a proper way
* to set it's state programmatically. We must add a delay or else it's inner views will
* still be null at this stage and calling setUnlockedStatus will fail with a NullReferenceException
* The proper way to do this is to change the library source code to allow setting the initial
* state properly and handle it gracefully.
*/
lifecycleScope.launch {
// this delay may be device dependent. It may not work (be sufficient) on less powerful devices.
delay(500)
var setUnlocked: Method = SlideToConfirm::class.java.getDeclaredMethod("setUnlockedStatus")
setUnlocked.isAccessible = true
setUnlocked.invoke(binding.slideToConfirm)
}
}
The code is called in the onViewCreated
method, but the SlideToConfirm
inner views are still null
at this stage. Calling init
(via reflection) then setUnlockedStatus
didn't work because when the inner views are actually created everything is reset. That's why a delay was used (I've tried using viewTreeObserver.addOnGlobalLayoutListener
and other methods to no avail)
A private boolean field mUnlocked
could be introduced to represent the current locked/unlocked state, and the control can use it to properly set it up when it is initialized and a setUnlocked()
method could be provided which is analogous to reset()
but doing the opposite work ;-)