/* * * * Copyright 2019 New Vector Ltd * * * * Licensed under the Apache License, Version 2.0 (the "License"); * * you may not use this file except in compliance with the License. * * You may obtain a copy of the License at * * * * http://www.apache.org/licenses/LICENSE-2.0 * * * * Unless required by applicable law or agreed to in writing, software * * distributed under the License is distributed on an "AS IS" BASIS, * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * * See the License for the specific language governing permissions and * * limitations under the License. * */ package im.vector.riotredesign.core.epoxy import android.view.View import com.airbnb.epoxy.EpoxyHolder import kotlin.properties.ReadOnlyProperty import kotlin.reflect.KProperty /** * A pattern for easier view binding with an [EpoxyHolder] * * See [SampleKotlinModelWithHolder] for a usage example. */ abstract class KotlinEpoxyHolder : EpoxyHolder() { private lateinit var view: View override fun bindView(itemView: View) { view = itemView } protected fun bind(id: Int): ReadOnlyProperty = Lazy { holder: KotlinEpoxyHolder, prop -> holder.view.findViewById(id) as V? ?: throw IllegalStateException("View ID $id for '${prop.name}' not found.") } /** * Taken from Kotterknife. * https://github.com/JakeWharton/kotterknife */ private class Lazy( private val initializer: (KotlinEpoxyHolder, KProperty<*>) -> V ) : ReadOnlyProperty { private object EMPTY private var value: Any? = EMPTY override fun getValue(thisRef: KotlinEpoxyHolder, property: KProperty<*>): V { if (value == EMPTY) { value = initializer(thisRef, property) } @Suppress("UNCHECKED_CAST") return value as V } } }