-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathApp.tsx
208 lines (182 loc) · 6.1 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
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
import React, { useEffect, useRef, useState } from "react";
import { Provider as ReactNativePaperProvider } from "react-native-paper";
import { ThemeProvider } from "styled-components";
import AppLoading from "expo-app-loading";
import {
carregarConfiguracoes,
salvarConfiguracoes,
} from "./src/controllers/configuracoesController";
import ThemeContextProvider, { useThemeContext } from "./src/contexts/Theme";
import Routes from "./src/routes";
import themes from "./src/themes";
import * as Linking from "expo-linking";
const prefix = Linking.createURL("/");
// import Constants from 'expo-constants';
import * as Notifications from "expo-notifications";
import { ParsedURL } from "expo-linking";
import { ThemeColors, ThemeType } from "./src/types/Theme";
import { Platform } from "react-native";
import * as Device from "expo-device";
// Configurações do Push Notification
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: false,
shouldSetBadge: false,
}),
});
export default function App() {
return (
<ThemeContextProvider>
<AppWrapper />
</ThemeContextProvider>
);
}
const AppWrapper: React.FC = () => {
// THEME STATE CONTEXT
const { setActualTheme, actualTheme } = useThemeContext();
// // Estado relacionados ao push
const notificationListener = useRef<any>(null);
const responseListener = useRef<any>(null);
const [isReady, setIsReady] = useState(false);
// const [actualTheme, setActualTheme] = useState<ThemeType>(
// themes.azulEscuroDefault
// );
const [linkData, setLinkData] = useState<ParsedURL>();
const linking = {
prefixes: [prefix],
config: {
screens: {
Home: "home",
Cronometro: "cronometro",
},
},
};
useEffect(() => {
// Carrega as configuracoes salvas no Storage
const carregaConfiguracoes = async () => {
// Chama o controller para carregar as configurações
await carregarConfiguracoes().then(async (configs) => {
// Checa se é a primeira vez que está carregando as configurações
if (configs.length === 0) {
// Se for a primeira vez, seta o tema padrão
handleSwicthTheme(themes["azulEscuroDefault"]);
// Salva o relatorio simplificado estado local
await salvarConfiguracoes({
actualTheme: "azulEscuroDefault",
darkMode: false,
isRelatorioSimplificado: true,
});
} else {
// Verifica se a nova opção não existe e salva ela
if (configs?.isRelatorioSimplificado === undefined) {
// Salva o relatorio simplificado estado local
await salvarConfiguracoes({
...configs,
isRelatorioSimplificado: true,
});
}
// Verifica qual tema aplicar
if (configs.darkMode === true) {
handleSwicthTheme(themes["darkMode"]);
} else {
handleSwicthTheme(themes[configs?.actualTheme as ThemeColors]);
}
}
});
};
carregaConfiguracoes();
async function getInitialURL() {
const initialURL = await Linking.getInitialURL();
if (initialURL) {
setLinkData(Linking.parse(initialURL));
}
}
// Lida com o Deep link do app
// Linking.addEventListener("url", handleDeepLink);
if (!linkData) {
getInitialURL();
}
// CONFIGURAÇÕES DO PUSH NOTIFICATION
// Pega o token do push
registerForPushNotificationsAsync().then((token) => {});
// setExpoPushToken(token)
notificationListener.current =
Notifications.addNotificationReceivedListener((notification) => {
// setNotification(notification);
});
// Pega o retorno do clique no push
responseListener.current =
Notifications.addNotificationResponseReceivedListener((response) => {
// console.log('Push clicado-aberto');
// navigation.navigate('Cronometro');
// Linking.openURL(
// `${[prefix]}${
// response.notification.request.content.data.deepLinkPage
// }`
// );
});
return () => {
// Linking.removeEventListener("url", linkData);
// // Remove as configurações do Push Notification
// Notifications.removeNotificationSubscription(
// notificationListener.current
// );
// Notifications.removeNotificationSubscription(responseListener.current);
};
}, []);
// function handleDeepLink(event: EventType) {
// let data = Linking.parse(event.url);
// setLinkData(data);
// }
// Controls theme selection
const handleSwicthTheme = (valor: ThemeType) => {
setActualTheme && setActualTheme(valor);
// Depois de setado o tema, sai da splash screen
setIsReady(true);
};
// Hold splash screen until theme is applied and avoid the flicker of the theme at the beginning
if (!isReady) {
return <AppLoading />;
}
return (
<ThemeProvider theme={actualTheme ?? themes.azulEscuroDefault}>
<ReactNativePaperProvider>
<Routes />
</ReactNativePaperProvider>
</ThemeProvider>
);
};
async function registerForPushNotificationsAsync() {
let token;
if (Platform.OS === "android") {
await Notifications.setNotificationChannelAsync("default", {
name: "default",
importance: Notifications.AndroidImportance.MAX,
vibrationPattern: [0, 250, 250, 250],
lightColor: "#FF231F7C",
});
}
if (Device.isDevice) {
const { status: existingStatus } =
await Notifications.getPermissionsAsync();
let finalStatus = existingStatus;
if (existingStatus !== "granted") {
const { status } = await Notifications.requestPermissionsAsync();
finalStatus = status;
}
if (finalStatus !== "granted") {
console.log("Failed to get push token for push notification!");
return;
}
token = (
await Notifications.getExpoPushTokenAsync({
projectId: "c3a50ca6-47e0-48f7-83a6-d0425212d430",
})
).data;
// console.log(token);
} else {
console.log("Must use physical device for Push Notifications");
}
return token;
}