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.
Add flowbinding-activity with a binding for OnBackPressedDispatcher.
- Loading branch information
1 parent
82c9800
commit 33f625c
Showing
12 changed files
with
155 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# FlowBinding Activity | ||
|
||
This module provides bindings for the **AndroidX Activity** library. | ||
|
||
## Transitive Dependency | ||
|
||
`androidx.activity:activity-ktx` | ||
|
||
## Download | ||
|
||
```groovy | ||
implementation "io.github.reactivecircus.flowbinding:flowbinding-activity:${flowbinding_version}" | ||
``` | ||
|
||
## Available Bindings | ||
|
||
```kotlin | ||
fun OnBackPressedDispatcher.backPresses(enabled: Boolean): Flow<Unit> | ||
``` |
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.activity.test' | ||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation project(':flowbinding-common') | ||
|
||
implementation "androidx.activity:activity-ktx:${versions.androidx.activity}" | ||
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:${versions.kotlinx.coroutines}" | ||
|
||
lintChecks project(":lint-rules") | ||
|
||
androidTestImplementation project(':testing-infra') | ||
androidTestImplementation project(':flowbinding-activity: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.activity:activity-ktx:${versions.androidx.activity}" | ||
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.activity.fixtures" /> |
5 changes: 5 additions & 0 deletions
5
...y/fixtures/src/main/java/reactivecircus/flowbinding/activity/fixtures/ActivityFragment.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.activity.fixtures | ||
|
||
import androidx.fragment.app.Fragment | ||
|
||
class ActivityFragment : Fragment(R.layout.fragment_activity) |
4 changes: 4 additions & 0 deletions
4
flowbinding-activity/fixtures/src/main/res/layout/fragment_activity.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-activity | ||
POM_NAME=FlowBinding Activity | ||
POM_DESCRIPTION=Kotlin Flow binding APIs for AndroidX Activity | ||
POM_PACKAGING=aar |
31 changes: 31 additions & 0 deletions
31
...st/java/reactivecircus/flowbinding/activity/OnBackPressedDispatcherBackPressedFlowTest.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,31 @@ | ||
package reactivecircus.flowbinding.activity | ||
|
||
import androidx.test.filters.LargeTest | ||
import org.amshove.kluent.shouldEqual | ||
import org.junit.Test | ||
import reactivecircus.blueprint.testing.action.pressBack | ||
import reactivecircus.flowbinding.activity.fixtures.ActivityFragment | ||
import reactivecircus.flowbinding.testing.FlowRecorder | ||
import reactivecircus.flowbinding.testing.launchTest | ||
import reactivecircus.flowbinding.testing.recordWith | ||
|
||
@LargeTest | ||
class OnBackPressedDispatcherBackPressedFlowTest { | ||
|
||
@Test | ||
fun onBackPressedDispatcherBackPresses() { | ||
launchTest<ActivityFragment> { | ||
val recorder = FlowRecorder<Unit>(testScope) | ||
fragment.requireActivity().onBackPressedDispatcher.backPresses(owner = fragment).recordWith(recorder) | ||
|
||
pressBack() | ||
recorder.takeValue() shouldEqual Unit | ||
recorder.assertNoMoreValues() | ||
|
||
cancelTestScope() | ||
|
||
pressBack() | ||
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.activity" /> |
45 changes: 45 additions & 0 deletions
45
...c/main/java/reactivecircus/flowbinding/activity/OnBackPressedDispatcherBackPressedFlow.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.activity | ||
|
||
import androidx.activity.OnBackPressedCallback | ||
import androidx.activity.OnBackPressedDispatcher | ||
import androidx.annotation.CheckResult | ||
import androidx.lifecycle.LifecycleOwner | ||
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 on back pressed events on the [OnBackPressedDispatcher] instance. | ||
* | ||
* @param owner the LifecycleOwner which controls when the callback should be invoked. | ||
* | ||
* Note: Created flow keeps a strong reference to the [OnBackPressedDispatcher] instance | ||
* until the coroutine that launched the flow collector is cancelled. | ||
* | ||
* Example of usage: | ||
* | ||
* ``` | ||
* onBackPressedDispatcher.backPresses(lifecycleOwner) | ||
* .onEach { | ||
* // handle back pressed | ||
* } | ||
* .launchIn(uiScope) | ||
* ``` | ||
*/ | ||
@CheckResult | ||
@UseExperimental(ExperimentalCoroutinesApi::class) | ||
fun OnBackPressedDispatcher.backPresses(owner: LifecycleOwner): Flow<Unit> = | ||
callbackFlow<Unit> { | ||
checkMainThread() | ||
val callback = object : OnBackPressedCallback(true) { | ||
override fun handleOnBackPressed() { | ||
safeOffer(Unit) | ||
} | ||
} | ||
addCallback(owner, callback) | ||
awaitClose { callback.remove() } | ||
}.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