pablisco
02/12/2020, 11:10 AM@get:RequiresApi(23)
however, I’m still getting a lint about the use of a new API level.
I’ll add more details as a thread…pablisco
02/12/2020, 11:11 AMfun <V: View, T> shared(
block: V.() -> T
): ReadOnlyProperty<V, T> =
object : ReadOnlyProperty<V, T> {
override fun getValue(thisRef: V, property: KProperty<*>): T =
restoreOrCreate(thisRef, block)
.also { thisRef.setTag(R.id.view_cache, it) }
@Suppress("UNCHECKED_CAST")
private fun restoreOrCreate(thisRef: V, block: V.() -> T) =
thisRef.getTag(R.id.view_cache) as? T ?: thisRef.block()
}
It’s a bit like by lazy
but relies on the view’s tagpablisco
02/12/2020, 11:12 AM@get:RequiresApi(VERSION_CODES.M)
val <V : View> V.plusDrawable: Flow<Pair<ScrollBy, ScrollBy>> by shared {
callbackFlow {
setOnScrollChangeListener { _, x, y, oX, oY ->
offer((x to y) to (oX to oY))
}
}
}
pablisco
02/12/2020, 11:12 AMpablisco
02/12/2020, 11:12 AMpablisco
02/12/2020, 11:13 AMGiorgos Neokleous
02/12/2020, 11:18 AMcallbackFlow
unwrapping. Does the lint persist if you move the setOnScrollChangeListener
outside of the callback flow?pablisco
02/12/2020, 11:20 AMshared
is confusing Lint 🤔pablisco
02/12/2020, 11:27 AM@get:SuppressLint(“NewApi”)
has zero effect 😭pablisco
02/12/2020, 11:32 AM@SuppressLint("NewApi")
private fun View.setOnScrollChangeListenerUnsafe(listener: OnScrollChangeListener?) =
setOnScrollChangeListener(listener)
Not sure if it’s a problem with Lint or Kotlin 🤔Ryan Mentley
02/12/2020, 8:24 PMRyan Mentley
02/12/2020, 8:26 PMRyan Mentley
02/12/2020, 8:26 PM