aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/ca/allanwang/kau/utils/Kotterknife.kt
blob: 8c7c03984510bb9d3c4389519c0b21ee6678fddb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
@file:Suppress("UNCHECKED_CAST")

package ca.allanwang.kau.utils

/**
 * Created by Allan Wang on 2017-05-29.
 *
 * Courtesy of Jake Wharton
 *
 * https://github.com/JakeWharton/kotterknife/blob/master/src/main/kotlin/kotterknife/ButterKnife.kt
 */
import android.app.Activity
import android.app.Dialog
import android.app.DialogFragment
import android.app.Fragment
import android.support.v7.widget.RecyclerView.ViewHolder
import android.view.View
import java.util.*
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty
import android.support.v4.app.DialogFragment as SupportDialogFragment
import android.support.v4.app.Fragment as SupportFragment

private const val DEPRECATION_MESSAGE = "Kotterknife will be removed in favour of the kotlin_android_extensions plugin"

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> View.bindView(id: Int)
        : ReadOnlyProperty<View, V> = required(id, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> Activity.bindView(id: Int)
        : ReadOnlyProperty<Activity, V> = required(id, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> Dialog.bindView(id: Int)
        : ReadOnlyProperty<Dialog, V> = required(id, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> DialogFragment.bindView(id: Int)
        : ReadOnlyProperty<DialogFragment, V> = required(id, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> SupportDialogFragment.bindView(id: Int)
        : ReadOnlyProperty<android.support.v4.app.DialogFragment, V> = required(id, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> Fragment.bindView(id: Int)
        : ReadOnlyProperty<Fragment, V> = required(id, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> SupportFragment.bindView(id: Int)
        : ReadOnlyProperty<android.support.v4.app.Fragment, V> = required(id, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> ViewHolder.bindView(id: Int)
        : ReadOnlyProperty<ViewHolder, V> = required(id, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> View.bindOptionalView(id: Int)
        : ReadOnlyProperty<View, V?> = optional(id, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> Activity.bindOptionalView(id: Int)
        : ReadOnlyProperty<Activity, V?> = optional(id, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> Dialog.bindOptionalView(id: Int)
        : ReadOnlyProperty<Dialog, V?> = optional(id, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> DialogFragment.bindOptionalView(id: Int)
        : ReadOnlyProperty<DialogFragment, V?> = optional(id, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> SupportDialogFragment.bindOptionalView(id: Int)
        : ReadOnlyProperty<android.support.v4.app.DialogFragment, V?> = optional(id, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> Fragment.bindOptionalView(id: Int)
        : ReadOnlyProperty<Fragment, V?> = optional(id, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> SupportFragment.bindOptionalView(id: Int)
        : ReadOnlyProperty<android.support.v4.app.Fragment, V?> = optional(id, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> ViewHolder.bindOptionalView(id: Int)
        : ReadOnlyProperty<ViewHolder, V?> = optional(id, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> View.bindViews(vararg ids: Int)
        : ReadOnlyProperty<View, List<V>> = required(ids, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> Activity.bindViews(vararg ids: Int)
        : ReadOnlyProperty<Activity, List<V>> = required(ids, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> Dialog.bindViews(vararg ids: Int)
        : ReadOnlyProperty<Dialog, List<V>> = required(ids, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> DialogFragment.bindViews(vararg ids: Int)
        : ReadOnlyProperty<DialogFragment, List<V>> = required(ids, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> SupportDialogFragment.bindViews(vararg ids: Int)
        : ReadOnlyProperty<android.support.v4.app.DialogFragment, List<V>> = required(ids, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> Fragment.bindViews(vararg ids: Int)
        : ReadOnlyProperty<Fragment, List<V>> = required(ids, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> SupportFragment.bindViews(vararg ids: Int)
        : ReadOnlyProperty<android.support.v4.app.Fragment, List<V>> = required(ids, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> ViewHolder.bindViews(vararg ids: Int)
        : ReadOnlyProperty<ViewHolder, List<V>> = required(ids, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> View.bindOptionalViews(vararg ids: Int)
        : ReadOnlyProperty<View, List<V>> = optional(ids, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> Activity.bindOptionalViews(vararg ids: Int)
        : ReadOnlyProperty<Activity, List<V>> = optional(ids, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> Dialog.bindOptionalViews(vararg ids: Int)
        : ReadOnlyProperty<Dialog, List<V>> = optional(ids, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> DialogFragment.bindOptionalViews(vararg ids: Int)
        : ReadOnlyProperty<DialogFragment, List<V>> = optional(ids, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> SupportDialogFragment.bindOptionalViews(vararg ids: Int)
        : ReadOnlyProperty<android.support.v4.app.DialogFragment, List<V>> = optional(ids, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> Fragment.bindOptionalViews(vararg ids: Int)
        : ReadOnlyProperty<Fragment, List<V>> = optional(ids, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> SupportFragment.bindOptionalViews(vararg ids: Int)
        : ReadOnlyProperty<android.support.v4.app.Fragment, List<V>> = optional(ids, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> ViewHolder.bindOptionalViews(vararg ids: Int)
        : ReadOnlyProperty<ViewHolder, List<V>> = optional(ids, viewFinder)

private inline val View.viewFinder: View.(Int) -> View?
    get() = { findViewById(it) }
private inline val Activity.viewFinder: Activity.(Int) -> View?
    get() = { findViewById(it) }
private inline val Dialog.viewFinder: Dialog.(Int) -> View?
    get() = { findViewById(it) }
private inline val DialogFragment.viewFinder: DialogFragment.(Int) -> View?
    get() = { dialog.findViewById(it) }
private inline val SupportDialogFragment.viewFinder: SupportDialogFragment.(Int) -> View?
    get() = { dialog.findViewById(it) }
private inline val Fragment.viewFinder: Fragment.(Int) -> View?
    get() = { view.findViewById(it) }
private inline val SupportFragment.viewFinder: SupportFragment.(Int) -> View?
    get() = { view!!.findViewById(it) }
private inline val ViewHolder.viewFinder: ViewHolder.(Int) -> View?
    get() = { itemView.findViewById(it) }

private fun viewNotFound(id: Int, desc: KProperty<*>): Nothing =
        throw IllegalStateException("View ID $id for '${desc.name}' not found.")

private fun <T, V : View> required(id: Int, finder: T.(Int) -> View?) = Lazy { t: T, desc ->
    (t.finder(id) as V?)?.apply { } ?: viewNotFound(id, desc)
}

private fun <T, V : View> optional(id: Int, finder: T.(Int) -> View?) = Lazy { t: T, _ -> t.finder(id) as V? }

private fun <T, V : View> required(ids: IntArray, finder: T.(Int) -> View?) = Lazy { t: T, desc ->
    ids.map {
        t.finder(it) as V? ?: viewNotFound(it, desc)
    }
}

private fun <T, V : View> optional(ids: IntArray, finder: T.(Int) -> View?) = Lazy { t: T, _ -> ids.map { t.finder(it) as V? }.filterNotNull() }

// Like Kotlin's lazy delegate but the initializer gets the target and metadata passed to it
private open class Lazy<in T, out V>(private val initializer: (T, KProperty<*>) -> V) : ReadOnlyProperty<T, V> {
    protected object EMPTY

    protected var value: Any? = EMPTY

    override fun getValue(thisRef: T, property: KProperty<*>): V {
        if (value == EMPTY)
            value = initializer(thisRef, property)

        return value as V
    }
}

/*
 * The components below are a variant of the view bindings with lazy resettables
 * All bindings are weakly held so that they may be reset through KotterknifeRegistry.reset
 *
 * This is typically only needed in cases such as Fragments,
 * where their lifecycle doesn't match that of an Activity or View
 *
 * Credits to <a href="https://github.com/MichaelRocks">MichaelRocks</a>
 */

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> View.bindViewResettable(id: Int)
        : ReadOnlyProperty<View, V> = requiredResettable(id, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> Activity.bindViewResettable(id: Int)
        : ReadOnlyProperty<Activity, V> = requiredResettable(id, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> Dialog.bindViewResettable(id: Int)
        : ReadOnlyProperty<Dialog, V> = requiredResettable(id, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> DialogFragment.bindViewResettable(id: Int)
        : ReadOnlyProperty<DialogFragment, V> = requiredResettable(id, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> SupportDialogFragment.bindViewResettable(id: Int)
        : ReadOnlyProperty<android.support.v4.app.DialogFragment, V> = requiredResettable(id, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> Fragment.bindViewResettable(id: Int)
        : ReadOnlyProperty<Fragment, V> = requiredResettable(id, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> SupportFragment.bindViewResettable(id: Int)
        : ReadOnlyProperty<android.support.v4.app.Fragment, V> = requiredResettable(id, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> ViewHolder.bindViewResettable(id: Int)
        : ReadOnlyProperty<ViewHolder, V> = requiredResettable(id, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> View.bindOptionalViewResettable(id: Int)
        : ReadOnlyProperty<View, V?> = optionalResettable(id, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> Activity.bindOptionalViewResettable(id: Int)
        : ReadOnlyProperty<Activity, V?> = optionalResettable(id, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> Dialog.bindOptionalViewResettable(id: Int)
        : ReadOnlyProperty<Dialog, V?> = optionalResettable(id, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> DialogFragment.bindOptionalViewResettable(id: Int)
        : ReadOnlyProperty<DialogFragment, V?> = optionalResettable(id, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> SupportDialogFragment.bindOptionalViewResettable(id: Int)
        : ReadOnlyProperty<android.support.v4.app.DialogFragment, V?> = optionalResettable(id, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> Fragment.bindOptionalViewResettable(id: Int)
        : ReadOnlyProperty<Fragment, V?> = optionalResettable(id, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> SupportFragment.bindOptionalViewResettable(id: Int)
        : ReadOnlyProperty<android.support.v4.app.Fragment, V?> = optionalResettable(id, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> ViewHolder.bindOptionalViewResettable(id: Int)
        : ReadOnlyProperty<ViewHolder, V?> = optionalResettable(id, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> View.bindViewsResettable(vararg ids: Int)
        : ReadOnlyProperty<View, List<V>> = requiredResettable(ids, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> Activity.bindViewsResettable(vararg ids: Int)
        : ReadOnlyProperty<Activity, List<V>> = requiredResettable(ids, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> Dialog.bindViewsResettable(vararg ids: Int)
        : ReadOnlyProperty<Dialog, List<V>> = requiredResettable(ids, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> DialogFragment.bindViewsResettable(vararg ids: Int)
        : ReadOnlyProperty<DialogFragment, List<V>> = requiredResettable(ids, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> SupportDialogFragment.bindViewsResettable(vararg ids: Int)
        : ReadOnlyProperty<android.support.v4.app.DialogFragment, List<V>> = requiredResettable(ids, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> Fragment.bindViewsResettable(vararg ids: Int)
        : ReadOnlyProperty<Fragment, List<V>> = requiredResettable(ids, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> SupportFragment.bindViewsResettable(vararg ids: Int)
        : ReadOnlyProperty<android.support.v4.app.Fragment, List<V>> = requiredResettable(ids, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> ViewHolder.bindViewsResettable(vararg ids: Int)
        : ReadOnlyProperty<ViewHolder, List<V>> = requiredResettable(ids, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> View.bindOptionalViewsResettable(vararg ids: Int)
        : ReadOnlyProperty<View, List<V>> = optionalResettable(ids, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> Activity.bindOptionalViewsResettable(vararg ids: Int)
        : ReadOnlyProperty<Activity, List<V>> = optionalResettable(ids, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> Dialog.bindOptionalViewsResettable(vararg ids: Int)
        : ReadOnlyProperty<Dialog, List<V>> = optionalResettable(ids, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> DialogFragment.bindOptionalViewsResettable(vararg ids: Int)
        : ReadOnlyProperty<DialogFragment, List<V>> = optionalResettable(ids, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> SupportDialogFragment.bindOptionalViewsResettable(vararg ids: Int)
        : ReadOnlyProperty<android.support.v4.app.DialogFragment, List<V>> = optionalResettable(ids, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> Fragment.bindOptionalViewsResettable(vararg ids: Int)
        : ReadOnlyProperty<Fragment, List<V>> = optionalResettable(ids, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> SupportFragment.bindOptionalViewsResettable(vararg ids: Int)
        : ReadOnlyProperty<android.support.v4.app.Fragment, List<V>> = optionalResettable(ids, viewFinder)

@Deprecated(DEPRECATION_MESSAGE)
fun <V : View> ViewHolder.bindOptionalViewsResettable(vararg ids: Int)
        : ReadOnlyProperty<ViewHolder, List<V>> = optionalResettable(ids, viewFinder)

private fun <T, V : View> requiredResettable(id: Int, finder: T.(Int) -> View?) = LazyResettable { t: T, desc ->
    (t.finder(id) as V?)?.apply { } ?: viewNotFound(id, desc)
}

private fun <T, V : View> optionalResettable(id: Int, finder: T.(Int) -> View?) = LazyResettable { t: T, _ -> t.finder(id) as V? }

private fun <T, V : View> requiredResettable(ids: IntArray, finder: T.(Int) -> View?) = LazyResettable { t: T, desc ->
    ids.map {
        t.finder(it) as V? ?: viewNotFound(it, desc)
    }
}

private fun <T, V : View> optionalResettable(ids: IntArray, finder: T.(Int) -> View?) = LazyResettable { t: T, _ -> ids.map { t.finder(it) as V? }.filterNotNull() }

//Like Kotterknife's lazy delegate but is resettable
private class LazyResettable<in T, out V>(initializer: (T, KProperty<*>) -> V) : Lazy<T, V>(initializer) {
    override fun getValue(thisRef: T, property: KProperty<*>): V {
        KotterknifeRegistry.register(thisRef!!, this)
        return super.getValue(thisRef, property)
    }

    fun reset() {
        value = EMPTY
    }
}

@Deprecated(DEPRECATION_MESSAGE)
object Kotterknife {
    fun reset(target: Any) {
        KotterknifeRegistry.reset(target)
    }
}

private object KotterknifeRegistry {
    private val lazyMap = WeakHashMap<Any, MutableCollection<LazyResettable<*, *>>>()

    fun register(target: Any, lazy: LazyResettable<*, *>) = lazyMap.getOrPut(target, { Collections.newSetFromMap(WeakHashMap()) }).add(lazy)

    fun reset(target: Any) = lazyMap[target]?.forEach(LazyResettable<*, *>::reset)
}