I came across an odd thing (and didn’t find anythi...
# android
p
I came across an odd thing (and didn’t find anything on google ) I have a delegate property. Inside this property I call a method that requires API 23. I’ve marked the property with:
@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…
This is the delegate:
Copy code
fun <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 tag
Then the code where I’m getting the error is written like this:
Copy code
@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))
        }
    }
}
image.png
I’m very puzzled since it has the annotation 🤔
If I remove the getter modifier I get this error, which is understandable tbh
g
🤔 interesting, I am going to wildly guess that it has to do with
callbackFlow
unwrapping. Does the lint persist if you move the
setOnScrollChangeListener
outside of the callback flow?
p
Yeah, same problem. I’m thinking that something in the delegate
shared
is confusing Lint 🤔
And even adding
@get:SuppressLint(“NewApi”)
has zero effect 😭
I ended adding this:
Copy code
@SuppressLint("NewApi")
private fun View.setOnScrollChangeListenerUnsafe(listener: OnScrollChangeListener?) =
    setOnScrollChangeListener(listener)
Not sure if it’s a problem with Lint or Kotlin 🤔
r
delegates and annotations get very weird sometimes
the short version is that they don't necessarily end up applied to the spot you'd expect
not quite the same thing, but in the vein of this: https://youtrack.jetbrains.com/issue/KT-32770
💯 1