File tree Expand file tree Collapse file tree 2 files changed +29
-13
lines changed
src/main/java/com/github/caoddx/rxlifecycle Expand file tree Collapse file tree 2 files changed +29
-13
lines changed Original file line number Diff line number Diff line change 1
1
apply plugin : ' com.android.library'
2
+ apply plugin : ' kotlin-android'
2
3
3
4
android {
4
5
compileSdkVersion 27
5
6
6
-
7
-
8
7
defaultConfig {
9
8
minSdkVersion 21
10
9
targetSdkVersion 27
11
10
versionCode 1
12
11
versionName " 1.0"
13
12
14
13
testInstrumentationRunner " android.support.test.runner.AndroidJUnitRunner"
15
-
16
14
}
17
-
18
- buildTypes {
19
- release {
20
- minifyEnabled false
21
- proguardFiles getDefaultProguardFile(' proguard-android.txt' ), ' proguard-rules.pro'
22
- }
23
- }
24
-
25
15
}
26
16
27
17
dependencies {
28
18
implementation fileTree(dir : ' libs' , include : [' *.jar' ])
19
+ compile " org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version "
29
20
30
21
implementation ' com.android.support:appcompat-v7:27.1.1'
31
22
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'
34
26
}
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments