Skip to content

Commit

Permalink
added example with nested viewmodels
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-dev committed Dec 22, 2017
1 parent a2f75c6 commit 7b6e772
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 8 deletions.
11 changes: 10 additions & 1 deletion app/src/main/java/ds/bindingtools/demo/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,16 @@ class MainActivity : AppCompatActivity() {
fillPrefs()

navigateButton.setOnClickListener { navigateNext() }

bindButton.setOnClickListener {
viewModel.onBindClick()
extraViewModel.message = "toast!"
}

nestedButton.setOnClickListener {
viewModel.assignNested()
}

}

override fun onResume() {
Expand All @@ -49,8 +54,12 @@ class MainActivity : AppCompatActivity() {
}

private fun bindViews() = withBindable(viewModel) {
bind(this::text, helloLabel::setText, helloLabel::getText)
bind(::text, helloLabel::setText, helloLabel::getText)
bind(::buttonText, { it: String -> navigateButton.text = it }, { navigateButton.text.toString() })

withBindable(nestedViewModel) {
bind(::secondaryText, { Toast.makeText(this@MainActivity, it, Toast.LENGTH_SHORT).show() })
}
}

private fun fillPrefs() {
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/ds/bindingtools/demo/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,25 @@ object MainViewModel : Bindable {
var text: String by binding()
var buttonText: String by binding("...")

val nestedViewModel = NestedViewModel

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

fun assignNested() {
nestedViewModel.secondaryText = "this is nested"
}

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

}

object NestedViewModel : Bindable {
var secondaryText: String by binding("")
}
7 changes: 7 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,11 @@
android:text="delayed binding"
/>

<Button
android:id="@+id/nestedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="nested binding"
/>

</LinearLayout>
6 changes: 2 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
buildscript {
ext.kotlin_version = '1.2.0-rc-39'
ext.kotlin_version = '1.2.10'
repositories {
google()
jcenter()
maven { url 'https://dl.bintray.com/kotlin/kotlin-eap' }
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
classpath 'com.android.tools.build:gradle:3.0.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0' // for jitpack
}
Expand All @@ -16,7 +15,6 @@ allprojects {
repositories {
google()
jcenter()
maven { url 'https://dl.bintray.com/kotlin/kotlin-eap' }
}
}

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.2.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-bin.zip
4 changes: 2 additions & 2 deletions lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ android {
defaultConfig {
minSdkVersion 19
targetSdkVersion 26
versionCode 1
versionName "1.0"
versionCode 15
versionName "0.15"

}

Expand Down

0 comments on commit 7b6e772

Please sign in to comment.