forked from Estimote/react-native-proximity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
157 lines (133 loc) · 3.9 KB
/
index.js
File metadata and controls
157 lines (133 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// @flow
"use strict";
import {
NativeModules,
NativeEventEmitter,
Platform,
PermissionsAndroid
} from "react-native";
const { RNEstimoteProximity } = NativeModules;
const RNEstimoteProximityEmitter = new NativeEventEmitter(RNEstimoteProximity);
export class ProximityContext {
tag: string;
attachments: { [string]: string };
deviceIdentifier: string;
static fromJSON(json: any): ProximityContext {
const context = new ProximityContext();
context.tag = json["tag"];
context.attachments = json["attachments"];
context.deviceIdentifier = json["deviceIdentifier"];
return context;
}
}
export class ProximityZone {
range: number;
tag: string;
onEnterAction: (context: ProximityContext) => void;
onExitAction: (context: ProximityContext) => void;
onChangeAction: (contexts: Array<ProximityContext>) => void;
_id: string;
constructor(range: number, tag: string) {
this.range = range;
this.tag = tag;
this._id = Math.random()
.toString(36)
.substring(2);
}
}
export class CloudCredentials {
appId: string;
appToken: string;
constructor(appId: string, appToken: string) {
this.appId = appId;
this.appToken = appToken;
}
}
type ProximityObserverConfig = {
notification?: {
title?: string,
text?: string,
icon?: string
}
};
// singleton object
export const proximityObserver = {
initialize(credentials: CloudCredentials, config: ProximityObserverConfig) {
RNEstimoteProximity.initialize(
Object.assign({}, config, {
appId: credentials.appId,
appToken: credentials.appToken
})
);
},
startObservingZones(zones: Array<ProximityZone>) {
const zonesById: Map<string, ProximityZone> = zones.reduce((map, z) => {
return map.set(z._id, z);
}, new Map());
this.onEnterSubscription = RNEstimoteProximityEmitter.addListener(
`Enter`,
event => {
// $FlowFixMe
const onEnterAction = zonesById.get(event.zoneId).onEnterAction;
if (typeof onEnterAction === "function") {
const context = ProximityContext.fromJSON(event.context);
onEnterAction(context);
}
}
);
this.onExitSubscription = RNEstimoteProximityEmitter.addListener(
`Exit`,
event => {
// $FlowFixMe
const onExitAction = zonesById.get(event.zoneId).onExitAction;
if (typeof onExitAction === "function") {
const context = ProximityContext.fromJSON(event.context);
onExitAction(context);
}
}
);
this.onChangeSubscription = RNEstimoteProximityEmitter.addListener(
`Change`,
event => {
// $FlowFixMe
const onChangeAction = zonesById.get(event.zoneId).onChangeAction;
if (typeof onChangeAction === "function") {
const contexts = event.contexts.map(context =>
ProximityContext.fromJSON(context)
);
onChangeAction(contexts);
}
}
);
const zonesJSON = zones.map(z => ({
_id: z._id,
range: z.range,
tag: z.tag
}));
RNEstimoteProximity.startObservingZones(zonesJSON);
},
stopObservingZones() {
RNEstimoteProximity.stopObservingZones();
this.onEnterSubscription.remove();
this.onExitSubscription.remove();
this.onChangeSubscription.remove();
}
};
type PermissionStatus = "always" | "when_in_use" | "denied";
export const locationPermission = {
ALWAYS: "always",
WHEN_IN_USE: "when_in_use",
DENIED: "denied",
request: async (): Promise<PermissionStatus> => {
if (Platform.OS === "ios") {
const result = await RNEstimoteProximity.requestLocationPermission();
return result;
} else if (Platform.OS === "android") {
const result = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION
);
return result === "granted" ? "always" : "denied";
}
throw "Unsupported platform";
}
};