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 ResType on PropDefault annotation #174
Support ResType on PropDefault annotation #174
Conversation
@pavlospt Thanks! I'll have a look today or tomorrow and give more detailed feedback. Before that could you please revert the style changes you made? We have our preferred formatting style and would rather not change it, also makes the PR harder to review. |
@emilsjolander Yeap that's what I wrote on the ps, could you share your formatting style so that I can revert it? Because right now I don't see how I could that. Let me know if you have any ideas! |
Code style issues are fixed! |
We don't have a formal formatting style really. We should probably work on one though 👍 Just look at how the rest of the code in the files are formatted and try to follow the same style. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @IanChilds for annotation processor changes.
@@ -8,8 +8,10 @@ | |||
|
|||
package com.facebook.samples.litho.lithography; | |||
|
|||
import android.graphics.drawable.Drawable; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems unused
import com.facebook.litho.Column; | ||
|
||
import com.facebook.litho.annotations.ResType; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems unused
|
||
@LayoutSpec | ||
public class PlaygroundComponentSpec { | ||
|
||
@PropDefault(resType = ResType.DIMEN_SIZE) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you revert these changes? Good to see example used but we don't want to add a bunch of stuff to the playground.
@@ -0,0 +1,4 @@ | |||
<?xml version="1.0" encoding="utf-8"?> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this file.
@@ -733,4 +768,21 @@ private static MethodSpec generateReleaseMethod(SpecModel specModel) { | |||
.addStatement("$L.release(this)", BUILDER_POOL_FIELD) | |||
.build(); | |||
} | |||
|
|||
private static String generateInitializer(PropDefaultModel propDefaultModel, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If a line exceeds 100 we put all the parameters on the next line, including the first one 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@emilsjolander Do you mean 100 chars? This line seems to be 78 chars including the leading space.
StringBuilder builtInitializer = new StringBuilder(); | ||
|
||
if (propDefaultModel.hasResType()) { | ||
if (propDefaultModel.mResType == ResType.DIMEN_SIZE) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should handle all res types. See how this is done for the builder methods.
@emilsjolander thank you for the review, will try to fix them and finalize it. |
@emilsjolander Fixed most of the review changes, one thing that is not clear though: in order to generate the initializers, should I create new variables? What I mean is:
|
I had a discussion with @charbgr for possible approaches on the above issue: The outcome was that we could add another key on That means that the user gets to declare the same type of variable as the Example: @PropDefault(resType = DRAWABLE, resId = R.mipmap.ic_launcher_round)
Drawable launcherImage; |
Looks pretty good! thanks 👍 Could you please add tests for the added features? |
@emilsjolander Sure thing, is there a specific directory for the Annotation Processor tests ? |
Oh shoot, we're still working on getting the annotation processor tests open-sourced. There were some issues with Gradle and @IanChilds is working on fixing those. |
@passy Hmmm, is there any way around this then ? |
We should have them available today or tomorrow :) |
@IanChilds Great! |
Any updates here? would be great to see this merged. |
@nscoding Did not find some free time to write the tests yet..Hopefully will be able to do so, in the following week! |
@IanChilds can you please review the last commit? I had to use |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome! Just a nit about the .gitignore
. I'll leave the annotation processor changes to @IanChilds to review. :)
.gitignore
Outdated
@@ -18,4 +18,9 @@ docs/.bundle/ | |||
# Android Studio/IntelliJ | |||
.idea | |||
*.iml | |||
.gradle/ | |||
lib/fb/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need those. :)
Can you revert that?
.gitignore
Outdated
lib/fb/ | ||
lib/yoga/ | ||
lib/yogajni/ | ||
*/**.iml |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should already be covered by the one above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is really awesome! Thanks for doing this!
for (PropDefaultModel propDefault : propDefaults) { | ||
if (!propDefault.isResResolvable()) continue; | ||
|
||
initResTypePropDefaultsSpec.addStatement("this.$L.$L = $L", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
one param per line please :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
propsBuilderClassBuilder.addMethod(initMethodSpec.build()); | ||
|
||
if (initResTypePropDefaultsSpec != null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why don't you just do everything at this point and then you don't have to do the null checks?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@IanChilds can you explain this a little bit more? :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think what he's saying is if you move this up to the bottom of the if on line 221, you can drop the null check.
@@ -53,6 +96,8 @@ public int hashCode() { | |||
int result = mType.hashCode(); | |||
result = 17 * result + mName.hashCode(); | |||
result = 31 * result + mModifiers.hashCode(); | |||
result = 31 * result + mResType.hashCode(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use a different number here and on the line below
@@ -97,6 +97,17 @@ public boolean hasDefault(ImmutableList<PropDefaultModel> propDefaults) { | |||
return false; | |||
} | |||
|
|||
public PropDefaultModel getDefault(ImmutableList<PropDefaultModel> propDefaults) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is this for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@IanChilds unused, I removed it. Thanks!
I'd update the docs for PropDefaults (https://fblitho.com/docs/props#prop-defaults) to cover this new feature :-) |
@lucasr I can do this as well and include it in this PR :) |
propsBuilderClassBuilder.addMethod(initMethodSpec.build()); | ||
|
||
if (initResTypePropDefaultsSpec != null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think what he's saying is if you move this up to the bottom of the if on line 221, you can drop the null check.
@@ -199,8 +216,26 @@ static TypeSpecDataHolder generateBuilder(SpecModel specModel) { | |||
initMethodSpec.addStatement("mRequired.clear()"); | |||
} | |||
|
|||
MethodSpec.Builder initResTypePropDefaultsSpec = null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move into the if statement below
import com.facebook.yoga.YogaAlign; | ||
|
||
import com.facebook.yoga.YogaFlexDirection; | ||
import com.facebook.litho.widget.Text; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: can you undo this change (it's nicer when we keep them alphabetized).
@@ -53,6 +96,8 @@ public int hashCode() { | |||
int result = mType.hashCode(); | |||
result = 17 * result + mName.hashCode(); | |||
result = 31 * result + mModifiers.hashCode(); | |||
result = 43 * result + mResType.hashCode(); | |||
result = 65 * result + mResId; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick - for consistency, can you replace 65 with 47 or 53 or 59 or 61 to keep it as prime numbers?
@ahmedre Updated to reflect review requests. Can you check again :) ? |
@ahmedre Done :) |
@IanChilds has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator. |
Initial work on adding support for
ResType
on@PropDefault
( #81 )I went with the solution of having the
Builder
initializing the Props that have a default value marked as ResType, instead of passing down theComponentContext
which I tried to implement but it was introducing a quite huge change.In order to reduce the review scope I have only added generation for
resolveDimenSizeRes()
and will add the rest before closing the PR, in order to have a naming and feature compliance agreement after the initial review. Also, I believe that after resolving the resource on a marked@PropDefault
we should generate resource helper methods like we do for@Prop
.Let me know what you think!
/cc: @emilsjolander
P.S.: There is a big diff on the
ComponentImplGenerator
due to different Code Style, so If you could share the one you are using so that we could be aligned and reduce the diffs, would be great.