Skip to content

Commit

Permalink
feat: add support for ahap
Browse files Browse the repository at this point in the history
  • Loading branch information
gtokman committed Nov 6, 2023
1 parent a859ec8 commit d6d7283
Show file tree
Hide file tree
Showing 8 changed files with 529 additions and 45 deletions.
24 changes: 14 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<br/>

The package only supports **iOS** 13+ using [Haptico](https://github.com/iSapozhnik/Haptico) under the hood.
Supports playing haptics on iOS with default UIImpactFeedbackGenerator and CoreHaptics for patterns and ahap files.

## Installation

Expand All @@ -41,19 +41,23 @@ import { haptic, hapticWithPattern } from '@candlefinance/haptics';
// light, medium, heavy, soft, rigid, warning, error, success, selectionChanged
haptic('medium');

// pattern, delay
hapticWithPattern(
['.', '.', '.', 'o', 'O', '-', 'O', 'o', '.', '.', '.', '.'],
0.1
);
// pattern
hapticWithPattern(['.', '.', '.', 'o', 'O', '-', 'O', 'o', '.', '.', '.', '.']);

// play ahap file
play('fileName');
```

The pattern format:

- `O` - heavy impact
- `o` - medium impact
- `.` - light impact
- `-` - wait 0.1 second
- 'o' // medium impact
- 'O' // heavy impact
- '.' // light impact
- ':' // soft impact
- '-' // wait of 0.1 second
- '=' // wait of 1 second

For playing ahap files to the root of your project add a folder called `haptics` and add your ahap files there. Use (Haptrix)[https://www.haptrix.com/] or equivalent to generate ahap files.

## Contributing

Expand Down
135 changes: 135 additions & 0 deletions example/ios/Haptics/test.ahap
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
{
"Version": 1,
"Pattern": [
{
"Event": {
"Time": 0.43787878787878787,
"EventType": "HapticTransient",
"EventParameters": [
{
"ParameterID": "HapticIntensity",
"ParameterValue": 0.5529411764705883
},
{
"ParameterID": "HapticSharpness",
"ParameterValue": 0.5529411764705883
}
]
}
},
{
"Event": {
"Time": 0.25681818181818183,
"EventType": "HapticTransient",
"EventParameters": [
{
"ParameterID": "HapticIntensity",
"ParameterValue": 0.4411764705882353
},
{
"ParameterID": "HapticSharpness",
"ParameterValue": 0.4411764705882353
}
]
}
},
{
"Event": {
"Time": 0.3325757575757576,
"EventType": "HapticContinuous",
"EventDuration": 0.005,
"EventParameters": [
{
"ParameterID": "HapticIntensity",
"ParameterValue": 0.4235294117647059
},
{
"ParameterID": "HapticSharpness",
"ParameterValue": 0.4235294117647059
}
]
}
},
{
"Event": {
"Time": 0.3325757575757576,
"EventType": "HapticTransient",
"EventParameters": [
{
"ParameterID": "HapticIntensity",
"ParameterValue": 0.4235294117647059
},
{
"ParameterID": "HapticSharpness",
"ParameterValue": 0.4235294117647059
}
]
}
},
{
"Event": {
"Time": 0.740909090909091,
"EventType": "HapticTransient",
"EventParameters": [
{
"ParameterID": "HapticIntensity",
"ParameterValue": 0.6529411764705882
},
{
"ParameterID": "HapticSharpness",
"ParameterValue": 0.6529411764705882
}
]
}
},
{
"Event": {
"Time": 0.6659090909090909,
"EventType": "HapticContinuous",
"EventDuration": 0.005,
"EventParameters": [
{
"ParameterID": "HapticIntensity",
"ParameterValue": 0.38235294117647056
},
{
"ParameterID": "HapticSharpness",
"ParameterValue": 0.38235294117647056
}
]
}
},
{
"Event": {
"Time": 0.5409090909090909,
"EventType": "HapticTransient",
"EventParameters": [
{
"ParameterID": "HapticIntensity",
"ParameterValue": 0.5058823529411764
},
{
"ParameterID": "HapticSharpness",
"ParameterValue": 0.5058823529411764
}
]
}
},
{
"Event": {
"Time": 0.12575757575757576,
"EventType": "HapticTransient",
"EventParameters": [
{
"ParameterID": "HapticIntensity",
"ParameterValue": 0.3941176470588235
},
{
"ParameterID": "HapticSharpness",
"ParameterValue": 0.3941176470588235
}
]
}
}
]
}
18 changes: 16 additions & 2 deletions example/ios/HapticsExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
13B07FBC1A68108700A75B9A /* AppDelegate.mm in Sources */ = {isa = PBXBuildFile; fileRef = 13B07FB01A68108700A75B9A /* AppDelegate.mm */; };
13B07FBF1A68108700A75B9A /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 13B07FB51A68108700A75B9A /* Images.xcassets */; };
13B07FC11A68108700A75B9A /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 13B07FB71A68108700A75B9A /* main.m */; };
4EDBA3772AF91EF900D5CF41 /* test.ahap in Resources */ = {isa = PBXBuildFile; fileRef = 4EDBA3752AF91EF900D5CF41 /* test.ahap */; };
7699B88040F8A987B510C191 /* libPods-HapticsExample-HapticsExampleTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 19F6CBCC0A4E27FBF8BF4A61 /* libPods-HapticsExample-HapticsExampleTests.a */; };
81AB9BB82411601600AC10FF /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 81AB9BB72411601600AC10FF /* LaunchScreen.storyboard */; };
/* End PBXBuildFile section */
Expand All @@ -38,6 +39,7 @@
13B07FB71A68108700A75B9A /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = main.m; path = HapticsExample/main.m; sourceTree = "<group>"; };
19F6CBCC0A4E27FBF8BF4A61 /* libPods-HapticsExample-HapticsExampleTests.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-HapticsExample-HapticsExampleTests.a"; sourceTree = BUILT_PRODUCTS_DIR; };
3B4392A12AC88292D35C810B /* Pods-HapticsExample.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-HapticsExample.debug.xcconfig"; path = "Target Support Files/Pods-HapticsExample/Pods-HapticsExample.debug.xcconfig"; sourceTree = "<group>"; };
4EDBA3752AF91EF900D5CF41 /* test.ahap */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = test.ahap; sourceTree = "<group>"; };
5709B34CF0A7D63546082F79 /* Pods-HapticsExample.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-HapticsExample.release.xcconfig"; path = "Target Support Files/Pods-HapticsExample/Pods-HapticsExample.release.xcconfig"; sourceTree = "<group>"; };
5B7EB9410499542E8C5724F5 /* Pods-HapticsExample-HapticsExampleTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-HapticsExample-HapticsExampleTests.debug.xcconfig"; path = "Target Support Files/Pods-HapticsExample-HapticsExampleTests/Pods-HapticsExample-HapticsExampleTests.debug.xcconfig"; sourceTree = "<group>"; };
5DCACB8F33CDC322A6C60F78 /* libPods-HapticsExample.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-HapticsExample.a"; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -86,6 +88,7 @@
13B07FAE1A68108700A75B9A /* HapticsExample */ = {
isa = PBXGroup;
children = (
4EDBA3742AF91EF900D5CF41 /* Haptics */,
13B07FAF1A68108700A75B9A /* AppDelegate.h */,
13B07FB01A68108700A75B9A /* AppDelegate.mm */,
13B07FB51A68108700A75B9A /* Images.xcassets */,
Expand All @@ -106,6 +109,14 @@
name = Frameworks;
sourceTree = "<group>";
};
4EDBA3742AF91EF900D5CF41 /* Haptics */ = {
isa = PBXGroup;
children = (
4EDBA3752AF91EF900D5CF41 /* test.ahap */,
);
path = Haptics;
sourceTree = "<group>";
};
832341AE1AAA6A7D00B99B32 /* Libraries */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -243,6 +254,7 @@
buildActionMask = 2147483647;
files = (
81AB9BB82411601600AC10FF /* LaunchScreen.storyboard in Resources */,
4EDBA3772AF91EF900D5CF41 /* test.ahap in Resources */,
13B07FBF1A68108700A75B9A /* Images.xcassets in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -609,7 +621,8 @@
"-ld_classic",
"-Wl",
"-ld_classic",
"-Wl -ld_classic ",
"-Wl",
"-ld_classic",
);
REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native";
SDKROOT = iphoneos;
Expand Down Expand Up @@ -686,7 +699,8 @@
"-ld_classic",
"-Wl",
"-ld_classic",
"-Wl -ld_classic ",
"-Wl",
"-ld_classic",
);
REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native";
SDKROOT = iphoneos;
Expand Down
66 changes: 36 additions & 30 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';

import type { HapticType } from '@candlefinance/haptics';
import { haptic, hapticWithPattern } from '@candlefinance/haptics';
import { haptic, hapticWithPattern, play } from '@candlefinance/haptics';
import { Pressable, StyleSheet, Text, View } from 'react-native';

export default function App() {
Expand Down Expand Up @@ -36,39 +36,45 @@ export default function App() {
style={styles.button}
onPress={() => {
console.log('hapticWithPattern');
hapticWithPattern(
[
'.',
'.',
'.',
'o',
'O',
'-',
'O',
'o',
'.',
'.',
'.',
'.',
'.',
'.',
'.',
'o',
'O',
'-',
'O',
'o',
'.',
'.',
'.',
'.',
],
1
);
hapticWithPattern([
'.',
'.',
'.',
'o',
'O',
'-',
'O',
'o',
'.',
'.',
'.',
'.',
'.',
'.',
'.',
'o',
'O',
'=',
'O',
'o',
'.',
'.',
'.',
'.',
]);
}}
>
<Text>Pattern</Text>
</Pressable>
<Pressable
style={styles.button}
onPress={() => {
console.log('hapticWithPatternFile');
play('test');
}}
>
<Text>Play File</Text>
</Pressable>
</View>
);
}
Expand Down
3 changes: 2 additions & 1 deletion ios/Haptics.mm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
@interface RCT_EXTERN_MODULE(Haptics, NSObject)

RCT_EXTERN_METHOD(haptic:(NSString *)type)
RCT_EXTERN_METHOD(hapticWithPattern:(NSArray<NSString *> *)pattern delay:(nonnull NSNumber *)delay)
RCT_EXTERN_METHOD(hapticWithPattern:(NSArray<NSString *> *)pattern)
RCT_EXTERN_METHOD(play:(nonnull NSString *)fileName loop:(nonnull BOOL)loop)

+ (BOOL)requiresMainQueueSetup
{
Expand Down
9 changes: 7 additions & 2 deletions ios/Haptics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class Haptics: NSObject {
}
}

@objc(hapticWithPattern:delay:)
func hapticWithPattern(pattern: [String], delay: Double) {
@objc(hapticWithPattern:)
func hapticWithPattern(pattern: [String]) {
print("running haptic pattern", pattern)
try? HapticsHelper.initialize()
var components: [HapticsHelper.HapticPatternComponent] = []
Expand Down Expand Up @@ -62,4 +62,9 @@ class Haptics: NSObject {
}
try? HapticsHelper.generateHaptic(fromComponents: components).play()
}

@objc(play:loop:)
func play(fileName: String, loop: Bool) {
Vibrator.shared.startHaptic(named: fileName, loop: loop)
}
}
Loading

0 comments on commit d6d7283

Please sign in to comment.