-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
173 lines (148 loc) · 5.61 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
// A mobile app to display interactive trail guide content.
// Copyright (C) 2021-2024 David Lougheed
// See NOTICE for more information.
import { memo, useCallback, useEffect, useState } from "react";
import * as Linking from "expo-linking";
import {Ionicons} from "@expo/vector-icons";
import { NavigationContainer, useNavigation } from "@react-navigation/native";
import {createBottomTabNavigator} from "@react-navigation/bottom-tabs";
import AsyncStorage from "@react-native-async-storage/async-storage";
import PageView from "./components/PageView";
import MapView from "./components/MapView";
import StationsView from "./components/StationsView";
const POINTS_OF_INTEREST = "Points of Interest";
const MAP = "Map";
import {localDataProvider} from "./dataSources";
import CustomModal from "./components/CustomModal";
import * as r from "./routes";
const pagesById = localDataProvider.pages.itemsByID;
const Tab = createBottomTabNavigator();
// noinspection JSUnusedGlobalSymbols
const getScreenOptions = ({route}) => ({
tabBarIcon: ({color, size}) => {
let iconName;
switch (route.name) {
case POINTS_OF_INTEREST:
iconName = "information-circle-outline";
break;
case MAP:
iconName = "map-outline";
break;
default:
// Page
iconName = pagesById?.[route.name]?.icon ?? "help-circle-outline";
break;
}
return <Ionicons name={iconName} size={size} color={color} />;
},
tabBarActiveTintColor: "#0f746f",
tabBarInactiveTintColor: "gray",
});
const urlPrefix = Linking.createURL("/");
const linking = {
prefixes: [urlPrefix],
config: {
screens: {
[POINTS_OF_INTEREST]: {
initialRouteName: r.STATION_LIST,
screens: {
[r.STATION_LIST]: "stations/list",
[r.PRIVACY_POLICY]: "privacy-policy",
...Object.fromEntries(
localDataProvider.stations.enabled.map(
s => [r.stationScreenName(s.id), `stations/detail/${s.id}`])),
},
},
[MAP]: {
initialRouteName: r.MAP_OVERVIEW,
screens: {
[r.MAP_OVERVIEW]: "map/overview",
...Object.fromEntries(
localDataProvider.stations.enabled.map(
s => [r.mapStationScreenName(s.id), `map/detail/${s.id}`])),
},
},
...Object.fromEntries(localDataProvider.pages.items.map(page => [page.id, `pages/${page.id}`])),
},
},
};
const KEY_TERMS_SEEN = "@termsSeen";
const SCREEN_OPTIONS = {
[POINTS_OF_INTEREST]: {headerShown: false},
[MAP]: {headerShown: false},
};
const PAGE_SCREENS = localDataProvider.pages.items.map(page =>
<Tab.Screen
key={page.id}
name={page.id}
component={PageView}
options={{title: page.title}}
initialParams={{pageId: page.id}}
/>
);
const TermsModalWithHandler = () => {
const navigation = useNavigation();
const termsModal = localDataProvider.settings.data?.terms_modal ?? null;
const [termsModalVisible, setTermsModalVisible] = useState(false);
useEffect(() => {
const navState = navigation.getState();
// Kind of hacky - beware of state object changing shape in future updates.
if (navState.stale && navState.routes?.[0]?.name === POINTS_OF_INTEREST) {
// We are on initial load; check if we're on the privacy policy
// We need to allow direct links to the privacy policy without showing the terms first, so we early-return
// in this special case.
const route = navState.routes[0].state;
if ("index" in route && "routes" in route && route.routes[route.index].name === r.PRIVACY_POLICY) {
return;
}
}
const fn = async () => {
try {
const termsSeen = await AsyncStorage.getItem(KEY_TERMS_SEEN);
if (termsModal && termsSeen === null) {
// There is a terms modal to show, and we haven't seen it yet, so set it to visible
setTermsModalVisible(true);
}
} catch (e) {
console.error(e);
}
};
fn().catch(console.error);
}, [navigation]);
const handleCloseModal = useCallback(async () => {
try {
await AsyncStorage.setItem(KEY_TERMS_SEEN, "true"); // Some non-null value, doesn't really matter
} catch (e) {
console.error(e);
} finally {
setTermsModalVisible(false);
}
}, []);
return (
<CustomModal
visible={termsModalVisible}
data={localDataProvider.modals.itemsByID[termsModal] ?? {}}
onRequestClose={handleCloseModal}
/>
);
};
const App = memo(() => (
<NavigationContainer linking={linking}>
<TermsModalWithHandler />
<Tab.Navigator screenOptions={getScreenOptions} initialRouteName={POINTS_OF_INTEREST}>
<Tab.Screen
name={POINTS_OF_INTEREST}
options={SCREEN_OPTIONS[POINTS_OF_INTEREST]}
component={StationsView}
/>
<Tab.Screen
name={MAP}
options={SCREEN_OPTIONS[MAP]}
component={MapView}
/>
{PAGE_SCREENS}
</Tab.Navigator>
</NavigationContainer>
));
// noinspection JSUnusedGlobalSymbols
export default App;