1818 * under the License.
1919 *
2020 */ ;
21- var AppRate , Locales , localeObj , exec ;
21+ var AppRate , Locales , localeObj , exec , Storage ;
2222
2323Locales = require ( './locales' ) ;
2424
2525exec = require ( 'cordova/exec' ) ;
2626
27+ Storage = require ( './storage' )
28+
2729AppRate = ( 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
0 commit comments