-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathApp.jsx
267 lines (232 loc) · 10.4 KB
/
App.jsx
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
import { TECH_PASSWORD, FIREBASE_PASSWORD } from "@env";
import * as eva from "@eva-design/eva";
import { DarkTheme, DefaultTheme, NavigationContainer } from "@react-navigation/native";
import { createStackNavigator } from "@react-navigation/stack";
import { ApplicationProvider, Icon, IconRegistry, Text } from "@ui-kitten/components";
import { EvaIconsPack } from "@ui-kitten/eva-icons";
import * as Device from "expo-device";
import * as Font from "expo-font";
import * as Notifications from "expo-notifications";
import { getAuth, signInWithEmailAndPassword } from "firebase/auth";
import { ref, runTransaction } from "firebase/database";
import { decode } from "html-entities";
import React, { useEffect, useRef, useState } from "react";
import { Appearance, Image, Linking, TouchableOpacity } from "react-native";
import { SafeAreaProvider } from "react-native-safe-area-context";
import { Author, Home, Post, Search, Section } from "./components/screens";
import { useFirebase } from "./hooks/useFirebase";
import mapping from "./mapping.json";
import { logoAssets, navigate } from "./navigation";
import { DailyBread as bread } from "./theme";
import { ThemeContext } from "./theme-context";
import { enableAnimationExperimental, onShare, registerForPushNotificationsAsync } from "./utils/action";
import { Fonts, Routing } from "./utils/constants";
import { getMostCommonTagsFromRecentPosts } from "./utils/format";
import Model from "./utils/model";
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: false,
shouldSetBadge: false,
}),
});
const Stack = createStackNavigator();
enableAnimationExperimental();
export default function App() {
const [fontsLoaded, setFontsLoaded] = useState(false);
const [expoPushToken, setExpoPushToken] = useState("");
const [notification, setNotification] = useState(false);
const notificationListener = useRef();
const responseListener = useRef();
const colorScheme = Appearance.getColorScheme();
const [theme, setTheme] = useState(colorScheme);
const [deviceType, setDeviceType] = useState(Device.DeviceType.PHONE);
const [tags, setTags] = useState([]);
const [sessionViews, setSessionViews] = useState({});
const toggleTheme = () => {
const next = theme === "light" ? "dark" : "light";
setTheme(next);
};
const navigatorTheme = {
light: DefaultTheme,
dark: DarkTheme,
};
const headerOptions = ({ navigation }) => ({
headerTitle: () => (
<Image
style={{ width: 260, height: 30 }}
source={logoAssets[theme]}
alt="Stanford Daily wordmark logo"
accessibilityRole="header"
/>
),
headerRight: () => (
<TouchableOpacity style={{ paddingHorizontal: 16 }} onPress={() => navigation.navigate(Routing.search, { tags })}>
<Icon name="search-outline" width={24} height={24} fill={theme === "dark" ? "white" : "black"} />
</TouchableOpacity>
),
});
const detailHeaderOptions = ({ route }) => ({
headerTitle: "",
headerTransparent: true,
headerTintColor: "white",
headerBackTitleVisible: false,
headerRight: () => (
<TouchableOpacity
style={{ paddingHorizontal: 16 }}
onPress={() => onShare(route.params.article.link, decode(route.params.article.title.rendered))}
>
<Icon name="share-outline" width={24} height={24} fill="white" />
</TouchableOpacity>
),
});
const sectionOptions = ({ route }) => ({
headerTitle: () => <Text category="h4">{decode(route.params.category.name).replace("'", "\u{2019}")}</Text>,
headerTitleStyle: { fontFamily: "MinionProBold" },
headerTintColor: bread[theme]["color-primary-500"],
});
const authorOptions = ({ route }) => ({
headerTitle: () => <Text category="h4">{route.params.name}</Text>,
headerTitleStyle: { fontFamily: "MinionProBold" },
headerTintColor: bread[theme]["color-primary-500"],
});
const searchHeaderOptions = {
headerTintColor: bread[theme]["color-primary-500"],
};
// const { app, database } = useFirebase(expoPushToken, FIREBASE_PASSWORD);
// Handles changes in the navigation state (as received from `NavigationContainer`) and logs them to a Firebase database.
const handleNavigationChange = async (state) => {
/*
if (!app || !state || !state.routes) return;
const auth = getAuth(app);
try {
await signInWithEmailAndPassword(auth, "[email protected]", TECH_PASSWORD);
const currentRoute = state.routes[state.index];
const currentView = currentRoute?.name;
const currentRouteParams = currentRoute?.params;
if (!currentView) return;
const datetime = new Date();
const year = datetime.getFullYear();
const month = String(datetime.getMonth() + 1).padStart(2, "0");
const day = String(datetime.getDate()).padStart(2, "0");
let currentViewPath = `Analytics/${year}/${month}/${day}/${currentView}`;
let viewIdentifier; // Used to track unique views for each screen in the sessions map.
let routeParamIdentifier;
// Leverage information about the current view to construct a unique identifier for use in Firebase.
switch (currentView) {
case Routing.post:
routeParamIdentifier = currentRouteParams?.article?.id; // Unique to the post being presented in the detail view.
break;
case Routing.section:
routeParamIdentifier = currentRouteParams?.category?.id; // Unique to the section being presented in the detail view.
break;
case Routing.author:
routeParamIdentifier = currentRouteParams?.id; // Unique to the author being presented in the detail view.
break;
default:
viewIdentifier = currentView;
}
if (routeParamIdentifier) {
// After the switch, if there is a `routeParamIdentifier`, it appends that to the `currentViewPath` string.
currentViewPath += `/${routeParamIdentifier}`;
viewIdentifier = `${currentView}/${routeParamIdentifier}`;
} else if (!viewIdentifier) {
// Otherwise, the default case must have been triggered, so it just uses the view identifier.
viewIdentifier = currentView;
}
const impressionsRef = ref(database, `${currentViewPath}/impressions`);
runTransaction(impressionsRef, (impressions) => {
return (impressions || 0) + 1;
});
if (!sessionViews[viewIdentifier]) {
const sessionsRef = ref(database, `${currentViewPath}/sessions`);
runTransaction(sessionsRef, (sessions) => {
return (sessions || 0) + 1;
});
// Update view information for future reference.
setSessionViews((prevSessionViews) => {
return { ...prevSessionViews, [viewIdentifier]: true };
});
}
} catch (error) {
console.trace(error);
}
*/
};
// Triggered when the app is opened from a web browser. It extracts the slug from the URL and navigates to the corresponding post.
const handleOpenURL = async (event) => {
// FIXME: Listener for when app is opened from web browser.
const slug = event?.url?.split("/")?.pop();
if (slug?.length > 0) {
const result = await Model.posts().embed().slug(slug).embed();
navigate(Routing.post, { item: result });
}
};
useEffect(() => {
// Loads fonts from static resource.
Font.loadAsync(Fonts.minion).then(() => setFontsLoaded(true));
registerForPushNotificationsAsync().then((token) => {
const matches = token?.match(/\[(.*?)\]/);
if (matches) {
const submatch = matches[1];
setExpoPushToken(submatch);
}
});
Device.getDeviceTypeAsync().then((type) => setDeviceType(type));
getMostCommonTagsFromRecentPosts(100, 10).then((tags) => setTags(tags));
// Handles any event in which appearance preferences change.
const themeListener = Appearance.addChangeListener((listener) => {
if (theme !== listener.colorScheme) {
setTheme(listener.colorScheme);
}
});
// This listener is fired whenever a notification is received while the app is foregrounded.
notificationListener.current = Notifications.addNotificationReceivedListener((notification) => {
setNotification(notification);
});
// This listener is fired whenever a user taps on or interacts with a notification.
responseListener.current = Notifications.addNotificationResponseReceivedListener((response) => {
// Works when app is foregrounded, backgrounded or killed.
Model.posts()
.id(response.notification.request.trigger.payload.body.postID)
.embed()
.get()
.then((result) => {
navigate(Routing.post, { item: result });
});
});
// Perform initial URL check in case the app was closed when the user opened a URL.
Linking.getInitialURL().then((url) => {
if (url) {
handleOpenURL({ url });
}
});
Linking.addEventListener("url", handleOpenURL);
return () => {
themeListener.remove();
Notifications.removeNotificationSubscription(notificationListener.current);
Notifications.removeNotificationSubscription(responseListener.current);
// Linking.removeEventListener("url", handleOpenURL); Seemed to be causing crashes.
};
}, [theme]);
return (
fontsLoaded && (
<NavigationContainer theme={navigatorTheme[theme]} onStateChange={handleNavigationChange}>
<IconRegistry icons={EvaIconsPack} />
<ThemeContext.Provider value={{ theme, toggleTheme, deviceType }}>
<ApplicationProvider {...eva} customMapping={mapping} theme={{ ...eva[theme], ...bread[theme] }}>
<SafeAreaProvider>
<Stack.Navigator initialRouteName={Routing.home}>
<Stack.Screen component={Home} name={Routing.home} options={headerOptions} />
<Stack.Screen component={Post} name={Routing.post} options={detailHeaderOptions} />
<Stack.Screen component={Section} name={Routing.section} options={sectionOptions} />
<Stack.Screen component={Author} name={Routing.author} options={authorOptions} />
<Stack.Screen component={Search} name={Routing.search} options={searchHeaderOptions} />
</Stack.Navigator>
</SafeAreaProvider>
</ApplicationProvider>
</ThemeContext.Provider>
</NavigationContainer>
)
);
}