Skip to content

Commit be4a6aa

Browse files
committed
LifecycleDispose3: Migrate to RxJava3
1 parent 3fcbff8 commit be4a6aa

File tree

12 files changed

+81
-88
lines changed

12 files changed

+81
-88
lines changed

LifecycleDispose/build.gradle

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
apply plugin: 'com.android.library'
22
apply plugin: 'kotlin-android'
3-
apply from: "$rootDir/gradle-use-latest-versions.gradle"
43

54
android {
65
compileSdkVersion 29
@@ -12,11 +11,14 @@ android {
1211
sourceCompatibility JavaVersion.VERSION_1_8
1312
targetCompatibility JavaVersion.VERSION_1_8
1413
}
14+
kotlinOptions {
15+
jvmTarget = JavaVersion.VERSION_1_8
16+
}
1517
}
1618

1719
dependencies {
1820
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
19-
implementation "io.reactivex.rxjava2:rxkotlin:2.4.0"
20-
implementation "io.reactivex.rxjava2:rxjava:2.2.14"
21-
implementation "androidx.fragment:fragment:1.2.0-rc04"
21+
implementation "io.reactivex.rxjava3:rxkotlin:3.0.0"
22+
implementation 'io.reactivex.rxjava3:rxjava:3.0.5'
23+
implementation "androidx.fragment:fragment:1.2.5"
2224
}

LifecycleDispose/src/main/java/com/wada811/lifecycledispose/LifecycleDispose.kt

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import androidx.lifecycle.Lifecycle.State.RESUMED
1717
import androidx.lifecycle.Lifecycle.State.STARTED
1818
import androidx.lifecycle.LifecycleEventObserver
1919
import androidx.lifecycle.LifecycleOwner
20-
import io.reactivex.disposables.Disposable
20+
import io.reactivex.rxjava3.disposables.Disposable
2121

