Skip to content

Instantly share code, notes, and snippets.

@jorgecasariego
Created March 21, 2019 13:37
Show Gist options
  • Save jorgecasariego/f86d0ca18c66c0b165c53447d8e21632 to your computer and use it in GitHub Desktop.
Save jorgecasariego/f86d0ca18c66c0b165c53447d8e21632 to your computer and use it in GitHub Desktop.
import java.util.*
import kotlin.properties.Delegates
interface ValueChangedListener {
fun onValueChanged(newValue: String)
}
class PrintingTextChangedListener: ValueChangedListener {
override fun onValueChanged(newValue: String) {
println("Value of the text is: $newValue")
}
}
class ObservableText(listener: ValueChangedListener) {
var text: String by Delegates.observable(
initialValue = "",
onChange = {
property, oldValue, newValue ->
listener.onValueChanged(newValue)
}
)
}
fun main(args: Array<String>) {
val initialObservableObject = ObservableText(PrintingTextChangedListener())
initialObservableObject.text = "Hello"
initialObservableObject.text = "World"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment