2019-06-29 13:37:21 +02:00
|
|
|
// @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';
|
2019-07-31 14:22:36 +02:00
|
|
|
import AsyncStorageManager from "./AsyncStorageManager";
|
2019-08-06 13:28:11 +02:00
|
|
|
|
2019-06-29 13:37:21 +02:00
|
|
|
/**
|
|
|
|
* Singleton class used to manage themes
|
|
|
|
*/
|
2019-06-25 22:20:24 +02:00
|
|
|
export default class ThemeManager {
|
|
|
|
|
2019-06-29 13:37:21 +02:00
|
|
|
static instance: ThemeManager | null = null;
|
|
|
|
updateThemeCallback: Function;
|
2019-06-25 22:20:24 +02:00
|
|
|
|
|
|
|
constructor() {
|
2019-06-29 13:37:21 +02:00
|
|
|
this.updateThemeCallback = null;
|
2019-06-25 22:20:24 +02:00
|
|
|
}
|
|
|
|
|
2019-06-29 13:37:21 +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
|
|
|
}
|
|
|
|
|
2019-06-29 13:37:21 +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;
|
|
|
|
}
|
|
|
|
|
2019-06-29 13:37:21 +02:00
|
|
|
/**
|
|
|
|
* Set night mode and save it to preferences
|
|
|
|
*
|
|
|
|
* @param isNightMode Whether to enable night mode
|
|
|
|
*/
|
|
|
|
setNightMode(isNightMode: boolean) {
|
2019-07-31 14:22:36 +02:00
|
|
|
let nightModeKey = AsyncStorageManager.getInstance().preferences.nightMode.key;
|
|
|
|
AsyncStorageManager.getInstance().savePref(nightModeKey, isNightMode ? '1' : '0');
|
2019-06-29 13:37:21 +02:00
|
|
|
if (this.updateThemeCallback !== null)
|
2019-06-25 22:20:24 +02:00
|
|
|
this.updateThemeCallback();
|
|
|
|
}
|
|
|
|
|
2019-06-29 13:37:21 +02:00
|
|
|
/**
|
|
|
|
* @returns {boolean} Night mode state
|
|
|
|
*/
|
2019-07-31 14:22:36 +02:00
|
|
|
static getNightMode(): boolean {
|
|
|
|
return AsyncStorageManager.getInstance().preferences.nightMode.current === '1';
|
2019-06-25 22:20:24 +02:00
|
|
|
}
|
|
|
|
|
2019-06-29 13:37:21 +02:00
|
|
|
/**
|
|
|
|
* Get the current theme based on night mode
|
|
|
|
* @returns {Object}
|
|
|
|
*/
|
2019-07-31 14:22:36 +02:00
|
|
|
static getCurrentTheme(): Object {
|
|
|
|
if (ThemeManager.getNightMode())
|
2019-06-25 22:20:24 +02:00
|
|
|
return getTheme(platformDark);
|
|
|
|
else
|
|
|
|
return getTheme(platform);
|
|
|
|
}
|
|
|
|
|
2019-06-29 13:37:21 +02:00
|
|
|
/**
|
|
|
|
* Get the variables contained in the current theme
|
|
|
|
* @returns {Object}
|
|
|
|
*/
|
2019-07-31 14:22:36 +02:00
|
|
|
static getCurrentThemeVariables(): Object {
|
|
|
|
return ThemeManager.getCurrentTheme().variables;
|
2019-06-28 11:35:15 +02:00
|
|
|
}
|
|
|
|
|
2019-06-25 22:20:24 +02:00
|
|
|
};
|