-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
121 lines (113 loc) · 4.33 KB
/
App.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
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
import { useKeepAwake } from "expo-keep-awake";
import * as SplashScreen from "expo-splash-screen";
import {
getTrackingPermissionsAsync,
requestTrackingPermissionsAsync,
} from "expo-tracking-transparency";
import React, {
useEffect,
useState,
useRef,
} from "react";
import {
BackHandler,
Platform,
StyleSheet,
} from "react-native";
import { SafeAreaProvider } from "react-native-safe-area-context";
import { WebView } from "react-native-webview";
import CustomStatusBar from "./client/components/CustomStatusBar";
import {
cookiePreferences,
getPermissionsGranted,
storePermissionsGranted,
} from "./client/helpers/permissions";
import { originAllowList } from "./client/services/originAllowList";
export default function App() {
const [ areTrackingPermissionsGranted, setAreTrackingPermissionsGranted ] = useState(false);
const [ didPermissionsChange, setDidPermissionsChange ] = useState(false);
const [ isRequestingPermissions, setIsRequestingPermissions ] = useState(true);
const [ uri, setUri ] = useState("https://stream.resonate.coop/discover");
const [ webViewKey, setWebViewKey ] = useState(0);
const hideSplashScreen = async () => await SplashScreen.hideAsync();
const reloadWebView = () => setWebViewKey(webViewKey + 1);
useKeepAwake();
const webViewRef = useRef<WebView>(null);
const onAndroidBackPress = (): boolean => {
if (webViewRef.current) {
webViewRef.current.goBack();
return true; // prevent default behavior (exit app)
}
return false;
};
useEffect((): (() => void) | undefined => {
if (Platform.OS === "android") {
BackHandler.addEventListener("hardwareBackPress", onAndroidBackPress);
return (): void => {
BackHandler.removeEventListener("hardwareBackPress", onAndroidBackPress);
};
}
}, []);
useEffect(() => {
(async () => {
await SplashScreen.preventAutoHideAsync();
const arePermissionsGrantedHistorically = await getPermissionsGranted();
const { granted } = await getTrackingPermissionsAsync();
if (granted) {
setAreTrackingPermissionsGranted(true);
if (!arePermissionsGrantedHistorically) {
setDidPermissionsChange(true);
storePermissionsGranted(true);
}
} else {
const { status } = await requestTrackingPermissionsAsync();
const arePermissionsGranted = status === "granted";
setAreTrackingPermissionsGranted(arePermissionsGranted);
if (arePermissionsGranted !== arePermissionsGrantedHistorically) {
setDidPermissionsChange(true);
storePermissionsGranted(arePermissionsGranted);
}
}
setIsRequestingPermissions(false);
})();
}, []);
return (
<SafeAreaProvider>
<CustomStatusBar />
{!isRequestingPermissions && <WebView
allowsBackForwardNavigationGestures
allowsInlineMediaPlayback
cacheEnabled={didPermissionsChange}
decelerationRate={"normal"}
ignoreSilentHardwareSwitch
injectedJavaScript={cookiePreferences(areTrackingPermissionsGranted)}
javaScriptCanOpenWindowsAutomatically
key={webViewKey}
mediaPlaybackRequiresUserAction={false}
onContentProcessDidTerminate={reloadWebView}
onNavigationStateChange={navState => setUri(navState.url)}
onLoadEnd={hideSplashScreen}
onRenderProcessGone={reloadWebView}
originWhitelist={originAllowList}
ref={webViewRef}
sharedCookiesEnabled={false}
source={{
headers: {
"doNotTrack": areTrackingPermissionsGranted
? "0"
: "1",
},
uri,
}}
startInLoadingState
style={styles.container}
/>}
</SafeAreaProvider>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
marginTop: -2,
},
});