Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of RotaryScrollAdapter for AndroidX Picker #1689

Merged
merged 3 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}