Skip to content

Commit 72fd54e

Browse files
committed
feat: Amend bringIntoView Modifier extension to be compatibel with nay ScrollableState type
1 parent 4c89476 commit 72fd54e

6 files changed

Lines changed: 79 additions & 74 deletions

File tree

patterns/src/main/java/uk/gov/android/ui/patterns/centrealignedscreen/CentreAlignedScreen.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ import uk.gov.android.ui.patterns.centrealignedscreen.CentreAlignedScreenDefault
4646
import uk.gov.android.ui.patterns.centrealignedscreen.CentreAlignedScreenDefaults.NoPadding
4747
import uk.gov.android.ui.patterns.centrealignedscreen.CentreAlignedScreenDefaults.VerticalPadding
4848
import uk.gov.android.ui.patterns.centrealignedscreen.CentreAlignedScreenTestTag.BODY_LAZY_COLUMN_TEST_TAG
49+
import uk.gov.android.ui.patterns.utils.ModifierExtensions.bringIntoView
4950
import uk.gov.android.ui.patterns.utils.clearListSemanticsForTalkBack
50-
import uk.gov.android.ui.patterns.utils.scroll.bringIntoView
5151
import uk.gov.android.ui.theme.m3.GdsTheme
5252
import uk.gov.android.ui.theme.m3.Typography
5353
import uk.gov.android.ui.theme.meta.ExcludeFromJacocoGeneratedReport

patterns/src/main/java/uk/gov/android/ui/patterns/errorscreen/v2/ErrorScreen.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ import uk.gov.android.ui.patterns.errorscreen.v2.ErrorScreenDefaults.HorizontalP
3939
import uk.gov.android.ui.patterns.errorscreen.v2.ErrorScreenDefaults.VerticalPadding
4040
import uk.gov.android.ui.patterns.errorscreen.v2.ErrorScreenTitleTestTag.ERROR_BODY_LAZY_COLUMN_TEST_TAG
4141
import uk.gov.android.ui.patterns.errorscreen.v2.ErrorScreenTitleTestTag.ERROR_SCREEN_TITLE_TEST_TAG
42+
import uk.gov.android.ui.patterns.utils.ModifierExtensions.bringIntoView
4243
import uk.gov.android.ui.patterns.utils.clearListSemanticsForTalkBack
43-
import uk.gov.android.ui.patterns.utils.scroll.bringIntoView
4444
import uk.gov.android.ui.theme.m3.GdsTheme
4545
import uk.gov.android.ui.theme.meta.ExcludeFromJacocoGeneratedReport
4646
import uk.gov.android.ui.theme.spacingDouble

patterns/src/main/java/uk/gov/android/ui/patterns/leftalignedscreen/LeftAlignedScreen.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import uk.gov.android.ui.patterns.leftalignedscreen.LeftAlignedScreenTestTag.BOD
3636
import uk.gov.android.ui.patterns.utils.clearListSemanticsForTalkBack
3737
import uk.gov.android.ui.theme.m3.GdsTheme
3838
import uk.gov.android.ui.theme.spacingDouble
39-
import uk.gov.android.ui.patterns.utils.scroll.bringIntoView as bringIntoViewV2
39+
import uk.gov.android.ui.patterns.utils.ModifierExtensions.bringIntoView as bringIntoViewV2
4040

4141
private const val ONE_THIRD = 1f / 3f
4242
private const val FONT_SCALE_DOUBLE = 2f

patterns/src/main/java/uk/gov/android/ui/patterns/leftalignedscreen/LeftAlignedScreenContentV2.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import uk.gov.android.ui.componentsv2.row.RowList
4141
import uk.gov.android.ui.componentsv2.supportingtext.GdsSupportingText
4242
import uk.gov.android.ui.componentsv2.warning.GdsWarningText
4343
import uk.gov.android.ui.theme.dividerThickness
44-
import uk.gov.android.ui.patterns.utils.scroll.bringIntoView as bringIntoViewV2
44+
import uk.gov.android.ui.patterns.utils.ModifierExtensions.bringIntoView as bringIntoViewV2
4545

