application-amicale/utils/ThemeManager.js

76 lines
2.1 KiB
JavaScript
Raw Normal View History

// @flow
2019-06-25 22:20:24 +02:00
import platform from '../native-base-theme/variables/platform';
import platformDark from '../native-base-theme/variables/platformDark';
import getTheme from '../native-base-theme/components';
import AsyncStorageManager from "./AsyncStorageManager";
/**
* Singleton class used to manage themes
*/
2019-06-25 22:20:24 +02:00
export default class ThemeManager {
static instance: ThemeManager | null = null;
updateThemeCallback: Function;
2019-06-25 22:20:24 +02:00
constructor() {
this.updateThemeCallback = null;
2019-06-25 22:20:24 +02:00
}
/**
* Get this class instance or create one if none is found
* @returns {ThemeManager}
*/
static getInstance(): ThemeManager {
return ThemeManager.instance === null ?
ThemeManager.instance = new ThemeManager() :
ThemeManager.instance;
2019-06-25 22:20:24 +02:00
}
/**
* Set the function to be called when the theme is changed (allows for general reload of the app)
* @param callback Function to call after theme change
*/
setUpdateThemeCallback(callback: ?Function) {
2019-06-25 22:20:24 +02:00
this.updateThemeCallback = callback;
}
/**
* Set night mode and save it to preferences
*
* @param isNightMode Whether to enable night mode
*/
setNightMode(isNightMode: boolean) {
let nightModeKey = AsyncStorageManager.getInstance().preferences.nightMode.key;
AsyncStorageManager.getInstance().savePref(nightModeKey, isNightMode ? '1' : '0');
if (this.updateThemeCallback !== null)
2019-06-25 22:20:24 +02:00
this.updateThemeCallback();
}
/**
* @returns {boolean} Night mode state
*/
static getNightMode(): boolean {
return AsyncStorageManager.getInstance().preferences.nightMode.current === '1';
2019-06-25 22:20:24 +02:00
}
/**
* Get the current theme based on night mode
* @returns {Object}
*/
static getCurrentTheme(): Object {
if (ThemeManager.getNightMode())
2019-06-25 22:20:24 +02:00
return getTheme(platformDark);
else
return getTheme(platform);
}
/**
* Get the variables contained in the current theme
* @returns {Object}
*/
static getCurrentThemeVariables(): Object {
return ThemeManager.getCurrentTheme().variables;
2019-06-28 11:35:15 +02:00
}
2019-06-25 22:20:24 +02:00
};