-
Notifications
You must be signed in to change notification settings - Fork 1
/
WebViewScreen.tsx
90 lines (83 loc) · 2.59 KB
/
WebViewScreen.tsx
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
import { StyleSheet, Text, View } from "react-native";
import { WebView } from "react-native-webview";
import React from "react";
import { useSafeAreaInsets } from "react-native-safe-area-context";
import Geolocation from '@react-native-community/geolocation';
const whiteList = [
"https://giessdeinviertel.codeforleipzig.de",
"https://leipziggiesst-login.codeforleipzig.de",
"https://www.giessdenkiez.de",
];
export const getGeoLocationJS = () => {
const geoLocCode = require('./assets/GeoLocation.js')
return `
(function() {
${geoLocCode}
})();
`;
};
const WebViewScreen = () => {
const insets = useSafeAreaInsets();
return (
<WebView
geolocationEnabled={ true }
injectedJavaScript={ getGeoLocationJS() }
javaScriptEnabled={ true }
onMessage={ event => {
try {
const data = JSON.parse(event.nativeEvent.data);
const eventTypes = [
{
event: 'getCurrentPosition',
fun: Geolocation.getCurrentPosition,
successCode: 'currentPosition',
errorCode: 'currentPositionError'
},
{
event: 'watchPosition',
fun: Geolocation.watchPosition,
successCode: 'watchPosition',
errorCode: 'watchPositionError'
},
{
event: 'clearWatch',
fun: Geolocation.clearWatch,
input: (param: { watchID: number }) => param.watchID
},
]
const postMessage = (msg: {}) => {
webview.postMessage(JSON.stringify(msg));
}
const eventType = eventTypes.find(eventType => data?.event && data.event == eventType.event);
if (eventType) {
if (eventType.successCode) {
eventType.fun(
input => postMessage({ event: eventType.successCode, data: input }),
error => postMessage({ event: eventType.errorCode, data: error })
);
} else if (eventType.input) {
eventType.fun(eventType.input(data));
}
}
} catch (e) {
console.log(e);
}
}}
ref={ ref => {
webview = ref;
}}
startInLoadingState={ true }
style={[styles.webView, { marginTop: insets.top }]}
source={{ uri: "https://giessdeinviertel.codeforleipzig.de" }}
originWhitelist={whiteList}
allowsBackForwardNavigationGestures
sharedCookiesEnabled
/>
);
};
const styles = StyleSheet.create({
webView: {
flex: 1,
},
});
export default WebViewScreen;