Skip to content

Commit 33888a7

Browse files
committed
feat: improve continuous player api
1 parent d1e5d99 commit 33888a7

File tree

8 files changed

+282
-69
lines changed

8 files changed

+282
-69
lines changed

android/src/main/java/com/margelo/nitro/ahap/Ahap.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,23 @@ class Ahap : HybridAhapSpec() {
2323
// TODO: Android haptics implementation not yet supported
2424
}
2525

26-
override fun createContinuousPlayer(initialIntensity: Double, initialSharpness: Double) {
26+
override fun createContinuousPlayer(playerId: String, initialIntensity: Double, initialSharpness: Double) {
2727
// TODO: Android haptics implementation not yet supported
2828
}
2929

30-
override fun startContinuousPlayer() {
30+
override fun startContinuousPlayer(playerId: String) {
3131
// TODO: Android haptics implementation not yet supported
3232
}
3333

34-
override fun updateContinuousPlayer(intensityControl: Double, sharpnessControl: Double) {
34+
override fun updateContinuousPlayer(playerId: String, intensityControl: Double, sharpnessControl: Double) {
3535
// TODO: Android haptics implementation not yet supported
3636
}
3737

38-
override fun stopContinuousPlayer() {
38+
override fun stopContinuousPlayer(playerId: String) {
39+
// TODO: Android haptics implementation not yet supported
40+
}
41+
42+
override fun destroyContinuousPlayer(playerId: String) {
3943
// TODO: Android haptics implementation not yet supported
4044
}
4145
}

example/src/components/ContinuousPalette.tsx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,16 @@ import Animated, {
77
useDerivedValue,
88
clamp,
99
} from 'react-native-reanimated';
10-
import {
11-
createContinuousPlayer,
12-
startContinuousPlayer,
13-
stopContinuousPlayer,
14-
updateContinuousPlayer,
15-
} from 'react-native-ahaps';
10+
import { useContinuousPlayer } from 'react-native-ahaps';
1611
import Header from './Header';
1712
import Footer from './Footer';
1813

1914
const TOUCH_INDICATOR_SIZE = 50;
2015
const INITIAL_INTENSITY = 1.0;
2116
const INITIAL_SHARPNESS = 0.5;
2217

18+
const CONTINUOUS_PALETTE_PLAYER = 'continuous-palette';
19+
2320
interface ContinuousPaletteProps {
2421
size: number;
2522
colors: {
@@ -46,12 +43,15 @@ const normalizeCoordinates = (x: number, y: number, size: number) => {
4643
};
4744
};
4845

49-
createContinuousPlayer(INITIAL_INTENSITY, INITIAL_SHARPNESS);
50-
5146
export default function ContinuousPalette({
5247
size,
5348
colors,
5449
}: ContinuousPaletteProps) {
50+
const player = useContinuousPlayer(
51+
CONTINUOUS_PALETTE_PLAYER,
52+
INITIAL_INTENSITY,
53+
INITIAL_SHARPNESS
54+
);
5555
const sharpness = useSharedValue(0.5);
5656
const intensity = useSharedValue(0.5);
5757
const touchX = useSharedValue(size / 2);
@@ -78,7 +78,7 @@ export default function ContinuousPalette({
7878
sharpness.set(clamp(perceivedSharpness, 0, 1));
7979
intensity.set(perceivedIntensity);
8080

81-
updateContinuousPlayer(dynamicIntensity, dynamicSharpness);
81+
player.update(dynamicIntensity, dynamicSharpness);
8282
};
8383

8484
const gesture = Gesture.Pan()
@@ -89,7 +89,7 @@ export default function ContinuousPalette({
8989
touchAlpha.set(1);
9090
bgOpacity.set(withTiming(1, { duration: 100 }));
9191

92-
startContinuousPlayer();
92+
player.start();
9393
updateHaptic(clipped.x, clipped.y);
9494
})
9595
.onUpdate((event) => {
@@ -101,11 +101,11 @@ export default function ContinuousPalette({
101101
})
102102
.onEnd(() => {
103103
bgOpacity.set(withTiming(0, { duration: 100 }));
104-
stopContinuousPlayer();
104+
player.stop();
105105
})
106106
.onFinalize(() => {
107107
bgOpacity.set(withTiming(0, { duration: 100 }));
108-
stopContinuousPlayer();
108+
player.stop();
109109
});
110110

111111
const touchStyle = useAnimatedStyle(() => {

example/src/components/MiniContinuousPalette.tsx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,12 @@ import Animated, {
88
clamp,
99
type SharedValue,
1010
} from 'react-native-reanimated';
11-
import {
12-
createContinuousPlayer,
13-
startContinuousPlayer,
14-
stopContinuousPlayer,
15-
updateContinuousPlayer,
16-
} from 'react-native-ahaps';
11+
import { useContinuousPlayer } from 'react-native-ahaps';
1712

1813
const TOUCH_INDICATOR_SIZE = 30;
1914
const INITIAL_INTENSITY = 1.0;
2015
const INITIAL_SHARPNESS = 0.5;
21-
22-
createContinuousPlayer(INITIAL_INTENSITY, INITIAL_SHARPNESS);
16+
const MINI_CONTINUOUS_PLAYER = 'mini-continuous-palette';
2317

2418
interface MiniContinuousPaletteProps {
2519
size: number;
@@ -58,6 +52,12 @@ export default function MiniContinuousPalette({
5852
onContinuousUpdate,
5953
onContinuousEnd,
6054
}: MiniContinuousPaletteProps) {
55+
const player = useContinuousPlayer(
56+
MINI_CONTINUOUS_PLAYER,
57+
INITIAL_INTENSITY,
58+
INITIAL_SHARPNESS
59+
);
60+
6161
const touchX = useSharedValue(size / 2);
6262
const touchY = useSharedValue(size / 2);
6363
const bgOpacity = useSharedValue(0);
@@ -88,7 +88,7 @@ export default function MiniContinuousPalette({
8888
1
8989
);
9090

91-
updateContinuousPlayer(dynamicIntensity, dynamicSharpness);
91+
player.update(dynamicIntensity, dynamicSharpness);
9292

9393
if (onContinuousUpdate) {
9494
onContinuousUpdate(perceivedIntensity, perceivedSharpness);
@@ -102,7 +102,7 @@ export default function MiniContinuousPalette({
102102
touchY.set(clipped.y);
103103
bgOpacity.set(withTiming(1, { duration: 100 }));
104104

105-
startContinuousPlayer();
105+
player.start();
106106
updateHaptic(clipped.x, clipped.y);
107107

108108
const normalized = normalizeCoordinates(clipped.x, clipped.y, size);
@@ -139,7 +139,7 @@ export default function MiniContinuousPalette({
139139
})
140140
.onEnd(() => {
141141
bgOpacity.set(withTiming(0, { duration: 100 }));
142-
stopContinuousPlayer();
142+
player.stop();
143143

144144
// Clear gesture state
145145
if (gestureActive) gestureActive.set(false);
@@ -150,7 +150,7 @@ export default function MiniContinuousPalette({
150150
})
151151
.onFinalize(() => {
152152
bgOpacity.set(withTiming(0, { duration: 100 }));
153-
stopContinuousPlayer();
153+
player.stop();
154154

155155
// Clear gesture state
156156
if (gestureActive) gestureActive.set(false);

example/src/contexts/RecorderContext.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import type { HapticEvent, HapticCurve } from 'react-native-ahaps';
1010
import {
1111
startHaptic,
1212
stopAllHaptics,
13-
stopContinuousPlayer,
1413
} from 'react-native-ahaps';
1514
import {
1615
trimHapticDataFromSeekTime,
@@ -173,9 +172,8 @@ export function RecorderProvider({ children }: { children: ReactNode }) {
173172

174173
isRecording.set(false);
175174

176-
if (wasContinuousActive) {
177-
stopContinuousPlayer();
178-
}
175+
// Note: The continuous player is managed by MiniContinuousPalette
176+
// and will be stopped automatically when the gesture ends
179177

180178
let events = recordingEvents.get();
181179

ios/Ahap.swift

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,29 @@ class Ahap: HybridAhapSpec {
2121
}
2222

2323
func destroyEngine() throws {
24-
haptics.clearHapticPlayers()
24+
haptics.destroyEngine()
2525
}
2626

2727
// MARK: - Continuous Player Methods
2828

29-
func createContinuousPlayer(initialIntensity: Double, initialSharpness: Double) throws {
30-
haptics.createContinuousPlayer(initialIntensity: Float(initialIntensity), initialSharpness: Float(initialSharpness))
29+
func createContinuousPlayer(playerId: String, initialIntensity: Double, initialSharpness: Double) throws {
30+
haptics.createContinuousPlayer(playerId: playerId, initialIntensity: Float(initialIntensity), initialSharpness: Float(initialSharpness))
3131
}
3232

33-
func startContinuousPlayer() throws {
34-
haptics.startContinuousPlayer()
33+
func startContinuousPlayer(playerId: String) throws {
34+
haptics.startContinuousPlayer(playerId: playerId)
3535
}
3636

37-
func updateContinuousPlayer(intensityControl: Double, sharpnessControl: Double) throws {
38-
haptics.updateContinuousPlayer(intensityControl: Float(intensityControl), sharpnessControl: Float(sharpnessControl))
37+
func updateContinuousPlayer(playerId: String, intensityControl: Double, sharpnessControl: Double) throws {
38+
haptics.updateContinuousPlayer(playerId: playerId, intensityControl: Float(intensityControl), sharpnessControl: Float(sharpnessControl))
3939
}
4040

41-
func stopContinuousPlayer() throws {
42-
haptics.stopContinuousPlayer()
41+
func stopContinuousPlayer(playerId: String) throws {
42+
haptics.stopContinuousPlayer(playerId: playerId)
43+
}
44+
45+
func destroyContinuousPlayer(playerId: String) throws {
46+
haptics.destroyContinuousPlayer(playerId: playerId)
4347
}
4448

4549

0 commit comments

Comments
 (0)