From c51d770cfdc5b068e702261bae77b460d1ec6e00 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Thu, 25 Apr 2024 13:00:26 -0400 Subject: [PATCH] Making haptic feedback use KEYBOARD_TAP constant. Fixes #862 (#863) --- .../thumbkey/ui/components/keyboard/KeyboardKey.kt | 12 +++++++----- .../ui/components/keyboard/KeyboardScreen.kt | 8 ++++---- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/dessalines/thumbkey/ui/components/keyboard/KeyboardKey.kt b/app/src/main/java/com/dessalines/thumbkey/ui/components/keyboard/KeyboardKey.kt index da8221d64..1b255b4b4 100644 --- a/app/src/main/java/com/dessalines/thumbkey/ui/components/keyboard/KeyboardKey.kt +++ b/app/src/main/java/com/dessalines/thumbkey/ui/components/keyboard/KeyboardKey.kt @@ -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 @@ -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 @@ -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) @@ -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) diff --git a/app/src/main/java/com/dessalines/thumbkey/ui/components/keyboard/KeyboardScreen.kt b/app/src/main/java/com/dessalines/thumbkey/ui/components/keyboard/KeyboardScreen.kt index 4a1371781..ae099c92c 100644 --- a/app/src/main/java/com/dessalines/thumbkey/ui/components/keyboard/KeyboardScreen.kt +++ b/app/src/main/java/com/dessalines/thumbkey/ui/components/keyboard/KeyboardScreen.kt @@ -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 @@ -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 @@ -210,7 +210,7 @@ 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. @@ -218,7 +218,7 @@ fun KeyboardScreen( 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)