-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.js
366 lines (347 loc) · 12.5 KB
/
App.js
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import { StyleSheet, Platform, Linking, Alert, BackHandler } from "react-native";
import { WebView } from "react-native-webview";
import { registerForPushNotificationsAsync } from "./services/expo-push-notifs";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { SafeAreaProvider, SafeAreaView, useSafeAreaInsets } from "react-native-safe-area-context";
import * as SplashScreen from "expo-splash-screen";
import * as Location from "expo-location";
import * as Contacts from "expo-contacts";
import * as DeepLinking from "expo-linking";
import * as Clipboard from "expo-clipboard";
import AsyncStorage from "@react-native-async-storage/async-storage";
const APP_URL = __DEV__ ? "http://192.168.178.49:3617" : "https://kiss-my-plan.com";
SplashScreen.preventAutoHideAsync();
const initScript = `window.ENV.APP_PLATFORM = "native";`;
function App() {
const deepLink = DeepLinking.useURL();
const receivedUrl = deepLink?.includes("kiss-my-plan.com") ? deepLink : APP_URL;
const { path } = DeepLinking.parse(receivedUrl);
const url = `${APP_URL}/${path || "plans"}`;
const ref = useRef(null);
const onLoadEnd = () => {
ref.current.injectJavaScript(`window.ENV.PLATFORM_OS = "${Platform.OS}";`);
setTimeout(() => {
SplashScreen.hideAsync();
}, 2000);
};
const onPushNotification = (event) => {
const requestPush = event.nativeEvent.data === "request-native-push-permission";
const requestExpoPush = event.nativeEvent.data === "request-native-expo-push-permission";
// const readToken = event.nativeEvent.data === "request-native-get-token-if-exists";
const readExpoToken = event.nativeEvent.data === "request-native-get-expo-token";
registerForPushNotificationsAsync({
force: requestPush || requestExpoPush,
expo: readExpoToken || requestExpoPush,
}).then((token) => {
if (!token) {
return;
}
ref.current.injectJavaScript(`window.onNativePushToken('${JSON.stringify(token)}');`);
});
};
const onRequestLocation = (event) => {
const eventName = event.nativeEvent.data;
Location.requestForegroundPermissionsAsync().then(async ({ status }) => {
if (status !== "granted") {
if (eventName === "request-native-force-current-position") {
Alert.alert(
"Permission not granted to access your location",
"You can change that in your settings",
[
{ text: "Open Settings", onPress: () => Linking.openSettings() },
{ text: "OK", style: "cancel", onPress: () => {} },
]
);
}
const centerOfTheWorld = { coords: { latitude: 0, longitude: 0 } };
ref.current.injectJavaScript(
`window.onGetCurrentPosition('${JSON.stringify(centerOfTheWorld)}');`
);
ref.current.injectJavaScript(`window.onUnZoom();`);
return null;
}
const location = await Location.getCurrentPositionAsync({});
ref.current.injectJavaScript(`window.onGetCurrentPosition('${JSON.stringify(location)}');`);
});
};
const onRequestContacts = async () => {
if (Platform.OS === "android") {
const permission = await Contacts.getPermissionsAsync();
if (!permission.granted) {
if (AsyncStorage.getItem("contactsPermission") === "never") return;
if (!permission.canAskAgain) return;
const prominentPermission = await new Promise((resolve) => {
Alert.alert(
"Kiss My Plan wants to access your contacts",
"In order to see if your contacts already use Kiss My Plan, or if you want to invite any of them, we need to access your contacts.",
[
{
text: "Not yet",
onPress: () => resolve(false),
style: "cancel",
},
// {
// text: "Never ask again",
// onPress: () => {
// AsyncStorage.setItem("contactsPermission", "never");
// resolve(false);
// },
// style: "destructive",
// },
{
text: "OK",
onPress: () => resolve(true),
},
]
);
});
if (!prominentPermission) return;
}
}
Contacts.requestPermissionsAsync()
.then(async ({ status }) => {
if (status === "granted") {
return Contacts.getContactsAsync({
fields: [
Contacts.Fields.Emails,
Contacts.Fields.FirstName,
Contacts.Fields.LastName,
Contacts.Fields.PhoneNumbers,
],
});
} else {
Alert.alert(
"Permission not granted to access contacts",
"You can change that in your settings",
[
{ text: "Open Settings", onPress: () => Linking.openSettings() },
{ text: "OK", style: "cancel", onPress: () => {} },
]
);
}
return { data: [] };
})
.then(({ data }) => {
const emailRegex = new RegExp("^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$");
const phoneRegex = new RegExp("^[0-9+ ]+$");
function normalizeName(name) {
// Replacing all accented characters with their normal counterparts
const normalized = name.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
// Removing anything that's not a letter or space
const cleaned = normalized.replace(/[^a-zA-Z\s]/g, " ");
// Converting to lower case
const lowercased = cleaned.toLowerCase();
// Removing leading and trailing spaces
const trimmed = lowercased.trim();
// Replacing multiple spaces with a single space
const finalName = trimmed.replace(/\s+/g, " ");
return finalName;
}
const properData = data.reduce((flattenedAndValidatedData, contact) => {
if (!contact?.phoneNumbers?.length && !contact?.emails?.length) {
return flattenedAndValidatedData;
}
const name = contact?.name?.replace(/'/g, " ");
// keep only letters for search, and replace special characters with proper letters
if (!contact?.name) {
return flattenedAndValidatedData;
}
const forSearch = normalizeName(contact?.name);
const { phoneNumbers = [], emails = [] } = contact;
const newContacts = [];
const userPhoneNumbers = [];
for (const phone of phoneNumbers) {
// validate phone number with regex : it can have numbers and '+' and spaces only
let number = phone.digits;
if (!number) {
number = phone.number.replace(/[^0-9+]/g, "");
}
if (!phoneRegex.test(number)) {
continue;
}
userPhoneNumbers.push(number);
}
if (!emails.length) {
newContacts.push({
name,
forSearch,
email: "",
phoneNumbers: userPhoneNumbers,
});
return [...flattenedAndValidatedData, ...newContacts];
}
for (const email of emails) {
// validate email with regex
if (!emailRegex.test(email.email)) {
continue;
}
newContacts.push({
name,
forSearch,
email: email.email,
phoneNumbers: userPhoneNumbers,
});
}
return [...flattenedAndValidatedData, ...newContacts];
}, []);
ref.current.injectJavaScript(`window.onGetContacts('${JSON.stringify(properData)}');`);
});
};
const [urlFromClipboard, setUrlFromClipboard] = useState(null);
const onRequestClipboardTextContent = async (event) => {
if (event.nativeEvent.data === "request-native-clipboard-text-content-on-first-opening") {
const numberOfOpenings = await AsyncStorage.getItem("@numberOfOpenings");
if (Number(numberOfOpenings) > 1) {
ref.current.injectJavaScript(
`window.onGetClipboardTextContent('numberOfOpenings: ${numberOfOpenings}');`
);
SplashScreen.hideAsync();
return;
}
const prominentPermission = await new Promise((resolve) => {
Alert.alert(
"It's your first time on Kiss My Plan, can we check your Clipboard if you were invited by one of your friends?",
"We need to access your clipboard, it will load the link you clicked on, and if it's a Kiss My Plan link, we will check if you were invited by one of your friends to add him/her to your friends on Kiss My Plan.",
[
{
text: "Not yet",
onPress: () => resolve(false),
style: "cancel",
},
{
text: "OK",
onPress: () => resolve(true),
},
]
);
});
if (!prominentPermission) return;
}
const text = await Clipboard.getStringAsync();
if (!text.startsWith(APP_URL)) {
// ref.current.injectJavaScript(
// `window.onGetClipboardTextContent('text.startsWith(APP_URL): ${text}');`
// );
SplashScreen.hideAsync();
return;
}
setUrlFromClipboard(text);
ref.current.injectJavaScript(`window.onGetClipboardTextContent('${text}');`);
setTimeout(() => {
SplashScreen.hideAsync();
}, 1500);
};
const source = useMemo(
() => ({
uri: urlFromClipboard ?? url,
}),
[url, urlFromClipboard]
);
const onAndroidBackPress = () => {
if (ref.current) {
ref.current.goBack();
return true; // prevent default behavior (exit app)
}
return false;
};
useEffect(() => {
if (Platform.OS === "android") {
BackHandler.addEventListener("hardwareBackPress", onAndroidBackPress);
return () => {
BackHandler.removeEventListener("hardwareBackPress", onAndroidBackPress);
};
}
}, []);
useEffect(() => {
AsyncStorage.getItem("@numberOfOpenings").then((numberOfOpenings) => {
AsyncStorage.setItem("@numberOfOpenings", `${(Number(numberOfOpenings) ?? 0) + 1}`);
});
}, []);
const [backgroundColor, setBackgroundColor] = useState("#fff");
const insets = useSafeAreaInsets();
const style = useMemo(
() => ({
flex: 1,
backgroundColor,
}),
[backgroundColor]
);
const onMessage = useCallback(
(event) => {
switch (event.nativeEvent.data) {
case "request-native-set-safe-background-black":
setBackgroundColor("#000");
break;
case "request-native-set-safe-background-white":
setBackgroundColor("#fff");
break;
case "request-native-get-inset-bottom-height":
ref.current.injectJavaScript(`window.onGetInsetBottomHeight('${insets.bottom}');`);
break;
case "request-native-get-contacts":
onRequestContacts();
break;
case "request-native-clipboard-text-content":
case "request-native-clipboard-text-content-on-first-opening":
onRequestClipboardTextContent(event);
break;
case "request-native-get-current-position":
case "request-native-force-current-position":
onRequestLocation(event);
break;
case "request-native-push-permission":
case "request-native-expo-push-permission":
case "request-native-get-token-if-exists":
case "request-native-get-expo-token":
onPushNotification(event);
break;
default:
break;
}
},
[
setBackgroundColor,
onRequestContacts,
onRequestClipboardTextContent,
onRequestLocation,
onPushNotification,
insets,
]
);
return (
<SafeAreaProvider>
<SafeAreaView style={style} edges={["left", "right", "top"]}>
<WebView
ref={ref}
style={styles.container}
startInLoadingState
onLoadEnd={onLoadEnd}
source={source}
pullToRefreshEnabled
allowsBackForwardNavigationGestures
// onNavigationStateChange={onNavigationStateChange}
onMessage={onMessage}
injectedJavaScript={initScript}
/>
</SafeAreaView>
</SafeAreaProvider>
);
}
export default function () {
return (
<SafeAreaProvider>
<App />
</SafeAreaProvider>
);
}
const styles = StyleSheet.create({
safeContainer: {
flex: 1,
backgroundColor: "#000",
// backgroundColor: "transparent",
},
container: {
flex: 1,
// backgroundColor: "#000",
},
});