2222
/**
2323
* Dispose on corresponding lifecycle event.
@@ -46,7 +46,7 @@ fun <T : Disposable> T.disposeOnPause(activity: FragmentActivity): T = disposeOn
4646
fun <T : Disposable> T.disposeOnDestroy(activity: FragmentActivity): T = disposeOnLifecycleEvent(activity, ON_DESTROY)
4747

4848
private fun <T : Disposable> T.disposeOnLifecycleEvent(activity: FragmentActivity, lifecycleEvent: Event): T =
49-
disposeOnLifecycleEvents(activity.lifecycle, listOf(lifecycleEvent, ON_DESTROY))
49+
disposeOnLifecycleEvents({ activity.lifecycle }, { listOf(lifecycleEvent, ON_DESTROY) })
5050

5151
/**
5252
* Dispose on corresponding lifecycle event.
@@ -69,7 +69,7 @@ private fun <T : Disposable> T.disposeOnLifecycleEvent(activity: FragmentActivit
6969
fun <T : Disposable> T.disposeOnLifecycle(fragment: Fragment): T =
7070
disposeOnLifecycleEvent(fragment, when (fragment.viewLifecycleOrLifecycle.currentState) {
7171
INITIALIZED -> ON_DESTROY // onAttach, onCreate, onViewCreated
72-
CREATED -> null // onCreateView: ON_DESTROY, onStart, onStop: ON_STOP
72+
CREATED -> if (fragment.viewLifecycleOwnerLiveData.value != null) ON_STOP else null // onCreateView: ON_DESTROY, onStart, onStop: ON_STOP
7373
STARTED, RESUMED -> ON_PAUSE // onResume, onPause
7474
DESTROYED -> ON_DESTROY // onDestroyView, onDestroy
7575
})
@@ -79,45 +79,36 @@ fun <T : Disposable> T.disposeOnPause(fragment: Fragment): T = disposeOnLifecycl
7979
fun <T : Disposable> T.disposeOnDestroy(fragment: Fragment): T = disposeOnLifecycleEvent(fragment, ON_DESTROY)
8080

8181
private fun <T : Disposable> T.disposeOnLifecycleEvent(fragment: Fragment, lifecycleEvent: Event?): T =
82-
if (fragment.viewLifecycleOwnerLiveData.value != null) {
83-
val lifecycle = fragment.viewLifecycleOwner.lifecycle
84-
// 1: In onStart, Fragment has view.
85-
disposeOnLifecycleEvents(lifecycle, listOfNotNull(lifecycleEvent ?: /* 1 */ON_STOP, ON_DESTROY))
86-
} else {
87-
val lifecycle = fragment.lifecycle
88-
when (lifecycle.currentState) {
89-
INITIALIZED -> disposeOnLifecycleEvents(lifecycle, listOfNotNull(lifecycleEvent, ON_DESTROY))
90-
CREATED -> doOnLifecycleEvents(lifecycle, listOf(ON_START, ON_DESTROY)) {
91-
if (fragment.viewLifecycleOwnerLiveData.value != null) {
92-
// 2: In onCreateView, Fragment has view.
93-
// 2-1: If disposeOnPause/disposeOnStop/disposeOnDestroy called, dispose in lifecycleEvent
94-
// 2-2: If disposeOnLifecycle called, dispose in onDestroyView
95-
disposeOnLifecycleEvents(fragment.viewLifecycleOwner.lifecycle, listOfNotNull(/* 2-1 */lifecycleEvent, /* 2-2 */ON_DESTROY))
96-
} else {
97-
// 3: In onStart, Fragment has no view.
98-
// 3-1: If disposeOnPause/disposeOnStop/disposeOnDestroy called, dispose in lifecycleEvent
99-
// 3-2: If disposeOnLifecycle called, dispose in onStop
100-
// 4: In onStop, Fragment has no view.
101-
// If disposeOnPause/disposeOnStop/disposeOnDestroy called, dispose in onDestroy
102-
// If disposeOnLifecycle called, dispose in onDestroy
103-
disposeOnLifecycleEvents(lifecycle, listOfNotNull(/* 3-1 */lifecycleEvent ?: /* 3-2 */ON_STOP, /* 4 */ON_DESTROY))
104-
}
105-
}
106-
else -> disposeOnLifecycleEvents(lifecycle, listOfNotNull(lifecycleEvent, ON_DESTROY))
107-
}
108-
}
82+
disposeOnLifecycleEvents(
83+
{ fragment.viewLifecycleOrLifecycle },
84+
{ listOf(lifecycleEvent ?: if (fragment.viewLifecycleOwnerLiveData.value != null) ON_DESTROY else ON_STOP, ON_DESTROY) }
85+
)
10986

11087
private val Fragment.viewLifecycleOrLifecycle: Lifecycle
11188
get() = if (viewLifecycleOwnerLiveData.value != null) viewLifecycleOwner.lifecycle else lifecycle
11289

113-
private fun <T : Disposable> T.disposeOnLifecycleEvents(lifecycle: Lifecycle, lifecycleEvents: List<Event>): T =
114-
if (lifecycle.currentState == DESTROYED) safeDispose() else doOnLifecycleEvents(lifecycle, lifecycleEvents) { safeDispose() }
90+
private fun <T : Disposable> T.disposeOnLifecycleEvents(lifecycle: () -> Lifecycle, lifecycleEvents: () -> List<Event>): T =
91+
if (lifecycle().currentState == DESTROYED) safeDispose() else doOnLifecycleEvents(lifecycle, lifecycleEvents) { safeDispose() }
11592

