Skip to content

Commit

Permalink
Fix focus search for reverse grids
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensousa committed Jun 15, 2024
1 parent a5f8065 commit 214b2e7
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2022 Rúben Sousa
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.rubensousa.dpadrecyclerview.test.tests.focus

import androidx.recyclerview.widget.RecyclerView
import com.rubensousa.dpadrecyclerview.ChildAlignment
import com.rubensousa.dpadrecyclerview.ParentAlignment
import com.rubensousa.dpadrecyclerview.test.TestLayoutConfiguration
import com.rubensousa.dpadrecyclerview.test.helpers.assertFocusAndSelection
import com.rubensousa.dpadrecyclerview.test.tests.DpadRecyclerViewTest
import com.rubensousa.dpadrecyclerview.testing.KeyEvents
import com.rubensousa.dpadrecyclerview.testing.rules.DisableIdleTimeoutRule
import org.junit.Rule
import org.junit.Test

class ReverseGridFocusTest : DpadRecyclerViewTest() {

@get:Rule
val idleTimeoutRule = DisableIdleTimeoutRule()

private val spanCount = 5

override fun getDefaultLayoutConfiguration(): TestLayoutConfiguration {
return TestLayoutConfiguration(
spans = spanCount,
orientation = RecyclerView.VERTICAL,
parentAlignment = ParentAlignment(
edge = ParentAlignment.Edge.MIN_MAX
),
reverseLayout = true,
childAlignment = ChildAlignment(offset = 0)
)
}

@Test
fun testFocusStartOfRow() {
// given
launchFragment()

// when
KeyEvents.pressLeft(times = spanCount)

// then
assertFocusAndSelection(spanCount - 1)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ internal class FocusDispatcher(
}
}

fun onInterceptFocusSearch(recyclerView: DpadRecyclerView?, focused: View, direction: Int): View? {
fun onInterceptFocusSearch(
recyclerView: DpadRecyclerView?,
focused: View,
direction: Int
): View? {
val currentRecyclerView = recyclerView ?: return focused

if (!isFocusSearchEnabled(currentRecyclerView)) {
Expand Down Expand Up @@ -434,7 +438,11 @@ internal class FocusDispatcher(
if (movement == FocusDirection.NEXT_COLUMN || movement == FocusDirection.PREVIOUS_COLUMN) {
return focusNextSpanColumn(
focusedPosition = focusedPosition,
next = movement == FocusDirection.NEXT_COLUMN,
next = if (!layoutInfo.shouldReverseLayout()) {
movement == FocusDirection.NEXT_COLUMN
} else {
movement == FocusDirection.PREVIOUS_COLUMN
},
views = views,
direction = direction,
focusableMode = focusableMode
Expand Down Expand Up @@ -477,11 +485,7 @@ internal class FocusDispatcher(
direction: Int,
focusableMode: Int
): Boolean {
val positionIncrement = if (next xor layoutInfo.shouldReverseLayout()) {
1
} else {
-1
}
val positionIncrement = layoutInfo.getPositionIncrement(next)
val nextPosition = focusedPosition + positionIncrement
if (nextPosition < 0 || nextPosition >= layout.itemCount) {
return false
Expand Down

0 comments on commit 214b2e7

Please sign in to comment.