Last active
July 23, 2019 11:18
-
-
Save eugene-drapoguz/d45add42a13668d6aa089ad638f7fe47 to your computer and use it in GitHub Desktop.
EpoxyKotlinHolder.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
abstract class EpoxyKotlinHolder : EpoxyHolder() { | |
private lateinit var _view: View | |
override fun bindView(itemView: View) { | |
_view = itemView | |
} | |
protected fun <V : View> bind(id: Int): ReadOnlyProperty<EpoxyKotlinHolder, V> = | |
Lazy { holder: EpoxyKotlinHolder, prop -> | |
holder._view.findViewById(id) as V? | |
?: throw IllegalStateException("View ID $id for '${prop.name}' not found.") | |
} | |
private class Lazy<V>( | |
private val initializer: (EpoxyKotlinHolder, KProperty<*>) -> V | |
) : ReadOnlyProperty<EpoxyKotlinHolder, V> { | |
private object EMPTY | |
private var value: Any? = EMPTY | |
override fun getValue(thisRef: EpoxyKotlinHolder, property: KProperty<*>): V { | |
if (value == EMPTY) { | |
value = initializer(thisRef, property) | |
} | |
@Suppress("UNCHECKED_CAST") | |
return value as V | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment