Hi, do you know if I can use delegated properties ...
# android
d
Hi, do you know if I can use delegated properties for things like properties in adapter? I am always writing code like this
Copy code
var products: List<Product> = listOf()
        set(value) {
            field = value
            notifyDatasetChanged()
        }
and I think that I can use delegated properties with some syntax like
var products: List<ApiProduct> by adapterProperty(initialValue = listOf())
but I dont have such experience with them
u
david.bilik:
Copy code
import kotlin.reflect.KProperty
class AdapterProperty<T>(private var value: T) {
    operator fun getValue(thisRef: Any?, property: KProperty<*>) = value
    operator fun setValue(thisRef: Any?, property: KProperty<*>, newValue: T) {
        value = newValue
        notifyDatasetChanged()
}
Do you understand my example?
d
Hi, thanks 🙂 yes I understand, i’ve looked ad Delegates.observable delegate and done it the same way