Skip to content

Commit

Permalink
Add AndroidX Lifecycle binding.
Browse files Browse the repository at this point in the history
  • Loading branch information
ychescale9 committed Jan 23, 2020
1 parent 9fdf3ee commit 82c9800
Show file tree
Hide file tree
Showing 16 changed files with 201 additions and 5 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ implementation "io.github.reactivecircus.flowbinding:flowbinding-android:${flowb
implementation "io.github.reactivecircus.flowbinding:flowbinding-appcompat:${flowbinding_version}"
implementation "io.github.reactivecircus.flowbinding:flowbinding-core:${flowbinding_version}"
implementation "io.github.reactivecircus.flowbinding:flowbinding-drawerlayout:${flowbinding_version}"
implementation "io.github.reactivecircus.flowbinding:flowbinding-lifecycle:${flowbinding_version}"
implementation "io.github.reactivecircus.flowbinding:flowbinding-navigation:${flowbinding_version}"
implementation "io.github.reactivecircus.flowbinding:flowbinding-preference:${flowbinding_version}"
implementation "io.github.reactivecircus.flowbinding:flowbinding-recyclerview:${flowbinding_version}"
Expand Down Expand Up @@ -149,6 +150,7 @@ List of all bindings available:
* [AndroidX AppCompat bindings][flowbinding-appcompat]
* [AndroidX Core bindings][flowbinding-core]
* [AndroidX DrawerLayout bindings][flowbinding-drawerlayout]
* [AndroidX Lifecycle bindings][flowbinding-lifecycle]
* [AndroidX Navigation Component bindings][flowbinding-navigation]
* [AndroidX Preference bindings][flowbinding-preference]
* [AndroidX RecyclerView bindings][flowbinding-recyclerview]
Expand Down Expand Up @@ -194,6 +196,7 @@ limitations under the License.
[flowbinding-core]: flowbinding-core/
[flowbinding-drawerlayout]: flowbinding-drawerlayout/
[flowbinding-material]: flowbinding-material/
[flowbinding-lifecycle]: flowbinding-lifecycle/
[flowbinding-navigation]: flowbinding-navigation/
[flowbinding-preference]: flowbinding-preference/
[flowbinding-recyclerview]: flowbinding-recyclerview/
Expand Down
1 change: 1 addition & 0 deletions buildSrc/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ rootProject.ext.versions = [
drawerLayout : '1.1.0-alpha03',
constraintLayout : '1.1.3',
arch : '2.1.0',
lifecycle : '2.2.0',
navigation : '2.2.0',
preference : '1.1.0',
test : [
Expand Down
19 changes: 19 additions & 0 deletions flowbinding-lifecycle/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# FlowBinding Lifecycle

This module provides bindings for the **AndroidX Lifecycle** library.

## Transitive Dependency

`androidx.lifecycle:lifecycle-common-java8`

## Download

```groovy
implementation "io.github.reactivecircus.flowbinding:flowbinding-lifecycle:${flowbinding_version}"
```

## Available Bindings

```kotlin
fun Lifecycle.events(): Flow<Lifecycle.Event>
```
26 changes: 26 additions & 0 deletions flowbinding-lifecycle/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
plugins {
id 'flowbinding-plugin'
id 'com.android.library'
id 'kotlin-android'
id 'com.vanniktech.maven.publish'
id 'io.github.reactivecircus.firestorm'
}

android {
defaultConfig {
testApplicationId 'reactivecircus.flowbinding.lifecycle.test'
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
}

dependencies {
implementation project(':flowbinding-common')

implementation "androidx.lifecycle:lifecycle-common-java8:${versions.androidx.lifecycle}"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:${versions.kotlinx.coroutines}"

lintChecks project(":lint-rules")

androidTestImplementation project(':testing-infra')
androidTestImplementation project(':flowbinding-lifecycle:fixtures')
}
13 changes: 13 additions & 0 deletions flowbinding-lifecycle/fixtures/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
plugins {
id 'flowbinding-plugin'
id 'com.android.library'
id 'kotlin-android'
}

android.buildFeatures.viewBinding = true

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:${versions.kotlin}"
implementation "androidx.lifecycle:lifecycle-common-java8:${versions.androidx.lifecycle}"
implementation "androidx.fragment:fragment:${versions.androidx.fragment}"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<manifest package="reactivecircus.flowbinding.lifecycle.fixtures" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package reactivecircus.flowbinding.lifecycle.fixtures

import androidx.fragment.app.Fragment

class LifecycleFragment : Fragment(R.layout.fragment_lifecycle)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" />
4 changes: 4 additions & 0 deletions flowbinding-lifecycle/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
POM_ARTIFACT_ID=flowbinding-lifecycle
POM_NAME=FlowBinding Lifecycle
POM_DESCRIPTION=Kotlin Flow binding APIs for AndroidX Lifecycle
POM_PACKAGING=aar
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package reactivecircus.flowbinding.lifecycle

import androidx.lifecycle.Lifecycle
import androidx.test.filters.LargeTest
import org.amshove.kluent.shouldEqual
import org.junit.Test
import reactivecircus.flowbinding.lifecycle.fixtures.LifecycleFragment
import reactivecircus.flowbinding.testing.FlowRecorder
import reactivecircus.flowbinding.testing.launchTest
import reactivecircus.flowbinding.testing.recordWith

@LargeTest
class LifecycleEventFlowTest {

@Test
fun lifecycleEvents() {
launchTest<LifecycleFragment> { scenario ->
val recorder = FlowRecorder<Lifecycle.Event>(testScope)
fragment.lifecycle.events().recordWith(recorder)

recorder.takeValue() shouldEqual Lifecycle.Event.ON_CREATE
recorder.takeValue() shouldEqual Lifecycle.Event.ON_RESUME
recorder.assertNoMoreValues()

scenario.moveToState(Lifecycle.State.CREATED)
recorder.takeValue() shouldEqual Lifecycle.Event.ON_PAUSE
recorder.takeValue() shouldEqual Lifecycle.Event.ON_STOP
recorder.assertNoMoreValues()

cancelTestScope()

scenario.moveToState(Lifecycle.State.DESTROYED)
recorder.assertNoMoreValues()
}
}
}
1 change: 1 addition & 0 deletions flowbinding-lifecycle/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<manifest package="reactivecircus.flowbinding.lifecycle" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package reactivecircus.flowbinding.lifecycle

import androidx.annotation.CheckResult
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.OnLifecycleEvent
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.conflate
import reactivecircus.flowbinding.common.checkMainThread
import reactivecircus.flowbinding.common.safeOffer

/**
* Create a [Flow] of [Lifecycle.Event]s on the [Lifecycle] instance.
*
* Note: Created flow keeps a strong reference to the [Lifecycle] instance
* until the coroutine that launched the flow collector is cancelled.
*
* Example of usage:
*
* ```
* lifecycle.events()
* .filter { it == Lifecycle.Event.ON_CREATE }
* .onEach { event ->
* // handle Lifecycle.Event.ON_CREATE event
* }
* .launchIn(uiScope)
* ```
*/
@CheckResult
@UseExperimental(ExperimentalCoroutinesApi::class)
fun Lifecycle.events(): Flow<Lifecycle.Event> = callbackFlow<Lifecycle.Event> {
checkMainThread()
val observer = object : LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
fun onEvent(owner: LifecycleOwner, event: Lifecycle.Event) {
safeOffer(event)
}
}
addObserver(observer)
awaitClose { removeObserver(observer) }
}.conflate()
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ import java.util.EnumSet
* `*Listener = *`,
* `add*Listener(*)`
* `add*Callback(*)`
* `add*Observer(*)`
* `register*(*)`
*
* then one of the following must be present in an `awaitClose` block:
* `set*Listener(null)`
* `*Listener = null`,
* `remove*Listener(*)`
* `remove*Callback(*)`
* `remove*Observer(*)`
* `unregister*(*)`
*/
@Suppress("UnstableApiUsage", "ComplexCondition", "ReturnCount")
Expand All @@ -58,11 +60,17 @@ class MissingListenerRemovalDetector : Detector(), SourceCodeScanner {
)

private const val CALLBACK_FLOW = "callbackFlow"

private const val AWAIT_CLOSE = "awaitClose"

private const val PATTERN_ADD_LISTENER_METHOD = "add.+Listener|add.*Callback|register.+"
private const val PATTERN_REMOVE_LISTENER_METHOD = "remove.+Listener|remove.*Callback|unregister.+"
private const val PATTERN_ADD_LISTENER_METHOD =
"add.+Listener|add.*Callback|add.*Observer|register.+"

private const val PATTERN_REMOVE_LISTENER_METHOD =
"remove.+Listener|remove.*Callback|remove.*Observer|unregister.+"

private const val PATTERN_SET_LISTENER = "set.+Listener"

private const val SUFFIX_LISTENER = "listener"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,32 @@ class MissingListenerRemovalDetectorTest {
.expectClean()
}

@Test
fun `observer removed`() {
lint()
.files(
kotlin(
"""
fun Lifecycle.events(): Flow<Lifecycle.Event> = callbackFlow<Lifecycle.Event> {
checkMainThread()
val observer = object : LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
fun onEvent(owner: LifecycleOwner, event: Lifecycle.Event) {
safeOffer(event)
}
}
addObserver(observer)
awaitClose { removeObserver(observer) }
}.conflate()
""".trimIndent()
)
)
.allowMissingSdk()
.issues(MissingListenerRemovalDetector.ISSUE)
.run()
.expectClean()
}

@Test
fun `callback unregistered`() {
lint()
Expand Down
3 changes: 3 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ includeProject(":flowbinding-core:fixtures", "flowbinding-core/fixtures")
include(":flowbinding-drawerlayout")
includeProject(":flowbinding-drawerlayout:fixtures", "flowbinding-drawerlayout/fixtures")

include(":flowbinding-lifecycle")
includeProject(":flowbinding-lifecycle:fixtures", "flowbinding-lifecycle/fixtures")

include(":flowbinding-material")
includeProject(":flowbinding-material:fixtures", "flowbinding-material/fixtures")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.view.View
import androidx.annotation.IdRes
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.testing.FragmentScenario
import androidx.fragment.app.testing.launchFragmentInContainer
import androidx.test.espresso.Espresso
import kotlinx.coroutines.CoroutineScope
Expand All @@ -14,12 +15,12 @@ import reactivecircus.blueprint.testing.currentActivity
import com.google.android.material.R as MaterialR

inline fun <reified F : Fragment> launchTest(
block: TestLauncher.() -> Unit
block: TestLauncher.(FragmentScenario<F>) -> Unit
) {
launchFragmentInContainer<F>(themeResId = MaterialR.style.Theme_MaterialComponents_DayNight)
val scenario = launchFragmentInContainer<F>(themeResId = MaterialR.style.Theme_MaterialComponents_DayNight)
Espresso.onIdle()
val testScope = MainScope()
TestLauncher(testScope).block()
TestLauncher(testScope).block(scenario)
testScope.cancel()
}

Expand Down

0 comments on commit 82c9800

Please sign in to comment.