Skip to content

Commit

Permalink
Implementation of RotaryScrollAdapter for AndroidX Picker (#1689)
Browse files Browse the repository at this point in the history
  • Loading branch information
yschimke authored Sep 18, 2023
1 parent 6168817 commit 4b86b8f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
1 change: 1 addition & 0 deletions composables/api/current.api
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ package com.google.android.horologist.composables.picker {
}

public final class PickerRotaryScrollAdapterKt {
method @com.google.android.horologist.annotations.ExperimentalHorologistApi public static com.google.android.horologist.compose.rotaryinput.RotaryScrollAdapter toRotaryScrollAdapter(androidx.wear.compose.material.PickerState);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ internal class PickerState constructor(
scalingLazyListState.animateScrollToItem(getClosestTargetItemIndex(index), 0)
}

public companion object {
internal companion object {
/**
* The default [Saver] implementation for [PickerState].
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ internal class PickerGroupState constructor(
*/
var selectedIndex by mutableIntStateOf(initiallySelectedIndex)

public companion object {
internal companion object {
val Saver = listSaver<PickerGroupState, Any?>(
save = {
listOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.android.horologist.composables.picker

import com.google.android.horologist.annotations.ExperimentalHorologistApi
import com.google.android.horologist.compose.rotaryinput.RotaryScrollAdapter

// TODO(b/294842202): Remove once rotary modifiers are in AndroidX
Expand Down Expand Up @@ -55,3 +56,42 @@ internal class PickerRotaryScrollAdapter(
override fun totalItemsCount(): Int =
scrollableState.scalingLazyListState.layoutInfo.totalItemsCount
}

/**
* Temporary implementation of RotaryScrollAdapter for PickerState
* from AndroidX wear-compose library.
*/
@ExperimentalHorologistApi
public fun androidx.wear.compose.material.PickerState.toRotaryScrollAdapter(): RotaryScrollAdapter =
AndroidxPickerRotaryScrollAdapter(this)

/**
* An implementation of rotary scroll adapter for [Picker]
*/
@Suppress("INVISIBLE_MEMBER")
internal class AndroidxPickerRotaryScrollAdapter(
override val scrollableState: androidx.wear.compose.material.PickerState,
) : RotaryScrollAdapter {

/**
* Returns a height of a first item, as all items in picker have the same height.
*/
override fun averageItemSize(): Float =
scrollableState.scalingLazyListState
.layoutInfo.visibleItemsInfo.first().unadjustedSize.toFloat()

/**
* Current (centred) item index
*/
override fun currentItemIndex(): Int =
scrollableState.scalingLazyListState.centerItemIndex

/**
* An offset from the item centre
*/
override fun currentItemOffset(): Float =
scrollableState.scalingLazyListState.centerItemScrollOffset.toFloat()

override fun totalItemsCount(): Int =
scrollableState.scalingLazyListState.layoutInfo.totalItemsCount
}

0 comments on commit 4b86b8f

Please sign in to comment.