Skip to content

Commit

Permalink
Making haptic feedback use KEYBOARD_TAP constant. Fixes #862 (#863)
Browse files Browse the repository at this point in the history
  • Loading branch information
dessalines authored Apr 25, 2024
1 parent c77c1c5 commit c51d770
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.dessalines.thumbkey.ui.components.keyboard
import android.content.Context
import android.media.AudioManager
import android.view.HapticFeedbackConstants
import android.view.KeyEvent
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.EnterTransition
Expand Down Expand Up @@ -40,10 +41,9 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalHapticFeedback
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
Expand Down Expand Up @@ -161,13 +161,15 @@ fun KeyboardKey(

val keyBorderColour = MaterialTheme.colorScheme.outline
val keySize = (keyHeight + keyWidth) / 2.0
val haptic = LocalHapticFeedback.current
val view = LocalView.current
val audioManager = ctx.getSystemService(Context.AUDIO_SERVICE) as AudioManager

LaunchedEffect(key1 = isPressed) {
if (isPressed) {
if (vibrateOnTap) {
haptic.performHapticFeedback(HapticFeedbackType.LongPress)
// This is a workaround for only having LongPress
// https://stackoverflow.com/questions/68333741/how-to-perform-a-haptic-feedback-in-jetpack-compose
view.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP)
}
if (soundOnTap) {
audioManager.playSoundEffect(AudioManager.FX_KEY_CLICK, .1f)
Expand Down Expand Up @@ -243,7 +245,7 @@ fun KeyboardKey(
)
doneKeyAction(scope, action, isDragged, releasedKey, animationHelperSpeed)
if (vibrateOnTap) {
haptic.performHapticFeedback(HapticFeedbackType.LongPress)
view.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP)
}
if (soundOnTap) {
audioManager.playSoundEffect(AudioManager.FX_KEY_CLICK, .1f)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.dessalines.thumbkey.ui.components.keyboard
import android.content.Context
import android.media.AudioManager
import android.util.Log
import android.view.HapticFeedbackConstants
import android.view.inputmethod.InputConnection.CURSOR_UPDATE_MONITOR
import androidx.compose.foundation.background
import androidx.compose.foundation.border
Expand All @@ -22,9 +23,8 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalHapticFeedback
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
Expand Down Expand Up @@ -210,15 +210,15 @@ fun KeyboardScreen(
)
.background(MaterialTheme.colorScheme.surface),
) {
val haptic = LocalHapticFeedback.current
val view = LocalView.current
val audioManager = ctx.getSystemService(Context.AUDIO_SERVICE) as AudioManager
AndroidView(
// Write the emoji to our text box when we tap one.
factory = { context ->
val emojiPicker = EmojiPickerView(context)
emojiPicker.setOnEmojiPickedListener {
if (vibrateOnTap) {
haptic.performHapticFeedback(HapticFeedbackType.LongPress)
view.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP)
}
if (soundOnTap) {
audioManager.playSoundEffect(AudioManager.FX_KEY_CLICK, .1f)
Expand Down

0 comments on commit c51d770

Please sign in to comment.