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

Support assisted injections + consolidate and clean up APIs #236

Merged
merged 17 commits into from
Oct 27, 2022
Merged

Conversation

ZacSweers
Copy link
Collaborator

Followup from #235 and more for #13

This adds support for assisted injections for common types (screen, navigator, config) and consolidates the Anvil APIs back into the same artifact for now as it really didn't end up being very useful to define it separately.

Final bit was moving remaining star sample factories to this, result was quite nice

image

Copy link
Collaborator

@jpetote jpetote left a comment

Choose a reason for hiding this comment

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

I have quite a few questions around CircuitSymbolProcessorProvider but i'll ask them in Slack instead.

codeBlock =
when (factoryType) {
FactoryType.PRESENTER ->
CodeBlock.of(
"%M·{·%M()·}",
"%M·{·%M(%L)·}",
Copy link
Collaborator

Choose a reason for hiding this comment

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

I see that %L is assistedParams and that comes from fd which is a KSFunctionDeclarationbut what is that? And what isfd` short hand for?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fd is just shorter to write than functionDeclaration. A KSFunctionDeclaration is KSP's modeling of a kotlin function. A %L template is for literals in kotlinpoet: https://square.github.io/kotlinpoet/#l-for-literals. assistedParams is a CodeBlock of any assisted parameters we're passing on to the factory/constructor/function like (screen = screen, navigator = navigator)

@Suppress("unused") public annotation class CircuitInject<out Screen>

public annotation class CircuitScope(val scope: KClass<*>)
public annotation class CircuitInject(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we add the kdoc for this now since this PR puts this in a pretty complete state.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

override fun create(screen: Screen, circuitConfig: CircuitConfig): ScreenUi? {
if (screen is HomeScreen) {
return ScreenUi(homeUi())
@CircuitInject(screen = HomeScreen::class, scope = AppScope::class)
Copy link
Collaborator

Choose a reason for hiding this comment

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

:chefs-kiss:

@@ -103,21 +128,27 @@ private class CircuitSymbolProcessor(
return emptyList()
}

@OptIn(KspExperimental::class)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since we have the file opt in at the top do we need this one here too?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Removed it as it wasn't necessary in the file level 9f36407

@ZacSweers
Copy link
Collaborator Author

extracted the annotations to a separate project so we keep @CircuitInject out of the core circuit artifact, since it's dagger and anvil-specific

}

private inline fun <reified T> Resolver.loadOptionalKSType(): KSType? {
return getClassDeclarationByName(getKSNameFromString(T::class.java.canonicalName))
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: there's a lint warning here about T::class.java.canonicalName being nullable

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm not seeing anything
image

Copy link
Collaborator

Choose a reason for hiding this comment

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

Weird..I'm still seeing it. canonicalName is an unannotated java method that returns a String, which supports the lint warning I'm seeing about expecting String but found String?

* [Ui.Factory] classes generated for them.
*
* **Presenter**
* ```kotlin
Copy link
Contributor

Choose a reason for hiding this comment

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

what is ```kotlin for?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Same as it is in github markdown!

@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
public annotation class CircuitInject(
val screen: KClass<out Screen>,
val scope: KClass<*>,
Copy link
Contributor

Choose a reason for hiding this comment

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

why can't the scope just be of type KClass. What is the purpose of the generic here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

You can't write raw types in kotlin like you can in Java. For example, in Java it's legal to write List foo = ..., but in Kotlin you must write the generic, even if it's just a star. val foo: List = ... isn't legal syntax

Comment on lines +3 to +5
threshold: 120
NestedBlockDepth:
threshold: 6
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Matches our internal config, detekt's defaults are a bit iffy. Especially with ktfmt

Copy link
Collaborator

@kierse kierse left a comment

Choose a reason for hiding this comment

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

One suggested change related to presenter package, and that lint warning regarding use of canonicalName. Looks good otherwise

Copy link
Contributor

@iamritav iamritav left a comment

Choose a reason for hiding this comment

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

yay for code gen!

@ZacSweers ZacSweers merged commit 15010e7 into main Oct 27, 2022
@ZacSweers ZacSweers deleted the z/assisted branch October 27, 2022 00:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants