taskNotificationPreferences.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. const TASK_NOTIFICATION_ENABLED_STORAGE_KEY = "zcode-notification-enabled";
  2. const TASK_NOTIFICATION_SOUND_ENABLED_STORAGE_KEY = "zcode-notification-sound-enabled";
  3. function readStoredBoolean(key: string, defaultValue: boolean): boolean {
  4. try {
  5. const value = localStorage.getItem(key);
  6. if (value == null) {
  7. return defaultValue;
  8. }
  9. return value !== "false";
  10. } catch {
  11. return defaultValue;
  12. }
  13. }
  14. function persistStoredBoolean(key: string, value: boolean): void {
  15. try {
  16. localStorage.setItem(key, String(value));
  17. } catch {
  18. // localStorage 不可用时静默忽略,保持 UI 主流程可继续工作。
  19. }
  20. }
  21. export function isTaskNotificationEnabled(): boolean {
  22. return readStoredBoolean(TASK_NOTIFICATION_ENABLED_STORAGE_KEY, true);
  23. }
  24. export function isTaskNotificationSoundPreferenceEnabled(): boolean {
  25. return readStoredBoolean(TASK_NOTIFICATION_SOUND_ENABLED_STORAGE_KEY, true);
  26. }
  27. export function isTaskNotificationSoundEnabled(): boolean {
  28. // 通知声音是任务通知的子能力,之前只有一个总开关时,
  29. // UI 无法表达“保留桌面通知但关闭提示音”,运行时也不知道声音必须依附通知存在。
  30. // 这里把声音偏好拆出来,但读取最终生效值时仍强制叠加通知总开关,
  31. // 保证设置页禁用态和实际播放行为一致,不会出现“通知关了却还能响”的错位。
  32. return isTaskNotificationEnabled() && isTaskNotificationSoundPreferenceEnabled();
  33. }
  34. export function persistTaskNotificationEnabled(enabled: boolean): void {
  35. persistStoredBoolean(TASK_NOTIFICATION_ENABLED_STORAGE_KEY, enabled);
  36. }
  37. export function persistTaskNotificationSoundEnabled(enabled: boolean): void {
  38. persistStoredBoolean(TASK_NOTIFICATION_SOUND_ENABLED_STORAGE_KEY, enabled);
  39. }