Last active
September 8, 2016 02:52
-
-
Save thomasnield/15e53f4de801964803722d7d6e5dc029 to your computer and use it in GitHub Desktop.
RxJava and JavaFX Autocomplete test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* Include these Gradle dependencies | |
| compile 'no.tornado:tornadofx:1.5.4' | |
| compile 'com.github.thomasnield:rxkotlinfx:0.1.4' | |
| compile 'io.reactivex:rxkotlin:0.60.0' | |
| */ | |
| import javafx.scene.input.KeyEvent | |
| import rx.javafx.kt.events | |
| import rx.javafx.kt.observeOnFx | |
| import rx.lang.kotlin.toObservable | |
| import tornadofx.App | |
| import tornadofx.View | |
| import tornadofx.textfield | |
| import tornadofx.vbox | |
| import java.util.concurrent.TimeUnit | |
| class MyApp: App(MyView::class) | |
| class MyView : View("My View") { | |
| override val root = vbox { | |
| textfield { | |
| val burstyMulticast = events(KeyEvent.KEY_TYPED).publish().refCount() | |
| .doOnNext { it.consume() } | |
| .map { it.character } | |
| burstyMulticast.throttleWithTimeout(200, TimeUnit.MILLISECONDS).startWith("") | |
| .switchMap { | |
| burstyMulticast.scan { x,y -> x + y } | |
| .switchMap { input -> | |
| states.filter { it.toUpperCase().startsWith(input.toUpperCase()) }.take(1) | |
| .map { it.split(Regex("(?<=$input)")) } | |
| .filter { it.size == 2 } | |
| } | |
| }.observeOnFx().subscribe { | |
| text = it[0] + it[1] | |
| positionCaret(it[0].length) | |
| selectEnd() | |
| } | |
| } | |
| } | |
| val states = listOf("Alabama", | |
| "Alaska", | |
| "Arizona", | |
| "Arkansas", | |
| "California", | |
| "Colorado", | |
| "Connecticut", | |
| "Delaware", | |
| "Florida", | |
| "Georgia", | |
| "Hawaii", | |
| "Idaho", | |
| "Illinois Indiana", | |
| "Iowa", | |
| "Kansas", | |
| "Kentucky", | |
| "Louisiana", | |
| "Maine", | |
| "Maryland", | |
| "Massachusetts", | |
| "Michigan", | |
| "Minnesota", | |
| "Mississippi", | |
| "Missouri", | |
| "Montana Nebraska", | |
| "Nevada", | |
| "New Hampshire", | |
| "New Jersey", | |
| "New Mexico", | |
| "New York", | |
| "North Carolina", | |
| "North Dakota", | |
| "Ohio", | |
| "Oklahoma", | |
| "Oregon", | |
| "Pennsylvania Rhode Island", | |
| "South Carolina", | |
| "South Dakota", | |
| "Tennessee", | |
| "Texas", | |
| "Utah", | |
| "Vermont", | |
| "Virginia", | |
| "Washington", | |
| "West Virginia", | |
| "Wisconsin", | |
| "Wyoming").toObservable() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment