1
1
angular . module ( 'emission.splash.startprefs' , [ 'emission.plugin.logger' ,
2
2
'emission.splash.referral' ,
3
+ 'emission.plugin.kvstore' ,
3
4
'angularLocalStorage' ] )
4
5
5
6
. factory ( 'StartPrefs' , function ( $window , $state , $interval , $rootScope , $ionicPlatform ,
6
- $ionicPopup , storage , $http , Logger , ReferralHandler ) {
7
+ $ionicPopup , KVStore , storage , $http , Logger , ReferralHandler ) {
7
8
var logger = Logger ;
8
9
var nTimesCalled = 0 ;
9
10
var startprefs = { } ;
@@ -47,11 +48,11 @@ angular.module('emission.splash.startprefs', ['emission.plugin.logger',
47
48
48
49
startprefs . markConsented = function ( ) {
49
50
logger . log ( "changing consent from " +
50
- $rootScope . curr_consented + " -> " + $rootScope . req_consent ) ;
51
+ $rootScope . curr_consented + " -> " + JSON . stringify ( $rootScope . req_consent ) ) ;
51
52
// mark in native storage
52
53
return startprefs . readConsentState ( ) . then ( writeConsentToNative ) . then ( function ( response ) {
53
54
// mark in local storage
54
- storage . set ( DATA_COLLECTION_CONSENTED_PROTOCOL ,
55
+ KVStore . set ( DATA_COLLECTION_CONSENTED_PROTOCOL ,
55
56
$rootScope . req_consent ) ;
56
57
// mark in local variable as well
57
58
$rootScope . curr_consented = angular . copy ( $rootScope . req_consent ) ;
@@ -60,72 +61,57 @@ angular.module('emission.splash.startprefs', ['emission.plugin.logger',
60
61
} ;
61
62
62
63
startprefs . markIntroDone = function ( ) {
63
- storage . set ( INTRO_DONE_KEY , true ) ;
64
- // Need to initialize this first because if we try to
65
- // create it inlike with {key: value}, the key becomes the
66
- // word "INTRO_DONE_KEY" and the stored object is
67
- // {"INTRO_DONE_KEY":"2018-01-31T06:26:02+00:00"}
68
- var to_store = { } ;
69
- to_store [ INTRO_DONE_KEY ] = moment ( ) . format ( ) ;
70
- $window . cordova . plugins . BEMUserCache . putLocalStorage ( INTRO_DONE_KEY , to_store ) ;
71
- $rootScope . $emit ( startprefs . INTRO_DONE_EVENT , $rootScope . req_consent ) ;
64
+ var currTime = moment ( ) . format ( ) ;
65
+ KVStore . set ( INTRO_DONE_KEY , currTime ) ;
66
+ $rootScope . $emit ( startprefs . INTRO_DONE_EVENT , currTime ) ;
72
67
}
73
68
74
69
// returns boolean
70
+ startprefs . readIntroDone = function ( ) {
71
+ return KVStore . get ( INTRO_DONE_KEY ) . then ( function ( read_val ) {
72
+ logger . log ( "in readIntroDone, read_val = " + JSON . stringify ( read_val ) ) ;
73
+ $rootScope . intro_done = read_val ;
74
+ } ) ;
75
+ }
76
+
75
77
startprefs . isIntroDone = function ( ) {
76
- var read_val = storage . get ( INTRO_DONE_KEY ) ;
77
- logger . log ( "in isIntroDone, read_val = " + read_val ) ;
78
- if ( read_val == null || read_val == "" ) {
78
+ if ( $rootScope . intro_done == null || $rootScope . intro_done == "" ) {
79
79
logger . log ( "in isIntroDone, returning false" ) ;
80
+ $rootScope . is_intro_done = false ;
80
81
return false ;
81
82
} else {
82
- logger . log ( "in isIntroDone, returning " + read_val ) ;
83
- return read_val ;
83
+ logger . log ( "in isIntroDone, returning true" ) ;
84
+ $rootScope . is_intro_done = true ;
85
+ return true ;
84
86
}
85
87
}
86
88
87
89
startprefs . isConsented = function ( ) {
88
- logger . log ( "curr_consented = " + $rootScope . curr_consented +
89
- "isIntroDone = " + startprefs . isIntroDone ( ) ) ;
90
- if ( startprefs . isIntroDone ( ) &&
91
- ( $rootScope . curr_consented == null || $rootScope . curr_consented == "" ) ) {
92
- alert ( "intro is done, but consent not found, re-consenting..." ) ;
93
- }
94
90
if ( $rootScope . curr_consented == null || $rootScope . curr_consented == "" ||
95
91
$rootScope . curr_consented . approval_date != $rootScope . req_consent . approval_date ) {
96
92
console . log ( "Not consented in local storage, need to show consent" ) ;
93
+ $rootScope . is_consented = false ;
97
94
return false ;
98
95
} else {
99
96
console . log ( "Consented in local storage, no need to show consent" ) ;
97
+ $rootScope . is_consented = true ;
100
98
return true ;
101
99
}
102
100
}
103
101
104
102
startprefs . readConsentState = function ( ) {
105
- /*
106
- * Read from local storage and move on so that we don't depend on native code.
107
- * Native code will be checked once the plugins are ready
108
- */
109
- if ( angular . isDefined ( $rootScope . req_consent ) &&
110
- angular . isDefined ( $rootScope . curr_consented ) &&
111
- $rootScope . curr_consented != null ) {
112
- // consent state is all populated
113
- logger . log ( "req_consent = " + $rootScope . req_consent
114
- + " curr_consented = " + $rootScope . curr_consented ) ;
115
- return new Promise ( function ( resolve , reject ) {
116
- logger . log ( "resolving with empty information" ) ;
117
- resolve ( ) ;
118
- } ) ;
119
- } else {
120
- // read consent state from the file and populate it
121
- return $http . get ( "json/startupConfig.json" )
122
- . then ( function ( startupConfigResult ) {
123
- $rootScope . req_consent = startupConfigResult . data . emSensorDataCollectionProtocol ;
124
- logger . log ( "required consent version = " + JSON . stringify ( $rootScope . req_consent ) ) ;
125
- $rootScope . curr_consented = storage . get (
126
- DATA_COLLECTION_CONSENTED_PROTOCOL ) ;
103
+ // read consent state from the file and populate it
104
+ return $http . get ( "json/startupConfig.json" )
105
+ . then ( function ( startupConfigResult ) {
106
+ $rootScope . req_consent = startupConfigResult . data . emSensorDataCollectionProtocol ;
107
+ logger . log ( "required consent version = " + JSON . stringify ( $rootScope . req_consent ) ) ;
108
+ return KVStore . get ( DATA_COLLECTION_CONSENTED_PROTOCOL ) ;
109
+ } ) . then ( function ( kv_store_consent ) {
110
+ $rootScope . curr_consented = kv_store_consent ;
111
+ console . assert ( angular . isDefined ( $rootScope . req_consent ) , "in readConsentState $rootScope.req_consent" , JSON . stringify ( $rootScope . req_consent ) ) ;
112
+ // we can just launch this, we don't need to wait for it
113
+ startprefs . checkNativeConsent ( ) ;
127
114
} ) ;
128
- }
129
115
}
130
116
131
117
/*
@@ -135,26 +121,33 @@ angular.module('emission.splash.startprefs', ['emission.plugin.logger',
135
121
*/
136
122
137
123
startprefs . getPendingOnboardingState = function ( ) {
138
- if ( ! startprefs . isIntroDone ( ) ) {
139
- // Since we must return a promise when the intro is done,
140
- // we create and return one here even though we don't need it
141
- return new Promise ( function ( resolve , reject ) {
142
- resolve ( 'root.intro' ) ;
143
- } ) ;
144
- } else {
145
- // intro is done. Now, let's read and check the current version
146
- // of the startup config
147
- return $http . get ( "json/startupConfig.json" )
148
- . then ( startprefs . readConsentState )
149
- . then ( startprefs . isConsented )
150
- . then ( function ( result ) {
151
- if ( result ) {
152
- return null ;
124
+ return startprefs . readStartupState ( ) . then ( function ( [ is_intro_done , is_consented ] ) {
125
+ if ( ! is_intro_done ) {
126
+ console . assert ( ! $rootScope . intro_done , "in getPendingOnboardingState first check, $rootScope.intro_done" , JSON . stringify ( $rootScope . intro_done ) ) ;
127
+ return 'root.intro' ;
128
+ } else {
129
+ // intro is done. Now let's check consent
130
+ console . assert ( is_intro_done , "in getPendingOnboardingState, local is_intro_done" , is_intro_done ) ;
131
+ console . assert ( $rootScope . is_intro_done , "in getPendingOnboardingState, $rootScope.intro_done" , $rootScope . intro_done ) ;
132
+ if ( is_consented ) {
133
+ return null ;
153
134
} else {
154
- return 'root.reconsent' ;
135
+ return 'root.reconsent' ;
155
136
}
156
- } ) ;
157
- }
137
+ }
138
+ } ) ;
139
+ } ;
140
+
141
+ /*
142
+ * Read the intro_done and consent_done variables into the $rootScope so that
143
+ * we can use them without making multiple native calls
144
+ */
145
+ startprefs . readStartupState = function ( ) {
146
+ var readIntroPromise = startprefs . readIntroDone ( )
147
+ . then ( startprefs . isIntroDone ) ;
148
+ var readConsentPromise = startprefs . readConsentState ( )
149
+ . then ( startprefs . isConsented ) ;
150
+ return Promise . all ( [ readIntroPromise , readConsentPromise ] ) ;
158
151
} ;
159
152
160
153
startprefs . getConsentDocument = function ( ) {
@@ -171,61 +164,16 @@ angular.module('emission.splash.startprefs', ['emission.plugin.logger',
171
164
startprefs . checkNativeConsent = function ( ) {
172
165
startprefs . getConsentDocument ( ) . then ( function ( resultDoc ) {
173
166
if ( resultDoc == null ) {
174
- startprefs . readConsentState ( )
175
- . then ( startprefs . isConsented )
176
- . then ( function ( consentState ) {
177
- if ( consentState == true ) {
178
- $ionicPopup . alert ( { template : "Local consent found, native consent missing, writing consent to native" } ) ;
179
- return writeConsentToNative ( ) ;
180
- }
181
- } ) ;
182
- }
183
- } ) ;
184
- }
185
-
186
- startprefs . checkUsercacheStorage = function ( key ) {
187
- // console.log("checkUsercacheStorage called");
188
- var ls_stored_val = storage . get ( key ) ;
189
- $window . cordova . plugins . BEMUserCache . getLocalStorage ( key , false ) . then ( function ( uc_stored_val ) {
190
- logger . log ( "uc_stored_val = " + JSON . stringify ( uc_stored_val ) + " ls_stored_val = " + ls_stored_val ) ;
191
- if ( angular . isDefined ( uc_stored_val ) && ( uc_stored_val != null )
192
- && ( key in uc_stored_val ) && angular . isDefined ( uc_stored_val [ key ] ) ) {
193
- if ( ls_stored_val == true ) {
194
- logger . log ( "local intro_done true, remote intro_done " + uc_stored_val [ key ] + ", already synced" ) ;
167
+ if ( startprefs . isConsented ( ) ) {
168
+ $ionicPopup . alert ( { template : "Local consent found, native consent missing, writing consent to native" } ) ;
169
+ return writeConsentToNative ( ) ;
195
170
} else {
196
- logger . log ( "local intro_done false, remote intro_done " + uc_stored_val [ key ] + ", setting local" ) ;
197
- $ionicPopup . alert ( { template : "Local " + key + " not found, native " + key + " found, writing " + key + " to local" } )
198
- storage . put ( key , true ) ;
199
- }
200
- } else {
201
- if ( ls_stored_val == true ) {
202
- logger . log ( "local intro_done found, remote intro_done not found, setting remote" ) ;
203
-
204
- // Need to initialize this first because if we try to
205
- // create it inlike with {key: value}, the key becomes the
206
- // word "key" and the stored object is
207
- // {"key":"2018-01-31T06:26:02+00:00"}
208
- var to_put = { } ;
209
- to_put [ key ] = moment ( ) . format ( ) ;
210
- $window . cordova . plugins . BEMUserCache . putLocalStorage ( key , to_put ) ;
211
- $ionicPopup . alert ( { template : "Local " + key + " found, native " + key + " missing, writing " + key + " to native" } )
212
- } else {
213
- logger . log ( "local intro_done false, remote intro_done not found, already synced" ) ;
171
+ logger . log ( "Both local and native consent not found, nothing to sync" ) ;
214
172
}
215
173
}
216
- } ) . catch ( function ( error ) {
217
- var display_msg = error . message + "\n" + error . stack ;
218
- logger . log ( "error in checkUsercacheStorage = " + display_msg ) ;
219
- $ionicPopup . alert ( { template : display_msg } ) ;
220
174
} ) ;
221
175
}
222
176
223
- startprefs . checkStorageConsistency = function ( ) {
224
- // console.log("checkStorageConsistency called");
225
- startprefs . checkNativeConsent ( ) ;
226
- startprefs . checkUsercacheStorage ( INTRO_DONE_KEY ) ;
227
- }
228
-
229
177
startprefs . getNextState = function ( ) {
230
178
return startprefs . getPendingOnboardingState ( ) . then ( function ( result ) {
231
179
if ( result == null ) {
@@ -295,7 +243,6 @@ angular.module('emission.splash.startprefs', ['emission.plugin.logger',
295
243
Logger . log ( "ionicPlatform.ready() called " + nTimesCalled + " times!" ) ;
296
244
nTimesCalled = nTimesCalled + 1 ;
297
245
startprefs . startWithPrefs ( ) ;
298
- startprefs . checkStorageConsistency ( ) ;
299
246
Logger . log ( "startprefs startup done" ) ;
300
247
} ) ;
301
248
0 commit comments