forked from Blendman974/faceclaw
85 lines
3.4 KiB
TypeScript
85 lines
3.4 KiB
TypeScript
import { getStringSetting, setStringSetting } from "./settings-store";
|
|
|
|
const SOURCES_KEY = "notifications.sources";
|
|
const DEFAULT_KEY = "notifications.newAppsEnabledByDefault";
|
|
|
|
/** Unset preserves the behavior of existing installations. */
|
|
export function newNotificationAppsEnabledByDefault(): boolean {
|
|
return getStringSetting(DEFAULT_KEY, "true") !== "false";
|
|
}
|
|
|
|
export function setNewNotificationAppsEnabledByDefault(enabled: boolean): void {
|
|
setStringSetting(DEFAULT_KEY, String(enabled));
|
|
}
|
|
|
|
export type NotificationSource = {
|
|
packageName: string;
|
|
appName: string;
|
|
showOnGlasses: boolean;
|
|
};
|
|
|
|
type SavedSource = Omit<NotificationSource, "showOnGlasses"> & { showOnGlasses?: boolean };
|
|
|
|
/** Sources outlive their active notifications so a muted app can always be re-enabled. */
|
|
export function readNotificationSources(): NotificationSource[] {
|
|
const enabled = newNotificationAppsEnabledByDefault();
|
|
return readSavedSources().map((source) => ({ ...source, showOnGlasses: source.showOnGlasses ?? enabled }));
|
|
}
|
|
|
|
function readSavedSources(): SavedSource[] {
|
|
try {
|
|
const parsed = JSON.parse(getStringSetting(SOURCES_KEY, "[]"));
|
|
if (!Array.isArray(parsed)) return [];
|
|
return parsed.filter((source) => source && typeof source.packageName === "string" && source.packageName)
|
|
.map((source) => ({
|
|
packageName: source.packageName,
|
|
appName: typeof source.appName === "string" && source.appName ? source.appName : source.packageName,
|
|
showOnGlasses: typeof source.showOnGlasses === "boolean" ? source.showOnGlasses : undefined,
|
|
}));
|
|
} catch {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
export function rememberNotificationSources(notifications: { packageName: string; appName: string }[]): void {
|
|
const sources = new Map(readSavedSources().map((source) => [source.packageName, source]));
|
|
let changed = false;
|
|
for (const notification of notifications) {
|
|
if (!notification.packageName) continue;
|
|
const previous = sources.get(notification.packageName);
|
|
const appName = notification.appName || previous?.appName || notification.packageName;
|
|
if (!previous || previous.appName !== appName) {
|
|
sources.set(notification.packageName, {
|
|
packageName: notification.packageName,
|
|
appName,
|
|
showOnGlasses: previous?.showOnGlasses,
|
|
});
|
|
changed = true;
|
|
}
|
|
}
|
|
if (changed) setStringSetting(SOURCES_KEY, JSON.stringify(Array.from(sources.values())));
|
|
}
|
|
|
|
export function shouldShowNotificationOnGlasses(packageName: string): boolean {
|
|
return readNotificationSources().find((source) => source.packageName === packageName)?.showOnGlasses
|
|
?? newNotificationAppsEnabledByDefault();
|
|
}
|
|
|
|
export function setNotificationSourceEnabled(source: { packageName: string; appName: string }, enabled: boolean): void {
|
|
if (!source.packageName) return;
|
|
const sources = new Map(readSavedSources().map((item) => [item.packageName, item]));
|
|
sources.set(source.packageName, {
|
|
packageName: source.packageName,
|
|
appName: source.appName || source.packageName,
|
|
showOnGlasses: enabled,
|
|
});
|
|
setStringSetting(SOURCES_KEY, JSON.stringify(Array.from(sources.values())));
|
|
}
|
|
|
|
/** Set explicit choices only for sources already discovered; leave the future default alone. */
|
|
export function setAllNotificationSourcesEnabled(enabled: boolean): void {
|
|
setStringSetting(SOURCES_KEY, JSON.stringify(readSavedSources().map((source) => ({
|
|
...source, showOnGlasses: enabled,
|
|
}))));
|
|
}
|