Skip to content

Instantly share code, notes, and snippets.

@sudomann
Last active December 18, 2020 23:48
Show Gist options
  • Save sudomann/788cd26c4a592b6e764d4d0e42a56a3f to your computer and use it in GitHub Desktop.
Save sudomann/788cd26c4a592b6e764d4d0e42a56a3f to your computer and use it in GitHub Desktop.
export default class ChoiceSetting {
static storageKey = undefined;
static label = undefined;
static defaultValue = undefined;
static set defaultValue(value) {
if (failsValidation(value))
throw new Error();
this.defaultValue = value;
}
static getValueAsync = async () => {
if (!(this.storageKey && this.label && this.defaultValue))
throw new Error();
return SecureStore.getItemAsync(this.storageKey).catch(() => this.defaultValue);
};
static setValueAsync = async (value) => {
if (failsValidation(value))
throw new Error();
return SecureStore.setItemAsync(this.storageKey, value);
};
}
export default class SettingsStore {
/**
*
* @param {string} userId UUID of currently signed in user
*/
constructor(userId) {
this.useBiometricFace = new BooleanSetting(
"useBiometricFace",
userId,
BooleanSetting.States.OFF
);
this.useBiometricFinger = new BooleanSetting(
"useBiometricFinger",
userId,
BooleanSetting.States.OFF
);
this.useSound = new BooleanSetting(
"useSound",
userId,
BooleanSetting.States.ON
);
}
static appLanguage = class extends ChoiceSetting {
static label = "appLanguage";
static CHOICES = Object.freeze({
en: "en_US",
fr: "fr_FR",
});
};
static appTheme = class extends ChoiceSetting {
static label = "appTheme";
static CHOICES = Object.freeze({
DEFAULT: "default", // light
DARK: "dark",
SYSTEM: "system",
});
};
}
import ChoiceSetting from './choice-setting'
export default class SettingsStore {
/**
*
* @param {string} userId UUID of currently signed in user
*/
constructor(userId) {
//... some setup
}
static appLanguage = class extends ChoiceSetting {
static label = "appLanguage";
static CHOICES = Object.freeze({
en: "en_US",
fr: "fr_FR",
});
};
static appTheme = class extends ChoiceSetting {
static label = "appTheme";
static CHOICES = Object.freeze({
DEFAULT: "default", // light
DARK: "dark",
SYSTEM: "system",
});
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment