Skip to content

Commit

Permalink
binding initialization fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-dev committed Oct 24, 2017
1 parent f98f472 commit f16d637
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
3 changes: 3 additions & 0 deletions app/src/main/java/ds/bindingtools/demo/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ds.bindingtools.demo

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import ds.bindingtools.bind
import ds.bindingtools.bundle
import ds.bindingtools.startActivity
Expand All @@ -12,6 +13,7 @@ import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {

private val leaksDetector = ByteArray(50_000_000) { 127 }
private val TAG = javaClass.simpleName

private lateinit var prefs: Prefs

Expand All @@ -21,6 +23,7 @@ class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Log.v(TAG, "onCreate")
prefs = Prefs(this)

bindViews()
Expand Down
9 changes: 5 additions & 4 deletions app/src/main/java/ds/bindingtools/demo/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@ import android.os.Handler
import ds.bindingtools.Bindable
import ds.bindingtools.binding


object MainViewModel : Bindable {
var text: String by binding("")
var buttonText: String by binding()
var text: String by binding()
var buttonText: String by binding("...")

fun sayHello() {
if (text.isEmpty())
text = "Hello, World!"
buttonText = "navigate"
}

fun onBindClick() {
Handler().postDelayed({
buttonText = "ooops"
buttonText = "navigate"
}, 2000)
}

}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
android:id="@+id/navigateButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="\???"/>
/>

<Button
android:id="@+id/bindButton"
Expand Down
13 changes: 8 additions & 5 deletions lib/src/main/java/ds/bindingtools/ViewBindings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,22 @@ class BindingProperty<T : Any?>(private var value: T?, private val type: Class<T
ensureUiThread()
val getter = Binder.getAccessors<T>(thisRef, property)?.getter
if (getter != null) {
log("${property.name}.get: filling from getter")
value = getter.invoke()
}
val value = value ?: default(type)
log("get value [$value]")
log("get: value [$value]")
return value
}

override fun setValue(thisRef: Bindable, property: KProperty<*>, value: T) {
ensureUiThread()
val oldValue = this.value
if (oldValue != value) {
log("internal value has been set")
log("${property.name}.set: internal value [$value] has been set")
this.value = value
Binder.getAccessors<T>(thisRef, property)?.setters?.forEach {
log("set value [$value]")
log("set: value [$value]")
it(value)
}
}
Expand Down Expand Up @@ -85,14 +86,16 @@ inline fun <reified T : Boolean> Bindable.bind(prop: KProperty0<T>, view: Compou
fun <T : Any?> Bindable.bind(prop: KProperty0<T>, setter: (T) -> Unit, getter: (() -> T)? = null) {
Binder.getOrPutAccessors<T>(this, prop).let {
log("bind ${prop.name}")
log("set default for ${prop.name}: ${prop.get()}")
setter(prop.get()) // initialize view

it.setters += setter
if (getter != null)
if (it.getter == null)
it.getter = getter
else
error("Only one getter per property allowed")

setter(prop.get()) // initialize view
}
}

Expand Down Expand Up @@ -173,7 +176,7 @@ private object Binder {
}

private fun log(message: String) {
if (BuildConfig.DEBUG) Log.v("DATABINDING", message)
/*if (BuildConfig.DEBUG) */Log.v("DATABINDING", message)
}

interface Bindable

0 comments on commit f16d637

Please sign in to comment.