Skip to content

Commit

Permalink
Use equals to compare styles (#403)
Browse files Browse the repository at this point in the history
  • Loading branch information
elihart authored Feb 25, 2018
1 parent 5dcdb93 commit 0a886c3
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,55 @@
package com.airbnb.epoxy

import android.support.annotation.*
import com.airbnb.epoxy.ClassNames.*
import com.airbnb.epoxy.Utils.*
import com.squareup.javapoet.*
import com.squareup.javapoet.MethodSpec.*
import com.squareup.javapoet.TypeName.*
import java.io.*
import java.lang.annotation.*
import java.lang.ref.*
import android.support.annotation.LayoutRes
import com.airbnb.epoxy.ClassNames.ANDROID_ASYNC_TASK
import com.airbnb.epoxy.Utils.EPOXY_CONTROLLER_TYPE
import com.airbnb.epoxy.Utils.EPOXY_VIEW_HOLDER_TYPE
import com.airbnb.epoxy.Utils.GENERATED_MODEL_INTERFACE
import com.airbnb.epoxy.Utils.MODEL_CLICK_LISTENER_TYPE
import com.airbnb.epoxy.Utils.MODEL_LONG_CLICK_LISTENER_TYPE
import com.airbnb.epoxy.Utils.ON_BIND_MODEL_LISTENER_TYPE
import com.airbnb.epoxy.Utils.ON_UNBIND_MODEL_LISTENER_TYPE
import com.airbnb.epoxy.Utils.UNTYPED_EPOXY_MODEL_TYPE
import com.airbnb.epoxy.Utils.WRAPPED_LISTENER_TYPE
import com.airbnb.epoxy.Utils.getClassName
import com.airbnb.epoxy.Utils.implementsMethod
import com.airbnb.epoxy.Utils.isDataBindingModel
import com.airbnb.epoxy.Utils.isEpoxyModel
import com.airbnb.epoxy.Utils.isEpoxyModelWithHolder
import com.airbnb.epoxy.Utils.isViewLongClickListenerType
import com.squareup.javapoet.ArrayTypeName
import com.squareup.javapoet.ClassName
import com.squareup.javapoet.CodeBlock
import com.squareup.javapoet.FieldSpec
import com.squareup.javapoet.JavaFile
import com.squareup.javapoet.MethodSpec
import com.squareup.javapoet.MethodSpec.Builder
import com.squareup.javapoet.ParameterSpec
import com.squareup.javapoet.ParameterizedTypeName
import com.squareup.javapoet.TypeName
import com.squareup.javapoet.TypeName.BOOLEAN
import com.squareup.javapoet.TypeName.BYTE
import com.squareup.javapoet.TypeName.CHAR
import com.squareup.javapoet.TypeName.DOUBLE
import com.squareup.javapoet.TypeName.FLOAT
import com.squareup.javapoet.TypeName.INT
import com.squareup.javapoet.TypeName.LONG
import com.squareup.javapoet.TypeName.SHORT
import com.squareup.javapoet.TypeSpec
import java.io.IOException
import java.lang.annotation.AnnotationTypeMismatchException
import java.lang.ref.WeakReference
import java.util.*
import javax.annotation.processing.*
import javax.lang.model.element.*
import javax.lang.model.element.Modifier.*
import javax.lang.model.util.*
import javax.annotation.processing.Filer
import javax.lang.model.element.Modifier
import javax.lang.model.element.Modifier.FINAL
import javax.lang.model.element.Modifier.PRIVATE
import javax.lang.model.element.Modifier.PROTECTED
import javax.lang.model.element.Modifier.PUBLIC
import javax.lang.model.element.Modifier.STATIC
import javax.lang.model.element.TypeElement
import javax.lang.model.util.Elements
import javax.lang.model.util.Types

internal class GeneratedModelWriter(
private val filer: Filer,
Expand Down Expand Up @@ -571,9 +607,10 @@ internal class GeneratedModelWriter(
// 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(
"if (!\$T.equals(\$L, \$L.getTag(\$T.id.epoxy_saved_view_style)))",
Objects::class.java, PARIS_STYLE_ATTR_NAME, boundObjectParam.name,
ClassNames.EPOXY_R)
.beginControlFlow("\$T.THREAD_POOL_EXECUTOR.execute(new \$T()",
ANDROID_ASYNC_TASK,
Runnable::class.java)
Expand Down Expand Up @@ -900,7 +937,8 @@ internal class GeneratedModelWriter(
}

val annotation = classElement.getAnnotation(
EpoxyModelClass::class.java) ?: // This is an error. The model must have an EpoxyModelClass annotation
EpoxyModelClass::class.java)
?: // This is an error. The model must have an EpoxyModelClass annotation
// since getDefaultLayout is not implemented
return null

Expand Down
22 changes: 13 additions & 9 deletions epoxy-processor/src/main/java/com/airbnb/epoxy/StyleWriter.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.airbnb.epoxy

import com.squareup.javapoet.*
import javax.lang.model.*
import javax.lang.model.util.*
import com.squareup.javapoet.ClassName
import com.squareup.javapoet.MethodSpec
import com.squareup.javapoet.ParameterSpec
import java.util.*
import javax.lang.model.AnnotatedConstruct
import javax.lang.model.util.Elements
import javax.lang.model.util.Types

internal fun addStyleApplierCode(
methodBuilder: MethodSpec.Builder,
Expand Down Expand Up @@ -36,11 +40,12 @@ internal fun addBindStyleCodeIfNeeded(
// 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 != that.\$L)",
PARIS_STYLE_ATTR_NAME, PARIS_STYLE_ATTR_NAME)
beginControlFlow("\nif (!\$T.equals(\$L, that.\$L))",
Objects::class.java, PARIS_STYLE_ATTR_NAME, PARIS_STYLE_ATTR_NAME)
} else {
beginControlFlow("\nif (\$L != \$L.getTag(\$T.id.epoxy_saved_view_style))",
PARIS_STYLE_ATTR_NAME, boundObjectParam.name, ClassNames.EPOXY_R)
beginControlFlow("\nif (!\$T.equals(\$L, \$L.getTag(\$T.id.epoxy_saved_view_style)))",
Objects::class.java, PARIS_STYLE_ATTR_NAME, boundObjectParam.name,
ClassNames.EPOXY_R)
}

addStyleApplierCode(this, styleInfo, boundObjectParam.name)
Expand All @@ -49,8 +54,7 @@ internal fun addBindStyleCodeIfNeeded(
}
}

internal fun AnnotatedConstruct.hasStyleableAnnotation(elements: Elements)
= annotationMirrors
internal fun AnnotatedConstruct.hasStyleableAnnotation(elements: Elements) = annotationMirrors
.map { it.annotationType.asElement() }
.any {
it.simpleName.toString() == "Styleable"
Expand Down

0 comments on commit 0a886c3

Please sign in to comment.