-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.ts
177 lines (151 loc) · 4.92 KB
/
util.ts
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
import moment from "moment";
import AsyncStorage from "@react-native-async-storage/async-storage";
import { showMessage } from "react-native-flash-message";
import * as Application from "expo-application";
import { Platform } from "react-native";
import * as Notifications from "expo-notifications";
import { useEffect, useState } from "react";
import analytics from "@react-native-firebase/analytics";
import * as Device from "expo-device";
export const ONE_HOUR_MS = 1000 * 60 * 60;
export const wait = (timeout: number): Promise<void> => {
return new Promise((resolve) => {
setTimeout(resolve, timeout);
});
};
const DEFAULT_TIMEOUT = 500;
// waitFunc wraps an async function and causes it to take at least a certain amount of time to resolve.
// This is used to improve the UX in case something loads so quickly that the user thinks nothing happened.
export const waitFunc = async (
fn: Promise<any>,
timeout: number = DEFAULT_TIMEOUT
): Promise<any> => {
const [value] = await Promise.all([fn, wait(timeout)]);
return value;
};
export const utcToLocal = (date: string | undefined): moment.Moment => {
if (date) {
return moment(date).utc(true).local();
}
return moment().utc(true).local();
};
export const utcToLocalDateString = (date: string | undefined): string => {
if (date) {
return moment(moment(date).utc(true).toDate()).local().format("YYYY-MM-DD");
}
return moment().utc(true).local().format("YYYY-MM-DD");
};
export const storeJSON = async (key: string, value: any) => {
try {
const jsonValue = JSON.stringify(value);
await AsyncStorage.setItem(key, jsonValue);
console.log(`stored ${key} data`);
} catch (e) {
console.error(`error storing json data for key ${key}`);
}
};
export const getStoredJSON = async (key: string): Promise<any> => {
try {
const jsonValue = await AsyncStorage.getItem(key);
console.log(`read ${key} data from local storage`);
return jsonValue != null ? JSON.parse(jsonValue) : null;
} catch (e) {
console.error("error reading stored json data");
}
};
export const showErrorMessage = (message: string) => {
showMessage({
message: "Error",
description: message,
type: "danger",
});
};
export const getDeviceID = async () => {
const storedUser = await getStoredJSON("user");
const storedDeviceID = storedUser?.deviceID;
if (storedDeviceID) {
return storedDeviceID;
}
if (Platform.OS === "android") {
const androidID = Application.getAndroidId();
if (androidID) {
return androidID;
}
}
if (Platform.OS === "ios") {
const iosID = await Application.getIosIdForVendorAsync();
if (iosID) {
return iosID;
}
}
throw new Error("Failed to get device ID");
};
// This function was adapted from: https://docs.expo.dev/push-notifications/push-notifications-setup/
export const registerForPushNotificationsAsync = async (): Promise<string> => {
if (!Device.isDevice) {
alert("Must use physical device for push notifications!");
return Promise.reject();
}
const { status: existingStatus } = await Notifications.getPermissionsAsync();
let finalStatus = existingStatus;
if (existingStatus !== "granted") {
const { status } = await Notifications.requestPermissionsAsync();
finalStatus = status;
}
if (finalStatus !== "granted") {
alert("Failed to get token for push notifications!");
return Promise.reject();
}
const token = (await Notifications.getExpoPushTokenAsync()).data;
console.log(`Got token: ${token}`);
if (Platform.OS === "android") {
await Notifications.setNotificationChannelAsync("default", {
name: "default",
importance: Notifications.AndroidImportance.MAX,
vibrationPattern: [0, 250, 250, 250],
lightColor: "#FF231F7C",
});
}
return Promise.resolve(token);
};
// useCurrentTime returns a Moment object of the current time. It updates every 5 seconds.
export const useCurrentTime = () => {
const [currentTime, setCurrentTime] = useState<moment.Moment>(moment());
useEffect(() => {
const interval = setInterval(() => {
setCurrentTime(moment());
}, 5000);
return () => clearInterval(interval);
}, [currentTime]);
return currentTime;
};
export const logAnalyticsScreenChange = async (
screenName: string | undefined
) => {
console.log(`Screen changed to ${screenName}`);
try {
await analytics().logScreenView({ screen_name: screenName });
} catch (e) {
console.warn(`Analytics logging failed: ${e}`);
}
};
export const logAnalyticsEvent = async (
eventName: string,
itemID: number,
itemDescription: string
) => {
console.log(`${eventName}: ${itemID} (${itemDescription})`);
try {
await analytics().logEvent(eventName, {
id: itemID,
description: itemDescription,
});
} catch (e) {
console.warn(`Analytics logging failed: ${e}`);
}
};
export const getOSName = () => {
return Device.osName === "iOS" || Device.osName === "iPadOS"
? Device.osName
: "Android";
};