-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathRatingsData.js
82 lines (68 loc) · 1.99 KB
/
RatingsData.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
import AsyncStorage from '@react-native-async-storage/async-storage';
const keyPrefix = '@RatingRequestData.';
const eventCountKey = keyPrefix + 'positiveEventCount';
const ratedTimestamp = keyPrefix + 'ratedTimestamp';
const declinedTimestamp = keyPrefix + 'declinedTimestamp';
/**
* Private class that let's us interact with AsyncStorage on the device
* @class
*/
class RatingsData {
constructor() {
this.initialize();
}
// Get current count of positive events
async getCount() {
try {
let countString = await AsyncStorage.getItem(eventCountKey);
return parseInt(countString, 10);
} catch (ex) {
console.warn('Couldn\'t retrieve positive events count. Error:', ex);
}
}
// Increment count of positive events
async incrementCount() {
try {
let currentCount = await this.getCount();
await AsyncStorage.setItem(eventCountKey, (currentCount + 1).toString());
return currentCount + 1;
} catch (ex) {
console.warn('Could not increment count. Error:', ex);
}
}
async getActionTimestamps() {
try {
let timestamps = await AsyncStorage.multiGet([ratedTimestamp, declinedTimestamp]);
return timestamps;
} catch (ex) {
console.warn('Could not retrieve rated or declined timestamps.', ex);
}
}
async recordDecline() {
try {
await AsyncStorage.setItem(declinedTimestamp, Date.now().toString());
} catch (ex) {
console.warn('Couldn\'t set declined timestamp.', ex);
}
}
async recordRated() {
try {
await AsyncStorage.setItem(ratedTimestamp, Date.now().toString());
} catch (ex) {
console.warn('Couldn\'t set rated timestamp.', ex);
}
}
// Initialize keys, if necessary
async initialize() {
try {
let keys = await AsyncStorage.getAllKeys();
if (!keys.some((key) => key === eventCountKey)) {
await AsyncStorage.setItem(eventCountKey, '0');
}
} catch (ex) {
// report error or maybe just initialize the values?
console.warn('Uh oh, something went wrong initializing values!', ex);
}
}
}
export default new RatingsData();