forked from jlyman/react-native-rating-requestor
-
Notifications
You must be signed in to change notification settings - Fork 4
/
RatingsData.js
122 lines (103 loc) · 2.93 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
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
import React from 'react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';
const keyPrefix = '@RNRatingRequest.';
const eventCountKey = keyPrefix + 'EventCount';
const usesCountKey = keyPrefix + 'usesCount';
const ratedTimestamp = keyPrefix + 'ratedTimestamp';
const declinedTimestamp = keyPrefix + 'declinedTimestamp';
const lastSeenTimestamp = keyPrefix + 'lastSeenTimestamp';
/**
* Private class that let's us interact with AsyncStorage on the device
* @class
*/
class RatingsData {
constructor() {
this.initialize();
}
async getKey(key) {
try {
let string = await AsyncStorage.getItem(key);
return parseInt(string, 10);
} catch (ex) {
console.warn('Couldn\'t retrieve ' + key + '. Error:', ex);
}
}
async incrementKey(key) {
try {
let currentCount = await this.getKey(key);
await AsyncStorage.setItem(key, (currentCount + 1).toString());
return currentCount + 1;
} catch (ex) {
console.warn('Could not increment count' + key + '. Error:', ex);
}
}
async getUsesCount() {
return await this.getKey(usesCountKey);
}
async incrementUsesCount() {
return await this.incrementKey(usesCountKey);
}
// Get current count of positive events
async getEventCount() {
return await this.getKey(eventCountKey);
}
// Increment count of positive events
async incrementEventCount() {
return await this.incrementKey(eventCountKey);
}
async getActionTimestamps() {
try {
let timestamps = await AsyncStorage.multiGet([ratedTimestamp, declinedTimestamp, lastSeenTimestamp]);
return timestamps;
} catch (ex) {
console.warn('Could not retrieve rated or declined timestamps.', ex);
}
}
async recordRatingSeen() {
try {
await AsyncStorage.setItem(lastSeenTimestamp, Date.now().toString());
} catch (ex) {
console.warn('Couldn\'t set declined timestamp.', 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);
}
}
async initializeKeyIfNull(key) {
let val = await AsyncStorage.getItem(key);
if (val === null) {
console.log('initializing ' + key);
await AsyncStorage.setItem(key, '0');
}
}
// Initialize keys, if necessary
async initialize() {
try {
let keys = [eventCountKey,usesCountKey];
for (var i = keys.length - 1; i >= 0; i--) {
await this.initializeKeyIfNull(keys[i]);
}
} catch (ex) {
// report error or maybe just initialize the values?
console.warn('Uh oh, something went wrong initializing values!', ex);
}
}
async clearKeys() {
const keys = [eventCountKey,usesCountKey];
for (var i = keys.length - 1; i >= 0; i--) {
await AsyncStorage.setItem(keys[i],'0');
}
}
}
export default new RatingsData();