Skip to content

Commit 248a7c2

Browse files
author
caodd
committed
add Disposable extension : bindTo
1 parent 3275456 commit 248a7c2

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

rxlifecycle/build.gradle

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,26 @@
11
apply plugin: 'com.android.library'
2+
apply plugin: 'kotlin-android'
23

34
android {
45
compileSdkVersion 27
56

6-
7-
87
defaultConfig {
98
minSdkVersion 21
109
targetSdkVersion 27
1110
versionCode 1
1211
versionName "1.0"
1312

1413
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
15-
1614
}
17-
18-
buildTypes {
19-
release {
20-
minifyEnabled false
21-
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
22-
}
23-
}
24-
2515
}
2616

2717
dependencies {
2818
implementation fileTree(dir: 'libs', include: ['*.jar'])
19+
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
2920

3021
implementation 'com.android.support:appcompat-v7:27.1.1'
3122
testImplementation 'junit:junit:4.12'
32-
androidTestImplementation 'com.android.support.test:runner:1.0.2'
33-
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
23+
24+
// rxjava
25+
implementation 'io.reactivex.rxjava2:rxjava:2.1.9'
3426
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.github.caoddx.rxlifecycle
2+
3+
import android.arch.lifecycle.GenericLifecycleObserver
4+
import android.arch.lifecycle.Lifecycle
5+
import android.arch.lifecycle.LifecycleOwner
6+
import io.reactivex.disposables.Disposable
7+
8+
fun Disposable.bindTo(owner: LifecycleOwner): Disposable {
9+
10+
owner.lifecycle.addObserver(object : GenericLifecycleObserver {
11+
override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
12+
13+
if (event == Lifecycle.Event.ON_DESTROY) {
14+
dispose()
15+
}
16+
17+
if (isDisposed) {
18+
source.lifecycle.removeObserver(this)
19+
}
20+
}
21+
})
22+
23+
return this
24+
}

0 commit comments

Comments
 (0)