Skip to content

A local-first personal finance app

[AI] Add native haptic feedback to the iOS and Android apps (#9382)

Matiss Janis Aboltins5226912e390a
diff --git a/knip.json b/knip.json
index 695ed22e3b..09e444b86d 100644
--- a/knip.json
+++ b/knip.json
@@ -85,7 +85,7 @@
"ios/App/App/public/**",
"ios/DerivedData/**"
],
- "ignoreDependencies": ["@capacitor/splash-screen"]
+ "ignoreDependencies": ["@capacitor/haptics", "@capacitor/splash-screen"]
},
"packages/plugins-service": {
"entry": ["src/plugin-service-worker.ts"]
diff --git a/packages/desktop-client/package.json b/packages/desktop-client/package.json
index 2df4433f3f..67794dbf90 100644
--- a/packages/desktop-client/package.json
+++ b/packages/desktop-client/package.json
@@ -151,6 +151,8 @@
"@actual-app/components": "workspace:*",
"@actual-app/core": "workspace:*",
"@babel/core": "^8.0.7",
+ "@capacitor/core": "^8.4.2",
+ "@capacitor/haptics": "^8.0.2",
"@codemirror/autocomplete": "^6.20.3",
"@codemirror/language": "^6.13.1",
"@codemirror/state": "^6.7.6",
diff --git a/packages/desktop-client/src/components/common/CalculatorButtons.tsx b/packages/desktop-client/src/components/common/CalculatorButtons.tsx
index 32e3f46924..bed3a3a356 100644
--- a/packages/desktop-client/src/components/common/CalculatorButtons.tsx
+++ b/packages/desktop-client/src/components/common/CalculatorButtons.tsx
@@ -6,6 +6,8 @@ import type { CSSProperties } from '@actual-app/components/styles';
import { theme } from '@actual-app/components/theme';
import { View } from '@actual-app/components/view';
+import { hapticKeyPress } from '#util/haptics';
+
export function CalculatorButtons({
inputRef,
style,
@@ -303,9 +305,7 @@ function CalculatorButton({
onPointerDown={e => {
e.preventDefault();
onInteractionStart?.();
- if ('vibrate' in navigator) {
- navigator.vibrate(3);
- }
+ hapticKeyPress();
}}
onPressStart={e => {
stopRepeating();
diff --git a/packages/desktop-client/src/components/mobile/ActionableGridListItem.tsx b/packages/desktop-client/src/components/mobile/ActionableGridListItem.tsx
index 0d30efdbd6..95e40a72c7 100644
--- a/packages/desktop-client/src/components/mobile/ActionableGridListItem.tsx
+++ b/packages/desktop-client/src/components/mobile/ActionableGridListItem.tsx
@@ -1,4 +1,4 @@
-import React, { useRef, useState } from 'react';
+import React, { useRef } from 'react';
import type { ReactNode } from 'react';
import { GridListItem } from 'react-aria-components';
import type { GridListItemProps } from 'react-aria-components';
@@ -10,6 +10,8 @@ import { theme } from '@actual-app/components/theme';
import type { WithRequired } from '@actual-app/core/types/util';
import { useDrag } from '@use-gesture/react';
+import { hapticThresholdCrossed } from '#util/haptics';
+
type ActionableGridListItemProps<T> = {
actions?: ReactNode | ((params: { close: () => void }) => ReactNode);
actionsBackgroundColor?: string;
@@ -28,7 +30,8 @@ export function ActionableGridListItem<T extends object>({
...props
}: ActionableGridListItemProps<T>) {
const dragStartedRef = useRef(false);
- const [isRevealed, setIsRevealed] = useState(false);
+ const isPastThresholdRef = useRef(false);
+ const dragStartXRef = useRef(0);
const hasActions = !!actions;
@@ -43,9 +46,23 @@ export function ActionableGridListItem<T extends object>({
// Handle drag gestures
const bind = useDrag(
- ({ active, movement: [mx], velocity: [vx] }) => {
- const startPos = isRevealed ? -actionsWidth : 0;
- const currentX = startPos + mx;
+ ({ active, first, movement: [mx], velocity: [vx] }) => {
+ // Start from where the item actually is, so a swipe that interrupts a
+ // snap animation continues from the current position.
+ if (first) {
+ dragStartXRef.current = x.get();
+ }
+ const currentX = dragStartXRef.current + mx;
+
+ // Tick when the drag crosses the point where letting go flips between
+ // revealed and closed, like native swipe actions do.
+ const isPastThreshold = currentX < -actionsWidth / 2;
+ if (first) {
+ isPastThresholdRef.current = isPastThreshold;
+ } else if (active && isPastThreshold !== isPastThresholdRef.current) {
+ isPastThresholdRef.current = isPastThreshold;
+ hapticThresholdCrossed();
+ }
if (active) {
dragStartedRef.current = true;
@@ -67,13 +84,11 @@ export function ActionableGridListItem<T extends object>({
to: { x: shouldReveal ? -actionsWidth : 0 },
onRest: () => {
dragStartedRef.current = false;
- setIsRevealed(shouldReveal);
},
});
},
{
axis: 'x',
- from: () => [isRevealed ? -actionsWidth : 0, 0],
enabled: hasActions,
},
);
@@ -143,12 +158,7 @@ export function ActionableGridListItem<T extends object>({
{typeof actions === 'function'
? actions({
close: () => {
- void api.start({
- to: { x: 0 },
- onRest: () => {
- setIsRevealed(false);
- },
- });
+ void api.start({ to: { x: 0 } });
},
})
: actions}
diff --git a/packages/desktop-client/src/components/mobile/PullToRefresh.tsx b/packages/desktop-client/src/components/mobile/PullToRefresh.tsx
index 0bea99dafe..5d8dda6391 100644
--- a/packages/desktop-client/src/components/mobile/PullToRefresh.tsx
+++ b/packages/desktop-client/src/components/mobile/PullToRefresh.tsx
@@ -1,17 +1,22 @@
-import React from 'react';
-import type { ComponentProps } from 'react';
+import React, { useEffect, useRef } from 'react';
+import type { ComponentProps, RefObject } from 'react';
import BasePullToRefresh from 'react-simple-pull-to-refresh';
import type { CSSProperties } from '@actual-app/components/styles';
import { css } from '@emotion/css';
+import { hapticThresholdCrossed } from '#util/haptics';
+
type PullToRefreshProps = ComponentProps<typeof BasePullToRefresh> & {
style?: CSSProperties;
};
export function PullToRefresh(props: PullToRefreshProps) {
+ const wrapperRef = useRef<HTMLDivElement>(null);
+ usePullThresholdHaptic(wrapperRef);
+
return (
- <div style={{ overflow: 'auto', flex: 1 }}>
+ <div ref={wrapperRef} style={{ overflow: 'auto', flex: 1 }}>
<BasePullToRefresh
pullDownThreshold={80}
resistance={2}
@@ -33,3 +38,40 @@ export function PullToRefresh(props: PullToRefreshProps) {
</div>
);
}
+
+// Set by the library on its container while the pull is past the point at
+// which letting go triggers a refresh.
+const THRESHOLD_BREACHED_CLASS = 'ptr--pull-down-treshold-breached';
+
+/**
+ * Gives haptic feedback the moment a pull arms the refresh, like native
+ * pull-to-refresh does. The library signals the threshold only through a
+ * class on its container (the wrapper's first child), so that class is
+ * watched.
+ */
+function usePullThresholdHaptic(wrapperRef: RefObject<HTMLDivElement | null>) {
+ useEffect(() => {
+ const container = wrapperRef.current?.firstElementChild;
+ if (!container) {
+ return;
+ }
+
+ let wasPastThreshold = container.classList.contains(
+ THRESHOLD_BREACHED_CLASS,
+ );
+ const observer = new MutationObserver(() => {
+ const isPastThreshold = container.classList.contains(
+ THRESHOLD_BREACHED_CLASS,
+ );
+ if (isPastThreshold && !wasPastThreshold) {
+ hapticThresholdCrossed();
+ }
+ wasPastThreshold = isPastThreshold;
+ });
+ observer.observe(container, {
+ attributes: true,
+ attributeFilter: ['class'],
+ });
+ return () => observer.disconnect();
+ }, [wrapperRef]);
+}
diff --git a/packages/desktop-client/src/components/mobile/transactions/CalculatorKeyboard.tsx b/packages/desktop-client/src/components/mobile/transactions/CalculatorKeyboard.tsx
index 8cbd0e6032..a42f5ab2a3 100644
--- a/packages/desktop-client/src/components/mobile/transactions/CalculatorKeyboard.tsx
+++ b/packages/desktop-client/src/components/mobile/transactions/CalculatorKeyboard.tsx
@@ -8,6 +8,7 @@ import { theme } from '@actual-app/components/theme';
import { View } from '@actual-app/components/view';
import { CalculatorButtons } from '#components/common/CalculatorButtons';
+import { hapticKeyPress } from '#util/haptics';
export const CALCULATOR_KEYBOARD_HEIGHT = 'min(400px, max(320px, 50dvh))';
@@ -127,12 +128,6 @@ export const CalculatorKeyboard = memo(function CalculatorAmountInput({
);
});
-const maybeVibrate = () => {
- if ('vibrate' in navigator) {
- navigator.vibrate(3);
- }
-};
-
function NonFocusableButton({
variant,
onPress,
@@ -154,7 +149,7 @@ function NonFocusableButton({
onPointerDown={e => {
e.preventDefault();
onInteractionStart?.();
- maybeVibrate();
+ hapticKeyPress();
}}
onPress={onPress}
>
diff --git a/packages/desktop-client/src/util/haptics.ts b/packages/desktop-client/src/util/haptics.ts
new file mode 100644
index 0000000000..ea89999100
--- /dev/null
+++ b/packages/desktop-client/src/util/haptics.ts
@@ -0,0 +1,40 @@
+import { Capacitor } from '@capacitor/core';
+import { Haptics, ImpactStyle } from '@capacitor/haptics';
+
+// Inside the Capacitor apps the native haptics engine is used. Elsewhere the
+// Vibration API is used when the browser has it: iOS Safari does not, so the
+// web app stays silent there.
+const isNativeApp = Capacitor.isNativePlatform();
+
+function vibrateUsingBrowserAPI(durationMs: number) {
+ if (typeof navigator !== 'undefined' && 'vibrate' in navigator) {
+ navigator.vibrate(durationMs);
+ }
+}
+
+function runIgnoringErrors(action: () => Promise<void>) {
+ // Haptics are a nicety: a device without a haptics engine (or a plugin
+ // that failed to load) must never surface an error to the user.
+ action().catch(() => undefined);
+}
+
+/** A key on an on-screen keypad was pressed. */
+export function hapticKeyPress() {
+ if (isNativeApp) {
+ runIgnoringErrors(() => Haptics.impact({ style: ImpactStyle.Light }));
+ return;
+ }
+ vibrateUsingBrowserAPI(3);
+}
+
+/**
+ * A drag crossed the point at which releasing it takes effect, e.g. a swipe
+ * revealing its actions or a pull far enough to refresh.
+ */
+export function hapticThresholdCrossed() {
+ if (isNativeApp) {
+ runIgnoringErrors(() => Haptics.impact({ style: ImpactStyle.Medium }));
+ return;
+ }
+ vibrateUsingBrowserAPI(10);
+}
diff --git a/packages/mobile-client/android/app/capacitor.build.gradle b/packages/mobile-client/android/app/capacitor.build.gradle
index fced7ebd55..75302392bb 100644
--- a/packages/mobile-client/android/app/capacitor.build.gradle
+++ b/packages/mobile-client/android/app/capacitor.build.gradle
@@ -9,6 +9,7 @@ android {
apply from: "../capacitor-cordova-android-plugins/cordova.variables.gradle"
dependencies {
+ implementation project(':capacitor-haptics')
implementation project(':capacitor-splash-screen')
}
diff --git a/packages/mobile-client/android/capacitor.settings.gradle b/packages/mobile-client/android/capacitor.settings.gradle
index 68ddb413e5..fa4d96f92c 100644
--- a/packages/mobile-client/android/capacitor.settings.gradle
+++ b/packages/mobile-client/android/capacitor.settings.gradle
@@ -2,5 +2,8 @@
include ':capacitor-android'
project(':capacitor-android').projectDir = new File('../../../node_modules/@capacitor/android/capacitor')
+include ':capacitor-haptics'
+project(':capacitor-haptics').projectDir = new File('../../../node_modules/@capacitor/haptics/android')
+
include ':capacitor-splash-screen'
project(':capacitor-splash-screen').projectDir = new File('../../../node_modules/@capacitor/splash-screen/android')
diff --git a/packages/mobile-client/ios/App/Podfile b/packages/mobile-client/ios/App/Podfile
index 9cf95009b7..482476c7cb 100644
--- a/packages/mobile-client/ios/App/Podfile
+++ b/packages/mobile-client/ios/App/Podfile
@@ -11,6 +11,7 @@ install! 'cocoapods', :disable_input_output_paths => true
def capacitor_pods
pod 'Capacitor', :path => '../../../../node_modules/@capacitor/ios'
pod 'CapacitorCordova', :path => '../../../../node_modules/@capacitor/ios'
+ pod 'CapacitorHaptics', :path => '../../../../node_modules/@capacitor/haptics'
pod 'CapacitorSplashScreen', :path => '../../../../node_modules/@capacitor/splash-screen'
end
diff --git a/packages/mobile-client/ios/App/Podfile.lock b/packages/mobile-client/ios/App/Podfile.lock
index 204952fe43..445390c5ec 100644
--- a/packages/mobile-client/ios/App/Podfile.lock
+++ b/packages/mobile-client/ios/App/Podfile.lock
@@ -2,12 +2,15 @@ PODS:
- Capacitor (8.5.3):
- CapacitorCordova
- CapacitorCordova (8.5.3)
+ - CapacitorHaptics (8.0.2):
+ - Capacitor
- CapacitorSplashScreen (8.0.2):
- Capacitor
DEPENDENCIES:
- "Capacitor (from `../../../../node_modules/@capacitor/ios`)"
- "CapacitorCordova (from `../../../../node_modules/@capacitor/ios`)"
+ - "CapacitorHaptics (from `../../../../node_modules/@capacitor/haptics`)"
- "CapacitorSplashScreen (from `../../../../node_modules/@capacitor/splash-screen`)"
EXTERNAL SOURCES:
@@ -15,14 +18,17 @@ EXTERNAL SOURCES:
:path: "../../../../node_modules/@capacitor/ios"
CapacitorCordova:
:path: "../../../../node_modules/@capacitor/ios"
+ CapacitorHaptics:
+ :path: "../../../../node_modules/@capacitor/haptics"
CapacitorSplashScreen:
:path: "../../../../node_modules/@capacitor/splash-screen"
SPEC CHECKSUMS:
Capacitor: 2f67eaeb8ed79909cd35b5e7439a5c2a9ca58e0f
CapacitorCordova: 0b7326439c7a09fc5d20eced5e0dab986327a493
+ CapacitorHaptics: 296f771ecd89c7a1bd92a7b6826a7d268e2e70f5
CapacitorSplashScreen: 7bf61c2001557ff9e195e601c6b87344480d3c65
-PODFILE CHECKSUM: bb5605552c513baeb6ee23af2520ffb9d495b447
+PODFILE CHECKSUM: 7b60617888e24bc1c74296c14c11098ab8d26aa4
COCOAPODS: 1.17.0
diff --git a/packages/mobile-client/package.json b/packages/mobile-client/package.json
index a22eed973d..caf49c60c0 100644
--- a/packages/mobile-client/package.json
+++ b/packages/mobile-client/package.json
@@ -21,6 +21,7 @@
"@actual-app/web": "workspace:*",
"@capacitor/android": "^8.4.3",
"@capacitor/core": "^8.4.2",
+ "@capacitor/haptics": "^8.0.2",
"@capacitor/ios": "^8.5.3",
"@capacitor/splash-screen": "^8.0.2"
},
diff --git a/upcoming-release-notes/add-native-haptics-to-mobile-app.md b/upcoming-release-notes/add-native-haptics-to-mobile-app.md
new file mode 100644
index 0000000000..2237ba5908
--- /dev/null
+++ b/upcoming-release-notes/add-native-haptics-to-mobile-app.md
@@ -0,0 +1,6 @@
+---
+category: Enhancements
+authors: [MatissJanis]
+---
+
+Add haptic feedback to the keypad, swipe actions and pull to refresh in the iOS and Android apps
diff --git a/yarn.lock b/yarn.lock
index d4b04e9704..bb4aaab744 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -239,6 +239,8 @@ __metadata:
"@actual-app/components": "workspace:*"
"@actual-app/core": "workspace:*"
"@babel/core": "npm:^8.0.7"
+ "@capacitor/core": "npm:^8.4.2"
+ "@capacitor/haptics": "npm:^8.0.2"
"@codemirror/autocomplete": "npm:^6.20.3"
"@codemirror/language": "npm:^6.13.1"
"@codemirror/state": "npm:^6.7.6"
@@ -2129,6 +2131,15 @@ __metadata:
languageName: node
linkType: hard
+"@capacitor/haptics@npm:^8.0.2":
+ version: 8.0.2
+ resolution: "@capacitor/haptics@npm:8.0.2"
+ peerDependencies:
+ "@capacitor/core": ">=8.0.0"
+ checksum: 10/cf8a685300119a2e6316b41f372fa00ba92ddae53e9ee2ed33a4e0ab154c83f4bf8d1fd6034326d477f41949bf018d0074d6db4417f44dd6a62da782196ced91
+ languageName: node
+ linkType: hard
+
"@capacitor/ios@npm:^8.5.3":
version: 8.5.3
resolution: "@capacitor/ios@npm:8.5.3"
@@ -20243,6 +20254,7 @@ __metadata:
"@capacitor/android": "npm:^8.4.3"
"@capacitor/cli": "npm:^8.4.2"
"@capacitor/core": "npm:^8.4.2"
+ "@capacitor/haptics": "npm:^8.0.2"
"@capacitor/ios": "npm:^8.5.3"
"@capacitor/splash-screen": "npm:^8.0.2"
languageName: unknown