Skip to content

Commit

Permalink
add Disposable extension : bindTo
Browse files Browse the repository at this point in the history
  • Loading branch information
caodd committed Aug 21, 2018
1 parent 3275456 commit 248a7c2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
18 changes: 5 additions & 13 deletions rxlifecycle/build.gradle
Original file line number Diff line number Diff line change
@@ -1,34 +1,26 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 27



defaultConfig {
minSdkVersion 21
targetSdkVersion 27
versionCode 1
versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

}

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

implementation 'com.android.support:appcompat-v7:27.1.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

// rxjava
implementation 'io.reactivex.rxjava2:rxjava:2.1.9'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.github.caoddx.rxlifecycle

import android.arch.lifecycle.GenericLifecycleObserver
import android.arch.lifecycle.Lifecycle
import android.arch.lifecycle.LifecycleOwner
import io.reactivex.disposables.Disposable

fun Disposable.bindTo(owner: LifecycleOwner): Disposable {

owner.lifecycle.addObserver(object : GenericLifecycleObserver {
override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {

if (event == Lifecycle.Event.ON_DESTROY) {
dispose()
}

if (isDisposed) {
source.lifecycle.removeObserver(this)
}
}
})

return this
}

0 comments on commit 248a7c2

Please sign in to comment.