-
Notifications
You must be signed in to change notification settings - Fork 728
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Databinding naming pattern support (#319)
* Easier way to add databinding layouts * Layout pattern option
- Loading branch information
Showing
19 changed files
with
333 additions
and
256 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
epoxy-annotations/src/main/java/com/airbnb/epoxy/EpoxyDataBindingPattern.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.airbnb.epoxy; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Used to specify a naming pattern for the databinding layouts that you want models generated for. | ||
* Use this instead of {@link EpoxyDataBindingLayouts} to avoid having to explicitly list every | ||
* databinding layout. | ||
* <p> | ||
* The layouts must not specify a custom databinding class name or package via the | ||
* class="com.example.CustomClassName" override in the layout xml. | ||
*/ | ||
@Target(ElementType.PACKAGE) | ||
@Retention(RetentionPolicy.CLASS) | ||
public @interface EpoxyDataBindingPattern { | ||
/** | ||
* The R class used in this module (eg "com.example.app.R.class"). This is needed so Epoxy can | ||
* look up layout files. | ||
*/ | ||
Class<?> rClass(); | ||
/** | ||
* A string prefix that your databinding layouts start with. Epoxy will generate a model for each | ||
* databinding layout whose name starts with this. | ||
* <p> | ||
* For example, if you set this prefix to "view_holder" and you have a "view_holder_header.xml" | ||
* databinding layout, Epoxy will generate a HeaderBindingModel_ class for that layout. | ||
*/ | ||
String layoutPrefix(); | ||
} |
2 changes: 2 additions & 0 deletions
2
epoxy-integrationtest/src/main/java/com/airbnb/epoxy/integrationtest/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
@EpoxyDataBindingPattern(rClass = R.class, layoutPrefix = "view_holder") | ||
@EpoxyDataBindingLayouts({R.layout.model_with_data_binding}) | ||
package com.airbnb.epoxy.integrationtest; | ||
|
||
import com.airbnb.epoxy.EpoxyDataBindingLayouts; | ||
import com.airbnb.epoxy.EpoxyDataBindingPattern; |
21 changes: 21 additions & 0 deletions
21
epoxy-integrationtest/src/main/res/layout/view_holder_databinding_test.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<layout xmlns:android="http://schemas.android.com/apk/res/android"> | ||
|
||
<data> | ||
|
||
<variable | ||
name="stringValue" | ||
type="String" /> | ||
|
||
<variable | ||
name="clickListener" | ||
type="android.view.View.OnClickListener" /> | ||
</data> | ||
|
||
<Button | ||
android:id="@+id/button" | ||
android:layout_width="match_parent" | ||
android:layout_height="40dp" | ||
android:text="@{stringValue}" /> | ||
|
||
</layout> |
9 changes: 9 additions & 0 deletions
9
epoxy-integrationtest/src/main/res/layout/view_holder_no_databinding.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:orientation="vertical"> | ||
|
||
<!-- This layout does nothing, but it tests that the databinding processor skips none databinding layouts that match the naming pattern. --> | ||
|
||
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 0 additions & 93 deletions
93
epoxy-processor/src/main/java/com/airbnb/epoxy/DataBindingModelInfo.java
This file was deleted.
Oops, something went wrong.
77 changes: 77 additions & 0 deletions
77
epoxy-processor/src/main/java/com/airbnb/epoxy/DataBindingModelInfo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.airbnb.epoxy | ||
|
||
import com.airbnb.epoxy.ClassNames.* | ||
import com.airbnb.epoxy.Utils.* | ||
import com.squareup.javapoet.* | ||
import javax.lang.model.element.* | ||
import javax.lang.model.util.* | ||
|
||
internal class DataBindingModelInfo( | ||
private val typeUtils: Types, | ||
private val elementUtils: Elements, | ||
val layoutResource: ResourceValue, | ||
private val moduleName: String, | ||
private val layoutPrefix: String = "" | ||
) : GeneratedModelInfo() { | ||
private val dataBindingClassName: ClassName | ||
|
||
val dataBindingClassElement: Element? | ||
get() = getElementByName(dataBindingClassName, elementUtils, typeUtils) | ||
|
||
init { | ||
|
||
dataBindingClassName = getDataBindingClassNameForResource(layoutResource, moduleName) | ||
|
||
superClassElement = Utils.getElementByName(EPOXY_DATA_BINDING_MODEL, | ||
elementUtils, typeUtils) as TypeElement | ||
superClassName = EPOXY_DATA_BINDING_MODEL | ||
generatedClassName = buildGeneratedModelName() | ||
parametrizedClassName = generatedClassName | ||
boundObjectTypeName = EPOXY_DATA_BINDING_HOLDER | ||
shouldGenerateModel = true | ||
|
||
collectMethodsReturningClassType(superClassElement, typeUtils) | ||
} | ||
|
||
/** | ||
* Look up the DataBinding class generated for this model's layout file and parse the attributes | ||
* for it. | ||
*/ | ||
fun parseDataBindingClass() { | ||
// This databinding class won't exist until the second round of annotation processing since | ||
// it is generated in the first round. | ||
|
||
val hashCodeValidator = HashCodeValidator(typeUtils, elementUtils) | ||
dataBindingClassElement!!.enclosedElements | ||
.filter { Utils.isSetterMethod(it) } | ||
.forEach { | ||
addAttribute( | ||
DataBindingAttributeInfo(this, it as ExecutableElement, | ||
hashCodeValidator)) | ||
} | ||
} | ||
|
||
private fun getDataBindingClassNameForResource( | ||
layoutResource: ResourceValue, | ||
moduleName: String | ||
): ClassName { | ||
val modelName = layoutResource.resourceName!!.toUpperCamelCase().plus(BINDING_SUFFIX) | ||
|
||
return ClassName.get(moduleName + ".databinding", modelName) | ||
} | ||
|
||
private fun buildGeneratedModelName(): ClassName { | ||
val modelName = layoutResource.resourceName!! | ||
.removePrefix(layoutPrefix) | ||
.toUpperCamelCase() | ||
.plus(BINDING_SUFFIX) | ||
.plus(GeneratedModelInfo.GENERATED_MODEL_SUFFIX) | ||
|
||
return ClassName.get(moduleName, modelName) | ||
} | ||
|
||
companion object { | ||
|
||
val BINDING_SUFFIX = "Binding" | ||
} | ||
} |
Oops, something went wrong.