Skip to content

Commit

Permalink
🗃️ Store the dynamic config as local storage in addition to RW document
Browse files Browse the repository at this point in the history
So it is stored both in the webview local storage and the SQLite database.

While reading, we might only have the RW document in the older versions, and we
use it to populate the local storage as needed for backwards compatibility and
a gradual migration.

We might want to remove the RW document in the future, but maybe at that time
we will have more reliable SQLite storage anyway.
  • Loading branch information
shankari committed Jul 12, 2023
1 parent dedf98c commit 0c748ed
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions www/js/config/dynamic_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import angular from 'angular';

angular.module('emission.config.dynamic', ['emission.plugin.logger'])
angular.module('emission.config.dynamic', ['emission.plugin.logger',
'emission.plugin.kvstore'])
.factory('DynamicConfig', function($http, $ionicPlatform,
$window, $state, $rootScope, $timeout, Logger) {
$window, $state, $rootScope, $timeout, KVStore, Logger) {
// also used in the startprefs class
// but without importing this
const CONFIG_PHONE_UI="config/app_ui_config";
const CONFIG_PHONE_UI_KVSTORE ="CONFIG_PHONE_UI";
const LOAD_TIMEOUT = 6000; // 6000 ms = 6 seconds

var dc = {};
Expand Down Expand Up @@ -62,8 +64,16 @@ angular.module('emission.config.dynamic', ['emission.plugin.logger'])

var loadSavedConfig = function() {
const nativePlugin = $window.cordova.plugins.BEMUserCache;
return nativePlugin.getDocument(CONFIG_PHONE_UI, false)
.then((savedConfig) => {
const rwDocRead = nativePlugin.getDocument(CONFIG_PHONE_UI, false);
const kvDocRead = KVStore.get(CONFIG_PHONE_UI_KVSTORE);
return Promise.all([rwDocRead, kvDocRead])
.then(([rwConfig, kvStoreConfig]) => {
const savedConfig = kvStoreConfig? kvStoreConfig : rwConfig;
if (!kvStoreConfig && rwConfig) {
// Backwards compat, can remove at the end of 2023
Logger.log("DYNAMIC CONFIG: rwConfig found, kvStoreConfig not found, setting to fix backwards compat");
KVStore.set(CONFIG_PHONE_UI_KVSTORE, rwConfig);
}
if (nativePlugin.isEmptyDoc(savedConfig)) {
Logger.log("Found empty saved ui config, returning null");
return undefined;
Expand Down Expand Up @@ -99,9 +109,11 @@ angular.module('emission.config.dynamic', ['emission.plugin.logger'])
{joined: {opcode: dc.scannedToken, study_name: newStudyLabel, subgroup: subgroup}});
const storeConfigPromise = $window.cordova.plugins.BEMUserCache.putRWDocument(
CONFIG_PHONE_UI, toSaveConfig);
const storeInKVStorePromise = KVStore.set(CONFIG_PHONE_UI_KVSTORE, toSaveConfig);
const logSuccess = (storeResults) => Logger.log("UI_CONFIG: Stored dynamic config successfully, result = "+JSON.stringify(storeResults));
// loaded new config, so it is both ready and changed
return storeConfigPromise.then((result) => {
return Promise.all([storeConfigPromise, storeInKVStorePromise]).then(
([result, kvStoreResult]) => {
logSuccess(result);
dc.saveAndNotifyConfigChanged(downloadedConfig);
dc.saveAndNotifyConfigReady(downloadedConfig);
Expand Down

0 comments on commit 0c748ed

Please sign in to comment.