@capawesome/capacitor-gyroscope 0.0.1

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.
Files changed (38) hide show
  1. package/CapawesomeCapacitorGyroscope.podspec +17 -0
  2. package/LICENSE +21 -0
  3. package/Package.swift +28 -0
  4. package/README.md +333 -0
  5. package/android/build.gradle +58 -0
  6. package/android/src/main/AndroidManifest.xml +2 -0
  7. package/android/src/main/java/io/capawesome/capacitorjs/plugins/gyroscope/Gyroscope.java +97 -0
  8. package/android/src/main/java/io/capawesome/capacitorjs/plugins/gyroscope/GyroscopePlugin.java +152 -0
  9. package/android/src/main/java/io/capawesome/capacitorjs/plugins/gyroscope/classes/Measurement.java +28 -0
  10. package/android/src/main/java/io/capawesome/capacitorjs/plugins/gyroscope/classes/interfaces/Callback.java +5 -0
  11. package/android/src/main/java/io/capawesome/capacitorjs/plugins/gyroscope/classes/interfaces/EmptyCallback.java +5 -0
  12. package/android/src/main/java/io/capawesome/capacitorjs/plugins/gyroscope/classes/interfaces/NonEmptyResultCallback.java +7 -0
  13. package/android/src/main/java/io/capawesome/capacitorjs/plugins/gyroscope/classes/interfaces/Result.java +7 -0
  14. package/android/src/main/java/io/capawesome/capacitorjs/plugins/gyroscope/classes/results/GetMeasurementResult.java +21 -0
  15. package/android/src/main/java/io/capawesome/capacitorjs/plugins/gyroscope/classes/results/IsAvailableResult.java +20 -0
  16. package/android/src/main/res/.gitkeep +0 -0
  17. package/dist/docs.json +358 -0
  18. package/dist/esm/definitions.d.ts +112 -0
  19. package/dist/esm/definitions.js +2 -0
  20. package/dist/esm/definitions.js.map +1 -0
  21. package/dist/esm/index.d.ts +4 -0
  22. package/dist/esm/index.js +7 -0
  23. package/dist/esm/index.js.map +1 -0
  24. package/dist/esm/web.d.ts +18 -0
  25. package/dist/esm/web.js +125 -0
  26. package/dist/esm/web.js.map +1 -0
  27. package/dist/plugin.cjs.js +139 -0
  28. package/dist/plugin.cjs.js.map +1 -0
  29. package/dist/plugin.js +142 -0
  30. package/dist/plugin.js.map +1 -0
  31. package/ios/Plugin/Classes/Measurement.swift +24 -0
  32. package/ios/Plugin/Classes/Results/IsAvailableResult.swift +15 -0
  33. package/ios/Plugin/Enums/CustomError.swift +20 -0
  34. package/ios/Plugin/Gyroscope.swift +87 -0
  35. package/ios/Plugin/GyroscopePlugin.swift +116 -0
  36. package/ios/Plugin/Info.plist +24 -0
  37. package/ios/Plugin/Protocols/Result.swift +5 -0
  38. package/package.json +91 -0
