Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for injecting dependencies into Composables #224

Merged
merged 11 commits into from
Nov 22, 2022

Conversation

snappdevelopment
Copy link
Contributor

This adds new functionality to inject classes into Composables, which have the ComposeFragment or ComposeScreen annotation applied.

Before this change users could provide UI specific dependencies via Composes' LocalCompositionProvider. This got removed in favour of injecting directly into the Composable.

Example:
We want to use an instance of MyDependency in the UI, which is provided through Dagger.

@ComposeFragment( //or @ComposeScreen
    scope = Example::class,
    parentScope = Singleton::class,
    stateMachine = ExampleStateMachine::class,
)
@Composable
internal fun ExampleUi(
    myDependency: MyDependency,
    state: ExampleState,
    sendAction: (ExampleAction) -> Unit,
) {


private fun TopLevelFunctionReference.parameterTypes(): List<ComposableParameter> {
return parameters
.filter { it.name != null && it.name != "state" && it.name != "sendAction" }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

state and sendAction are specific to our codebase. Not sure how this could be prevented

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine, for a first step. A better long term solution would be to see how we can get the type parameters of the state machine class and then match based on those. E.g. FooStateMachine which we have extends StateMachine<FooState, FooAction> so we can extract those 2 to use them here.


private fun TopLevelFunctionReference.parameterTypes(): List<ComposableParameter> {
return parameters
.filter { it.name != null && it.name != "state" && it.name != "sendAction" }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine, for a first step. A better long term solution would be to see how we can get the type parameters of the state machine class and then match based on those. E.g. FooStateMachine which we have extends StateMachine<FooState, FooAction> so we can extract those 2 to use them here.

whetstone/README.md Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants