Skip to content

Commit

Permalink
Add support for Kotlin delegation via annotated interface properties
Browse files Browse the repository at this point in the history
  • Loading branch information
ngsilverman committed Aug 20, 2019
1 parent c631edb commit b26b70f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.airbnb.epoxy.integrationtest

import android.content.Context
import android.view.View
import com.airbnb.epoxy.ModelProp
import com.airbnb.epoxy.ModelView

@ModelView(autoLayout = ModelView.Size.WRAP_WIDTH_MATCH_HEIGHT)
class ViewWithDelegate @JvmOverloads constructor(
context: Context,
implementation: InterfaceImplementation = InterfaceImplementation()
) : View(context), InterfaceWithModelProp by implementation

interface InterfaceWithModelProp {

@set:ModelProp
var flag: Boolean
}

class InterfaceImplementation : InterfaceWithModelProp {

override var flag: Boolean = false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.airbnb.epoxy

import com.airbnb.epoxy.integrationtest.BuildConfig
import com.airbnb.epoxy.integrationtest.ViewWithDelegateModel_
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config

@RunWith(RobolectricTestRunner::class)
@Config(constants = BuildConfig::class, sdk = intArrayOf(21))
class ModelViewDelegateTest {

@Test
fun propMethodIsOnModel() {
val model = ViewWithDelegateModel_()
model.flag(true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ internal class ModelViewProcessor(

for (propAnnotation in modelPropAnnotations) {
for (prop in roundEnv.getElementsAnnotatedWith(propAnnotation)) {

// Interfaces can use model property annotations freely, they will be processed if
// and when implementors of that interface are processed. This is particularly
// useful for Kotlin delegation where the model view class may not be overriding
// the interface properties directly, and so doesn't have an opportunity to annotate
// them with Epoxy model property annotations.
if (prop.enclosingElement.kind == ElementKind.INTERFACE) {
continue
}

val info = getModelInfoForPropElement(prop)
if (info == null) {
errorLogger.logError(
Expand Down

0 comments on commit b26b70f

Please sign in to comment.