Created
August 21, 2020 11:22
-
-
Save hansemannn/d43b046c489017b6f4750e80a3dfe11a to your computer and use it in GitHub Desktop.
Firebase remote config example for Axway Titanium
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import TiFirebaseConfig from 'firebase.config'; | |
export default class ConfigManager { | |
static fetch () { | |
return new Promise(resolve => { | |
const lastVersions = Ti.App.Properties.getList('kMyAppLastVersions', []); | |
const lastVersionLegacy = Ti.App.Properties.getString('lastVersion', null); | |
// Migrate prior updates | |
if (lastVersionLegacy) { | |
lastVersions.push(lastVersionLegacy); | |
Ti.App.Properties.setString('lastVersion', null); | |
} | |
const shouldForceFetch = lastVersions.length === 0 || !lastVersions.includes(Ti.App.version); | |
let didResolve = false; | |
const params = { | |
callback: _ => { | |
TiFirebaseConfig.activateFetched(); | |
if (!didResolve) { | |
didResolve = true; | |
setTimeout(() => { | |
resolve(); | |
}, 500); | |
} | |
} | |
}; | |
// If the app version changed, force a remote config update | |
if (shouldForceFetch) { | |
Ti.API.debug('App version changed, force remote config update!'); | |
lastVersions.push(Ti.App.version); | |
Ti.App.Properties.setList('kMyAppLastVersions', lastVersions); | |
params.expirationDuration = 0; | |
} | |
TiFirebaseConfig.setDefaults(Alloy.CFG.firebaseConfigDefaults); | |
TiFirebaseConfig.fetch(params); | |
// If the settings could not be received after 5 seconds, try again but start the app already | |
setTimeout(() => { | |
if (!didResolve) { | |
Ti.API.warn('Force re-fetch since timeout was exceeded'); | |
TiFirebaseConfig.fetch(params); | |
didResolve = true; | |
resolve(); | |
} | |
}, 5000); | |
}); | |
} | |
static getString(key) { | |
const value = TiFirebaseConfig.configValueForKey(key); | |
if (!value) { return undefined; } | |
return value.string; | |
} | |
static getBool(key) { | |
const value = TiFirebaseConfig.configValueForKey(key); | |
if (!value) { return false; } | |
return value.bool; | |
} | |
static getInt(key) { | |
const value = TiFirebaseConfig.configValueForKey(key); | |
if (!value) { return undefined; } | |
return parseInt(value.number); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment