Skip to content

Commit c0abfb5

Browse files
author
Ivan Mikhailovskii
committed
core motion
1 parent 8d01a2e commit c0abfb5

20 files changed

Lines changed: 197 additions & 34694 deletions

example/src/App.tsx

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +0,0 @@
1-
import * as React from 'react';
2-
3-
import { StyleSheet, View, Text } from 'react-native';
4-
import { multiply } from 'react-native-sq-core-motion';
5-
6-
export default function App() {
7-
const [result, setResult] = React.useState<number | undefined>();
8-
9-
React.useEffect(() => {
10-
multiply(3, 7).then(setResult);
11-
}, []);
12-
13-
return (
14-
<View style={styles.container}>
15-
<Text>Result: {result}</Text>
16-
</View>
17-
);
18-
}
19-
20-
const styles = StyleSheet.create({
21-
container: {
22-
flex: 1,
23-
alignItems: 'center',
24-
justifyContent: 'center',
25-
},
26-
box: {
27-
width: 60,
28-
height: 60,
29-
marginVertical: 20,
30-
},
31-
});

ios/SQCoreMotion.m

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#import <React/RCTBridgeModule.h>
2+
3+
@interface RCT_EXTERN_MODULE (SQCoreMotion, NSObject)
4+
5+
RCT_EXTERN_METHOD(subscribeStepCount:
6+
(RCTPromiseResolveBlock)resolve
7+
reject:(RCTPromiseRejectBlock)reject)
8+
9+
RCT_EXTERN_METHOD(subscribeDistance:
10+
(RCTPromiseResolveBlock)resolve
11+
reject:(RCTPromiseRejectBlock)reject)
12+
13+
RCT_EXTERN_METHOD(unSubscribeStepCount:
14+
(RCTPromiseResolveBlock)resolve
15+
reject:(RCTPromiseRejectBlock)reject)
16+
17+
RCT_EXTERN_METHOD(unSubscribeDistance:
18+
(RCTPromiseResolveBlock)resolve
19+
reject:(RCTPromiseRejectBlock)reject)
20+
21+
@end
Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ class SQCoreMotionManager {
4343
self.onStart()
4444
}
4545

46+
private func checkAuthorizationStatus() {
47+
switch CMMotionActivityManager.authorizationStatus() {
48+
case CMAuthorizationStatus.denied:
49+
onStop()
50+
self.activityTypeHandler?(.notAvailable)
51+
self.stepCountHandler?(0)
52+
default:break
53+
}
54+
}
55+
4656
private func onStart() {
4757
if startDate != nil { return }
4858

@@ -51,7 +61,7 @@ class SQCoreMotionManager {
5161
startUpdating()
5262
}
5363

54-
private func onStop() {
64+
func onStop() {
5565
self.startDate = nil
5666
self.stopUpdating()
5767

@@ -74,16 +84,6 @@ class SQCoreMotionManager {
7484
}
7585
}
7686

77-
private func checkAuthorizationStatus() {
78-
switch CMMotionActivityManager.authorizationStatus() {
79-
case CMAuthorizationStatus.denied:
80-
onStop()
81-
self.activityTypeHandler?(.notAvailable)
82-
self.stepCountHandler?(0)
83-
default:break
84-
}
85-
}
86-
8787
private func stopUpdating() {
8888
activityManager.stopActivityUpdates()
8989
pedometer.stopUpdates()
@@ -141,4 +141,3 @@ class SQCoreMotionManager {
141141
}
142142

143143
}
144-

ios/SqCoreMotion-Bridging-Header.h

Lines changed: 0 additions & 2 deletions
This file was deleted.

ios/SqCoreMotion.mm

Lines changed: 0 additions & 9 deletions
This file was deleted.

ios/SqCoreMotion.swift

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class SQCoreMotion: RCTEventEmitter {
1010
override init() {
1111
super.init()
1212

13-
RNHealthTracker.emitter = self
13+
SQCoreMotion.emitter = self
1414
}
1515

1616
@objc
@@ -28,21 +28,49 @@ class SQCoreMotion: RCTEventEmitter {
2828
}
2929

3030
@objc public func subscribeStepCount(
31-
resolve: @escaping RCTPromiseResolveBlock,
31+
_ resolve: @escaping RCTPromiseResolveBlock,
3232
reject: @escaping RCTPromiseRejectBlock
3333
) {
3434
self.coreMotionManager
3535
.subscribeStepCount { stepCount in
36-
RNHealthTracker.emitter?.sendEvent(
36+
SQCoreMotion.emitter?.sendEvent(
3737
withName: "update_step_count",
38-
body: [stepCount]
38+
body: stepCount
3939
)
4040
} activityTypeHandler: { activityType in
41-
RNHealthTracker.emitter?.sendEvent(
41+
SQCoreMotion.emitter?.sendEvent(
4242
withName: "update_activity_type",
43-
body: [activityType.rawValue]
43+
body: activityType.rawValue
4444
)
4545
}
46-
resolve(true)
46+
resolve(())
47+
}
48+
49+
@objc public func subscribeDistance(
50+
_ resolve: @escaping RCTPromiseResolveBlock,
51+
reject: @escaping RCTPromiseRejectBlock
52+
) {
53+
self.coreMotionManager
54+
.subscribeDistance { distance in
55+
SQCoreMotion.emitter?.sendEvent(
56+
withName: "update_distance",
57+
body: distance
58+
)
59+
}
60+
resolve(())
61+
}
62+
63+
@objc public func unSubscribeStepCount(
64+
_ resolve: @escaping RCTPromiseResolveBlock,
65+
reject: @escaping RCTPromiseRejectBlock
66+
) {
67+
self.coreMotionManager.onStop()
68+
}
69+
70+
@objc public func unSubscribeDistance(
71+
_ resolve: @escaping RCTPromiseResolveBlock,
72+
reject: @escaping RCTPromiseRejectBlock
73+
) {
74+
self.coreMotionManager.onStop()
4775
}
4876
}

lefthook.yml

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +0,0 @@
1-
pre-commit:
2-
parallel: true
3-
commands:
4-
lint:
5-
glob: "*.{js,ts,jsx,tsx}"
6-
run: npx eslint {staged_files}
7-
types:
8-
glob: "*.{js,ts, jsx, tsx}"
9-
run: npx tsc --noEmit
10-
commit-msg:
11-
parallel: true
12-
commands:
13-
commitlint:
14-
run: npx commitlint --edit

0 commit comments

Comments
 (0)