@stingjs/haptics 1.0.0-bootstrap.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/Package.swift ADDED
@@ -0,0 +1,26 @@
1
+ // swift-tools-version: 5.10
2
+
3
+ import PackageDescription
4
+
5
+ let package = Package(
6
+ name: "StingHaptics",
7
+ platforms: [
8
+ .iOS(.v16)
9
+ ],
10
+ products: [
11
+ .library(name: "StingHaptics", targets: ["StingHaptics"])
12
+ ],
13
+ dependencies: [
14
+ .package(path: "../../../native/ios")
15
+ ],
16
+ targets: [
17
+ .target(
18
+ name: "StingHaptics",
19
+ dependencies: [
20
+ .product(name: "StingRuntime", package: "ios")
21
+ ],
22
+ path: "ios"
23
+ )
24
+ ],
25
+ swiftLanguageVersions: [.v5]
26
+ )
@@ -0,0 +1,21 @@
1
+ plugins {
2
+ id("com.android.library")
3
+ }
4
+
5
+ android {
6
+ namespace = "run.stingjs.modules.haptics"
7
+ compileSdk = 36
8
+
9
+ defaultConfig {
10
+ minSdk = 23
11
+ }
12
+
13
+ compileOptions {
14
+ sourceCompatibility = JavaVersion.VERSION_17
15
+ targetCompatibility = JavaVersion.VERSION_17
16
+ }
17
+ }
18
+
19
+ dependencies {
20
+ implementation(project(":sting-runtime"))
21
+ }
@@ -0,0 +1,3 @@
1
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android">
2
+ <uses-permission android:name="android.permission.VIBRATE" />
3
+ </manifest>
@@ -0,0 +1,54 @@
1
+ package run.stingjs.modules.haptics
2
+
3
+ import android.content.Context
4
+ import android.os.Build
5
+ import android.os.VibrationEffect
6
+ import android.os.Vibrator
7
+ import android.os.VibratorManager
8
+ import run.stingjs.runtime.StingNativeModule
9
+ import run.stingjs.runtime.StingNativeModuleError
10
+
11
+ class HapticsModule(context: Context) : StingNativeModule {
12
+ override val name = "Haptics"
13
+ override val version = "0.1.0"
14
+
15
+ private val appContext = context.applicationContext
16
+
17
+ override fun callSync(method: String, arguments: List<Any?>): Any? {
18
+ if (method != "impact") {
19
+ throw StingNativeModuleError(
20
+ code = "E_METHOD_NOT_FOUND",
21
+ message = "Haptics does not implement $method",
22
+ )
23
+ }
24
+
25
+ val style = arguments.firstOrNull() as? String ?: "medium"
26
+ val (durationMs, amplitude) = when (style) {
27
+ "light" -> 12L to 70
28
+ "heavy" -> 28L to 220
29
+ "soft" -> 18L to 90
30
+ "rigid" -> 16L to 180
31
+ else -> 20L to 140
32
+ }
33
+
34
+ vibrate(durationMs, amplitude)
35
+ return null
36
+ }
37
+
38
+ @Suppress("DEPRECATION")
39
+ private fun vibrate(durationMs: Long, amplitude: Int) {
40
+ val vibrator = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
41
+ appContext.getSystemService(VibratorManager::class.java).defaultVibrator
42
+ } else {
43
+ appContext.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
44
+ }
45
+
46
+ if (!vibrator.hasVibrator()) return
47
+
48
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
49
+ vibrator.vibrate(VibrationEffect.createOneShot(durationMs, amplitude))
50
+ } else {
51
+ vibrator.vibrate(durationMs)
52
+ }
53
+ }
54
+ }
@@ -0,0 +1,33 @@
1
+ import StingRuntime
2
+ import UIKit
3
+
4
+ public final class HapticsModule: StingNativeModule {
5
+ public let name = "Haptics"
6
+ public let version = "0.1.0"
7
+
8
+ public init() {}
9
+
10
+ public func callSync(method: String, arguments: [Any]) throws -> Any? {
11
+ guard method == "impact" else {
12
+ throw StingNativeModuleError(
13
+ code: "E_METHOD_NOT_FOUND",
14
+ message: "Haptics does not implement \(method)"
15
+ )
16
+ }
17
+
18
+ let rawStyle = arguments.first as? String ?? "medium"
19
+ let style: UIImpactFeedbackGenerator.FeedbackStyle
20
+ switch rawStyle {
21
+ case "light": style = .light
22
+ case "heavy": style = .heavy
23
+ case "soft": style = .soft
24
+ case "rigid": style = .rigid
25
+ default: style = .medium
26
+ }
27
+
28
+ let generator = UIImpactFeedbackGenerator(style: style)
29
+ generator.prepare()
30
+ generator.impactOccurred()
31
+ return nil
32
+ }
33
+ }
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "@stingjs/haptics",
3
+ "version": "1.0.0-bootstrap.0",
4
+ "type": "module",
5
+ "exports": {
6
+ ".": "./src/index.ts"
7
+ },
8
+ "scripts": {
9
+ "typecheck": "tsc -p tsconfig.json --noEmit"
10
+ },
11
+ "dependencies": {
12
+ "@stingjs/modules-core": "1.0.0-bootstrap.0"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/chrisbirster/stingjs.git",
17
+ "directory": "packages/modules/haptics"
18
+ },
19
+ "publishConfig": {
20
+ "access": "public"
21
+ },
22
+ "license": "MIT"
23
+ }
package/src/index.ts ADDED
@@ -0,0 +1,15 @@
1
+ import { createNativeModule } from '@stingjs/modules-core';
2
+
3
+ export type ImpactStyle = 'light' | 'medium' | 'heavy' | 'soft' | 'rigid';
4
+
5
+ const nativeHaptics = createNativeModule('Haptics');
6
+
7
+ export const Haptics = {
8
+ isAvailable(): boolean {
9
+ return nativeHaptics.isAvailable();
10
+ },
11
+
12
+ impact(style: ImpactStyle = 'medium'): void {
13
+ nativeHaptics.callSync('impact', [style]);
14
+ },
15
+ };
@@ -0,0 +1,17 @@
1
+ {
2
+ "schemaVersion": 1,
3
+ "name": "Haptics",
4
+ "package": "@stingjs/haptics",
5
+ "version": "1.0.0-bootstrap.0",
6
+ "ios": {
7
+ "module": "HapticsModule",
8
+ "factory": "default",
9
+ "permissions": []
10
+ },
11
+ "android": {
12
+ "module": "run.stingjs.modules.haptics.HapticsModule",
13
+ "factory": "context",
14
+ "permissions": ["android.permission.VIBRATE"]
15
+ },
16
+ "capabilities": ["sync-functions"]
17
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,7 @@
1
+ {
2
+ "extends": "../../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "rootDir": "src"
5
+ },
6
+ "include": ["src"]
7
+ }