diff --git a/www/js/config/dynamicConfig.ts b/www/js/config/dynamicConfig.ts index f88255d4c..7994391a4 100644 --- a/www/js/config/dynamicConfig.ts +++ b/www/js/config/dynamicConfig.ts @@ -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"; @@ -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) { @@ -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]) => { @@ -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; diff --git a/www/js/control/ProfileSettings.jsx b/www/js/control/ProfileSettings.jsx index 8035c9462..566c100c1 100644 --- a/www/js/control/ProfileSettings.jsx +++ b/www/js/control/ProfileSettings.jsx @@ -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 = () => { @@ -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'); @@ -387,15 +387,15 @@ const ProfileSettings = () => { style={settingStyles.dialog(colors.elevation.level3)}> {t('general-settings.clear-data')} - - - diff --git a/www/js/metrics-factory.js b/www/js/metrics-factory.js index ce813fbaa..4fc29fa7a 100644 --- a/www/js/metrics-factory.js +++ b/www/js/metrics-factory.js @@ -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', @@ -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; diff --git a/www/js/metrics-mappings.js b/www/js/metrics-mappings.js index 2b71df739..91db216da 100644 --- a/www/js/metrics-mappings.js +++ b/www/js/metrics-mappings.js @@ -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']) @@ -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; @@ -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"); } diff --git a/www/js/onboarding/SaveQrPage.tsx b/www/js/onboarding/SaveQrPage.tsx index 157ff4093..57fe8f679 100644 --- a/www/js/onboarding/SaveQrPage.tsx +++ b/www/js/onboarding/SaveQrPage.tsx @@ -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 = ({ }) => { @@ -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) { diff --git a/www/js/onboarding/onboardingHelper.ts b/www/js/onboarding/onboardingHelper.ts index 659382079..b1cdff0ed 100644 --- a/www/js/onboarding/onboardingHelper.ts +++ b/www/js/onboarding/onboardingHelper.ts @@ -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'; @@ -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; + return storageGet(INTRO_DONE_KEY).then((read_val) => !!read_val) as Promise; } 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); } diff --git a/www/js/splash/referral.js b/www/js/splash/referral.js index 9e4707200..4c0d2823b 100644 --- a/www/js/splash/referral.js +++ b/www/js/splash/referral.js @@ -1,4 +1,5 @@ import angular from 'angular'; +import { storageGetDirect, storageRemove, storageSet } from '../plugin/storage'; angular.module('emission.splash.referral', ['emission.plugin.kvstore']) @@ -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; diff --git a/www/js/splash/startprefs.js b/www/js/splash/startprefs.js index e535d179a..bec593131 100644 --- a/www/js/splash/startprefs.js +++ b/www/js/splash/startprefs.js @@ -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', @@ -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); @@ -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; }); @@ -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));