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
896c220
commit ad35629
Showing
19 changed files
with
541 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
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,21 @@ | ||
# FlowBinding ViewPager | ||
|
||
This module provides bindings for the **AndroidX ViewPager** library. | ||
|
||
## Transitive Dependency | ||
|
||
`androidx.viewpager:viewpager` | ||
|
||
## Download | ||
|
||
```groovy | ||
implementation "io.github.reactivecircus.flowbinding:flowbinding-viewpager:${flowbinding_version}" | ||
``` | ||
|
||
## Available Bindings | ||
|
||
```kotlin | ||
fun ViewPager.pageScrollEvents(): Flow<ViewPagerPageScrollEvent> | ||
fun ViewPager.pageScrollStateChanges(): Flow<Int> | ||
fun ViewPager.pageSelections(emitImmediately: Boolean = false): Flow<Int> | ||
``` |
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,21 @@ | ||
public final class reactivecircus/flowbinding/viewpager/ViewPagerPageScrollEvent { | ||
public fun <init> (Landroidx/viewpager/widget/ViewPager;IFI)V | ||
public final fun getPosition ()I | ||
public final fun getPositionOffset ()F | ||
public final fun getPositionOffsetPixel ()I | ||
public final fun getView ()Landroidx/viewpager/widget/ViewPager; | ||
} | ||
|
||
public final class reactivecircus/flowbinding/viewpager/ViewPagerPageScrollStateChangedFlowKt { | ||
public static final fun pageScrollStateChanges (Landroidx/viewpager/widget/ViewPager;)Lkotlinx/coroutines/flow/Flow; | ||
} | ||
|
||
public final class reactivecircus/flowbinding/viewpager/ViewPagerPageScrolledFlowKt { | ||
public static final fun pageScrollEvents (Landroidx/viewpager/widget/ViewPager;)Lkotlinx/coroutines/flow/Flow; | ||
} | ||
|
||
public final class reactivecircus/flowbinding/viewpager/ViewPagerPageSelectedFlowKt { | ||
public static final fun pageSelections (Landroidx/viewpager/widget/ViewPager;Z)Lkotlinx/coroutines/flow/Flow; | ||
public static synthetic fun pageSelections$default (Landroidx/viewpager/widget/ViewPager;ZILjava/lang/Object;)Lkotlinx/coroutines/flow/Flow; | ||
} | ||
|
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,35 @@ | ||
plugins { | ||
id 'flowbinding-plugin' | ||
id 'com.android.library' | ||
id 'kotlin-android' | ||
id 'com.vanniktech.maven.publish' | ||
id 'io.github.reactivecircus.firestorm' | ||
id 'org.jetbrains.dokka' | ||
} | ||
|
||
afterEvaluate { project -> | ||
project.tasks.dokka { | ||
outputDirectory = "$rootDir/docs/api" | ||
outputFormat = 'gfm' | ||
} | ||
} | ||
|
||
android { | ||
defaultConfig { | ||
testApplicationId 'reactivecircus.flowbinding.viewpager.test' | ||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation project(':flowbinding-common') | ||
|
||
implementation "androidx.viewpager:viewpager:${versions.androidx.viewPager}" | ||
implementation "androidx.fragment:fragment:${versions.androidx.fragment}" | ||
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:${versions.kotlinx.coroutines}" | ||
|
||
lintChecks project(":lint-rules") | ||
|
||
androidTestImplementation project(':testing-infra') | ||
androidTestImplementation project(':flowbinding-viewpager: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.viewpager:viewpager:${versions.androidx.viewPager}" | ||
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.viewpager.fixtures" /> |
41 changes: 41 additions & 0 deletions
41
...fixtures/src/main/java/reactivecircus/flowbinding/viewpager/fixtures/ViewPagerFragment.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,41 @@ | ||
@file:Suppress("DEPRECATION") | ||
|
||
package reactivecircus.flowbinding.viewpager.fixtures | ||
|
||
import android.os.Bundle | ||
import android.view.View | ||
import androidx.fragment.app.Fragment | ||
import androidx.fragment.app.FragmentManager | ||
import androidx.fragment.app.FragmentStatePagerAdapter | ||
import reactivecircus.flowbinding.viewpager.fixtures.databinding.FragmentPageItemBinding | ||
import reactivecircus.flowbinding.viewpager.fixtures.databinding.FragmentViewpagerBinding | ||
|
||
class ViewPagerFragment : Fragment(R.layout.fragment_viewpager) { | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
super.onViewCreated(view, savedInstanceState) | ||
val binding = FragmentViewpagerBinding.bind(view) | ||
binding.viewPager.adapter = PagerAdapter(parentFragmentManager) | ||
} | ||
} | ||
|
||
private val pages = listOf("1", "2", "3") | ||
|
||
class PagerAdapter( | ||
fragmentManager: FragmentManager | ||
) : FragmentStatePagerAdapter(fragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) { | ||
override fun getCount(): Int = pages.size | ||
|
||
override fun getItem(position: Int): Fragment = PageItemFragment( | ||
pages[position] | ||
) | ||
} | ||
|
||
class PageItemFragment(private val pageTitle: String) : Fragment(R.layout.fragment_page_item) { | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
super.onViewCreated(view, savedInstanceState) | ||
val binding = FragmentPageItemBinding.bind(view) | ||
binding.pageTitleTextView.text = pageTitle | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
flowbinding-viewpager/fixtures/src/main/res/layout/fragment_page_item.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,12 @@ | ||
<?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"> | ||
|
||
<TextView | ||
android:id="@+id/pageTitleTextView" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_gravity="center" /> | ||
|
||
</FrameLayout> |
5 changes: 5 additions & 0 deletions
5
flowbinding-viewpager/fixtures/src/main/res/layout/fragment_viewpager.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,5 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.viewpager.widget.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:id="@+id/viewPager" | ||
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-viewpager | ||
POM_NAME=FlowBinding ViewPager | ||
POM_DESCRIPTION=Kotlin Flow binding APIs for AndroidX ViewPager | ||
POM_PACKAGING=aar |
61 changes: 61 additions & 0 deletions
61
...Test/java/reactivecircus/flowbinding/viewpager/ViewPagerPageScrollStateChangedFlowTest.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,61 @@ | ||
package reactivecircus.flowbinding.viewpager | ||
|
||
import androidx.test.filters.LargeTest | ||
import androidx.test.internal.runner.junit4.statement.UiThreadStatement.runOnUiThread | ||
import androidx.viewpager.widget.ViewPager | ||
import org.amshove.kluent.shouldEqual | ||
import org.junit.Test | ||
import reactivecircus.blueprint.testing.action.swipeLeftOnView | ||
import reactivecircus.blueprint.testing.action.swipeRightOnView | ||
import reactivecircus.flowbinding.testing.FlowRecorder | ||
import reactivecircus.flowbinding.testing.launchTest | ||
import reactivecircus.flowbinding.testing.recordWith | ||
import reactivecircus.flowbinding.viewpager.fixtures.ViewPagerFragment | ||
import reactivecircus.flowbinding.viewpager.test.R | ||
|
||
@LargeTest | ||
class ViewPagerPageScrollStateChangedFlowTest { | ||
|
||
@Test | ||
fun pageScrollStateChanges_swipe() { | ||
launchTest<ViewPagerFragment> { | ||
val recorder = FlowRecorder<Int>(testScope) | ||
getViewById<ViewPager>(R.id.viewPager).pageScrollStateChanges().recordWith(recorder) | ||
|
||
recorder.assertNoMoreValues() | ||
|
||
swipeLeftOnView(R.id.viewPager) | ||
recorder.takeValue() shouldEqual ViewPager.SCROLL_STATE_DRAGGING | ||
recorder.takeValue() shouldEqual ViewPager.SCROLL_STATE_SETTLING | ||
recorder.takeValue() shouldEqual ViewPager.SCROLL_STATE_IDLE | ||
recorder.assertNoMoreValues() | ||
|
||
cancelTestScope() | ||
|
||
swipeRightOnView(R.id.viewPager) | ||
recorder.assertNoMoreValues() | ||
} | ||
} | ||
|
||
@Test | ||
fun pageScrollStateChanges_programmatic() { | ||
launchTest<ViewPagerFragment> { | ||
val recorder = FlowRecorder<Int>(testScope) | ||
val viewPager = getViewById<ViewPager>(R.id.viewPager) | ||
viewPager.pageScrollStateChanges().recordWith(recorder) | ||
|
||
recorder.assertNoMoreValues() | ||
|
||
runOnUiThread { viewPager.currentItem = 1 } | ||
// SCROLL_STATE_DRAGGING state is not emitted for programmatic page change | ||
recorder.takeValue() shouldEqual ViewPager.SCROLL_STATE_SETTLING | ||
recorder.takeValue() shouldEqual ViewPager.SCROLL_STATE_IDLE | ||
recorder.assertNoMoreValues() | ||
|
||
cancelTestScope() | ||
|
||
runOnUiThread { viewPager.currentItem = 0 } | ||
recorder.assertNoMoreValues() | ||
} | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
...rc/androidTest/java/reactivecircus/flowbinding/viewpager/ViewPagerPageScrolledFlowTest.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,70 @@ | ||
package reactivecircus.flowbinding.viewpager | ||
|
||
import androidx.test.filters.LargeTest | ||
import androidx.test.internal.runner.junit4.statement.UiThreadStatement.runOnUiThread | ||
import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation | ||
import androidx.viewpager.widget.ViewPager | ||
import org.amshove.kluent.shouldBeGreaterThan | ||
import org.amshove.kluent.shouldEqual | ||
import org.junit.Test | ||
import reactivecircus.blueprint.testing.action.swipeLeftOnView | ||
import reactivecircus.blueprint.testing.action.swipeRightOnView | ||
import reactivecircus.flowbinding.testing.FlowRecorder | ||
import reactivecircus.flowbinding.testing.launchTest | ||
import reactivecircus.flowbinding.testing.recordWith | ||
import reactivecircus.flowbinding.viewpager.fixtures.ViewPagerFragment | ||
import reactivecircus.flowbinding.viewpager.test.R | ||
|
||
@LargeTest | ||
class ViewPagerPageScrolledFlowTest { | ||
|
||
@Test | ||
fun pageScrollEvents_swipe() { | ||
launchTest<ViewPagerFragment> { | ||
val recorder = FlowRecorder<ViewPagerPageScrollEvent>(testScope) | ||
val viewPager = getViewById<ViewPager>(R.id.viewPager) | ||
viewPager.pageScrollEvents().recordWith(recorder) | ||
|
||
recorder.assertNoMoreValues() | ||
|
||
swipeLeftOnView(R.id.viewPager) | ||
val event = recorder.takeValue() | ||
event.view shouldEqual viewPager | ||
event.position shouldEqual 0 | ||
event.positionOffset shouldBeGreaterThan 0f | ||
event.positionOffsetPixel shouldBeGreaterThan 0 | ||
|
||
cancelTestScope() | ||
recorder.clearValues() | ||
|
||
swipeRightOnView(R.id.viewPager) | ||
recorder.assertNoMoreValues() | ||
} | ||
} | ||
|
||
@Test | ||
fun pageScrollEvents_programmatic() { | ||
launchTest<ViewPagerFragment> { | ||
val recorder = FlowRecorder<ViewPagerPageScrollEvent>(testScope) | ||
val viewPager = getViewById<ViewPager>(R.id.viewPager) | ||
viewPager.pageScrollEvents().recordWith(recorder) | ||
|
||
recorder.assertNoMoreValues() | ||
|
||
runOnUiThread { viewPager.currentItem = 1 } | ||
getInstrumentation().waitForIdleSync() | ||
val event = recorder.takeValue() | ||
event.view shouldEqual viewPager | ||
event.position shouldEqual 0 | ||
event.positionOffset shouldBeGreaterThan 0f | ||
event.positionOffsetPixel shouldBeGreaterThan 0 | ||
|
||
cancelTestScope() | ||
recorder.clearValues() | ||
|
||
runOnUiThread { viewPager.currentItem = 0 } | ||
getInstrumentation().waitForIdleSync() | ||
recorder.assertNoMoreValues() | ||
} | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
...rc/androidTest/java/reactivecircus/flowbinding/viewpager/ViewPagerPageSelectedFlowTest.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,78 @@ | ||
package reactivecircus.flowbinding.viewpager | ||
|
||
import androidx.test.filters.LargeTest | ||
import androidx.test.internal.runner.junit4.statement.UiThreadStatement.runOnUiThread | ||
import androidx.viewpager.widget.ViewPager | ||
import org.amshove.kluent.shouldEqual | ||
import org.junit.Test | ||
import reactivecircus.blueprint.testing.action.swipeLeftOnView | ||
import reactivecircus.blueprint.testing.action.swipeRightOnView | ||
import reactivecircus.flowbinding.testing.FlowRecorder | ||
import reactivecircus.flowbinding.testing.launchTest | ||
import reactivecircus.flowbinding.testing.recordWith | ||
import reactivecircus.flowbinding.viewpager.fixtures.ViewPagerFragment | ||
import reactivecircus.flowbinding.viewpager.test.R | ||
|
||
@LargeTest | ||
class ViewPagerPageSelectedFlowTest { | ||
|
||
@Test | ||
fun pageSelections_swipe() { | ||
launchTest<ViewPagerFragment> { | ||
val recorder = FlowRecorder<Int>(testScope) | ||
getViewById<ViewPager>(R.id.viewPager).pageSelections().recordWith(recorder) | ||
|
||
recorder.assertNoMoreValues() | ||
|
||
swipeLeftOnView(R.id.viewPager) | ||
recorder.takeValue() shouldEqual 1 | ||
recorder.assertNoMoreValues() | ||
|
||
cancelTestScope() | ||
|
||
swipeRightOnView(R.id.viewPager) | ||
recorder.assertNoMoreValues() | ||
} | ||
} | ||
|
||
@Test | ||
fun pageSelections_programmatic() { | ||
launchTest<ViewPagerFragment> { | ||
val recorder = FlowRecorder<Int>(testScope) | ||
val viewPager = getViewById<ViewPager>(R.id.viewPager) | ||
viewPager.pageSelections().recordWith(recorder) | ||
|
||
recorder.assertNoMoreValues() | ||
|
||
runOnUiThread { viewPager.currentItem = 1 } | ||
recorder.takeValue() shouldEqual 1 | ||
recorder.assertNoMoreValues() | ||
|
||
cancelTestScope() | ||
|
||
runOnUiThread { viewPager.currentItem = 0 } | ||
recorder.assertNoMoreValues() | ||
} | ||
} | ||
|
||
@Test | ||
fun pageSelections_emitImmediately() { | ||
launchTest<ViewPagerFragment> { | ||
val recorder = FlowRecorder<Int>(testScope) | ||
val viewPager = getViewById<ViewPager>(R.id.viewPager) | ||
viewPager.pageSelections(emitImmediately = true).recordWith(recorder) | ||
|
||
recorder.takeValue() shouldEqual 0 | ||
recorder.assertNoMoreValues() | ||
|
||
runOnUiThread { viewPager.currentItem = 1 } | ||
recorder.takeValue() shouldEqual 1 | ||
recorder.assertNoMoreValues() | ||
|
||
cancelTestScope() | ||
|
||
runOnUiThread { viewPager.currentItem = 0 } | ||
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.viewpager" /> |
Oops, something went wrong.