Slide 1

Slide 1 text

AutoDispose @lvla0805

Slide 2

Slide 2 text

lvla Ѫᖒ๖ (Moyuru Aizawa) - Kotlin engineer at CyberAgent, Inc. - FRESH! lvla0805

Slide 3

Slide 3 text

override fun onCreate(…) { …
 Flowable.interval(1, TimeUnit.SECONDS)
 .subscribe { sec -> textView.text = "${sec}s" }
 } Disposable

Slide 4

Slide 4 text

var disposable: Disposable? = null 
 override fun onCreate(…) { …
 disposable = Flowable.interval(1, TimeUnit.SECONDS)
 .subscribe { sec -> textView.text = "${sec}s" }
 } 
 override fun onDestroy() {
 disposable?.dispose()
 } Disposable

Slide 5

Slide 5 text

val disposables = CompositeDisposable() 
 override fun onCreate(…) { …
 Flowable.interval(1, TimeUnit.SECONDS)
 .subscribe { sec -> textView.text = "${sec}s" }
 .addTo(disposables)
 } 
 override fun onDestroy() {
 disposables.dispose()
 super.onDestroy()
 } CompositeDisposable

Slide 6

Slide 6 text

AutoDispose

Slide 7

Slide 7 text

override fun onCreate(…) { …
 Flowable.interval(1, TimeUnit.SECONDS)
 .to(FlowableScoper(this))
 .subscribe { sec -> textView.text = "${sec}s" }
 } AutoDispose

Slide 8

Slide 8 text

enum class ActivityLifeCycle {
 CREATE, …, DESTROY
 } AutoDispose

Slide 9

Slide 9 text

public interface LifecycleScopeProvider {
 Observable lifecycle();
 Function correspondingEvents();
 E peekLifecycle();
 } AutoDispose

Slide 10

Slide 10 text

abstract class RxActivity : AppCompatActivity(), LifecycleScopeProvider { … } AutoDispose

Slide 11

Slide 11 text

‣ lifecycle() ‣ returns an Observable of lifecycle events. ‣ This should be backed by a BehaviorSubject or something similar ‣ correspondingEvents() ‣ a mapping of events to corresponding ones. ‣ i.e. CREATE -> DESTROY ‣ peekLifecycle() ‣ returns the current lifecycle state of the object. AutoDispose

Slide 12

Slide 12 text

abstract class RxActivity : AppCompatActivity(),
 LifecycleScopeProvider {
 
 private val lifecycle = BehaviorSubject.create()
 private lateinit var currentEvent: ActivityLifeCycle
 
 override fun lifecycle(): Observable = lifecycle
 
 override fun correspondingEvents(): Function {
 return Function { lastEvent: ActivityLifeCycle ->
 when (lastEvent) {
 CREATE -> DESTROY
 else -> throw OutsideLifecycleException("Activity was destroyed")
 }
 }
 }
 
 override fun peekLifecycle() = currentEvent
 } AutoDispose

Slide 13

Slide 13 text

class MainActivity : RxActivity() {
 …
 override fun onCreate(…) {
 Flowable.interval(1, TimeUnit.SECONDS)
 .to(FlowableScoper(this))
 .subscribe { sec -> textView.text = "${sec}s" }
 }
 } AutoDispose

Slide 14

Slide 14 text

Thank you