ha-android/wear/src/main/java/io/homeassistant/companion/android/views/ThemeLazyColumn.kt

56 lines
2.0 KiB
Kotlin

package io.homeassistant.companion.android.views
import androidx.compose.foundation.focusable
import androidx.compose.foundation.gestures.scrollBy
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.input.rotary.onRotaryScrollEvent
import androidx.compose.ui.unit.dp
import androidx.wear.compose.foundation.lazy.ScalingLazyColumn
import androidx.wear.compose.foundation.lazy.ScalingLazyListScope
import androidx.wear.compose.foundation.lazy.ScalingLazyListState
import androidx.wear.compose.foundation.lazy.rememberScalingLazyListState
import kotlinx.coroutines.launch
@Composable
fun ThemeLazyColumn(
state: ScalingLazyListState = rememberScalingLazyListState(),
content: ScalingLazyListScope.() -> Unit
) {
val coroutineScope = rememberCoroutineScope()
val focusRequester = remember { FocusRequester() }
ScalingLazyColumn(
modifier = Modifier
.fillMaxSize()
.onRotaryScrollEvent {
coroutineScope.launch {
state.scrollBy(it.verticalScrollPixels)
}
true
}
.focusRequester(focusRequester)
.focusable(),
contentPadding = PaddingValues(
start = 8.dp,
end = 8.dp
),
verticalArrangement = Arrangement.spacedBy(4.dp),
horizontalAlignment = Alignment.CenterHorizontally,
state = state,
content = content
)
LaunchedEffect(Unit) {
focusRequester.requestFocus()
}
}