Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlin + RxBinding = ❤️

Lovis
October 14, 2016

Kotlin + RxBinding = ❤️

Lightning talk from GDG Devfest Hamburg 2016

Lovis

October 14, 2016
Tweet

More Decks by Lovis

Other Decks in Programming

Transcript

  1. RxBinding • JVM language • developed by JetBrains • Null

    safety, immutability • Extension functions • It’s an Island
  2. RxBinding • JVM language • developed by JetBrains • Null

    safety, immutability • Extension functions • It’s an Island • Make your Android Views reactive
  3. RxBinding • JVM language • developed by JetBrains • Null

    safety, immutability • Extension functions • It’s an Island • Make your Android Views reactive RxView.clicks(myView) .filter(…) .flatMap(…) .subscribe(…)
  4. RxBinding • JVM language • developed by JetBrains • Null

    safety, immutability • Extension functions • It’s an Island • Make your Android Views reactive RxView.clicks(myView) .filter(…) .flatMap(…) .subscribe(…) • JakeWharton
  5. RxBinding • JVM language • developed by JetBrains • Null

    safety, immutability • Extension functions • It’s an Island • Make your Android Views reactive RxView.clicks(myView) .filter(…) .flatMap(…) .subscribe(…) • JakeWharton ✔
  6. Kotlin • JVM language • developed by JetBrains • Null

    safety, immutability • Extension functions • It’s an Island
  7. Kotlin • JVM language • developed by JetBrains • Null

    safety, immutability • Extension functions • It’s an Island
  8. Kotlin • JVM language • developed by JetBrains • Null

    safety, immutability • Extension functions • It’s an Island
  9. Kotlin • JVM language • developed by JetBrains • Null

    safety, immutability • Extension functions • It’s an Island
  10. Kotlin • JVM language • developed by JetBrains • Null

    safety, immutability • Extension functions • It’s an Island
  11. Kotlin • JVM language • developed by JetBrains • Null

    safety, immutability • Extension functions • It’s an Island
  12. Kotlin - Extension Functions fun String?.orEmpty(): String { if(this ==

    null) return "" return this } val name: String? = null val option = name.orEmpty()
  13. Kotlin - Extension Functions fun String?.orEmpty(): String { return this

    ?: "" } val name: String? = null val option = name.orEmpty()
  14. Kotlin - Extension Functions fun String?.orEmpty() = this ?: ""

    return this ?: "" } val name: String? = null val option = name.orEmpty()
  15. Java + RxBinding EditText name = ... RxTextView.textChanges(name) .debounce(400, TimeUnit.MILLISECONDS)

    .flatMap(text -> api.sendToServer(text)) .observeOn(AndroidSchedulers.mainThread()) .subscribe( this::handleResponse, this::handleError ); return this ?: "" }
  16. Java + RxBinding EditText name = ... RxTextView.textChanges(name) .debounce(400, TimeUnit.MILLISECONDS)

    .flatMap(text -> api.sendToServer(text)) .observeOn(AndroidSchedulers.mainThread()) .subscribe( this::handleResponse, this::handleError ); return this ?: "" }
  17. Java + RxBinding EditText name = ... RxTextView.textChanges(name) .debounce(400, TimeUnit.MILLISECONDS)

    .flatMap(text -> api.sendToServer(text)) .observeOn(AndroidSchedulers.mainThread()) .subscribe( this::handleResponse, this::handleError ); return this ?: "" }
  18. Java + RxBinding EditText name = ... RxTextView.textChanges(name) .debounce(400, TimeUnit.MILLISECONDS)

    .flatMap(text -> api.sendToServer(text)) .observeOn(AndroidSchedulers.mainThread()) .subscribe( this::handleResponse, this::handleError ); return this ?: "" }
  19. Java + RxBinding EditText name = ... RxTextView.textChanges(name) .debounce(400, TimeUnit.MILLISECONDS)

    .flatMap(text -> api.sendToServer(text)) .observeOn(AndroidSchedulers.mainThread()) .subscribe( this::handleResponse, this::handleError ); return this ?: "" }
  20. Java + RxBinding EditText name = ... RxTextView.textChanges(name) .debounce(400, TimeUnit.MILLISECONDS)

    .flatMap(text -> api.sendToServer(text)) .observeOn(AndroidSchedulers.mainThread()) .subscribe( this::handleResponse, this::handleError ); return this ?: "" }
  21. Java + RxBinding EditText name = ... RxTextView.textChanges(name) .debounce(400, TimeUnit.MILLISECONDS)

    .flatMap(text -> api.sendToServer(text)) .observeOn(AndroidSchedulers.mainThread()) .subscribe( this::handleResponse, this::handleError ); return this ?: "" }
  22. Java + RxBinding EditText name = ... RxTextView.textChanges(name) .debounce(400, TimeUnit.MILLISECONDS)

    .flatMap(text -> api.sendToServer(text)) .observeOn(AndroidSchedulers.mainThread()) .subscribe( this::handleResponse, this::handleError ); return this ?: "" } static method looks ugly
  23. Java + RxBinding EditText name = ... RxTextView.textChanges(name) .debounce(400, TimeUnit.MILLISECONDS)

    .flatMap(text -> api.sendToServer(text)) .observeOn(AndroidSchedulers.mainThread()) .subscribe( this::handleResponse, this::handleError ); return this ?: "" } NOT RxEditText! static method looks ugly
  24. Java + RxBinding • RxAbsListView • RxAdapter • RxAdapterView •

    RxAutoCompleteTextView • RxCheckedTextView • RxCompoundButton • RxProgressBar • RxRadioGroup • RxRecyclerView • RxSeekBar • RxView • RxViewGroup • RxSnackbar • RxAppBarLayout • RxDrawerLayout • RxNestedScrollView • RxSwipeRefreshLayout • RxSearchView • RxToolbar • …
  25. Kotlin + RxBinding val name: EditText = ... name.textChanges() .debounce(400,

    TimeUnit.MILLISECONDS) .flatMap { text -> api.sendToServer(text) } .observeOn(AndroidSchedulers.mainThread()) .subscribe( { handleResponse(it) }, { handleError(it) } ); return this ?: "" }
  26. Kotlin + RxBinding val name: EditText = ... name.textChanges() .debounce(400,

    TimeUnit.MILLISECONDS) .flatMap { text -> api.sendToServer(text) } .observeOn(AndroidSchedulers.mainThread()) .subscribe( { handleResponse(it) }, { handleError(it) } ); return this ?: "" } extension method
  27. Kotlin + RxBinding val name: EditText = ... name.textChanges() .debounce(400,

    TimeUnit.MILLISECONDS) .flatMap { text -> api.sendToServer(text) } .observeOn(AndroidSchedulers.mainThread()) .subscribe( { handleResponse(it) }, { handleError(it) } ); return this ?: "" } extension method No need to know!
  28. Kotlin + RxBinding val button: Button = ... val buttonClicks

    = button.clicks().share() buttonClicks.subscribe { //do something useful } buttonClicks.subscribe { //two listeners on one button! } return this ?: "" }
  29. Kotlin + RxBinding val button: Button = ... // in

    Java: Observable<Void> buttonClicks = ... val buttonClicks = button.clicks().share() buttonClicks.subscribe { //do something useful } buttonClicks.subscribe { //two listeners on one button! } return this ?: "" }
  30. Kotlin + RxBinding val button: Button = ... val buttonClicks

    = button.clicks().share() buttonClicks.subscribe { //do something useful } buttonClicks.subscribe { //two listeners on one button! } return this ?: "" }
  31. Kotlin + RxBinding val button: Button = ... val buttonClicks

    = button.clicks().share() buttonClicks.subscribe { //do something useful } buttonClicks.subscribe { //two listeners on one button! } return this ?: "" }
  32. Kotlin + RxBinding val button: Button = ... val buttonClicks

    = button.clicks().share() buttonClicks.subscribe { //do something useful } buttonClicks.subscribe { //two listeners on one button! } return this ?: "" }
  33. Kotlin + RxBinding val button: Button = ... val buttonClicks

    = button.clicks().share() val sub1 = buttonClicks.subscribe { //do something useful } val sub2 = buttonClicks.subscribe { //two listeners on one button! } return this ?: "" }
  34. Kotlin + RxBinding val button: Button = ... val compositeSub

    = CompositeSubscription() val buttonClicks = button.clicks().share() val sub1 = buttonClicks.subscribe { //do something useful } val sub2 = buttonClicks.subscribe { //two listeners on one button! } return this ?: "" }
  35. Kotlin + RxBinding val button: Button = ... val compositeSub

    = CompositeSubscription() val buttonClicks = button.clicks().share() compositeSub += buttonClicks.subscribe { //do something useful } compositeSub += buttonClicks.subscribe { //two listeners on one button! } return this ?: "" }