-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathApp.tsx
114 lines (96 loc) · 2.9 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
import { StatusBar } from "expo-status-bar";
import React, { useEffect, useState } from "react";
import { SafeAreaProvider } from "react-native-safe-area-context";
import Amplify, { DataStore, Hub, Auth } from "aws-amplify";
import { withAuthenticator } from "aws-amplify-react-native";
import config from "./src/aws-exports";
import { Message, User } from "./src/models";
import { ActionSheetProvider } from "@expo/react-native-action-sheet";
import useCachedResources from "./hooks/useCachedResources";
import Navigation from "./navigation";
import { LogBox } from 'react-native';
import * as Notifications from 'expo-notifications';
LogBox.ignoreLogs(['Warning: ...']); // Ignore log notification by message
LogBox.ignoreAllLogs();//Ignore all log notifications
Amplify.configure(config);
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: false,
shouldSetBadge: false,
}),
});
function App() {
const isLoadingComplete = useCachedResources();
const [user, setUser] = useState<User | null>(null);
useEffect(() => {
const listener = Hub.listen("datastore", async (hubData) => {
const { event, data } = hubData.payload;
if (
event === "outboxMutationProcessed" &&
data.model === Message &&
!["DELIVERED", "READ"].includes(data.element.status)
) {
// set the message status to delivered
DataStore.save(
Message.copyOf(data.element, (updated) => {
updated.status = "DELIVERED";
})
);
}
});
// Remove listener
return () => listener();
}, []);
useEffect(() => {
if (!user) {
return;
}
const subscription = DataStore.observe(User, user.id).subscribe((msg) => {
if (msg.model === User && msg.opType === "UPDATE") {
setUser(msg.element);
}
});
return () => subscription.unsubscribe();
}, [user?.id]);
useEffect(() => {
fetchUser();
}, []);
useEffect(() => {
const interval = setInterval(async () => {
await updateLastOnline();
}, 1 * 60 * 1000);
return () => clearInterval(interval);
}, [user]);
const fetchUser = async () => {
const userData = await Auth.currentAuthenticatedUser();
const user = await DataStore.query(User, userData.attributes.sub);
if (user) {
setUser(user);
}
};
const updateLastOnline = async () => {
if (!user) {
return;
}
const response = await DataStore.save(
User.copyOf(user, (updated) => {
updated.lastOnlineAt = +new Date();
})
);
setUser(response);
};
if (!isLoadingComplete) {
return null;
} else {
return (
<SafeAreaProvider>
<ActionSheetProvider>
<Navigation colorScheme={"light"} />
</ActionSheetProvider>
<StatusBar />
</SafeAreaProvider>
);
}
}
export default withAuthenticator(App);