🚀 Horizontal gesture sensitivity option

* Additional option for Horizontal gesture (#127) - Sensitivity
This commit is contained in:
Acclorite 2024-12-27 16:44:05 +02:00
parent efadc85d76
commit 96e0068150
13 changed files with 74 additions and 4 deletions

View file

@ -45,6 +45,7 @@ object DataStoreConstants {
val SCREEN_BRIGHTNESS = doublePreferencesKey("screen_brightness")
val HORIZONTAL_GESTURE = stringPreferencesKey("horizontal_gesture")
val HORIZONTAL_GESTURE_SCROLL = doublePreferencesKey("horizontal_gesture_scroll")
val HORIZONTAL_GESTURE_SENSITIVITY = doublePreferencesKey("horizontal_gesture_sensitivity")
val BOTTOM_BAR_PADDING = intPreferencesKey("bottom_bar_padding")
// Browse settings

View file

@ -49,6 +49,7 @@ fun ReaderContent(
verticalPadding: Dp,
horizontalGesture: ReaderHorizontalGesture,
horizontalGestureScroll: Float,
horizontalGestureSensitivity: Dp,
paragraphHeight: Dp,
sidePadding: Dp,
bottomBarPadding: Dp,
@ -123,6 +124,7 @@ fun ReaderContent(
verticalPadding = verticalPadding,
horizontalGesture = horizontalGesture,
horizontalGestureScroll = horizontalGestureScroll,
horizontalGestureSensitivity = horizontalGestureSensitivity,
paragraphHeight = paragraphHeight,
sidePadding = sidePadding,
bottomBarPadding = bottomBarPadding,

View file

@ -15,8 +15,8 @@ import androidx.compose.ui.draw.alpha
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.launch
import ua.acclorite.book_story.domain.reader.ReaderHorizontalGesture
import kotlin.math.abs
@ -27,10 +27,12 @@ fun Modifier.readerHorizontalGesture(
listState: LazyListState,
horizontalGesture: ReaderHorizontalGesture,
horizontalGestureScroll: Float,
horizontalGestureSensitivity: Dp,
isLoading: Boolean
): Modifier {
if (horizontalGesture == ReaderHorizontalGesture.OFF || isLoading) return this
val inverted = rememberUpdatedState(horizontalGesture == ReaderHorizontalGesture.INVERSE)
val sensitivity = rememberUpdatedState(horizontalGestureSensitivity)
val scope = rememberCoroutineScope()
val density = LocalDensity.current
@ -61,7 +63,7 @@ fun Modifier.readerHorizontalGesture(
@Composable
fun calculateAlpha(): Float {
return animateFloatAsState(
1f - (abs(with(density) { horizontalOffset.floatValue.toDp() }.value) / 16f)
1f - (abs(with(density) { horizontalOffset.floatValue.toDp() }.value) / sensitivity.value.value)
).value
}
@ -91,12 +93,12 @@ fun Modifier.readerHorizontalGesture(
val horizontalOffsetDp = with(density) { horizontalOffset.floatValue.toDp() }
when {
horizontalOffsetDp > 16.dp -> {
horizontalOffsetDp > sensitivity.value -> {
if (inverted.value) scrollForward()
else scrollBackward()
}
horizontalOffsetDp < (-16).dp -> {
horizontalOffsetDp < -sensitivity.value -> {
if (inverted.value) scrollBackward()
else scrollForward()
}

View file

@ -41,6 +41,7 @@ fun ReaderLayout(
verticalPadding: Dp,
horizontalGesture: ReaderHorizontalGesture,
horizontalGestureScroll: Float,
horizontalGestureSensitivity: Dp,
paragraphHeight: Dp,
sidePadding: Dp,
backgroundColor: Color,
@ -132,6 +133,7 @@ fun ReaderLayout(
listState = listState,
horizontalGesture = horizontalGesture,
horizontalGestureScroll = horizontalGestureScroll,
horizontalGestureSensitivity = horizontalGestureSensitivity,
isLoading = isLoading
),
verticalArrangement = Arrangement.spacedBy(paragraphHeight),

View file

@ -54,6 +54,7 @@ fun ReaderScaffold(
verticalPadding: Dp,
horizontalGesture: ReaderHorizontalGesture,
horizontalGestureScroll: Float,
horizontalGestureSensitivity: Dp,
paragraphHeight: Dp,
sidePadding: Dp,
bottomBarPadding: Dp,
@ -149,6 +150,7 @@ fun ReaderScaffold(
verticalPadding = verticalPadding,
horizontalGesture = horizontalGesture,
horizontalGestureScroll = horizontalGestureScroll,
horizontalGestureSensitivity = horizontalGestureSensitivity,
paragraphHeight = paragraphHeight,
sidePadding = sidePadding,
backgroundColor = backgroundColor,

View file

@ -11,6 +11,7 @@ import ua.acclorite.book_story.R
import ua.acclorite.book_story.presentation.settings.components.SettingsSubcategory
import ua.acclorite.book_story.presentation.settings.reader.reading_mode.components.HorizontalGestureOption
import ua.acclorite.book_story.presentation.settings.reader.reading_mode.components.HorizontalGestureScrollOption
import ua.acclorite.book_story.presentation.settings.reader.reading_mode.components.HorizontalGestureSensitivityOption
fun LazyListScope.ReadingModeSubcategory(
titleColor: @Composable () -> Color = { MaterialTheme.colorScheme.primary },
@ -31,5 +32,9 @@ fun LazyListScope.ReadingModeSubcategory(
item {
HorizontalGestureScrollOption()
}
item {
HorizontalGestureSensitivityOption()
}
}
}

View file

@ -0,0 +1,36 @@
package ua.acclorite.book_story.presentation.settings.reader.reading_mode.components
import androidx.compose.runtime.Composable
import androidx.compose.ui.res.stringResource
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import ua.acclorite.book_story.R
import ua.acclorite.book_story.domain.reader.ReaderHorizontalGesture
import ua.acclorite.book_story.presentation.core.components.settings.SliderWithTitle
import ua.acclorite.book_story.ui.main.MainEvent
import ua.acclorite.book_story.ui.main.MainModel
import ua.acclorite.book_story.ui.theme.ExpandingTransition
@Composable
fun HorizontalGestureSensitivityOption() {
val mainModel = hiltViewModel<MainModel>()
val state = mainModel.state.collectAsStateWithLifecycle()
ExpandingTransition(
visible = when (state.value.horizontalGesture) {
ReaderHorizontalGesture.OFF -> false
else -> true
}
) {
SliderWithTitle(
value = state.value.horizontalGestureSensitivity to "%",
toValue = 100,
title = stringResource(id = R.string.horizontal_gesture_sensitivity_option),
onValueChange = {
mainModel.onEvent(
MainEvent.OnChangeHorizontalGestureSensitivity(it)
)
}
)
}
}

View file

@ -47,5 +47,6 @@ sealed class MainEvent {
data class OnChangeScreenBrightness(val value: Float) : MainEvent()
data class OnChangeHorizontalGesture(val value: String) : MainEvent()
data class OnChangeHorizontalGestureScroll(val value: Float) : MainEvent()
data class OnChangeHorizontalGestureSensitivity(val value: Float) : MainEvent()
data class OnChangeBottomBarPadding(val value: Int) : MainEvent()
}

View file

@ -395,6 +395,14 @@ class MainModel @Inject constructor(
}
)
is MainEvent.OnChangeHorizontalGestureSensitivity -> handleDatastoreUpdate(
key = DataStoreConstants.HORIZONTAL_GESTURE_SENSITIVITY,
value = event.value.toDouble(),
updateState = {
it.copy(horizontalGestureSensitivity = this.toFloat())
}
)
is MainEvent.OnChangeBottomBarPadding -> handleDatastoreUpdate(
key = DataStoreConstants.BOTTOM_BAR_PADDING,
value = event.value,

View file

@ -90,6 +90,7 @@ data class MainState(
ReaderHorizontalGesture.OFF
},
val horizontalGestureScroll: Float = provideDefaultValue { 0.7f },
val horizontalGestureSensitivity: Float = provideDefaultValue { 0.6f },
val bottomBarPadding: Int = provideDefaultValue { 0 },
// Browse Settings
@ -297,6 +298,10 @@ data class MainState(
HORIZONTAL_GESTURE_SCROLL, convert = { toFloat() }
) { horizontalGestureScroll },
horizontalGestureSensitivity = provideValue(
HORIZONTAL_GESTURE_SENSITIVITY, convert = { toFloat() }
) { horizontalGestureSensitivity },
bottomBarPadding = provideValue(
BOTTOM_BAR_PADDING
) { bottomBarPadding },

View file

@ -161,6 +161,9 @@ data class ReaderScreen(val bookId: Int) : Screen, Parcelable {
) {
(mainState.value.perceptionExpanderThickness * 0.25f).dp
}
val horizontalGestureSensitivity = remember(mainState.value.horizontalGestureSensitivity) {
(36f + mainState.value.horizontalGestureSensitivity * (4f - 36f)).dp
}
val density = LocalDensity.current
val layoutDirection = LocalLayoutDirection.current
@ -321,6 +324,7 @@ data class ReaderScreen(val bookId: Int) : Screen, Parcelable {
verticalPadding = verticalPadding,
horizontalGesture = mainState.value.horizontalGesture,
horizontalGestureScroll = mainState.value.horizontalGestureScroll,
horizontalGestureSensitivity = horizontalGestureSensitivity,
paragraphHeight = paragraphHeight,
sidePadding = sidePadding,
bottomBarPadding = bottomBarPadding,

View file

@ -267,6 +267,7 @@
<string name="screen_brightness_option">Яскравість</string>
<string name="horizontal_gesture_option">Горизонтальний жест</string>
<string name="horizontal_gesture_scroll_option">Частка прокрутки</string>
<string name="horizontal_gesture_sensitivity_option">Чутливість</string>
<string name="bottom_bar_padding_option">Відступ нижньої панелі</string>
<!-- Browse -->

View file

@ -268,6 +268,7 @@
<string name="screen_brightness_option">Brightness</string>
<string name="horizontal_gesture_option">Horizontal gesture</string>
<string name="horizontal_gesture_scroll_option">Scroll fraction</string>
<string name="horizontal_gesture_sensitivity_option">Sensitivity</string>
<string name="bottom_bar_padding_option">Bottom bar margin</string>
<!-- Browse -->