2019-03-27 22:39:25 +01:00
|
|
|
import { defaultTheme, themes } from "../types/HyperspaceTheme";
|
2019-04-01 19:48:00 +02:00
|
|
|
import { getNotificationRequestPermission } from './notifications';
|
2019-04-08 00:31:18 +02:00
|
|
|
import axios from 'axios';
|
|
|
|
import { Config } from "../types/Config";
|
2019-03-27 22:39:25 +01:00
|
|
|
|
|
|
|
type SettingsTemplate = {
|
|
|
|
[key:string]: any;
|
|
|
|
darkModeEnabled: boolean;
|
|
|
|
enablePushNotifications: boolean;
|
|
|
|
clearNotificationsOnRead: boolean;
|
2019-04-19 20:57:35 +02:00
|
|
|
displayAllOnNotificationBadge: boolean;
|
2019-03-27 22:39:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the user default from localStorage
|
|
|
|
* @param key The settings key to retrieve from localStorage
|
|
|
|
* @returns The boolean value associated with the key
|
|
|
|
*/
|
|
|
|
export function getUserDefaultBool(key: string): boolean {
|
|
|
|
if (localStorage.getItem(key) === null) {
|
|
|
|
console.warn('This key has not been set before, so the default value is FALSE for now.');
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return localStorage.getItem(key) === "true";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a user default to localStorage
|
|
|
|
* @param key The settings key in localStorage to change
|
|
|
|
* @param value The boolean value for the key
|
|
|
|
*/
|
|
|
|
export function setUserDefaultBool(key: string, value: boolean) {
|
|
|
|
if (localStorage.getItem(key) === null) {
|
|
|
|
console.warn('This key has not been set before.');
|
|
|
|
}
|
|
|
|
localStorage.setItem(key, value.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the user's default theme or the default theme
|
|
|
|
*/
|
|
|
|
export function getUserDefaultTheme() {
|
|
|
|
let returnTheme = defaultTheme;
|
|
|
|
themes.forEach((theme) => {
|
|
|
|
if(theme.key === localStorage.getItem('theme')) {
|
|
|
|
returnTheme = theme;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return returnTheme;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the user's default theme
|
|
|
|
* @param themeName The name of the theme
|
|
|
|
*/
|
|
|
|
export function setUserDefaultTheme(themeName: string) {
|
|
|
|
localStorage.setItem('theme', themeName);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the user defaults if they do not exist already.
|
|
|
|
*/
|
|
|
|
export function createUserDefaults() {
|
|
|
|
let defaults: SettingsTemplate = {
|
|
|
|
darkModeEnabled: false,
|
|
|
|
enablePushNotifications: true,
|
2019-04-19 20:57:35 +02:00
|
|
|
clearNotificationsOnRead: false,
|
2019-04-20 23:14:47 +02:00
|
|
|
displayAllOnNotificationBadge: false,
|
|
|
|
defaultVisibility: "public"
|
2019-03-27 22:39:25 +01:00
|
|
|
}
|
|
|
|
|
2019-04-20 23:14:47 +02:00
|
|
|
let settings = ["darkModeEnabled", "clearNotificationsOnRead", "displayAllOnNotificationBadge", "defaultVisibility"];
|
2019-03-27 22:39:25 +01:00
|
|
|
settings.forEach((setting: string) => {
|
|
|
|
if (localStorage.getItem(setting) === null) {
|
2019-04-20 23:14:47 +02:00
|
|
|
if (typeof defaults[setting] === "boolean") {
|
|
|
|
setUserDefaultBool(setting, defaults[setting]);
|
|
|
|
} else {
|
|
|
|
localStorage.setItem(setting, defaults[setting]);
|
|
|
|
}
|
|
|
|
|
2019-03-27 22:39:25 +01:00
|
|
|
}
|
|
|
|
})
|
2019-04-01 19:48:00 +02:00
|
|
|
getNotificationRequestPermission();
|
2019-04-08 00:31:18 +02:00
|
|
|
}
|
|
|
|
|
2019-04-12 19:39:24 +02:00
|
|
|
/**
|
|
|
|
* Gets the configuration data from `config.json`
|
|
|
|
* @returns The Promise data from getting the config.
|
|
|
|
*/
|
|
|
|
export async function getConfig() {
|
|
|
|
try {
|
|
|
|
const resp = await axios.get('config.json');
|
2019-04-08 00:31:18 +02:00
|
|
|
let config: Config = resp.data;
|
|
|
|
return config;
|
2019-04-12 19:39:24 +02:00
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
console.error("Couldn't configure Hyperspace with the config file. Reason: " + err.name);
|
|
|
|
}
|
2019-03-27 22:39:25 +01:00
|
|
|
}
|