Skip to content

Commit

Permalink
Merge pull request #211 from polleverywhere/native-storage-upstream-r…
Browse files Browse the repository at this point in the history
…ebase

Use NativeStorage for persistance across installs
  • Loading branch information
westonganger authored Jun 13, 2018
2 parents 0764b98 + 8ffee2b commit 6c0bcc2
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 51 deletions.
5 changes: 5 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ under the License.
<dependency id="cordova-plugin-dialogs"/>
<dependency id="cordova-plugin-globalization"/>
<dependency id="cordova-plugin-inappbrowser"/>
<dependency id="cordova-plugin-nativestorage"/>

<js-module src="www/AppRate.js" name="AppRate">
<clobbers target="AppRate"/>
Expand All @@ -45,6 +46,10 @@ under the License.
<runs target="AppRateLocales"/>
</js-module>

<js-module src="www/storage.js" name="storage">
<runs target="AppRateStorage"/>
</js-module>

<platform name="android">
<source-file src="src/android/AppRate.java" target-dir="src/org/pushandplay/cordova/apprate"/>

Expand Down
85 changes: 34 additions & 51 deletions www/AppRate.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@
* under the License.
*
*/;
var AppRate, Locales, localeObj, exec;
var AppRate, Locales, localeObj, exec, Storage;

Locales = require('./locales');

exec = require('cordova/exec');

Storage = require('./storage')

AppRate = (function() {
var FLAG_NATIVE_CODE_SUPPORTED, LOCAL_STORAGE_COUNTER, PREF_STORE_URL_FORMAT_IOS, counter, getAppTitle, getAppVersion, localStorageParam, promptForRatingWindowButtonClickHandler, showDialog, updateCounter;
var FLAG_NATIVE_CODE_SUPPORTED, LOCAL_STORAGE_COUNTER, PREF_STORE_URL_FORMAT_IOS, counter, getAppTitle, getAppVersion, promptForRatingWindowButtonClickHandler, showDialog, updateCounter;

function AppRate() {}

Expand Down Expand Up @@ -126,7 +128,7 @@ AppRate = (function() {
case 'stop':
counter.countdown = AppRate.preferences.usesUntilPrompt + 1;
}
localStorageParam(LOCAL_STORAGE_COUNTER, JSON.stringify(counter));
Storage.set(LOCAL_STORAGE_COUNTER, counter);
return counter;
};

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

localStorageParam(LOCAL_STORAGE_IOS_RATING, JSON.stringify(iOSRating));
Storage.set(LOCAL_STORAGE_IOS_RATING, iOSRating);
}

showDialog = function(immediately) {
Expand All @@ -159,28 +161,6 @@ AppRate = (function() {
return AppRate;
};

localStorageParam = function(itemName, itemValue, action) {
if (itemValue == null) {
itemValue = null;
}
if (action == null) {
action = false;
}
if (itemValue !== null) {
action = true;
}
switch (action) {
case true:
localStorage.setItem(itemName, itemValue);
break;
case false:
return localStorage.getItem(itemName);
case null:
localStorage.removeItem(itemName);
}
return this;
};

getAppVersion = function(successCallback, errorCallback) {
if (FLAG_NATIVE_CODE_SUPPORTED) {
exec(successCallback, errorCallback, 'AppRate', 'getAppVersion', []);
Expand All @@ -200,17 +180,18 @@ AppRate = (function() {
};

AppRate.init = function() {
if(localStorageParam(LOCAL_STORAGE_COUNTER)){
counter = JSON.parse(localStorageParam(LOCAL_STORAGE_COUNTER)) || counter;
}

if (localStorageParam(LOCAL_STORAGE_IOS_RATING)){
iOSRating = JSON.parse(localStorageParam(LOCAL_STORAGE_IOS_RATING)) || iOSRating;

if (iOSRating.lastPromptDate) {
iOSRating.lastPromptDate = new Date(iOSRating.lastPromptDate);
}
}
AppRate.ready = Promise.all([
Storage.get(LOCAL_STORAGE_COUNTER).then(function (storedCounter) {
counter = storedCounter || counter
}),
Storage.get(LOCAL_STORAGE_IOS_RATING).then(function (storedRating) {
iOSRating = storedRating || iOSRating

if (iOSRating.lastPromptDate) {
iOSRating.lastPromptDate = new Date(iOSRating.lastPromptDate);
}
})
])

getAppVersion((function(_this) {
return function(applicationVersion) {
Expand Down Expand Up @@ -258,20 +239,22 @@ AppRate = (function() {
};

AppRate.promptForRating = function(immediately) {
if (immediately == null) {
immediately = true;
}
if (this.preferences.useLanguage === null) {
navigator.globalization.getPreferredLanguage((function(_this) {
return function(language) {
_this.preferences.useLanguage = language.value;
return showDialog(immediately);
};
})(this));
} else {
showDialog(immediately);
}
updateCounter();
AppRate.ready.then(function() {
if (immediately == null) {
immediately = true;
}
if (AppRate.preferences.useLanguage === null) {
navigator.globalization.getPreferredLanguage((function(_this) {
return function(language) {
_this.preferences.useLanguage = language.value;
return showDialog(immediately);
};
})(AppRate));
} else {
showDialog(immediately);
}
updateCounter();
});
return this;
};

Expand Down
18 changes: 18 additions & 0 deletions www/storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = {
get: function (key) {
return new Promise(function(resolve, reject) {
NativeStorage.getItem(key, resolve, function(e) {
if (e.code === 2) {
resolve(null)
} else {
reject(e)
}
})
})
},
set: function (key, value) {
return new Promise(function(resolve, reject) {
NativeStorage.setItem(key, value, resolve, reject);
})
}
}

0 comments on commit 6c0bcc2

Please sign in to comment.