From bffe5b2b96887916fb0a859e8135d318b8ced340 Mon Sep 17 00:00:00 2001 From: Gary Tokman Date: Sat, 11 Nov 2023 13:51:56 -0500 Subject: [PATCH] fix: add stop --- example/src/App.tsx | 11 ++++++++++- ios/Haptics.mm | 1 + ios/Haptics.swift | 5 +++++ src/index.tsx | 8 ++++++++ 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/example/src/App.tsx b/example/src/App.tsx index 141009d..afda1fd 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -1,7 +1,7 @@ import * as React from 'react'; import type { HapticType } from '@candlefinance/haptics'; -import { haptic, hapticWithPattern, play } from '@candlefinance/haptics'; +import { haptic, hapticWithPattern, play, stop } from '@candlefinance/haptics'; import { Pressable, StyleSheet, Text, View } from 'react-native'; export default function App() { @@ -75,6 +75,15 @@ export default function App() { > Play File + { + console.log('hapticWithPatternFile'); + stop(); + }} + > + stop File + ); } diff --git a/ios/Haptics.mm b/ios/Haptics.mm index 0d0efbb..f4a657d 100644 --- a/ios/Haptics.mm +++ b/ios/Haptics.mm @@ -5,6 +5,7 @@ @interface RCT_EXTERN_MODULE(Haptics, NSObject) RCT_EXTERN_METHOD(haptic:(NSString *)type) RCT_EXTERN_METHOD(hapticWithPattern:(NSArray *)pattern) RCT_EXTERN_METHOD(play:(nonnull NSString *)fileName loop:(nonnull BOOL)loop) +RCT_EXTERN_METHOD(stop) + (BOOL)requiresMainQueueSetup { diff --git a/ios/Haptics.swift b/ios/Haptics.swift index 3fa944a..7e1de0b 100644 --- a/ios/Haptics.swift +++ b/ios/Haptics.swift @@ -67,4 +67,9 @@ class Haptics: NSObject { func play(fileName: String, loop: Bool) { Vibrator.shared.startHaptic(named: fileName, loop: loop) } + + @objc(stop) + func stop() { + Vibrator.shared.stopHaptic() + } } diff --git a/src/index.tsx b/src/index.tsx index 3176d64..37fc51a 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -54,3 +54,11 @@ export function play(fileName: string, loop: boolean = false) { } Haptics.play(fileName, loop); } + +export function stop() { + if (Platform.OS === 'android') { + console.log('Haptics is not supported on Android'); + return; + } + Haptics.stop(); +}