Skip to content

Commit

Permalink
Fix rotary input after first screen & for picker
Browse files Browse the repository at this point in the history
 - The recent switch to Horologist modifiers for rotary input broke after the first screen because of manual focus requesting, and the refresh interval picker in general so also switch that to a Horologist modifier.
  • Loading branch information
jpelgrom committed Sep 20, 2023
1 parent a31cd2f commit 24debf5
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 38 deletions.
4 changes: 3 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ wearInput = "1.2.0-alpha02"
webkit = "1.8.0"
wear-remote-interactions = "1.0.0"
workRuntimeKtx = "2.8.1"
horologist = "0.5.5"
horologist = "0.5.7"

[plugins]
android-application = { id = "com.android.application", version.ref = "androidPlugin" }
Expand Down Expand Up @@ -121,6 +121,7 @@ constraintlayout = { module = "androidx.constraintlayout:constraintlayout", vers
guava = { module = "com.google.guava:guava", version.ref = "guava" }
hilt-android-compiler = { module = "com.google.dagger:hilt-android-compiler", version.ref = "hilt" }
hilt-android = { module = "com.google.dagger:hilt-android", version.ref = "hilt" }
horologist-composables = { module = "com.google.android.horologist:horologist-composables", version.ref = "horologist" }
horologist-layout = { module = "com.google.android.horologist:horologist-compose-layout", version.ref = "horologist" }
iconics-compose = { module = "com.mikepenz:iconics-compose", version.ref = "iconics" }
iconics-core = { module = "com.mikepenz:iconics-core", version.ref = "iconics" }
Expand Down Expand Up @@ -163,5 +164,6 @@ wear-tiles = { module = "androidx.wear.tiles:tiles", version.ref = "wear-tiles"
webkit = { module = "androidx.webkit:webkit", version.ref = "webkit" }

[bundles]
horologist = ["horologist-layout", "horologist-composables"]
media3 = ["media3-exoplayer", "media3-exoplayer-hls", "media3-ui"]
wear-tiles = ["wear-tiles", "wear-protolayout-main", "wear-protolayout-expression", "wear-protolayout-material"]
2 changes: 1 addition & 1 deletion wear/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ dependencies {
implementation(libs.wear.compose.material)
implementation(libs.wear.compose.navigation)

implementation(libs.horologist.layout)
implementation(libs.bundles.horologist)

implementation(libs.guava)
implementation(libs.bundles.wear.tiles)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
package io.homeassistant.companion.android.home.views

import androidx.compose.foundation.focusable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
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.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Devices
Expand All @@ -25,15 +18,17 @@ import androidx.wear.compose.material.ButtonDefaults
import androidx.wear.compose.material.Picker
import androidx.wear.compose.material.Text
import androidx.wear.compose.material.rememberPickerState
import com.google.android.horologist.annotations.ExperimentalHorologistApi
import com.google.android.horologist.composables.picker.toRotaryScrollAdapter
import com.google.android.horologist.compose.rotaryinput.rotaryWithSnap
import com.mikepenz.iconics.compose.Image
import com.mikepenz.iconics.typeface.library.community.material.CommunityMaterial
import io.homeassistant.companion.android.theme.wearColorPalette
import io.homeassistant.companion.android.util.intervalToString
import io.homeassistant.companion.android.views.ListHeader
import kotlinx.coroutines.launch
import kotlin.math.sign
import io.homeassistant.companion.android.common.R as R

@OptIn(ExperimentalHorologistApi::class)
@Composable
fun RefreshIntervalPickerView(
currentInterval: Int,
Expand All @@ -46,8 +41,6 @@ fun RefreshIntervalPickerView(
initiallySelectedOption = if (initialIndex != -1) initialIndex else 0,
repeatItems = true
)
val coroutineScope = rememberCoroutineScope()
val focusRequester = remember { FocusRequester() }

Column(
modifier = Modifier.fillMaxWidth(),
Expand All @@ -60,16 +53,7 @@ fun RefreshIntervalPickerView(
modifier = Modifier
.weight(1f)
.padding(all = 8.dp)
.onRotaryScrollEvent {
coroutineScope.launch {
state.scrollToOption(
state.selectedOption + it.verticalScrollPixels.sign.toInt()
)
}
true
}
.focusRequester(focusRequester)
.focusable()
.rotaryWithSnap(state.toRotaryScrollAdapter())
) {
Text(
intervalToString(LocalContext.current, options[it]),
Expand All @@ -87,10 +71,6 @@ fun RefreshIntervalPickerView(
)
}
}

LaunchedEffect(Unit) {
focusRequester.requestFocus()
}
}

@Preview(device = Devices.WEAR_OS_LARGE_ROUND)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@ 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.unit.dp
import androidx.wear.compose.foundation.lazy.ScalingLazyColumn
import androidx.wear.compose.foundation.lazy.ScalingLazyListScope
Expand All @@ -24,12 +20,10 @@ fun ThemeLazyColumn(
state: ScalingLazyListState = rememberScalingLazyListState(),
content: ScalingLazyListScope.() -> Unit
) {
val coroutineScope = rememberCoroutineScope()
val focusRequester = remember { FocusRequester() }
ScalingLazyColumn(
modifier = Modifier
.fillMaxSize()
.rotaryWithScroll(state, focusRequester),
.rotaryWithScroll(state),
contentPadding = PaddingValues(
start = 8.dp,
end = 8.dp
Expand All @@ -39,8 +33,4 @@ fun ThemeLazyColumn(
state = state,
content = content
)

LaunchedEffect(Unit) {
focusRequester.requestFocus()
}
}

0 comments on commit 24debf5

Please sign in to comment.