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

Franck

11/17/2023, 10:13 AM
Coming from C++/Qt/QML land, just got started on Kotlin/Compose about 2 weeks ago. So far so good, I believe this is a really nice and powerful library. Most difficult part to understand for me is how do you expose some of your business logic to the UI? Say I have a service for making grpc calls. How do i access the services from my Composable functions? Do I need to create some sort of Service Locator? Use Dependency injection? What is the right pattern to use for exposing backend services or adapters to the frontend?
c

CLOVIS

11/17/2023, 10:28 AM
Hi! There are many ways to do this. The two most simple solutions: • either instantiate your services in the main function, and pass them down through the composable calls as parameters • or you can use
remember
to instantiate your services directly in the composables
f

Franck

11/17/2023, 10:29 AM
Right. But I just need my services to be instantiated once, not mutlitplied by the number of functions.
Like in a Service Locator: give me the instance of such service so I can use it
a

Adam McDonald

11/17/2023, 10:30 AM
I’d explore using an architecture pattern like MVVM coupled with a dependency injection framework like Koin or Dagger - these allow for handling the instantiation of single/multiple instances of services 👍
f

Franck

11/17/2023, 10:30 AM
I don't to pass the services down to all my functions... That would be terrible!
Why do you always need to use extra libraries for such simple things as a Service Locator?
c

CLOVIS

11/17/2023, 10:32 AM
Because Compose was created by Android devs, and the ecosystem is not yet completely in place for non-Android environments 😕
p

Pablichjenkov

11/17/2023, 10:33 AM
My advice is using a state management and/or navigation library.
c

CLOVIS

11/17/2023, 10:33 AM
A lot of this will be simpler with context receivers, but that's not for the near future
p

Pablichjenkov

11/17/2023, 10:39 AM
Don't pass services down to all your functions. Instead, structure your App in pages or screens or sections. Each of the above will represent and operate on a corresponding State class(sealed class most of the time). Said state is usually called ViewModel, it will be served to your first function or the root function in your screen. Then each part/section of your screen will map to a section in that big encapsulating State, that precisely does that, encapsulates smaller states. All the grpc, network, whatever logic and state mutation happens in that ViewModel or parent State. You then update the section of the State and the compose machinery will call the corresponding composable function that renders the recently updated state, the way you wanted to render it
a

ascii

11/17/2023, 10:46 AM
> Why do you always need to use extra libraries for such simple things as a Service Locator? Because they make it easy? I'm not sure what your expectation is here. In any other scenario, you'd still be using some sort of a library to handle your deps for you. In Java land, Guice is an example, as is Spring. Handrolling your own DI/locator is too much boilerplate and it's never worth it.
f

Franck

11/17/2023, 12:48 PM
Nice. Like the idea of the ViewModel indeed, which I already tried. But wanted some expert feedback first 🙂
I'm only 2w old in Kotlin/Compose learning. Sorry if my questions don't make sense. Got another one: is it possible to render a Compose UI to OpenGL texture?
Is there a library available for Kotlin/Compose UI testing?
a

Adam McDonald

11/17/2023, 2:32 PM
There’s plenty of toolings within androidx.compose for testing: https://developer.android.com/jetpack/compose/testing
c

CLOVIS

11/17/2023, 2:36 PM
All of this is Android-only, no?
a

Adam McDonald

11/17/2023, 2:38 PM
Ahh for Kotlin Mutliplatform compose you can use *`org.jetbrains.compose.uk:ui-test-junit4`* instead
f

Franck

11/17/2023, 2:59 PM
ok thanks a lot!
6 Views