forked from ReactiveCircus/FlowBinding
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9fdf3ee
commit 82c9800
Showing
16 changed files
with
201 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<manifest package="reactivecircus.flowbinding.lifecycle.fixtures" /> |
5 changes: 5 additions & 0 deletions
5
...fixtures/src/main/java/reactivecircus/flowbinding/lifecycle/fixtures/LifecycleFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
4 changes: 4 additions & 0 deletions
4
flowbinding-lifecycle/fixtures/src/main/res/layout/fragment_lifecycle.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
36 changes: 36 additions & 0 deletions
36
...cycle/src/androidTest/java/reactivecircus/flowbinding/lifecycle/LifecycleEventFlowTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<manifest package="reactivecircus.flowbinding.lifecycle" /> |
45 changes: 45 additions & 0 deletions
45
...inding-lifecycle/src/main/java/reactivecircus/flowbinding/lifecycle/LifecycleEventFlow.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters