Skip to content

Commit

Permalink
use storage.ts everywhere instead of KVStore
Browse files Browse the repository at this point in the history
storage.ts replaces KVStore, so we can replace all uses of KVStore throughout the codebase, substituting the new functions from storage.ts as follows:

- `get` -> `storageGet`
- `set` -> `storageSet`
- `remove` -> `storageRemove`
- `getDirect` -> `storageGetDirect`
- `syncAllWebAndNativeValues` -> `storageSyncLocalAndNative`
  • Loading branch information
JGreenlee committed Sep 27, 2023
1 parent 73a8259 commit b99e001
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 41 deletions.
14 changes: 5 additions & 9 deletions www/js/config/dynamicConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import i18next from "i18next";
import { displayError, logDebug, logWarn } from "../plugin/logger";
import { getAngularService } from "../angular-react-helper";
import { fetchUrlCached } from "../commHelper";
import { storageClear, storageGet, storageSet } from "../plugin/storage";

export const CONFIG_PHONE_UI="config/app_ui_config";
export const CONFIG_PHONE_UI_KVSTORE ="CONFIG_PHONE_UI";
Expand Down Expand Up @@ -174,7 +175,6 @@ function extractSubgroup(token, config) {
* @returns {boolean} boolean representing whether the config was updated or not
*/
function loadNewConfig(newToken, existingVersion = null) {
const KVStore = getAngularService('KVStore');
const newStudyLabel = extractStudyName(newToken);
return readConfigFromServer(newStudyLabel).then((downloadedConfig) => {
if (downloadedConfig.version == existingVersion) {
Expand All @@ -190,7 +190,7 @@ function loadNewConfig(newToken, existingVersion = null) {
}
const storeConfigPromise = window['cordova'].plugins.BEMUserCache.putRWDocument(
CONFIG_PHONE_UI, toSaveConfig);
const storeInKVStorePromise = KVStore.set(CONFIG_PHONE_UI_KVSTORE, toSaveConfig);
const storeInKVStorePromise = storageSet(CONFIG_PHONE_UI_KVSTORE, toSaveConfig);
// loaded new config, so it is both ready and changed
return Promise.all([storeConfigPromise, storeInKVStorePromise]).then(
([result, kvStoreResult]) => {
Expand Down Expand Up @@ -220,17 +220,13 @@ export function initByUser(urlComponents) {
}

export function resetDataAndRefresh() {
const KVStore = getAngularService('KVStore');
const resetNativePromise = window['cordova'].plugins.BEMUserCache.putRWDocument(CONFIG_PHONE_UI, {});
const resetKVStorePromise = KVStore.clearAll();
return Promise.all([resetNativePromise, resetKVStorePromise])
.then(() => window.location.reload());
// const resetNativePromise = window['cordova'].plugins.BEMUserCache.putRWDocument(CONFIG_PHONE_UI, {});
storageClear({ local: true, native: true }).then(() => window.location.reload());
}

export function getConfig() {
if (storedConfig) return Promise.resolve(storedConfig);
const KVStore = getAngularService('KVStore');
return KVStore.get(CONFIG_PHONE_UI_KVSTORE).then((config) => {
return storageGet(CONFIG_PHONE_UI_KVSTORE).then((config) => {
if (config) {
storedConfig = config;
return config;
Expand Down
8 changes: 4 additions & 4 deletions www/js/control/ProfileSettings.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import ControlCollectionHelper, {getHelperCollectionSettings, getState, isMedium
import { resetDataAndRefresh } from "../config/dynamicConfig";
import { AppContext } from "../App";
import { shareQR } from "../components/QrCode";
import { storageClear } from "../plugin/storage";

//any pure functions can go outside
const ProfileSettings = () => {
Expand All @@ -35,7 +36,6 @@ const ProfileSettings = () => {
const CarbonDatasetHelper = getAngularService('CarbonDatasetHelper');
const UploadHelper = getAngularService('UploadHelper');
const EmailHelper = getAngularService('EmailHelper');
const KVStore = getAngularService('KVStore');
const NotificationScheduler = getAngularService('NotificationScheduler');
const ControlHelper = getAngularService('ControlHelper');
const ClientStats = getAngularService('ClientStats');
Expand Down Expand Up @@ -387,15 +387,15 @@ const ProfileSettings = () => {
style={settingStyles.dialog(colors.elevation.level3)}>
<Dialog.Title>{t('general-settings.clear-data')}</Dialog.Title>
<Dialog.Content>
<Button onPress={() => {KVStore.clearOnlyLocal;
<Button onPress={() => {storageClear({local: true})
setNukeVis(false);}}>
{t('general-settings.nuke-ui-state-only')}
</Button>
<Button onPress={() => {KVStore.clearOnlyNative;
<Button onPress={() => {storageClear({native: true});
setNukeVis(false);}}>
{t('general-settings.nuke-native-cache-only')}
</Button>
<Button onPress={() => {KVStore.clearAll;
<Button onPress={() => {storageClear({local: true, native: true});
setNukeVis(false);}}>
{t('general-settings.nuke-everything')}
</Button>
Expand Down
7 changes: 4 additions & 3 deletions www/js/metrics-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import angular from 'angular';
import { getBaseModeByValue } from './diary/diaryHelper'
import { labelOptions } from './survey/multilabel/confirmHelper';
import { storageGet, storageRemove, storageSet } from './plugin/storage';

angular.module('emission.main.metrics.factory',
['emission.main.metrics.mappings',
Expand Down Expand Up @@ -164,13 +165,13 @@ angular.module('emission.main.metrics.factory',
}

cc.set = function(info) {
return KVStore.set(USER_DATA_KEY, info);
return storageSet(USER_DATA_KEY, info);
};
cc.get = function() {
return KVStore.get(USER_DATA_KEY);
return storageGet(USER_DATA_KEY);
};
cc.delete = function() {
return KVStore.remove(USER_DATA_KEY);
return storageRemove(USER_DATA_KEY);
};
Number.prototype.between = function (min, max) {
return this >= min && this <= max;
Expand Down
5 changes: 3 additions & 2 deletions www/js/metrics-mappings.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import angular from 'angular';
import { getLabelOptions } from './survey/multilabel/confirmHelper';
import { getConfig } from './config/dynamicConfig';
import { storageGet, storageSet } from './plugin/storage';

angular.module('emission.main.metrics.mappings', ['emission.plugin.logger',
'emission.plugin.kvstore'])
Expand Down Expand Up @@ -146,7 +147,7 @@ angular.module('emission.main.metrics.mappings', ['emission.plugin.logger',
}

this.loadCarbonDatasetLocale = function() {
return KVStore.get(CARBON_DATASET_KEY).then(function(localeCode) {
return storageGet(CARBON_DATASET_KEY).then(function(localeCode) {
Logger.log("CarbonDatasetHelper.loadCarbonDatasetLocale() obtained value from storage [" + localeCode + "]");
if (!localeCode) {
localeCode = defaultCarbonDatasetCode;
Expand All @@ -158,7 +159,7 @@ angular.module('emission.main.metrics.mappings', ['emission.plugin.logger',

this.saveCurrentCarbonDatasetLocale = function (localeCode) {
setCurrentCarbonDatasetLocale(localeCode);
KVStore.set(CARBON_DATASET_KEY, currentCarbonDatasetCode);
storageSet(CARBON_DATASET_KEY, currentCarbonDatasetCode);
Logger.log("CarbonDatasetHelper.saveCurrentCarbonDatasetLocale() saved value [" + currentCarbonDatasetCode + "] to storage");
}

Expand Down
4 changes: 2 additions & 2 deletions www/js/onboarding/SaveQrPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { useTranslation } from "react-i18next";
import QrCode, { shareQR } from "../components/QrCode";
import { onboardingStyles } from "./OnboardingStack";
import { preloadDemoSurveyResponse } from "./SurveyPage";
import { storageSet } from "../plugin/storage";

const SaveQrPage = ({ }) => {

Expand All @@ -33,10 +34,9 @@ const SaveQrPage = ({ }) => {

function login(token) {
const CommHelper = getAngularService('CommHelper');
const KVStore = getAngularService('KVStore');
const EXPECTED_METHOD = "prompted-auth";
const dbStorageObject = {"token": token};
return KVStore.set(EXPECTED_METHOD, dbStorageObject).then((r) => {
return storageSet(EXPECTED_METHOD, dbStorageObject).then((r) => {
CommHelper.registerUser((successResult) => {
refreshOnboardingState();
}, function(errorResult) {
Expand Down
7 changes: 3 additions & 4 deletions www/js/onboarding/onboardingHelper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { DateTime } from "luxon";
import { getAngularService } from "../angular-react-helper";
import { getConfig } from "../config/dynamicConfig";
import { storageGet, storageSet } from "../plugin/storage";

export const INTRO_DONE_KEY = 'intro_done';

Expand Down Expand Up @@ -39,12 +40,10 @@ async function readConsented() {
}

async function readIntroDone() {
const KVStore = getAngularService('KVStore');
return KVStore.get(INTRO_DONE_KEY).then((read_val) => !!read_val) as Promise<boolean>;
return storageGet(INTRO_DONE_KEY).then((read_val) => !!read_val) as Promise<boolean>;
}

export async function markIntroDone() {
const currDateTime = DateTime.now().toISO();
const KVStore = getAngularService('KVStore');
return KVStore.set(INTRO_DONE_KEY, currDateTime);
return storageSet(INTRO_DONE_KEY, currDateTime);
}
27 changes: 14 additions & 13 deletions www/js/splash/referral.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import angular from 'angular';
import { storageGetDirect, storageRemove, storageSet } from '../plugin/storage';

angular.module('emission.splash.referral', ['emission.plugin.kvstore'])

Expand All @@ -11,32 +12,32 @@ angular.module('emission.splash.referral', ['emission.plugin.kvstore'])
var REFERRED_USER_ID = 'referred_user_id';

referralHandler.getReferralNavigation = function() {
const toReturn = KVStore.getDirect(REFERRAL_NAVIGATION_KEY);
KVStore.remove(REFERRAL_NAVIGATION_KEY);
const toReturn = storageGetDirect(REFERRAL_NAVIGATION_KEY);
storageRemove(REFERRAL_NAVIGATION_KEY);
return toReturn;
}

referralHandler.setupGroupReferral = function(kvList) {
KVStore.set(REFERRED_KEY, true);
KVStore.set(REFERRED_GROUP_ID, kvList['groupid']);
KVStore.set(REFERRED_USER_ID, kvList['userid']);
KVStore.set(REFERRAL_NAVIGATION_KEY, 'goals');
storageSet(REFERRED_KEY, true);
storageSet(REFERRED_GROUP_ID, kvList['groupid']);
storageSet(REFERRED_USER_ID, kvList['userid']);
storageSet(REFERRAL_NAVIGATION_KEY, 'goals');
};

referralHandler.clearGroupReferral = function(kvList) {
KVStore.remove(REFERRED_KEY);
KVStore.remove(REFERRED_GROUP_ID);
KVStore.remove(REFERRED_USER_ID);
KVStore.remove(REFERRAL_NAVIGATION_KEY);
storageRemove(REFERRED_KEY);
storageRemove(REFERRED_GROUP_ID);
storageRemove(REFERRED_USER_ID);
storageRemove(REFERRAL_NAVIGATION_KEY);
};

referralHandler.getReferralParams = function(kvList) {
return [KVStore.getDirect(REFERRED_GROUP_ID),
KVStore.getDirect(REFERRED_USER_ID)];
return [storageGetDirect(REFERRED_GROUP_ID),
storageGetDirect(REFERRED_USER_ID)];
}

referralHandler.hasPendingRegistration = function() {
return KVStore.getDirect(REFERRED_KEY)
return storageGetDirect(REFERRED_KEY)
};

return referralHandler;
Expand Down
9 changes: 5 additions & 4 deletions www/js/splash/startprefs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import angular from 'angular';
import { getConfig } from '../config/dynamicConfig';
import { storageGet, storageSet } from '../plugin/storage';

angular.module('emission.splash.startprefs', ['emission.plugin.logger',
'emission.splash.referral',
Expand Down Expand Up @@ -31,7 +32,7 @@ angular.module('emission.splash.startprefs', ['emission.plugin.logger',
// mark in native storage
return startprefs.readConsentState().then(writeConsentToNative).then(function(response) {
// mark in local storage
KVStore.set(DATA_COLLECTION_CONSENTED_PROTOCOL,
storageSet(DATA_COLLECTION_CONSENTED_PROTOCOL,
$rootScope.req_consent);
// mark in local variable as well
$rootScope.curr_consented = angular.copy($rootScope.req_consent);
Expand All @@ -41,13 +42,13 @@ angular.module('emission.splash.startprefs', ['emission.plugin.logger',

startprefs.markIntroDone = function() {
var currTime = moment().format();
KVStore.set(INTRO_DONE_KEY, currTime);
storageSet(INTRO_DONE_KEY, currTime);
$rootScope.$emit(startprefs.INTRO_DONE_EVENT, currTime);
}

// returns boolean
startprefs.readIntroDone = function() {
return KVStore.get(INTRO_DONE_KEY).then(function(read_val) {
return storageGet(INTRO_DONE_KEY).then(function(read_val) {
logger.log("in readIntroDone, read_val = "+JSON.stringify(read_val));
$rootScope.intro_done = read_val;
});
Expand Down Expand Up @@ -84,7 +85,7 @@ angular.module('emission.splash.startprefs', ['emission.plugin.logger',
.then(function(startupConfigResult) {
$rootScope.req_consent = startupConfigResult.data.emSensorDataCollectionProtocol;
logger.log("required consent version = " + JSON.stringify($rootScope.req_consent));
return KVStore.get(DATA_COLLECTION_CONSENTED_PROTOCOL);
return storageGet(DATA_COLLECTION_CONSENTED_PROTOCOL);
}).then(function(kv_store_consent) {
$rootScope.curr_consented = kv_store_consent;
console.assert(angular.isDefined($rootScope.req_consent), "in readConsentState $rootScope.req_consent", JSON.stringify($rootScope.req_consent));
Expand Down

0 comments on commit b99e001

Please sign in to comment.