@@ -0,0 +1,20 @@
1
+ import Foundation
2
+
3
+ enum CustomError: Error {
4
+ case dataNotReceived
5
+ case notAvailable
6
+ case privacyDescriptionsMissing
7
+ }
8
+
9
+ extension CustomError: LocalizedError {
10
+ public var errorDescription: String? {
11
+ switch self {
12
+ case .dataNotReceived:
13
+ return NSLocalizedString("No gyroscope data received.", comment: "dataNotReceived")
14
+ case .notAvailable:
15
+ return NSLocalizedString("Not available on this device.", comment: "notAvailable")
16
+ case .privacyDescriptionsMissing:
17
+ return NSLocalizedString("One or more privacy descriptions are missing.", comment: "privacyDescriptionsMissing")
18
+ }
19
+ }
20
+ }
@@ -0,0 +1,87 @@
1
+ import Foundation
2
+ import CoreMotion
3
+
4
+ @objc public class Gyroscope: NSObject {
5
+ private var isRunning = false
6
+ private var lastMeasurement: Measurement?
7
+ private lazy var motionManager = CMMotionManager()
8
+ private let plugin: GyroscopePlugin
9
+ private let queue = OperationQueue()
10
+ private var updateInterval: TimeInterval = 0.25
11
+
12
+ init(plugin: GyroscopePlugin) {
13
+ self.plugin = plugin
14
+ }
15
+
16
+ @objc func getMeasurement(completion: @escaping (Measurement?, Error?) -> Void) {
17
+ guard motionManager.isGyroAvailable else {
18
+ completion(nil, CustomError.notAvailable)
19
+ return
20
+ }
21
+
22
+ if isRunning, let lastMeasurement = lastMeasurement {
23
+ completion(lastMeasurement, nil)
24
+ return
25
+ }
26
+
27
+ motionManager.gyroUpdateInterval = updateInterval
28
+ motionManager.startGyroUpdates(to: queue) { data, error in
29
+ if let error = error {
30
+ self.motionManager.stopGyroUpdates()
31
+ completion(nil, error)
32
+ return
33
+ }
34
+
35
+ guard let data = data else {
36
+ self.motionManager.stopGyroUpdates()
37
+ completion(nil, CustomError.dataNotReceived)
38
+ return
39
+ }
40
+
41
+ self.lastMeasurement = Measurement(data)
42
+ self.motionManager.stopGyroUpdates()
43
+ completion(self.lastMeasurement, nil)
44
+ }
45
+ }
46
+
47
+ @objc func isAvailable(completion: @escaping (IsAvailableResult?, Error?) -> Void) {
48
+ completion(IsAvailableResult(isAvailable: motionManager.isGyroAvailable), nil)
49
+ }
50
+
51
+ @objc public func startMeasurementUpdates(completion: @escaping (Error?) -> Void) {
52
+ guard motionManager.isGyroAvailable else {
53
+ completion(CustomError.notAvailable)
54
+ return
55
+ }
56
+ guard !isRunning else {
57
+ completion(nil)
58
+ return
59
+ }
60
+
61
+ motionManager.gyroUpdateInterval = updateInterval
62
+ motionManager.startGyroUpdates(to: queue) { data, _ in
63
+ if let data = data {
64
+ self.lastMeasurement = Measurement(data)
65
+ if let measurement = self.lastMeasurement {
66
+ self.plugin.handleMeasurementEvent(measurement)
67
+ }
68
+ }
69
+ }
70
+
71
+ isRunning = true
72
+ completion(nil)
73
+ }
74
+
75
+ @objc func stopMeasurementUpdates(completion: ((Error?) -> Void)? = nil) {
76
+ guard motionManager.isGyroAvailable else {
77
+ completion?(CustomError.notAvailable)
78
+ return
79
+ }
80
+
81
+ motionManager.stopGyroUpdates()
82
+ if let completion = completion {
83
+ isRunning = false
84
+ completion(nil)
85
+ }
86
+ }
87
+ }
@@ -0,0 +1,116 @@
1
+ import Foundation
2
+ import Capacitor
3
+
4
+ @objc(GyroscopePlugin)
5
+ public class GyroscopePlugin: CAPPlugin, CAPBridgedPlugin {
6
+ public let identifier = "GyroscopePlugin"
7
+ public let jsName = "Gyroscope"
8
+ public let pluginMethods: [CAPPluginMethod] = [
9
+ CAPPluginMethod(name: "getMeasurement", returnType: CAPPluginReturnPromise),
10
+ CAPPluginMethod(name: "isAvailable", returnType: CAPPluginReturnPromise),
11
+ CAPPluginMethod(name: "startMeasurementUpdates", returnType: CAPPluginReturnPromise),
12
+ CAPPluginMethod(name: "stopMeasurementUpdates", returnType: CAPPluginReturnPromise),
13
+ CAPPluginMethod(name: "checkPermissions", returnType: CAPPluginReturnPromise),
14
+ CAPPluginMethod(name: "requestPermissions", returnType: CAPPluginReturnPromise)
15
+ ]
16
+
17
+ public static let measurementEvent = "measurement"
18
+
19
+ public static let tag = "GyroscopePlugin"
20
+
21
+ private var implementation: Gyroscope?
22
+
23
+ override public func load() {
24
+ self.implementation = Gyroscope(plugin: self)
25
+ }
26
+
27
+ @objc override public func checkPermissions(_ call: CAPPluginCall) {
28
+ call.resolve(["gyroscope": "granted"])
29
+ }
30
+
31
+ @objc func getMeasurement(_ call: CAPPluginCall) {
32
+ if !hasUsageDescription(forKey: "NSMotionUsageDescription") {
33
+ rejectCall(call, CustomError.privacyDescriptionsMissing)
34
+ return
35
+ }
36
+
37
+ implementation?.getMeasurement { result, error in
38
+ if let error = error {
39
+ self.rejectCall(call, error)
40
+ return
41
+ }
42
+ self.resolveCall(call, result)
43
+ }
44
+ }
45
+
46
+ @objc func handleMeasurementEvent(_ event: Measurement) {
47
+ if let data = event.toJSObject() as? JSObject {
48
+ notifyListeners(GyroscopePlugin.measurementEvent, data: data)
49
+ }
50
+ }
51
+
52
+ @objc func isAvailable(_ call: CAPPluginCall) {
53
+ implementation?.isAvailable { result, error in
54
+ if let error = error {
55
+ self.rejectCall(call, error)
56
+ return
57
+ }
58
+ self.resolveCall(call, result)
59
+ }
60
+ }
61
+
62
+ @objc override public func removeAllListeners(_ call: CAPPluginCall) {
63
+ super.removeAllListeners(call)
64
+ implementation?.stopMeasurementUpdates()
65
+ }
66
+
67
+ @objc override public func requestPermissions(_ call: CAPPluginCall) {
68
+ call.resolve(["gyroscope": "granted"])
69
+ }
70
+
71
+ @objc func startMeasurementUpdates(_ call: CAPPluginCall) {
72
+ if !hasUsageDescription(forKey: "NSMotionUsageDescription") {
73
+ rejectCall(call, CustomError.privacyDescriptionsMissing)
74
+ return
75
+ }
76
+
77
+ implementation?.startMeasurementUpdates { error in
78
+ if let error = error {
79
+ self.rejectCall(call, error)
80
+ return
81
+ }
82
+ self.resolveCall(call)
83
+ }
84
+ }
85
+
86
+ @objc func stopMeasurementUpdates(_ call: CAPPluginCall) {
87
+ implementation?.stopMeasurementUpdates { error in
88
+ if let error = error {
89
+ self.rejectCall(call, error)
90
+ return
91
+ }
92
+ self.resolveCall(call)
93
+ }
94
+ }
95
+
96
+ private func hasUsageDescription(forKey key: String) -> Bool {
97
+ return Bundle.main.object(forInfoDictionaryKey: key) is String
98
+ }
99
+
100
+ private func rejectCall(_ call: CAPPluginCall, _ error: Error) {
101
+ CAPLog.print("[", GyroscopePlugin.tag, "] ", error)
102
+ call.reject(error.localizedDescription)
103
+ }
104
+
105
+ private func resolveCall(_ call: CAPPluginCall) {
106
+ call.resolve()
107
+ }
108
+
109
+ private func resolveCall(_ call: CAPPluginCall, _ result: Result?) {
110
+ if let result = result?.toJSObject() as? JSObject {
111
+ call.resolve(result)
112
+ } else {
113
+ call.resolve()
114
+ }
115
+ }
116
+ }
@@ -0,0 +1,24 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3
+ <plist version="1.0">
4
+ <dict>
5
+ <key>CFBundleDevelopmentRegion</key>
6
+ <string>$(DEVELOPMENT_LANGUAGE)</string>
7
+ <key>CFBundleExecutable</key>
8
+ <string>$(EXECUTABLE_NAME)</string>
9
+ <key>CFBundleIdentifier</key>
10
+ <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
11
+ <key>CFBundleInfoDictionaryVersion</key>
12
+ <string>6.0</string>
13
+ <key>CFBundleName</key>
14
+ <string>$(PRODUCT_NAME)</string>
15
+ <key>CFBundlePackageType</key>
16
+ <string>FMWK</string>
17
+ <key>CFBundleShortVersionString</key>
18
+ <string>1.0</string>
19
+ <key>CFBundleVersion</key>
20
+ <string>$(CURRENT_PROJECT_VERSION)</string>
21
+ <key>NSPrincipalClass</key>
22
+ <string></string>
23
+ </dict>
24
+ </plist>
@@ -0,0 +1,5 @@
1
+ import Capacitor
2
+
3
+ @objc public protocol Result {
4
+ @objc func toJSObject() -> AnyObject
5
+ }
package/package.json ADDED
@@ -0,0 +1,91 @@
1
+ {
2
+ "name": "@capawesome/capacitor-gyroscope",
3
+ "version": "0.0.1",
4
+ "description": "Capacitor plugin to read the device's gyroscope sensor.",
5
+ "main": "dist/plugin.cjs.js",
6
+ "module": "dist/esm/index.js",
7
+ "types": "dist/esm/index.d.ts",
8
+ "unpkg": "dist/plugin.js",
9
+ "files": [
10
+ "android/src/main/",
11
+ "android/build.gradle",
12
+ "dist/",
13
+ "ios/Plugin/",
14
+ "CapawesomeCapacitorGyroscope.podspec",
15
+ "Package.swift"
16
+ ],
17
+ "author": "Robin Genz <mail@robingenz.dev>",
18
+ "license": "MIT",
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/capawesome-team/capacitor-plugins.git"
22
+ },
23
+ "bugs": {
24
+ "url": "https://github.com/capawesome-team/capacitor-plugins/issues"
25
+ },
26
+ "funding": [
27
+ {
28
+ "type": "github",
29
+ "url": "https://github.com/sponsors/capawesome-team/"
30
+ },
31
+ {
32
+ "type": "opencollective",
33
+ "url": "https://opencollective.com/capawesome"
34
+ }
35
+ ],
36
+ "homepage": "https://capawesome.io/docs/plugins/gyroscope/",
37
+ "keywords": [
38
+ "capacitor",
39
+ "plugin",
40
+ "native"
41
+ ],
42
+ "scripts": {
43
+ "verify": "npm run verify:ios && npm run verify:android && npm run verify:web",
44
+ "verify:ios": "cd ios && pod install && xcodebuild -workspace Plugin.xcworkspace -scheme Plugin -destination generic/platform=iOS && cd ..",
45
+ "verify:android": "cd android && ./gradlew clean build test && cd ..",
46
+ "verify:web": "npm run build",
47
+ "lint": "npm run eslint && npm run prettier -- --check && npm run swiftlint -- lint",
48
+ "fmt": "npm run eslint -- --fix && npm run prettier -- --write && npm run swiftlint -- --fix --format",
49
+ "eslint": "eslint . --ext ts",
50
+ "prettier": "prettier \"**/*.{css,html,ts,js,java}\"",
51
+ "swiftlint": "node-swiftlint",
52
+ "docgen": "docgen --api GyroscopePlugin --output-readme README.md --output-json dist/docs.json",
53
+ "build": "npm run clean && npm run docgen && tsc && rollup -c rollup.config.mjs",
54
+ "clean": "rimraf ./dist",
55
+ "watch": "tsc --watch",
56
+ "ios:pod:install": "cd ios && pod install --repo-update && cd ..",
57
+ "ios:spm:install": "cd ios && swift package resolve && cd ..",
58
+ "prepublishOnly": "npm run build"
59
+ },
60
+ "devDependencies": {
61
+ "@capacitor/android": "8.0.0",
62
+ "@capacitor/cli": "8.0.0",
63
+ "@capacitor/core": "8.0.0",
64
+ "@capacitor/docgen": "0.3.1",
65
+ "@capacitor/ios": "8.0.0",
66
+ "@ionic/eslint-config": "0.4.0",
67
+ "eslint": "8.57.0",
68
+ "prettier-plugin-java": "2.6.7",
69
+ "rimraf": "6.1.2",
70
+ "rollup": "4.53.3",
71
+ "swiftlint": "2.0.0",
72
+ "typescript": "5.9.3"
73
+ },
74
+ "peerDependencies": {
75
+ "@capacitor/core": ">=8.0.0"
76
+ },
77
+ "eslintConfig": {
78
+ "extends": "@ionic/eslint-config/recommended"
79
+ },
80
+ "capacitor": {
81
+ "ios": {
82
+ "src": "ios"
83
+ },
84
+ "android": {
85
+ "src": "android"
86
+ }
87
+ },
88
+ "publishConfig": {
89
+ "access": "public"
90
+ }
91
+ }