Upload
avitotech
View
459
Download
11
Embed Size (px)
Citation preview
Kotlin и Rx в Android
Rx & Single Abstract Method
Rx & Single Abstract Method
Observable.just("Hello, world!")
.map(new Func1<String, Integer>() {
@Override
public Integer call(String s) {
return s.hashCode();
}
})
.subscribe(new Action1<Integer>() {
@Override
public void call(Integer i) {
System.out.println(i);
}
});
Java
Rx & Single Abstract Methodpublic interface Func1<T, R> extends Function {
R call(T t);
}
Rx & Single Abstract Method
Observable.just("Hello, world!")
.map(new Func1<String, Integer>() {
@Override
public Integer call(String s) {
return s.hashCode();
}
})
.subscribe(new Action1<Integer>() {
@Override
public void call(Integer i) {
System.out.println(i);
}
});
Java
Observable.just("Hello, world!")
.map { s -> s.hashCode() }
.subscribe { i -> println(i) }
Kotlin
Rx & Single Abstract Method
Observable.just("Hello, world!")
.map(new Func1<String, Integer>() {
@Override
public Integer call(String s) {
return s.hashCode();
}
})
.subscribe(new Action1<Integer>() {
@Override
public void call(Integer i) {
System.out.println(i);
}
});
Java
Observable.just("Hello, world!")
.map { s -> s.hashCode() }
.subscribe { i -> println(i) }
Kotlin
Rx & Single Abstract Method
Observable.just("Hello, world!")
.map { s -> s.hashCode() }
.subscribe { i -> println(i) }
Rx & Single Abstract Method
Observable.just("Hello, world!")
.map { string -> string.hashCode() }
.subscribe { hashCode -> println(hashCode) }
Rx & Single Abstract Method
Observable.just("Hello, world!")
.map { it.hashCode() }
.subscribe { println(it) }
Rx & High-Order functions
Rx & High-Order functionsfun yourFunc(one: String, two: Func0<Int>)
Rx & High-Order functionsfun yourFunc(one: String, two: () -> Int)
Rx & High-Order functionsfun yourFunc(one: String, two: () -> Int)
yourFunc("Hi") { 1 }
Rx & High-Order functionsObservable.combineLatest(one, two) { a, b -> a + b }
Observable.zip(one, two, three) { a, b, c -> a + b + c }
Rx & Data types
Rx & Data typesObservable.combineLatest(location, webResponse){ a, b -> Pair(a, b) }
.distinctUntilChanged()
.map { doSomething(it.first, it.second) }
Rx & Data typesObservable.combineLatest(location, webResponse){ a, b -> LocationAndResponse(a, b)}
.distinctUntilChanged()
.map { doSomething(it.location, it.response) }
Rx & Data typesdata class LocationAndResponse(val location: Location, val response: Response)
Observable.combineLatest(location, webResponse){ a, b -> LocationAndResponse(a, b)}
.distinctUntilChanged()
.map { doSomething(it.location, it.response) }
Rx & Null safety
Rx & Null Safetyvar a: String = "abc"
a = null // compilation error
Rx & Null Safetyvar a: String = "abc"
a = null // compilation error
var b: String? = "abc"
b = null // ok
Rx & Null Safetyvar a: String = "abc"
a = null // compilation error
var b: String? = "abc"
b = null // ok
val l = b.length // error: variable 'b' can be null
Rx & Null Safetyif (b != null && b.length > 0)
print("String of length ${b.length}")
else
print("Empty string")
Rx & Null Safetyobservable //Observable<Int?>
.filter { it != null }
.subscribe { println(it) } //Observable<Int?>
Rx & Null SafetylistOf(1, 2, 3, null) // Array<Int?>
.filter { it != null }
.forEach { println(it) } // Array<Int?>
Rx & Null SafetylistOf(1, 2, 3, null) // Array<Int?>
.filterNotNull()
.forEach { println(it) } // Array<Int?>
Rx & Null Safetyfun filterNotNull() {
}
Rx & Null Safetyfun Observable.filterNotNull() {
}
Rx & Null Safetyfun <T> Observable<T?>.filterNotNull(): Observable<T> {
}
Rx & Null Safetyfun <T> Observable<T?>.filterNotNull(): Observable<T> {
}
Rx & Null Safetyfun <T> Observable<T?>.filterNotNull(): Observable<T> {
return filter { it != null } as Observable<T>
}
Rx & Null Safetyobservable //Observable<Int?>
.filterNotNull()
.subscribe { println(it) } //Observable<Int> PROFIT!!!
Rx & Sealed classes
Rx & Sealed classessealed class State {
class Progress() : State()
class Content(val data: Response) : State()
class Error(val error: Throwable) : State()
}
Rx & Sealed classesstateObservable.subscribe {
when(it) {
is State.Progress -> { /* show progress */ }
is State.Content -> { it.data /* show content */ }
is State.Error -> { it.error /* show error */ }
}
}
Rx & Sealed classesstateObservable
.filterByType(State.Content::class.java)
.subscribe { it.data }
Rx & Sealed classesstateObservable
.filterByType(State.Content::class.java)
.subscribe { it.data }
Rx & Sealed classesfun <T, R : T> Observable<T>.filterByType(type: Class<R>): Observable<R> =
filter { type.isInstance(it) } .map { type.cast(it) }
Rx & Operator overloading
Rx & Operator overloadingval subscription = CompositeSubscription()
subscription.add(observable1.subscribe { ... })
subscription.add(observable2.subscribe { ... })
subscription.add(observable3.subscribe { ... })
Rx & Operator overloadingval subscription = CompositeSubscription()
subscription.add(observable1.subscribe { ... })
subscription.add(observable2.subscribe { ... })
subscription.add(observable3.subscribe { ... })
operator fun CompositeSubscription.plusAssign(subscription: Subscription) = add(subscription)
Rx & Operator overloadingval subscription = CompositeSubscription()
subscription += observable1.subscribe { ... }
subscription += observable2.subscribe { ... }
subscription += observable3.subscribe { ... }
operator fun CompositeSubscription.plusAssign(subscription: Subscription) = add(subscription)
Rx & Android Lifecycle
Rx & Lifecycle
View Model
UI events
Data events
RxBinding
RxBindingJava
RxView.clicks(button)
RxBindingJava
RxView.clicks(button)
Kotlin
button.clicks()
RxBindingJava
RxView.clicks(button)
.subscribe(RxTextView.text(textView));
Kotlin
button.clicks()
RxBindingKotlin
button.clicks()
.subscribe(textView.text())
Java
RxView.clicks(button)
.subscribe(RxTextView.text(textView));
RxBinding
RxLifecycle
RxLifecycleJAVA
myObservable
.compose(RxLifecycle.bindActivity(lifecycle))
.subscribe();
RxLifecycleKOTLIN
myObservable
.bindWithLifecycle(activity)
.subscribe {}
JAVA
myObservable
.compose(RxLifecycle.bindActivity(activity))
.subscribe();
RxLifecycleKOTLIN
myObservable
.bindWithLifecycle(activity)
.subscribe {}
myObservable
.bindUntilEvent(activity,ActivityEvent.PAUSE)
.subscribe {}
JAVA
myObservable
.compose(RxLifecycle.bindActivity(activity))
.subscribe();
Rx & Type safe builders
Rx & Type safe builders model.dataStream()
.bindWithLifecycle(view)
.subscribe(dataHandleAction)
model.editIntent()
.bindWithLifecycle(view)
.subscribe(gotoEditAction)
model.mapData()
.bindWithLifecycle(view)
.subscribe(markersHandleAction)
Rx & Type safe builders view.bindWithLifecycle {
model.dataStream() with dataHandleAction
model.editIntent() with gotoEditAction
model.mapData() with markersHandleAction
}
Rx & Type safe buildersobservable.subscribe { /* on next here*/ }
Rx & Type safe buildersobservable.subscribe { /* on next here*/ }
observable.subscribe ({ /* onNext */ }, { /* onError*/ })
Rx & Type safe buildersobservable.subscribe { /* on next here*/ }
observable.subscribe ({ /* onNext */ }, { /* onError*/ })
observable.subscribe ({ /* onNext */ }, { /* onError*/ }, { /* onComplete*/ })
Rx & Type safe buildersobservable.subscribeWith {
onNext { }
onError { }
}
Testing Rx & Kotlin
Testing Rx & Kotlinval finiteObservable = Observable.from(arrayOf("1", "2", "3"))
val subscriber = TestSubscriber<String>()
finiteObservable.subscribe(subscriber)
subscriber.awaitTerminalEvent()
subscriber.assertValues("1", "2", "3")
Testing Rx & Kotlinval finiteObservable = Observable.from(arrayOf("1", "2", "3"))
fun <T> Observable<T>.assertValues(vararg values: T) {
val subscriber = TestSubscriber<T>()
this.subscribe(subscriber)
subscriber.awaitTerminalEvent()
subscriber.assertValues(*values)
}
Testing Rx & Kotlinval finiteObservable = Observable.from(arrayOf("1", "2", "3"))
fun <T> Observable<T>.assertValues(vararg values: T) {
val subscriber = TestSubscriber<T>()
this.subscribe(subscriber)
subscriber.awaitTerminalEvent()
subscriber.assertValues(*values)
}
finiteObservable.assertValues("1", "2", "3")
Testing Rx & Kotlinval finiteObservable = Observable.from(arrayOf("1", "2", "3"))
infix fun <T> Observable<T>.assertValues(values: Array<T>) {
val subscriber = TestSubscriber<T>()
this.subscribe(subscriber)
subscriber.awaitTerminalEvent()
subscriber.assertValues(*values)
}
finiteObservable assertValues arrayOf("1", "2", "3")
Testing Rx & Kotlinval finiteObservable = Observable.from(arrayOf("1", "2", "3"))
infix fun <T> Observable<T>.shouldProduce(values: Array<T>) {
val subscriber = TestSubscriber<T>()
this.subscribe(subscriber)
subscriber.awaitTerminalEvent()
subscriber.assertValues(*values)
}
finiteObservable shouldProduce arrayOf("1", "2", "3")