Skip to content

Commit

Permalink
Merge pull request #283 from rubensousa/postponing_selection
Browse files Browse the repository at this point in the history
Fix position being reset if new adapter contents are still pending
  • Loading branch information
rubensousa authored Nov 11, 2024
2 parents ef7df35 + 5f91976 commit 470850e
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ jobs:
name: ${{ matrix.api-level }}-${{ matrix.arch }}-instrumentation-test-results
path: |
logs/**
build/outputs/
./**/build/reports/androidTests/connected/**
./**/build/outputs/allure-results
./**/build/outputs/connected_android_test_additional_output/debugAndroidTest/connected/**
if: always()
10 changes: 7 additions & 3 deletions dpadrecyclerview-compose/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ android {
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}

kotlinOptions {
jvmTarget = '1.8'
jvmTarget = '17'
}

}
Expand All @@ -62,3 +62,7 @@ dependencies {
androidTestImplementation libs.androidx.test.compose.ui.junit4
androidTestUtil libs.androidx.test.services
}

allureReport {
outputDir = rootProject.file("build/outputs/allure-results")
}
10 changes: 7 additions & 3 deletions dpadrecyclerview-testing/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ android {
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}

kotlinOptions {
jvmTarget = '1.8'
jvmTarget = '17'
}

testOptions {
Expand All @@ -63,3 +63,7 @@ dependencies {
androidTestImplementation project (':dpadrecyclerview-test-fixtures')
androidTestUtil libs.androidx.test.services
}

allureReport {
outputDir = rootProject.file("build/outputs/allure-results")
}
10 changes: 7 additions & 3 deletions dpadrecyclerview/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ android {
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = '1.8'
jvmTarget = '17'
}
}

Expand All @@ -65,3 +65,7 @@ dependencies {
androidTestImplementation project (':dpadrecyclerview-test-fixtures')
androidTestUtil libs.androidx.test.services
}

allureReport {
outputDir = rootProject.file("build/outputs/allure-results")
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ abstract class AbstractTestAdapter<VH : RecyclerView.ViewHolder>(
notifyItemInserted(items.size - 1)
}

fun addAll(list: List<Int>) {
currentVersion++
val nextIndex = items.size
items.addAll(list)
notifyItemRangeInserted(nextIndex, list.size)
}

override fun getItemCount(): Int = items.size

fun getItem(position: Int) = items[position]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import androidx.test.espresso.Espresso
import androidx.test.espresso.matcher.ViewMatchers
import com.google.common.truth.Truth.assertThat
import com.rubensousa.dpadrecyclerview.ChildAlignment
import com.rubensousa.dpadrecyclerview.DpadRecyclerView
import com.rubensousa.dpadrecyclerview.ParentAlignment
import com.rubensousa.dpadrecyclerview.ParentAlignment.Edge
import com.rubensousa.dpadrecyclerview.test.TestAdapterConfiguration
Expand All @@ -35,6 +36,7 @@ import com.rubensousa.dpadrecyclerview.test.helpers.onRecyclerView
import com.rubensousa.dpadrecyclerview.test.helpers.selectPosition
import com.rubensousa.dpadrecyclerview.test.helpers.waitForCondition
import com.rubensousa.dpadrecyclerview.test.helpers.waitForIdleScrollState
import com.rubensousa.dpadrecyclerview.test.helpers.waitForLayout
import com.rubensousa.dpadrecyclerview.test.tests.DpadRecyclerViewTest
import com.rubensousa.dpadrecyclerview.testfixtures.DpadDeselectionEvent
import com.rubensousa.dpadrecyclerview.testfixtures.DpadSelectionEvent
Expand Down Expand Up @@ -302,4 +304,37 @@ class SelectionTest : DpadRecyclerViewTest() {
assertThat(receivedEvents.first().viewHolder.layoutPosition).isEqualTo(0)
}

@Test
fun testSelectionIsPostponedUntilLayoutExists() = report {
val position = 4
Given("Setup clean layout") {
step("Launch fragment") {
launchFragment()
}
step("Clear layout") {
mutateAdapter { adapter ->
adapter.setList(mutableListOf())
adapter.notifyDataSetChanged()
}
}
step("Wait for layout completed") {
waitForLayout()
}
}
When("Set async adapter content, followed by position update") {
var currentRecyclerView: DpadRecyclerView? = null
onRecyclerView("Get recyclerView") { recyclerView ->
currentRecyclerView = recyclerView
}
mutateAdapter { adapter ->
adapter.addAll(listOf(0, 1, 2, 3, 4, 5))
currentRecyclerView?.setSelectedPosition(position)
}
}

Then("Assert position $position is selected") {
assertSelectedPosition(position)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,14 @@ internal class PivotLayout(

fun onLayoutChildren(recycler: RecyclerView.Recycler, state: RecyclerView.State) {
if (state.itemCount == 0) {
// Clear the selected position if there are no more items
if (pivotSelector.position != RecyclerView.NO_POSITION) {
pivotSelector.setSelectionUpdatePending()
// Clear the selected position if there are no items
// in the layout and the adapter
if (layoutInfo.getItemCount() == 0) {
if (pivotSelector.position != RecyclerView.NO_POSITION) {
pivotSelector.setSelectionUpdatePending()
}
pivotSelector.reset()
}
pivotSelector.reset()
} else if (pivotSelector.position >= state.itemCount) {
pivotSelector.update(newPosition = state.itemCount - 1)
pivotSelector.setSelectionUpdatePending()
Expand Down
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ jacoco = "0.8.11"
test-junit = '4.13.2'
test-truth = '1.4.1'
test-mockk = '1.13.10'
carioca-report = "1.0.0-alpha01"
carioca-allure = "1.0.0-alpha01"
carioca-report = "1.0.0-beta01"
carioca-allure = "1.0.0-beta01"

[plugins]
android-application = { id = "com.android.application", version.ref = "android-gradle-plugin" }
Expand Down
3 changes: 3 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ pluginManagement {
gradlePluginPortal()
google()
mavenCentral()
maven {
url "https://s01.oss.sonatype.org/content/repositories/releases/"
}
}
}
dependencyResolutionManagement {
Expand Down

0 comments on commit 470850e

Please sign in to comment.