-
Notifications
You must be signed in to change notification settings - Fork 1
/
hooks.js
219 lines (184 loc) · 5.96 KB
/
hooks.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
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
// @flow
import {
useContext, useEffect, useRef, useState,
} from 'react';
import { useLocation } from 'react-router-dom';
import InAppReview from 'react-native-in-app-review';
import { RewardedAd } from 'react-native-google-mobile-ads';
import AsyncStorage from '@react-native-async-storage/async-storage';
import Sound from 'react-native-sound';
import {
addMonths, minutesToMilliseconds, secondsToMilliseconds,
} from 'date-fns';
import { useSelector } from 'react-redux';
import { isEqual, keys } from 'lodash';
import { localStorageKeys } from '../tokens';
import { rewardedKeywords } from '../tokens/keywords';
import { symbolFlat, symbolSharp } from './patterns';
import { PortalContext } from '../context';
import { isPromise } from '.';
import type { PortalProps } from '../context';
import type { ReduxState } from '../types';
import type { ChordPlaying } from '../components/containers/bottom/BottomChords';
export const getItem = async (key: string): any => {
try {
const response = await AsyncStorage.getItem(key);
return response;
} catch (error) {
Promise.reject(error);
}
};
export const setItem = async (key: string, data: string) => {
try {
await AsyncStorage.setItem(key, data);
} catch (error) {
Promise.reject(error);
}
};
export const removeItem = async (key: string) => {
try {
await AsyncStorage.removeItem(key);
} catch (error) {
Promise.reject(error);
}
};
export const useLocalStorage = (): {
getItem: Function,
setItem: Function,
removeItem: Function,
} => ({
getItem,
setItem,
removeItem,
});
export const useReview = (): Function => {
const { loadTime, reviewMinutes, unlocked }: {
loadTime: number,
reviewMinutes: number,
unlocked: boolean,
} = useSelector((state: ReduxState) => ({
loadTime: state.static.loadTime,
reviewMinutes: state.static.reviewMinutes,
unlocked: state.global.unlocked,
}), isEqual);
const localStorage = useLocalStorage();
const isAvailable = InAppReview.isAvailable();
const handleReview = async () => {
const currentTime = Date.now();
const reviewEnabled = (loadTime + minutesToMilliseconds(reviewMinutes)) <= currentTime;
if (!isAvailable) return;
if (unlocked && reviewEnabled) {
const reviewTimestamp = await localStorage.getItem(localStorageKeys.reviewTimestamp);
if (!reviewTimestamp || Number(reviewTimestamp) <= currentTime) {
InAppReview.RequestInAppReview().then((successful) => {
if (successful) {
const newTimestamp = addMonths(currentTime, 1).valueOf();
localStorage.setItem(localStorageKeys.reviewTimestamp, JSON.stringify(newTimestamp));
}
});
}
}
};
return handleReview;
};
export type LocationInfo = {
current: string,
isScales: boolean,
isChords: boolean,
isRewarded: boolean,
isInfo: boolean,
}
export const useLocationInfo = (): LocationInfo => {
const location = useLocation();
const isScales = location.pathname === '/';
const isChords = location.pathname === '/chords';
const isRewarded = location.pathname === '/rewarded';
const isInfo = location.pathname === '/info';
return {
current: location.pathname,
isScales,
isChords,
isRewarded,
isInfo,
};
};
export const useTeleport = (): PortalProps => useContext(PortalContext);
export const useRewardedAd = (
rewardedId: string,
showPersonalisedAds: boolean,
): Object|null => {
const [rewardedAd, setRewardedAd] = useState(null);
useEffect(() => {
const handleNewAd = async (): Object => {
const response = await RewardedAd.createForAdRequest(rewardedId, {
requestNonPersonalizedAdsOnly: !showPersonalisedAds,
keywords: rewardedKeywords,
});
setRewardedAd(response);
};
if (!rewardedAd || !isPromise(rewardedAd)) handleNewAd();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
return rewardedAd;
};
export const useCountdown = (onTimeEnd: Function, countdownFrom: ?number) => {
const [time, setTime] = useState(countdownFrom || 0);
const timerRef = useRef(time);
useEffect(() => {
if (countdownFrom) {
timerRef.current = countdownFrom;
setTime(timerRef.current);
}
}, [countdownFrom]);
useEffect(() => {
if (!countdownFrom) return;
const timerId = setInterval(() => {
timerRef.current -= secondsToMilliseconds(1);
if (timerRef.current < 0 && countdownFrom) {
onTimeEnd();
clearInterval(timerId);
} else {
setTime(timerRef.current);
}
}, secondsToMilliseconds(1));
return () => clearInterval(timerId);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
};
export type Note = { diatonic: boolean, note: string };
export type UseSoundChords = {
chordsPause: Function,
chordsPlay: Function,
};
export const useSoundChords = (): UseSoundChords => {
Sound.setCategory('Playback');
const playbackRef = useRef<{[string]: any}>({});
const chordsPause = () => {
const notes = keys(playbackRef.current);
for (let index = 0; index < notes.length; index++) {
const note = notes[index];
playbackRef.current[note].stop();
playbackRef.current[note].release();
}
playbackRef.current = {};
};
const chordsPlay = (notes: Note[], type: ChordPlaying) => {
const isNegative = type === 'negative';
const chord = [...notes];
for (let index = 0; index < chord.length; index++) {
const { note } = chord[index];
const soundPath = (note.includes(symbolSharp) || note.includes(symbolFlat)) ? note.charAt(0) + '_sharp' : note;
const soundKey = (isNegative ? 'low_' : '') + soundPath.toLowerCase();
const sound = new Sound(`${soundKey}.mp3`, Sound.MAIN_BUNDLE, (error) => {
if (error) return;
sound.setVolume(0.8);
sound.play();
});
playbackRef.current[soundKey] = sound;
}
};
return {
chordsPause: () => chordsPause(),
chordsPlay: (notes: Note[], type: ChordPlaying) => chordsPlay(notes, type),
};
};