Skip to content

Commit

Permalink
Paris improvements (#310)
Browse files Browse the repository at this point in the history
  • Loading branch information
elihart authored Oct 10, 2017
1 parent e882af7 commit b69fdff
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import javax.lang.model.util.Types;

import static com.airbnb.epoxy.ClassNames.ANDROID_ASYNC_TASK;
import static com.airbnb.epoxy.ModelViewWriterKt.addStyleApplierCode;
import static com.airbnb.epoxy.ParisStyleAttributeInfoKt.PARIS_DEFAULT_STYLE_CONSTANT_NAME;
import static com.airbnb.epoxy.ParisStyleAttributeInfoKt.PARIS_STYLE_ATTR_NAME;
import static com.airbnb.epoxy.ParisStyleAttributeInfoKt.weakReferenceFieldForStyle;
Expand Down Expand Up @@ -251,11 +250,11 @@ private Iterable<FieldSpec> buildStyleConstant(GeneratedModelInfo info) {

// We store styles in a weak reference since if a controller uses it
// once it is likely to be used in other models and when models are rebuilt
for (String styleName : styleBuilderInfo.getStyleNames()) {
for (ParisStyle style : styleBuilderInfo.getStyles()) {
constantFields.add(
FieldSpec.builder(
ParameterizedTypeName.get(ClassName.get(WeakReference.class), ClassNames.PARIS_STYLE),
weakReferenceFieldForStyle(styleName),
weakReferenceFieldForStyle(style.getName()),
PRIVATE, STATIC
)
.build());
Expand Down Expand Up @@ -468,11 +467,6 @@ private Iterable<MethodSpec> generateProgrammaticViewMethods(GeneratedModelInfo
.addStatement("v.setLayoutParams(new $T($L, $L))",
ClassNames.ANDROID_MARGIN_LAYOUT_PARAMS, layoutWidth, layoutHeight);

ParisStyleAttributeInfo styleBuilderInfo = modelInfo.getStyleBuilderInfo();
if (styleBuilderInfo != null) {
addStyleApplierCode(builder, styleBuilderInfo, "v");
}

methods.add(builder.addStatement("return v").build());

return methods;
Expand Down Expand Up @@ -629,6 +623,8 @@ private MethodSpec buildPreBindMethod(GeneratedModelInfo modelInfo,
// recycling will not work correctly. It is done in the background since it is fairly slow
// and can noticeably add jank to scrolling in dev
preBindBuilder
.beginControlFlow("if ($L != $L.getTag($T.id.epoxy_saved_view_style))",
PARIS_STYLE_ATTR_NAME, boundObjectParam.name, ClassNames.EPOXY_R)
.beginControlFlow("$T.THREAD_POOL_EXECUTOR.execute(new $T()", ANDROID_ASYNC_TASK,
Runnable.class)
.beginControlFlow("public void run()")
Expand All @@ -645,7 +641,8 @@ private MethodSpec buildPreBindMethod(GeneratedModelInfo modelInfo,
positionParamName)
.endControlFlow()
.endControlFlow()
.endControlFlow(")");
.endControlFlow(")")
.endControlFlow();
}

ClassName clickWrapperType = getClassName(WRAPPED_LISTENER_TYPE);
Expand Down Expand Up @@ -703,14 +700,20 @@ private Iterable<MethodSpec> generateStyleableViewMethods(GeneratedModelInfo mod
.build());

// Methods for setting each defined style directly
for (String styleName : styleBuilderInfo.getStyleNames()) {
String capitalizedStyle = Utils.capitalizeFirstLetter(styleName);
for (ParisStyle style : styleBuilderInfo.getStyles()) {
String capitalizedStyle = Utils.capitalizeFirstLetter(style.getName());
String methodName = "with" + capitalizedStyle + "Style";
String fieldName = weakReferenceFieldForStyle(styleName);
String fieldName = weakReferenceFieldForStyle(style.getName());

// The style is stored in a static weak reference since it is likely to be reused in other
// models are when models are rebuilt.
methods.add(MethodSpec.methodBuilder(methodName)
Builder styleMethodBuilder = MethodSpec.methodBuilder(methodName);

if (style.getJavadoc() != null) {
styleMethodBuilder.addJavadoc(style.getJavadoc());
}

methods.add(styleMethodBuilder
.addModifiers(PUBLIC)
.returns(modelInfo.getParameterizedGeneratedName())
.addStatement("$T style = $L != null ? $L.get() : null", ClassNames.PARIS_STYLE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,10 @@ internal class ModelViewWriter(
// Compare against the style on the previous model if it exists,
// otherwise we look up the saved style from the view tag
if (hasPreviousModel) {
beginControlFlow("\nif (!\$L.equals(that.\$L))",
beginControlFlow("\nif (\$L != that.\$L)",
PARIS_STYLE_ATTR_NAME, PARIS_STYLE_ATTR_NAME)
} else {
beginControlFlow("\nif (!\$L.equals(\$L.getTag(\$T.id.epoxy_saved_view_style)))",
beginControlFlow("\nif (\$L != \$L.getTag(\$T.id.epoxy_saved_view_style))",
PARIS_STYLE_ATTR_NAME, boundObjectParam.name, ClassNames.EPOXY_R)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ fun weakReferenceFieldForStyle(styleName: String) = "parisStyleReference_$styleN
*/
internal class ParisStyleAttributeInfo(
modelInfo: ModelViewInfo,
elements: Elements,
val elements: Elements,
val types: Types,
packageName: String,
styleBuilderClassName: ClassName,
styleBuilderElement: TypeMirror
) : AttributeInfo() {

val styleNames: List<String>
val styles: List<ParisStyle>
val styleApplierClass: ClassName
val styleBuilderClass: ClassName

Expand All @@ -38,27 +38,34 @@ internal class ParisStyleAttributeInfo(
isGenerated = true
useInHash = true
isNullable = false
styleNames = findStyleNames(styleBuilderElement)
styles = findStyleNames(styleBuilderElement)

// the builder is nested in the style applier class
styleApplierClass = styleBuilderClassName.topLevelClassName()

codeToSetDefault.explicit = CodeBlock.of(PARIS_DEFAULT_STYLE_CONSTANT_NAME)
}

private fun findStyleNames(typeMirror: TypeMirror): List<String> {
private fun findStyleNames(typeMirror: TypeMirror): List<ParisStyle> {
return types.asElement(typeMirror)
.enclosedElements
.filter {
it.kind == ElementKind.METHOD
&& it.simpleName.startsWith(BUILDER_STYLE_METHOD_PREFIX)
}
.map {
it.simpleName
val name = it.simpleName
.toString()
.removePrefix(BUILDER_STYLE_METHOD_PREFIX)
.lowerCaseFirstLetter()

ParisStyle(name, elements.getDocComment(it))
}
}

}
}

data class ParisStyle(
val name: String,
val javadoc: String?
)
5 changes: 4 additions & 1 deletion epoxy-sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ android {

defaultConfig {
applicationId "com.airbnb.android.epoxysample"
minSdkVersion rootProject.MIN_SDK_VERSION_LITHO
minSdkVersion 16
targetSdkVersion rootProject.TARGET_SDK_VERSION
vectorDrawables.useSupportLibrary = true
versionCode 1
Expand Down Expand Up @@ -44,6 +44,9 @@ dependencies {
compile project(':epoxy-adapter')
annotationProcessor project(':epoxy-processor')

// compile "com.airbnb.android:paris:0.2.0"
// annotationProcessor "com.airbnb.android:paris-processor:0.2.0"

// Dependencies for the optional litho integration
compile project(':epoxy-litho')
annotationProcessor rootProject.deps.lithoProcessor
Expand Down

0 comments on commit b69fdff

Please sign in to comment.