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

Add haptic feedback when seeking between segments #68

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
16 changes: 16 additions & 0 deletions seeker/src/main/java/dev/vivvvek/seeker/Seeker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ import androidx.compose.ui.graphics.drawscope.inset
import androidx.compose.ui.graphics.drawscope.rotateRad
import androidx.compose.ui.graphics.drawscope.translate
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalHapticFeedback
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.semantics.disabled
import androidx.compose.ui.semantics.semantics
Expand Down Expand Up @@ -99,6 +101,7 @@ import kotlin.math.atan2
* The first segment must start form the start value of the [range], and all the segments must lie in
* the specified [range], else an [IllegalArgumentException] will be thrown.
* will be thrown.
* @param segmentChangeHapticFeedback enables haptic feedback when the current segment gets updated
* @param enabled whether or not component is enabled and can be interacted with or not
* @param colors [SeekerColors] that will be used to determine the color of the Slider parts in
* different state. See [SeekerDefaults.seekerColors] to customize.
Expand All @@ -122,6 +125,7 @@ fun Seeker(
onValueChange: (Float) -> Unit,
onValueChangeFinished: (() -> Unit)? = null,
segments: List<Segment> = emptyList(),
segmentChangeHapticFeedback: Boolean = false,
enabled: Boolean = true,
colors: SeekerColors = SeekerDefaults.seekerColors(),
dimensions: SeekerDimensions = SeekerDefaults.seekerDimensions(),
Expand Down Expand Up @@ -165,6 +169,16 @@ fun Seeker(
segmentToPxValues(segments, range, widthPx)
}

var isDragging = false
if (segmentChangeHapticFeedback) {
val haptics = LocalHapticFeedback.current

LaunchedEffect(state.currentSegment) {
if (!isDragging) return@LaunchedEffect
haptics.performHapticFeedback(HapticFeedbackType.LongPress)
}
}

LaunchedEffect(thumbValue, segments) {
state.currentSegment(thumbValue, segments)
}
Expand Down Expand Up @@ -193,6 +207,7 @@ fun Seeker(

LaunchedEffect(widthPx, range) {
state.onDrag = {
isDragging = true
dragPositionX += it + pressOffset

pressOffset = 0f
Expand Down Expand Up @@ -231,6 +246,7 @@ fun Seeker(
reverseDirection = isRtl,
orientation = Orientation.Horizontal,
onDragStopped = {
isDragging = false
onValueChangeFinished?.invoke()
},
interactionSource = interactionSource
Expand Down
Loading