@@ -8,11 +8,16 @@ import android.app.DialogFragment
88import android.app.Fragment
99import android.support.v7.widget.RecyclerView.ViewHolder
1010import android.view.View
11+ import java.util.*
1112import kotlin.properties.ReadOnlyProperty
1213import kotlin.reflect.KProperty
1314import android.support.v4.app.DialogFragment as SupportDialogFragment
1415import android.support.v4.app.Fragment as SupportFragment
1516
17+ object KotterKnife {
18+ fun reset (target : Any ) = LazyRegistry .reset(target)
19+ }
20+
1621fun <V : View > View.bindView (id : Int )
1722 : ReadOnlyProperty <View , V > = required(id, viewFinder)
1823
@@ -155,10 +160,29 @@ private class Lazy<in T, out V>(private val initializer: (T, KProperty<*>) -> V)
155160 private var value: Any? = EMPTY
156161
157162 override fun getValue (thisRef : T , property : KProperty <* >): V {
163+ LazyRegistry .register(thisRef!! , this )
164+
158165 if (value == EMPTY ) {
159166 value = initializer(thisRef, property)
160167 }
168+
161169 @Suppress(" UNCHECKED_CAST" )
162170 return value as V
163171 }
172+
173+ fun reset () {
174+ value = EMPTY
175+ }
176+ }
177+
178+ private object LazyRegistry {
179+ private val lazyMap = WeakHashMap <Any , MutableCollection <Lazy <* , * >>>()
180+
181+ fun register (target : Any , lazy : Lazy <* , * >) {
182+ lazyMap.getOrPut(target, { Collections .newSetFromMap(WeakHashMap ()) }).add(lazy)
183+ }
184+
185+ fun reset (target : Any ) {
186+ lazyMap[target]?.forEach { it.reset() }
187+ }
164188}
0 commit comments