Skip to content

Commit

Permalink
Merge pull request #163 from rubensousa/span_count_xml
Browse files Browse the repository at this point in the history
Get spanCount from layout configuration to avoid different values
  • Loading branch information
rubensousa authored Sep 15, 2023
2 parents 30b5b26 + 828b969 commit 802487a
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 22 deletions.
8 changes: 8 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

## Version 1.1.0

### 1.1.0-beta02

2023-09-15

#### Bug fixes

- Fixed wrong scrolling behavior when the `app:spanCount` attribute is used for grids ([#162](https://github.com/rubensousa/DpadRecyclerView/issues/162))

### 1.1.0-beta01

2023-09-10
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.rubensousa.dpadrecyclerview.layoutmanager

import androidx.recyclerview.widget.RecyclerView.LayoutManager.Properties
import com.google.common.truth.Truth.assertThat
import org.junit.Test

class PivotLayoutManagerTest {

@Test
fun testDefaultSpanCountIsSetThroughConstructor() {
val properties = Properties()
properties.spanCount = 5
val pivotLayoutManager = PivotLayoutManager(properties)
assertThat(pivotLayoutManager.getSpanCount()).isEqualTo(properties.spanCount)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import android.os.Parcelable
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import androidx.annotation.VisibleForTesting
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat
import androidx.recyclerview.widget.RecyclerView
import com.rubensousa.dpadrecyclerview.ChildAlignment
Expand Down Expand Up @@ -53,7 +54,7 @@ class PivotLayoutManager(properties: Properties) : RecyclerView.LayoutManager()
private val layoutInfo = LayoutInfo(this, configuration)
private val pivotSelector = PivotSelector(this, layoutInfo)
private val layoutAlignment = LayoutAlignment(this, layoutInfo)
private val spanFocusFinder = SpanFocusFinder()
private val spanFocusFinder = SpanFocusFinder(configuration)
private val scroller = LayoutScroller(
this, layoutInfo, layoutAlignment, configuration, pivotSelector, spanFocusFinder
)
Expand Down Expand Up @@ -396,7 +397,7 @@ class PivotLayoutManager(properties: Properties) : RecyclerView.LayoutManager()
fun setSpanCount(spanCount: Int) {
if (configuration.spanCount != spanCount) {
configuration.setSpanCount(spanCount)
spanFocusFinder.setSpanCount(spanCount)
spanFocusFinder.clearSpanCache()
pivotLayout.updateStructure()
requestLayout()
}
Expand All @@ -407,7 +408,7 @@ class PivotLayoutManager(properties: Properties) : RecyclerView.LayoutManager()
fun setSpanSizeLookup(spanSizeLookup: DpadSpanSizeLookup) {
if (spanSizeLookup !== configuration.spanSizeLookup) {
configuration.setSpanSizeLookup(spanSizeLookup)
spanFocusFinder.setSpanCount(configuration.spanCount)
spanFocusFinder.clearSpanCache()
requestLayout()
}
}
Expand Down Expand Up @@ -570,4 +571,9 @@ class PivotLayoutManager(properties: Properties) : RecyclerView.LayoutManager()
}
}

@VisibleForTesting
internal fun getFocusFinderSpanCount(): Int {
return spanFocusFinder.spanCount
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,20 @@ package com.rubensousa.dpadrecyclerview.layoutmanager.focus

import androidx.recyclerview.widget.RecyclerView
import com.rubensousa.dpadrecyclerview.DpadSpanSizeLookup
import com.rubensousa.dpadrecyclerview.layoutmanager.LayoutConfiguration

/**
* Holds information about the previous focused spanIndex on all spanGroups
*/
internal class SpanFocusFinder {
internal class SpanFocusFinder(private val configuration: LayoutConfiguration) {

val spanCount: Int
get() = configuration.spanCount

private var spanCount = 1
private var cachedSpanIndex = RecyclerView.NO_POSITION
private var cachedSpanSize = 1

fun setSpanCount(newSpanCount: Int) {
spanCount = newSpanCount
fun clearSpanCache() {
cachedSpanIndex = RecyclerView.NO_POSITION
cachedSpanSize = 1
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,39 +19,44 @@ package com.rubensousa.dpadrecyclerview.test.layoutmanager.focus
import androidx.recyclerview.widget.RecyclerView
import com.google.common.truth.Truth.assertThat
import com.rubensousa.dpadrecyclerview.DpadSpanSizeLookup
import com.rubensousa.dpadrecyclerview.layoutmanager.LayoutConfiguration
import com.rubensousa.dpadrecyclerview.layoutmanager.focus.SpanFocusFinder
import org.junit.Before
import org.junit.Test

class SpanFocusFinderTest {

private val itemCount = 1000
private var spanCount = 5
private val finder = SpanFocusFinder()
private val configuration = LayoutConfiguration(RecyclerView.LayoutManager.Properties().apply {
spanCount = 5
})
private val spanCount: Int
get() = configuration.spanCount
private val finder = SpanFocusFinder(configuration)
private val headerPosition = 0
private val headerSpanSizeLookup = object : DpadSpanSizeLookup() {
override fun getSpanSize(position: Int): Int {
return if (position == headerPosition) {
spanCount
configuration.spanCount
} else {
1
}
}
}
private val multipleHeadersLookup = object : DpadSpanSizeLookup() {
override fun getSpanSize(position: Int): Int {
return if (position.rem(spanCount + 1) == 0 || position == 0) {
spanCount
return if (position.rem(configuration.spanCount + 1) == 0 || position == 0) {
configuration.spanCount
} else {
1
}
}
}
private val secondHeaderPosition = spanCount + 1
private val secondHeaderPosition = configuration.spanCount + 1

@Before
fun setup() {
finder.setSpanCount(newSpanCount = spanCount)
finder.clearSpanCache()
}

@Test
Expand Down Expand Up @@ -357,10 +362,10 @@ class SpanFocusFinderTest {

@Test
fun `finding next position does not query span size out of bounds`() {
spanCount = 3
finder.setSpanCount(spanCount)
configuration.setSpanCount(3)
finder.clearSpanCache()
val spanSizeQueries = mutableSetOf<Int>()
val spanSizeLookup = object: DpadSpanSizeLookup() {
val spanSizeLookup = object : DpadSpanSizeLookup() {
override fun getSpanSize(position: Int): Int {
spanSizeQueries.add(position)
return 1
Expand All @@ -383,10 +388,10 @@ class SpanFocusFinderTest {

@Test
fun `finding previous position does not query span size out of bounds`() {
spanCount = 3
finder.setSpanCount(spanCount)
configuration.setSpanCount(3)
finder.clearSpanCache()
val spanSizeQueries = mutableSetOf<Int>()
val spanSizeLookup = object: DpadSpanSizeLookup() {
val spanSizeLookup = object : DpadSpanSizeLookup() {
override fun getSpanSize(position: Int): Int {
spanSizeQueries.add(position)
return 1
Expand All @@ -407,5 +412,10 @@ class SpanFocusFinderTest {
assertThat(spanSizeQueries).contains(0)
}

@Test
fun `span count is derived from the layout configuration`() {
configuration.setSpanCount(10)
assertThat(finder.spanCount).isEqualTo(10)
}

}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ kotlin.code.style=official
# thereby reducing the size of the R class for that library
android.nonTransitiveRClass=true
android.enableR8.fullMode=true
LIBRARY_VERSION=1.1.0-beta01
LIBRARY_VERSION=1.1.0-beta02
2 changes: 1 addition & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ theme:

extra:
dpadrecyclerview:
version: '1.1.0-alpha03'
version: '1.1.0-beta02'
social:
- icon: 'fontawesome/brands/github'
link: 'https://github.com/rubensousa/DpadRecyclerView'
Expand Down

0 comments on commit 802487a

Please sign in to comment.