Skip to content

Commit 6c0bcc2

Browse files
authored
Merge pull request #211 from polleverywhere/native-storage-upstream-rebase
Use NativeStorage for persistance across installs
2 parents 0764b98 + 8ffee2b commit 6c0bcc2

3 files changed

Lines changed: 57 additions & 51 deletions

File tree

plugin.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ under the License.
3636
<dependency id="cordova-plugin-dialogs"/>
3737
<dependency id="cordova-plugin-globalization"/>
3838
<dependency id="cordova-plugin-inappbrowser"/>
39+
<dependency id="cordova-plugin-nativestorage"/>
3940

4041
<js-module src="www/AppRate.js" name="AppRate">
4142
<clobbers target="AppRate"/>
@@ -45,6 +46,10 @@ under the License.
4546
<runs target="AppRateLocales"/>
4647
</js-module>
4748

49+
<js-module src="www/storage.js" name="storage">
50+
<runs target="AppRateStorage"/>
51+
</js-module>
52+
4853
<platform name="android">
4954
<source-file src="src/android/AppRate.java" target-dir="src/org/pushandplay/cordova/apprate"/>
5055

www/AppRate.js

Lines changed: 34 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@
1818
* under the License.
1919
*
2020
*/;
21-
var AppRate, Locales, localeObj, exec;
21+
var AppRate, Locales, localeObj, exec, Storage;
2222

2323
Locales = require('./locales');
2424

2525
exec = require('cordova/exec');
2626

27+
Storage = require('./storage')
28+
2729
AppRate = (function() {
28-
var FLAG_NATIVE_CODE_SUPPORTED, LOCAL_STORAGE_COUNTER, PREF_STORE_URL_FORMAT_IOS, counter, getAppTitle, getAppVersion, localStorageParam, promptForRatingWindowButtonClickHandler, showDialog, updateCounter;
30+
var FLAG_NATIVE_CODE_SUPPORTED, LOCAL_STORAGE_COUNTER, PREF_STORE_URL_FORMAT_IOS, counter, getAppTitle, getAppVersion, promptForRatingWindowButtonClickHandler, showDialog, updateCounter;
2931

3032
function AppRate() {}
3133

@@ -126,7 +128,7 @@ AppRate = (function() {
126128
case 'stop':
127129
counter.countdown = AppRate.preferences.usesUntilPrompt + 1;
128130
}
129-
localStorageParam(LOCAL_STORAGE_COUNTER, JSON.stringify(counter));
131+
Storage.set(LOCAL_STORAGE_COUNTER, counter);
130132
return counter;
131133
};
132134

@@ -138,7 +140,7 @@ AppRate = (function() {
138140
iOSRating.timesPrompted++;
139141
iOSRating.lastPromptDate = new Date();
140142

141-
localStorageParam(LOCAL_STORAGE_IOS_RATING, JSON.stringify(iOSRating));
143+
Storage.set(LOCAL_STORAGE_IOS_RATING, iOSRating);
142144
}
143145

144146
showDialog = function(immediately) {
@@ -159,28 +161,6 @@ AppRate = (function() {
159161
return AppRate;
160162
};
161163

162-
localStorageParam = function(itemName, itemValue, action) {
163-
if (itemValue == null) {
164-
itemValue = null;
165-
}
166-
if (action == null) {
167-
action = false;
168-
}
169-
if (itemValue !== null) {
170-
action = true;
171-
}
172-
switch (action) {
173-
case true:
174-
localStorage.setItem(itemName, itemValue);
175-
break;
176-
case false:
177-
return localStorage.getItem(itemName);
178-
case null:
179-
localStorage.removeItem(itemName);
180-
}
181-
return this;
182-
};
183-
184164
getAppVersion = function(successCallback, errorCallback) {
185165
if (FLAG_NATIVE_CODE_SUPPORTED) {
186166
exec(successCallback, errorCallback, 'AppRate', 'getAppVersion', []);
@@ -200,17 +180,18 @@ AppRate = (function() {
200180
};
201181

202182
AppRate.init = function() {
203-
if(localStorageParam(LOCAL_STORAGE_COUNTER)){
204-
counter = JSON.parse(localStorageParam(LOCAL_STORAGE_COUNTER)) || counter;
205-
}
206-
207-
if (localStorageParam(LOCAL_STORAGE_IOS_RATING)){
208-
iOSRating = JSON.parse(localStorageParam(LOCAL_STORAGE_IOS_RATING)) || iOSRating;
209-
210-
if (iOSRating.lastPromptDate) {
211-
iOSRating.lastPromptDate = new Date(iOSRating.lastPromptDate);
212-
}
213-
}
183+
AppRate.ready = Promise.all([
184+
Storage.get(LOCAL_STORAGE_COUNTER).then(function (storedCounter) {
185+
counter = storedCounter || counter
186+
}),
187+
Storage.get(LOCAL_STORAGE_IOS_RATING).then(function (storedRating) {
188+
iOSRating = storedRating || iOSRating
189+
190+
if (iOSRating.lastPromptDate) {
191+
iOSRating.lastPromptDate = new Date(iOSRating.lastPromptDate);
192+
}
193+
})
194+
])
214195

215196
getAppVersion((function(_this) {
216197
return function(applicationVersion) {
@@ -258,20 +239,22 @@ AppRate = (function() {
258239
};
259240

260241
AppRate.promptForRating = function(immediately) {
261-
if (immediately == null) {
262-
immediately = true;
263-
}
264-
if (this.preferences.useLanguage === null) {
265-
navigator.globalization.getPreferredLanguage((function(_this) {
266-
return function(language) {
267-
_this.preferences.useLanguage = language.value;
268-
return showDialog(immediately);
269-
};
270-
})(this));
271-
} else {
272-
showDialog(immediately);
273-
}
274-
updateCounter();
242+
AppRate.ready.then(function() {
243+
if (immediately == null) {
244+
immediately = true;
245+
}
246+
if (AppRate.preferences.useLanguage === null) {
247+
navigator.globalization.getPreferredLanguage((function(_this) {
248+
return function(language) {
249+
_this.preferences.useLanguage = language.value;
250+
return showDialog(immediately);
251+
};
252+
})(AppRate));
253+
} else {
254+
showDialog(immediately);
255+
}
256+
updateCounter();
257+
});
275258
return this;
276259
};
277260

www/storage.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module.exports = {
2+
get: function (key) {
3+
return new Promise(function(resolve, reject) {
4+
NativeStorage.getItem(key, resolve, function(e) {
5+
if (e.code === 2) {
6+
resolve(null)
7+
} else {
8+
reject(e)
9+
}
10+
})
11+
})
12+
},
13+
set: function (key, value) {
14+
return new Promise(function(resolve, reject) {
15+
NativeStorage.setItem(key, value, resolve, reject);
16+
})
17+
}
18+
}

0 commit comments

Comments
 (0)