-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathMoneyFirstTimeDepositView.tsx
More file actions
321 lines (266 loc) · 11.3 KB
/
Copy pathMoneyFirstTimeDepositView.tsx
File metadata and controls
321 lines (266 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import React, { useCallback, useEffect, useRef } from 'react';
import { BackHandler, PixelRatio, Platform } from 'react-native';
import { useNavigation } from '@react-navigation/native';
import type { AppNavigationProp } from '../../../../../core/NavigationService/types';
import { Accelerometer } from 'expo-sensors';
import Rive, {
AutoBind,
useRive,
useRiveString,
useRiveTrigger,
Fit,
RNRiveError,
} from 'rive-react-native';
import { createProjectLogger } from '@metamask/utils';
import { strings } from '../../../../../../locales/i18n';
import Routes from '../../../../../constants/navigation/Routes';
import { useMoneyAnalytics } from '../../hooks/useMoneyAnalytics';
import { SCREEN_NAMES } from '../../constants/moneyEvents';
import { MoneyFirstTimeDepositViewTestIds } from './MoneyFirstTimeDepositView.testIds';
import useMountEffect from '../../hooks/useMountEffect';
const log = createProjectLogger('money-first-time-deposit');
// -- Rive animation assets ------------------------------------------------
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires, import-x/no-commonjs
const MoneyFirstTimeDepositAnimationWithParallaxV2 = require('../../../../../animations/money_account_first_time_deposit_with_parallax_v2.riv');
// -- Rive data-binding names ----------------------------------------------
// These MUST match the names authored in the .riv file. If the Rive designer
// renames any of these, update the constants here to keep the binding working.
/** Name of the Rive artboard that contains the first-time deposit animation. */
const RIVE_ARTBOARD_NAME = 'Intro';
/** Trigger that kicks off the intro animation sequence. Fire once on mount. */
const RIVE_START_TRIGGER = 'start';
/** Trigger fired by the Rive "Done" button. Listened to for navigating home. */
const RIVE_DONE_TRIGGER = 'done';
/** Text data-binding path for the headline shown during the animation. */
const RIVE_TITLE_PATH = 'title';
/** Text data-binding path for the button text shown during the animation. */
const BUTTON_TEXT_PATH = 'button';
/** Text data-binding path for the body copy shown during the animation. */
const RIVE_CONTENT_PATH = 'content';
/** Number data-binding (0–100) for horizontal parallax driven by device tilt. */
const RIVE_X_VALUE_PATH = 'xValue';
/** Number data-binding (0–100) for vertical parallax driven by device tilt. */
const RIVE_Y_VALUE_PATH = 'yValue';
// -- Accelerometer / parallax tuning constants ----------------------------
/**
* How often (ms) the device delivers a new accelerometer reading.
* 33 ms ≈ 30 readings/sec. Lower = smoother but more CPU/battery.
*/
const SENSOR_POLL_INTERVAL_MS = 33;
/**
* Exponential Moving Average (EMA) smoothing factor applied each frame.
* Formula: smoothedValue += SMOOTHING_FACTOR * (targetValue - smoothedValue)
*
* Range 0 to 1:
* 0.05 = very smooth / floaty, significant lag behind real tilt
* 0.12 = balanced — absorbs hand tremor, still tracks intentional tilt
* 0.40 = snappy, almost raw — good for fast-moving effects
* 1.00 = no smoothing at all (raw sensor, jittery)
*/
const SMOOTHING_FACTOR = 0.04;
/**
* Maximum accelerometer g-force that maps to the 0 or 100 parallax extreme.
* Anything beyond this is clamped. Determines how far you must tilt to reach
* full parallax range.
*
* 0.15 = ~8° tilt, very sensitive, small wrist movements register
* 0.30 = ~17° tilt, moderate, deliberate tilt needed
* 0.50 = ~30° tilt, conservative, phone must be angled significantly
*/
const TILT_SENSITIVITY = 0.5;
/**
* Duration (ms) of the calibration window on mount. During this window the
* accelerometer averages the device's resting orientation to establish a
* "neutral baseline". This means parallax is relative to how the user
* actually holds their phone, not relative to perfectly flat.
*
* Parallax stays centered (50, 50) during calibration so the text reveal
* animation plays cleanly regardless of hold angle.
*/
const CALIBRATION_DURATION_MS = 300;
/**
* Converts a raw accelerometer g-force value (relative to baseline) into
* the 0-100 parallax range expected by the Rive data bindings.
*
* -TILT_SENSITIVITY maps to 0 (tilted fully one direction)
* 0 maps to 50 (neutral / resting position)
* +TILT_SENSITIVITY maps to 100 (tilted fully the other direction)
*
* Values outside the range are clamped to prevent overshooting.
*/
function gForceToParallax(gForce: number): number {
const clamped = Math.max(
-TILT_SENSITIVITY,
Math.min(TILT_SENSITIVITY, gForce),
);
return ((clamped / TILT_SENSITIVITY + 1) / 2) * 100;
}
const MoneyFirstTimeDepositView = () => {
const navigation = useNavigation<AppNavigationProp>();
const { trackScreenViewed } = useMoneyAnalytics({
screen_name: SCREEN_NAMES.MONEY_FIRST_TIME_DEPOSIT,
});
const goHome = useCallback(() => {
navigation.navigate(Routes.HOME_TABS, {
screen: Routes.MONEY.ROOT,
params: { screen: Routes.MONEY.HOME },
});
}, [navigation]);
useMountEffect(trackScreenViewed);
// Intercept Android hardware-back so it behaves like tapping "Done".
useEffect(() => {
const sub = BackHandler.addEventListener('hardwareBackPress', () => {
goHome();
return true;
});
return () => sub.remove();
}, [goHome]);
// -- Rive setup ---------------------------------------------------------
const [ref, riveRef] = useRive();
// Flips true once data bindings are established. Guards accelerometer
// pushes so they don't fire before Rive is ready to accept values.
const dataBindingsReady = useRef(false);
const [, setTitle] = useRiveString(riveRef, RIVE_TITLE_PATH);
const [, setContent] = useRiveString(riveRef, RIVE_CONTENT_PATH);
const [, setButtonText] = useRiveString(riveRef, BUTTON_TEXT_PATH);
const fireStart = useRiveTrigger(riveRef, RIVE_START_TRIGGER);
// Listen for the Rive "Done" button trigger → navigate home.
useRiveTrigger(riveRef, RIVE_DONE_TRIGGER, () => {
goHome();
});
// Once the Rive ref is ready, inject i18n text and kick off the animation.
useEffect(() => {
if (!riveRef) return;
setTitle(strings('money.first_time_deposit.title'));
setContent(strings('money.first_time_deposit.content'));
setButtonText(strings('money.first_time_deposit.button_text'));
fireStart?.();
dataBindingsReady.current = true;
}, [riveRef, setTitle, setContent, fireStart, setButtonText]);
// -- Accelerometer-driven parallax --------------------------------------
//
// Architecture (two decoupled loops):
//
// Accelerometer listener → writes raw target values into refs
// (runs at SENSOR_POLL_INTERVAL_MS)
//
// requestAnimationFrame → reads targets, applies EMA smoothing,
// pushes smoothed values to Rive
// (runs at display refresh rate)
//
// This avoids flooding the React Native bridge when the sensor fires
// faster than the screen can render.
//
// On mount, a short calibration phase averages the device's resting
// orientation so that "neutral" = however the user holds their phone.
// EMA-smoothed values that get pushed to Rive each frame. Start centered.
const smoothedX = useRef(50);
const smoothedY = useRef(50);
// Raw parallax targets written by the sensor listener.
const targetX = useRef(50);
const targetY = useRef(50);
// Resting orientation learned during calibration. Null until calibrated.
const restingBaseline = useRef<{ x: number; y: number } | null>(null);
// Running sum/count for the calibration window's average.
const calibrationAccumulator = useRef({ sumX: 0, sumY: 0, count: 0 });
// Per-platform accelerometer sign correction.
//
// iOS (CoreMotion) reports accelerometer axes with the opposite sign to
// Android (SensorManager): e.g. resting upright in portrait, iOS reads
// y ≈ -1 while Android reads y ≈ +1. expo-sensors passes both through without
// normalizing. Flipping iOS makes a given physical tilt drive parallax
// identically on both platforms (tilt down → parallax down).
//
// Read at runtime (not module scope) so the value reflects Platform.OS on
// each mount, which also lets tests exercise both platforms by mocking it.
const axisSign = Platform.OS === 'ios' ? -1 : 1;
useEffect(() => {
if (!riveRef) return;
Accelerometer.setUpdateInterval(SENSOR_POLL_INTERVAL_MS);
let active = true;
let rafId: number;
let subscription: ReturnType<typeof Accelerometer.addListener> | null =
null;
const calibrationStartedAt = Date.now();
try {
subscription = Accelerometer.addListener(({ x, y }) => {
if (!active) return;
const baseline = restingBaseline.current;
// --- Calibration phase ---
// Average the accelerometer readings to learn the device's held-rest
// orientation. Parallax stays centered (50, 50) so the text reveal
// plays cleanly regardless of how the user is holding the phone.
if (baseline === null) {
const acc = calibrationAccumulator.current;
acc.sumX += x;
acc.sumY += y;
acc.count += 1;
targetX.current = 50;
targetY.current = 50;
if (
Date.now() - calibrationStartedAt >= CALIBRATION_DURATION_MS &&
acc.count > 0
) {
const baselineValue = {
x: acc.sumX / acc.count,
y: acc.sumY / acc.count,
};
restingBaseline.current = baselineValue;
}
return;
}
// --- Live phase ---
// Subtract the baseline so parallax is relative to the user's
// resting hold, not relative to "device perfectly flat on a table".
targetX.current = gForceToParallax(axisSign * (x - baseline.x));
targetY.current = gForceToParallax(axisSign * (y - baseline.y));
});
} catch {
// Sensor unavailable — parallax stays at static center.
}
/** rAF render loop: smooth the raw targets and push to Rive. */
const renderParallax = () => {
if (!active || !dataBindingsReady.current) {
rafId = requestAnimationFrame(renderParallax);
return;
}
smoothedX.current +=
SMOOTHING_FACTOR * (targetX.current - smoothedX.current);
smoothedY.current +=
SMOOTHING_FACTOR * (targetY.current - smoothedY.current);
try {
riveRef.setNumber(RIVE_X_VALUE_PATH, smoothedX.current);
riveRef.setNumber(RIVE_Y_VALUE_PATH, smoothedY.current);
} catch {
// Data binding not ready yet — skip this frame.
}
rafId = requestAnimationFrame(renderParallax);
};
rafId = requestAnimationFrame(renderParallax);
return () => {
active = false;
cancelAnimationFrame(rafId);
subscription?.remove();
};
}, [riveRef, axisSign]);
const handleError = useCallback(
(riveError: RNRiveError) => {
log(`Rive error: ${riveError.message}`);
goHome();
},
[goHome],
);
return (
<Rive
ref={ref}
source={MoneyFirstTimeDepositAnimationWithParallaxV2}
artboardName={RIVE_ARTBOARD_NAME}
dataBinding={AutoBind(true)}
fit={Fit.Layout}
layoutScaleFactor={PixelRatio.get()}
onError={handleError}
testID={MoneyFirstTimeDepositViewTestIds.RIVE_ANIMATION}
/>
);
};
export default MoneyFirstTimeDepositView;