4646
internal data class LeftAlignedScreenContentV2(
4747
val title: String,
@@ -345,6 +345,9 @@ private fun LazyListScope.toAnnotatedText(
345345
* @param scrollState [LazyListState] represents the list state
346346
* @return augmented [Modifier]
347347
*/
348-
@Deprecated("Use uk.gov.android.ui.patterns.utils.scroll.bringIntoView")
348+
@Deprecated(
349+
"Use uk.gov.android.ui.patterns.utils.scroll.bringIntoView",
350+
level = DeprecationLevel.WARNING,
351+
)
349352
@Composable
350353
fun Modifier.bringIntoView(scrollState: LazyListState): Modifier = this.bringIntoViewV2(scrollState)
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package uk.gov.android.ui.patterns.utils
2+
3+
import androidx.compose.foundation.ScrollState
4+
import androidx.compose.foundation.focusable
5+
import androidx.compose.foundation.gestures.ScrollableState
6+
import androidx.compose.foundation.gestures.animateScrollBy
7+
import androidx.compose.foundation.interaction.MutableInteractionSource
8+
import androidx.compose.foundation.lazy.LazyListState
9+
import androidx.compose.runtime.Composable
10+
import androidx.compose.runtime.remember
11+
import androidx.compose.runtime.rememberCoroutineScope
12+
import androidx.compose.ui.Modifier
13+
import androidx.compose.ui.focus.FocusRequester
14+
import androidx.compose.ui.focus.focusRequester
15+
import androidx.compose.ui.input.key.Key
16+
import androidx.compose.ui.input.key.KeyEventType
17+
import androidx.compose.ui.input.key.key
18+
import androidx.compose.ui.input.key.onKeyEvent
19+
import androidx.compose.ui.input.key.type
20+
import kotlinx.coroutines.launch
21+
22+
object ModifierExtensions {
23+
/**
24+
* Adds a downwards and upwards scroll when a keyboard down or up arrow is pressed
25+
*
26+
* @param scrollState [ScrollState] represents the list state
27+
* @return augmented [Modifier]
28+
*/
29+
@Composable
30+
fun Modifier.bringIntoView(scrollState: ScrollableState): Modifier {
31+
val coroutineScope = rememberCoroutineScope()
32+
val interactionSource = remember { MutableInteractionSource() }
33+
val focusRequester = remember { FocusRequester() }
34+
return this
35+
.onKeyEvent {
36+
when {
37+
it.type == KeyEventType.KeyDown && it.key == Key.DirectionDown &&
38+
scrollState.canScrollForward -> {
39+
coroutineScope.launch {
40+
scrollState.animateScrollBy(
41+
SCROLL_MULTIPLIER * scrollState.viewportHeight(),
42+
)
43+
}
44+
true
45+
}
46+
47+
it.type == KeyEventType.KeyDown && it.key == Key.DirectionUp &&
48+
scrollState.canScrollBackward -> {
49+
coroutineScope.launch {
50+
scrollState.animateScrollBy(
51+
-SCROLL_MULTIPLIER * scrollState.viewportHeight(),
52+
)
53+
}
54+
true
55+
}
56+
57+
else -> false
58+
}
59+
}
60+
.focusRequester(focusRequester)
61+
.focusable(interactionSource = interactionSource)
62+
}
63+
64+
private fun ScrollableState.viewportHeight(): Float = when (this) {
65+
is LazyListState -> layoutInfo.viewportSize.height.toFloat()
66+
is ScrollState -> viewportSize.toFloat()
67+
else -> error("scrollable type not yet supported")
68+
}
69+
70+
private const val SCROLL_MULTIPLIER = 0.4f
71+
}

patterns/src/main/java/uk/gov/android/ui/patterns/utils/scroll/BringIntoView.kt

Lines changed: 0 additions & 69 deletions
This file was deleted.

0 commit comments

Comments
 (0)