https://kotlinlang.org logo
#compose
Title
# compose
l

Leland Richardson [G]

12/13/2019, 6:38 PM
it’s possible without it, but that artifact does add a lot of “adapters” that would make your life a lot easier
z

Zach Klippenstein (he/him) [MOD]

12/13/2019, 6:59 PM
Cool, so I should just be able to “call” any
View
subclass from a composable function? Are there any requirements about which view constructors are required or anything? I’m trying to do this with a custom view and I’m only seeing the constructors I’ve defined, no versions with
ref
or anything. So I’ve got a custom View that looks like this:
Copy code
class WorkflowViewStub
@JvmOverloads constructor(
  context: Context,
  attributeSet: AttributeSet? = null,
  defStyle: Int = 0,
  defStyleRes: Int = 0
) : View(context, attributeSet, defStyle, defStyleRes) { … }
And I should just be able to do something like this?
Copy code
@Composable fun myComponent() {
  WorkflowViewStub()
}
(https://github.com/square/workflow/pull/703/commits/6fc61720a281055ee410ad41566546a640481661#diff-f3d0030f5e5c4962c8c2e1b4d5300553R47-R54)
l

Leland Richardson [G]

12/13/2019, 7:15 PM
Great question… A couple of things to understand here: 1. IDE support for these calls is really lacking. If it is intercepted successfully, it should not show up as red/unresolved, but you will also not (yet) be able to “command-click” to go to declaration of the call. That’s the “quick test” to figure out if it worked without doing a full compile. 2. Right now for views it targets constructors of the form
(Context) -> T
where
T: View
. 3. Properties (and getters/setters from java) will be resolvable as
name = T
in the argument list of the call. ALL parameters must be named in order for this to work. For example,
TextView(text="hello world")
works because of
setText(String)
and
TextView(text=R.string.foo)
works because of the setter overload.
i actually haven’t checked to see if the
@JvmOverloads
constructor like you showed will get properly intercepted. if it’s not working, try splitting it out and creating one with just
Context
z

Zach Klippenstein (he/him) [MOD]

12/13/2019, 8:35 PM
Awesome, thanks for the tips!
I simplified the constructor to only take a
Context
, and it compiles but i’m getting a runtime crash:
IllegalStateException: Could not convert WorkflowViewStub to an Emittable
.
ViewComposer
seems to be trying to find an adapter for my view, but the
adapters
list is empty.
l

Leland Richardson [G]

12/13/2019, 10:23 PM
in order to use views, you will want to start off compose using
setViewContent
instead of
setContent
at some point we will create ways to easily switch between the two. right now it is a bit of a pain
z

Zach Klippenstein (he/him) [MOD]

12/13/2019, 10:24 PM
I’m using
setViewContent
on a
FrameLayout
.
l

Leland Richardson [G]

12/13/2019, 10:26 PM
so the error above means that you’re trying to add a
WorkFlowViewStub
as a child to a ComponentNode
are you trying to wrap this with other compose UI composables like `Row`/`Column?`
because right now that won’t work
z

Zach Klippenstein (he/him) [MOD]

12/13/2019, 10:28 PM
Ah, yes I am (a
Container
).
I’ll shelve this project for now. Thanks again for your help!
l

Leland Richardson [G]

12/13/2019, 10:46 PM
np
3 Views