116-
private fun <T : Disposable> T.doOnLifecycleEvents(lifecycle: Lifecycle, lifecycleEvents: List<Event>, onEvent: T.() -> Unit): T = this.also {
117-
lifecycle.addObserver(object : LifecycleEventObserver {
93+
private fun <T : Disposable> T.doOnLifecycleEvents(
94+
lifecycle: () -> Lifecycle,
95+
lifecycleEvents: () -> List<Event>,
96+
onEvent: T.() -> Unit
97+
): T = this.also {
98+
lifecycle().addObserver(object : LifecycleEventObserver {
11899
override fun onStateChanged(source: LifecycleOwner, event: Event) {
119-
if (event in lifecycleEvents) {
120-
lifecycle.removeObserver(this)
100+
if (event == ON_START) {
101+
source.lifecycle.removeObserver(this)
102+
lifecycle().addObserver(object : LifecycleEventObserver {
103+
override fun onStateChanged(source: LifecycleOwner, event: Event) {
104+
if (event in lifecycleEvents()) {
105+
source.lifecycle.removeObserver(this)
106+
onEvent()
107+
}
108+
}
109+
})
110+
} else if (event in lifecycleEvents()) {
111+
source.lifecycle.removeObserver(this)
121112
onEvent()
122113
}
123114
}

build.gradle

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22

33
buildscript {
4-
ext.kotlin_version = '1.3.50'
4+
ext.kotlin_version = '1.3.72'
55
repositories {
66
google()
77
jcenter()
8-
maven {
9-
url "https://plugins.gradle.org/m2/"
10-
}
8+
gradlePluginPortal()
119
}
1210
dependencies {
13-
classpath 'com.android.tools.build:gradle:3.5.1'
11+
classpath 'com.android.tools.build:gradle:4.0.1'
1412
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
15-
classpath "se.patrikerdes:gradle-use-latest-versions-plugin:0.2.13"
16-
classpath 'com.github.ben-manes:gradle-versions-plugin:0.27.0'
1713
}
1814
}
1915

2016
allprojects {
21-
apply from: "$rootDir/gradle-use-latest-versions.gradle"
2217
repositories {
2318
google()
2419
mavenCentral()

gradle-use-latest-versions.gradle

Lines changed: 0 additions & 15 deletions
This file was deleted.

gradle.properties

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,43 @@
11
# Project-wide Gradle settings.
22
# IDE (e.g. Android Studio) users:
3-
# Gradle settings configured through the IDE *will override*
4-
# any settings specified in this file.
3+
# Settings specified in this file will override any Gradle settings
4+
# configured through the IDE.
55
# For more details on how to configure your build environment visit
66
# http://www.gradle.org/docs/current/userguide/build_environment.html
7+
org.gradle.daemon=true
78
# Specifies the JVM arguments used for the daemon process.
89
# The setting is particularly useful for tweaking memory settings.
9-
org.gradle.jvmargs=-Xmx1536m
10+
# Default value: -Xmx10248m -XX:MaxPermSize=256m
11+
org.gradle.jvmargs=-Xmx4096m -XX:MaxPermSize=2048m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
1012
# When configured, Gradle will run in incubating parallel mode.
1113
# This option should only be used with decoupled projects. More details, visit
1214
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
1315
org.gradle.parallel=true
16+
org.gradle.caching=true
17+
org.gradle.configureondemand=true
1418
# Kotlin code style for this project: "official" or "obsolete":
1519
kotlin.code.style=official
16-
# enable binary resources for Robolectric
17-
android.enableUnitTestBinaryResources=true
20+
# Kapt improvements
21+
# https://blog.jetbrains.com/kotlin/2018/08/kotlin-1-2-60-is-out/
22+
kapt.use.worker.api=true
23+
# Faster Gradle builds by parallelizing tasks
24+
# https://blog.jetbrains.com/kotlin/2019/01/kotlin-1-3-20-released/
25+
# For Android, the debug and release build types can be compiled in parallel.
26+
kotlin.parallel.tasks.in.project=true
27+
# Compile avoidance for kapt
28+
kapt.include.compile.classpath=false
29+
# Incremental annotation processing in KAPT
30+
# https://blog.jetbrains.com/kotlin/2019/04/kotlin-1-3-30-released/
31+
kapt.incremental.apt=true
32+
# Incremental annotation processing for DataBinding
33+
# https://developer.android.com/studio/releases/gradle-plugin#incremental_ap
34+
android.databinding.incremental=true
35+
# AndroidX package structure to make it clearer which packages are bundled with the
36+
# Android operating system, and which are packaged with your app"s APK
37+
# https://developer.android.com/topic/libraries/support-library/androidx-rn
38+
android.useAndroidX=true
39+
# Automatically convert third-party libraries to use AndroidX
40+
android.enableJetifier=true
41+
# Cacheable unit tests
42+
# https://developer.android.com/studio/releases/gradle-plugin#cacheable_tests
43+
android.testConfig.useRelativePath=true
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Tue Aug 20 12:13:43 JST 2019
1+
#Sun Aug 02 22:27:14 JST 2020
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip

test/build.gradle

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ android {
1212
sourceCompatibility JavaVersion.VERSION_1_8
1313
targetCompatibility JavaVersion.VERSION_1_8
1414
}
15+
kotlinOptions {
16+
jvmTarget = JavaVersion.VERSION_1_8
17+
}
1518
testOptions {
1619
unitTests.includeAndroidResources = true
1720
}
@@ -25,10 +28,10 @@ dependencies {
2528
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
2629
implementation project(":LifecycleDispose")
2730
// implementation 'com.github.wada811:LifecycleDispose:2.0.3'
28-
implementation "io.reactivex.rxjava2:rxkotlin:2.4.0"
29-
implementation "io.reactivex.rxjava2:rxjava:2.2.14"
30-
implementation "androidx.fragment:fragment:1.2.0-rc04"
31-
implementation 'androidx.fragment:fragment-testing:1.2.0-rc04'
31+
implementation "io.reactivex.rxjava3:rxkotlin:3.0.0"
32+
implementation 'io.reactivex.rxjava3:rxjava:3.0.5'
33+
implementation "androidx.fragment:fragment:1.2.5"
34+
debugImplementation 'androidx.fragment:fragment-testing:1.2.5'
3235

3336
testImplementation 'junit:junit:4.12'
3437

@@ -45,5 +48,5 @@ dependencies {
4548
testImplementation 'androidx.test.ext:junit:1.1.1'
4649
testImplementation 'androidx.test.ext:junit-ktx:1.1.1'
4750
testImplementation 'androidx.test.ext:truth:1.2.0'
48-
testImplementation 'com.google.truth:truth:1.0'
51+
testImplementation 'com.google.truth:truth:1.0.1'
4952
}

test/src/main/java/com/wada811/lifecycledispose/test/DisposeActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import androidx.lifecycle.Lifecycle.Event.ON_PAUSE
1010
import androidx.lifecycle.Lifecycle.Event.ON_RESUME
1111
import androidx.lifecycle.Lifecycle.Event.ON_START
1212
import androidx.lifecycle.Lifecycle.Event.ON_STOP
13-
import io.reactivex.Observable
13+
import io.reactivex.rxjava3.core.Observable
1414
import java.util.concurrent.TimeUnit.SECONDS
1515

1616
class DisposeActivity : FragmentActivity() {

test/src/main/java/com/wada811/lifecycledispose/test/DisposeNoViewFragment.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import android.content.Context
44
import android.os.Bundle
55
import androidx.fragment.app.Fragment
66
import androidx.lifecycle.Lifecycle
7-
import io.reactivex.Observable
7+
import io.reactivex.rxjava3.core.Observable
88
import java.util.concurrent.TimeUnit.SECONDS
99

1010
class DisposeNoViewFragment : Fragment() {

test/src/main/java/com/wada811/lifecycledispose/test/DisposeStrategy.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import com.wada811.lifecycledispose.disposeOnDestroy
66
import com.wada811.lifecycledispose.disposeOnLifecycle
77
import com.wada811.lifecycledispose.disposeOnPause
88
import com.wada811.lifecycledispose.disposeOnStop
9-
import io.reactivex.disposables.Disposable
9+
import io.reactivex.rxjava3.disposables.Disposable
1010

1111
enum class DisposeStrategy {
1212
OnLifecycle {

0 commit comments

Comments
 (0)