@interval-health/capacitor-health 1.0.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.
@@ -0,0 +1,137 @@
1
+ import Foundation
2
+ import Capacitor
3
+
4
+ @objc(HealthPlugin)
5
+ public class HealthPlugin: CAPPlugin, CAPBridgedPlugin {
6
+ private let pluginVersion: String = "8.0.1"
7
+ public let identifier = "HealthPlugin"
8
+ public let jsName = "Health"
9
+ public let pluginMethods: [CAPPluginMethod] = [
10
+ CAPPluginMethod(name: "isAvailable", returnType: CAPPluginReturnPromise),
11
+ CAPPluginMethod(name: "requestAuthorization", returnType: CAPPluginReturnPromise),
12
+ CAPPluginMethod(name: "checkAuthorization", returnType: CAPPluginReturnPromise),
13
+ CAPPluginMethod(name: "readSamples", returnType: CAPPluginReturnPromise),
14
+ CAPPluginMethod(name: "saveSample", returnType: CAPPluginReturnPromise),
15
+ CAPPluginMethod(name: "getPluginVersion", returnType: CAPPluginReturnPromise)
16
+ ]
17
+
18
+ private let implementation = Health()
19
+
20
+ @objc func isAvailable(_ call: CAPPluginCall) {
21
+ call.resolve(implementation.availabilityPayload())
22
+ }
23
+
24
+ @objc func requestAuthorization(_ call: CAPPluginCall) {
25
+ let read = (call.getArray("read") as? [String]) ?? []
26
+ let write = (call.getArray("write") as? [String]) ?? []
27
+
28
+ implementation.requestAuthorization(readIdentifiers: read, writeIdentifiers: write) { result in
29
+ DispatchQueue.main.async {
30
+ switch result {
31
+ case let .success(payload):
32
+ call.resolve(payload.toDictionary())
33
+ case let .failure(error):
34
+ call.reject(error.localizedDescription, nil, error)
35
+ }
36
+ }
37
+ }
38
+ }
39
+
40
+ @objc func checkAuthorization(_ call: CAPPluginCall) {
41
+ let read = (call.getArray("read") as? [String]) ?? []
42
+ let write = (call.getArray("write") as? [String]) ?? []
43
+
44
+ implementation.checkAuthorization(readIdentifiers: read, writeIdentifiers: write) { result in
45
+ DispatchQueue.main.async {
46
+ switch result {
47
+ case let .success(payload):
48
+ call.resolve(payload.toDictionary())
49
+ case let .failure(error):
50
+ call.reject(error.localizedDescription, nil, error)
51
+ }
52
+ }
53
+ }
54
+ }
55
+
56
+ @objc func readSamples(_ call: CAPPluginCall) {
57
+ guard let dataType = call.getString("dataType") else {
58
+ call.reject("dataType is required")
59
+ return
60
+ }
61
+
62
+ let startDate = call.getString("startDate")
63
+ let endDate = call.getString("endDate")
64
+ let limit = call.getInt("limit")
65
+ let ascending = call.getBool("ascending") ?? false
66
+
67
+ do {
68
+ try implementation.readSamples(
69
+ dataTypeIdentifier: dataType,
70
+ startDateString: startDate,
71
+ endDateString: endDate,
72
+ limit: limit,
73
+ ascending: ascending
74
+ ) { result in
75
+ DispatchQueue.main.async {
76
+ switch result {
77
+ case let .success(samples):
78
+ call.resolve(["samples": samples])
79
+ case let .failure(error):
80
+ call.reject(error.localizedDescription, nil, error)
81
+ }
82
+ }
83
+ }
84
+ } catch {
85
+ call.reject(error.localizedDescription, nil, error)
86
+ }
87
+ }
88
+
89
+ @objc func saveSample(_ call: CAPPluginCall) {
90
+ guard let dataType = call.getString("dataType") else {
91
+ call.reject("dataType is required")
92
+ return
93
+ }
94
+
95
+ guard let value = call.getDouble("value") else {
96
+ call.reject("value is required")
97
+ return
98
+ }
99
+
100
+ let unit = call.getString("unit")
101
+ let startDate = call.getString("startDate")
102
+ let endDate = call.getString("endDate")
103
+ let metadataAny = call.getObject("metadata") as? [String: Any]
104
+ let metadata = metadataAny?.reduce(into: [String: String]()) { result, entry in
105
+ if let stringValue = entry.value as? String {
106
+ result[entry.key] = stringValue
107
+ }
108
+ }
109
+
110
+ do {
111
+ try implementation.saveSample(
112
+ dataTypeIdentifier: dataType,
113
+ value: value,
114
+ unitIdentifier: unit,
115
+ startDateString: startDate,
116
+ endDateString: endDate,
117
+ metadata: metadata
118
+ ) { result in
119
+ DispatchQueue.main.async {
120
+ switch result {
121
+ case .success:
122
+ call.resolve()
123
+ case let .failure(error):
124
+ call.reject(error.localizedDescription, nil, error)
125
+ }
126
+ }
127
+ }
128
+ } catch {
129
+ call.reject(error.localizedDescription, nil, error)
130
+ }
131
+ }
132
+
133
+ @objc func getPluginVersion(_ call: CAPPluginCall) {
134
+ call.resolve(["version": self.pluginVersion])
135
+ }
136
+
137
+ }
@@ -0,0 +1,15 @@
1
+ import XCTest
2
+ @testable import HealthPlugin
3
+
4
+ class HealthTests: XCTestCase {
5
+ func testEcho() {
6
+ // This is an example of a functional test case for a plugin.
7
+ // Use XCTAssert and related functions to verify your tests produce the correct results.
8
+
9
+ let implementation = Health()
10
+ let value = "Hello, World!"
11
+ let result = implementation.echo(value)
12
+
13
+ XCTAssertEqual(value, result)
14
+ }
15
+ }
package/package.json ADDED
@@ -0,0 +1,82 @@
1
+ {
2
+ "name": "@interval-health/capacitor-health",
3
+ "version": "1.0.0",
4
+ "description": "Capacitor plugin to interact with data from Apple HealthKit and Health Connect",
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/Sources",
14
+ "ios/Tests",
15
+ "Package.swift",
16
+ "CapgoCapacitorHealth.podspec"
17
+ ],
18
+ "author": "Martin Donadieu <martin@capgo.app>",
19
+ "license": "MPL-2.0",
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "git+https://github.com/sandip-3008/capacitor-health"
23
+ },
24
+ "bugs": {
25
+ "url": "https://github.com/sandip-3008/capacitor-health/issues"
26
+ },
27
+ "keywords": [
28
+ "capacitor",
29
+ "plugin",
30
+ "native"
31
+ ],
32
+ "scripts": {
33
+ "verify": "npm run verify:ios && npm run verify:android && npm run verify:web",
34
+ "verify:ios": "xcodebuild -scheme CapgoCapacitorHealth -destination generic/platform=iOS",
35
+ "verify:android": "cd android && ./gradlew clean build test && cd ..",
36
+ "verify:web": "npm run build",
37
+ "lint": "npm run eslint && npm run prettier -- --check && npm run swiftlint -- lint",
38
+ "fmt": "npm run eslint -- --fix && npm run prettier -- --write && npm run swiftlint -- --fix --format",
39
+ "eslint": "eslint . --ext ts",
40
+ "prettier": "prettier \"**/*.{css,html,ts,js,java}\" --plugin=prettier-plugin-java",
41
+ "swiftlint": "node-swiftlint",
42
+ "docgen": "docgen --api HealthPlugin --output-readme README.md --output-json dist/docs.json",
43
+ "build": "npm run clean && npm run docgen && tsc && rollup -c rollup.config.mjs",
44
+ "clean": "rimraf ./dist",
45
+ "watch": "tsc --watch",
46
+ "prepublishOnly": "npm run build"
47
+ },
48
+ "devDependencies": {
49
+ "@capacitor/android": "^8.0.0",
50
+ "@capacitor/core": "^8.0.0",
51
+ "@capacitor/docgen": "^0.3.1",
52
+ "@capacitor/ios": "^8.0.0",
53
+ "@ionic/eslint-config": "^0.4.0",
54
+ "@ionic/prettier-config": "^4.0.0",
55
+ "@ionic/swiftlint-config": "^2.0.0",
56
+ "@types/node": "^24.10.1",
57
+ "eslint": "^8.57.1",
58
+ "eslint-plugin-import": "^2.31.0",
59
+ "prettier": "^3.6.2",
60
+ "prettier-plugin-java": "^2.7.7",
61
+ "rimraf": "^6.1.0",
62
+ "rollup": "^4.53.2",
63
+ "swiftlint": "^2.0.0",
64
+ "typescript": "^5.9.3"
65
+ },
66
+ "peerDependencies": {
67
+ "@capacitor/core": ">=8.0.0"
68
+ },
69
+ "prettier": "@ionic/prettier-config",
70
+ "swiftlint": "@ionic/swiftlint-config",
71
+ "eslintConfig": {
72
+ "extends": "@ionic/eslint-config/recommended"
73
+ },
74
+ "capacitor": {
75
+ "ios": {
76
+ "src": "ios"
77
+ },
78
+ "android": {
79
+ "src": "android"
80
+ }
81
+ }
82
+ }