leosan
03/23/2021, 10:10 AMisEnabled
to an AbstractComposeView
? For instance I’m trying to make a Wrapper of a composable Button
like that:
My intention was to try to reuse some of the View’s API, otherwise I will have to deprecate every View’s methods so people unaware of how compose works don’t try to update the view using the View’s API
Code in the 🧵Colton Idle
03/23/2021, 11:06 AMleosan
03/23/2021, 11:07 AMleosan
03/23/2021, 11:08 AMclass ButtonPrimary @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyle: Int = 0
) : AbstractComposeView(context, attrs, defStyle) {
var text by mutableStateOf("")
var onClickListener by mutableStateOf({})
private var _enabled by mutableStateOf<Boolean>(true)
@Composable
override fun Content() {
CanvasTheme {
ButtonPrimary(text = text, enabled = _enabled, onClick = onClickListener)
}
}
override fun isEnabled(): Boolean {
_enabled = super.isEnabled()
return _enabled
}
}
The problem how to make it to invalidate/recompose the view when I call the isEnabled= true
findViewById<ButtonPrimary>(R.id.myButton).apply {
text = "My Compose in XML"
onClickListener = {
Toast.makeText(this@HomeActivity, "Toast", Toast.LENGTH_LONG).show()
isEnabled = false // On the first click this is set but the view is not recomposed, on the second click the view changes to the disabled state
}
}
Adam Powell
03/23/2021, 2:48 PMsetEnabled
too and use that to write your _enabled
propertyleosan
03/23/2021, 4:07 PMoverride fun isEnabled(): Boolean {
_enabled = super.isEnabled()
return _enabled
}
but for some reason the view is not recomposed properly (bug maybe?)
findViewById<ButtonPrimary>(R.id.myButton).apply {
text = "My Compose in XML"
onClickListener = {
Toast.makeText(this@HomeActivity, "Toast", Toast.LENGTH_LONG).show()
isEnabled = false // On the first click this is set but the view is not recomposed, on the second click the view changes to the disabled state
}
}
By setting the View’s isEnabled
to false inside the clickListener, it only updates the value to false after the second clickleosan
03/23/2021, 4:29 PMisEnabled = false
in the AbstractComposeView make false
but in the Compose is true, the UI doesn’t reflect until the second clickleosan
03/23/2021, 4:46 PMleosan
03/23/2021, 4:53 PMleosan
03/23/2021, 4:56 PM_enabled
here: https://github.com/leobrujah/CountdownCompose/commit/214b70dbf1e24188e478f641fbf19df38c576723#diff-0a0e9f84d9fcc545d192[…]9fe501d81b67252e8f248adR52
It works properly, only inside the override method it doesn’tleosan
03/23/2021, 5:06 PMsetEnabled
🤦