Files
notesnook/apps/mobile/app/stores/use-setting-store.ts

277 lines
8.5 KiB
TypeScript
Raw Normal View History

/*
This file is part of the Notesnook project (https://notesnook.com/)
2023-01-16 13:44:52 +05:00
Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2022-08-30 16:13:11 +05:00
2023-01-16 13:44:52 +05:00
import { Dimensions, PlatformOSType } from "react-native";
import Config from "react-native-config";
2023-02-13 16:49:25 +05:00
import { Sound } from "react-native-notification-sounds";
import { initialWindowMetrics } from "react-native-safe-area-context";
import { FileType } from "react-native-scoped-storage";
import { create } from "zustand";
global: implement the new theme engine (#2196) * mobile: theme * theme: add theme engine * mobile: migrate app colors to new theme engine * mobile: fixed some colors * mobile: fix colors * mobile: store theme info in store * theme: `ColorsType` -> `Variants` * theme: use explicit return type for `useThemeColors` * theme: add `backdrop` color * mobile: `const colors` -> `const {colors} * theme: add default pitch-black theme * mobile: manage theme state via theme-engine * mobile: add theme scopes * mobile: commit * mobile: fix button width on applock screen * mobile: fix typings * mobile: fix theme definition * web: add partial support for custom themes only context menus & popups are left. * theme: add dialog & sheet scopes * global: sync with master branch and make everything work again * mobile: fix theme-engine usage in editor & app * mobile: fix colors * mobile: fix colors * mobile: cleanup * mobile: fix status bar color incorrect on entering foreground * mobile: fix dark color scheme * web: move emotion theme provider to @notesnook/theme * editor: add support for theme enging * web: adjust hover & focus colors on list item * mobile: migrate share ext to theme engine * mobile: fix editor theme provider * clipper: add support for the new theme engine * mobile: fix statusbar color on switch from bg * misc: fix build * mobile: fix build * misc: fix colors * mobile: fix theme colors * mobile: fix bottom padding * server: add theme server * theme: add previewColors * server: support themes query pagination * mobile: add client from theme server * server: reset cache on sync repo * server: fix types * server: show ip & port on start server * server: theme updates * web: finalize new theme engine on web * editor: fix build * global: fix @emotion/react version to 11.11.1 * editor: update katex patch * web: fix imports * global: fix @trpc/* versions * global: a huge set of changes 1. get rid of ThemeVariant. All variants can now be accessed anywhere. 2. remove unnecessary button variants 3. make buttons more responsive 4. implement themes server * web: add support for theme search and theme switching * global: update lockfiles * mobile: fix error * theme: use vite-plugin-react to start theme server * web: add support for auto updating themes * mobile: update theme selector * mobile: update theme if new verison available * theme: add `isomorphic-fetch` package * global: update lockfiles * web: add theme details dialog * setup: add scope for themes server in bootstrap script * web: add production server url * web: update lockfile * web: update lockfile * mobile: remove `react-native-blob-util` * web: add support for endless scrolling in themes * web: bring back dark/light mode option in settings * web: fix colors in places * theme: add selected variant * global: use single typescript version across the projects * web: fix sort & group options not having submenus * web: apply selected variant where appropriate * ui: use unique id for all menu items * config: add ui scope for commits * theme: export button variant creation fn * web: fix only 1 theme showing in theme selector * web: fix navigation item hover & other colors * mobile: update theme * editor: fix toolbar group alignments * editor: set theme provider at app level * theme: use scope name to get current scope * mobile: fix color usage in message card * theme: remove caching * editor: bring back icons in table menus * theme: use zustand to manage theme engine state * web: fix login/signup theming * mobile: fix webpack build * misc: remove ThemeProvider usage * editor: adjust theming and styling of editor toolbar * mobile: refactor * editor: fix toolbar group padding everywhere * web: fix settings sidebar is not scrollable * web: add loading indicator for themes loading * mobile: fix warning * mobile: fix ui issues * web: fix Loader errors on build * theme: add getPreviewColors & validateTheme * theme: fix theme validation * mobile: load theme from file * mobile: fix share extension crash * mobile: rename state * theme: add sourceURL property * theme: refactor theme-engine * web: add support for loading theme from file * web: improve button hover interaction * mobile: fix floating button color * mobile: update theme * mobile: fix border radius of context menu * mobile: set sheet overlay color to theme backdrop * mobile: set sidemenu backdrop to theme backdrop --------- Co-authored-by: Abdullah Atta <abdullahatta@streetwriters.co>
2023-08-01 12:07:21 +05:00
import { ThemeDark, ThemeLight, ThemeDefinition } from "@notesnook/theme";
2026-01-09 11:22:03 +05:00
import { DayFormat, WeekFormat, Reminder } from "@notesnook/core";
2025-12-30 09:38:18 +05:00
import { db } from "../common/database";
2026-01-09 11:22:03 +05:00
import { EDITOR_LINE_HEIGHT } from "../utils/constants";
import { ShortcutItem } from "react-native-actions-shortcuts";
2024-10-30 14:42:47 +05:00
export const HostIds = [
"API_HOST",
"AUTH_HOST",
"SSE_HOST",
"MONOGRAPH_HOST"
] as const;
2024-08-07 15:07:48 +05:00
export type HostId = (typeof HostIds)[number];
2022-04-24 05:59:14 +05:00
export type Settings = {
showToolbarOnTop?: boolean;
showKeyboardOnOpen?: boolean;
fontScale?: number;
forcePortraitOnTablet?: boolean;
useSystemTheme?: boolean;
reminder: "daily" | "off" | "useroff" | "weekly" | "monthly";
fullBackupReminder: "never" | "weekly" | "monthly";
2022-04-24 05:59:14 +05:00
encryptedBackup?: boolean;
homepage?: string;
homepageV2?: {
id: string;
type: "notebook" | "tag" | "color" | "default";
};
2022-04-24 05:59:14 +05:00
sort?: string;
sortOrder?: string;
screenshotMode?: boolean;
privacyScreen?: boolean;
2024-01-17 12:23:34 +05:00
appLockTimer: number;
appLockEnabled?: boolean;
appLockMode?: "none" | "background" | "launch";
notebooksListMode?: "normal" | "compact";
notesListMode?: "normal" | "compact";
searchListMode?: "normal" | "compact";
2022-04-24 05:59:14 +05:00
devMode?: boolean;
notifNotes?: boolean;
pitchBlack?: boolean;
reduceAnimations?: boolean;
rateApp?: boolean | number;
migrated?: boolean;
introCompleted?: boolean;
nextBackupRequestTime?: number;
lastBackupDate?: number;
2022-04-24 05:59:14 +05:00
userEmailConfirmed?: boolean;
recoveryKeySaved?: boolean;
backupDirectoryAndroid?: FileType | null;
showBackupCompleteSheet: boolean;
lastRecoveryEmailTime?: number;
lastVerificationEmailTime?: number;
sessionExpired: boolean;
version: string | null;
2022-08-06 23:37:33 +05:00
doubleSpacedLines?: boolean;
2022-12-16 12:35:22 +05:00
disableAutoSync?: boolean;
disableSync?: boolean;
2023-01-05 21:52:28 +05:00
reminderNotifications?: boolean;
2023-01-04 19:13:09 +05:00
defaultSnoozeTime?: string;
reminderNotificationMode: Reminder["priority"];
2023-01-09 13:51:22 +05:00
corsProxy: string;
disableRealtimeSync?: boolean;
notificationSound?: Sound & { platform: PlatformOSType };
2023-08-03 18:24:25 +05:00
defaultFontSize: number;
defaultFontFamily: string;
global: implement the new theme engine (#2196) * mobile: theme * theme: add theme engine * mobile: migrate app colors to new theme engine * mobile: fixed some colors * mobile: fix colors * mobile: store theme info in store * theme: `ColorsType` -> `Variants` * theme: use explicit return type for `useThemeColors` * theme: add `backdrop` color * mobile: `const colors` -> `const {colors} * theme: add default pitch-black theme * mobile: manage theme state via theme-engine * mobile: add theme scopes * mobile: commit * mobile: fix button width on applock screen * mobile: fix typings * mobile: fix theme definition * web: add partial support for custom themes only context menus & popups are left. * theme: add dialog & sheet scopes * global: sync with master branch and make everything work again * mobile: fix theme-engine usage in editor & app * mobile: fix colors * mobile: fix colors * mobile: cleanup * mobile: fix status bar color incorrect on entering foreground * mobile: fix dark color scheme * web: move emotion theme provider to @notesnook/theme * editor: add support for theme enging * web: adjust hover & focus colors on list item * mobile: migrate share ext to theme engine * mobile: fix editor theme provider * clipper: add support for the new theme engine * mobile: fix statusbar color on switch from bg * misc: fix build * mobile: fix build * misc: fix colors * mobile: fix theme colors * mobile: fix bottom padding * server: add theme server * theme: add previewColors * server: support themes query pagination * mobile: add client from theme server * server: reset cache on sync repo * server: fix types * server: show ip & port on start server * server: theme updates * web: finalize new theme engine on web * editor: fix build * global: fix @emotion/react version to 11.11.1 * editor: update katex patch * web: fix imports * global: fix @trpc/* versions * global: a huge set of changes 1. get rid of ThemeVariant. All variants can now be accessed anywhere. 2. remove unnecessary button variants 3. make buttons more responsive 4. implement themes server * web: add support for theme search and theme switching * global: update lockfiles * mobile: fix error * theme: use vite-plugin-react to start theme server * web: add support for auto updating themes * mobile: update theme selector * mobile: update theme if new verison available * theme: add `isomorphic-fetch` package * global: update lockfiles * web: add theme details dialog * setup: add scope for themes server in bootstrap script * web: add production server url * web: update lockfile * web: update lockfile * mobile: remove `react-native-blob-util` * web: add support for endless scrolling in themes * web: bring back dark/light mode option in settings * web: fix colors in places * theme: add selected variant * global: use single typescript version across the projects * web: fix sort & group options not having submenus * web: apply selected variant where appropriate * ui: use unique id for all menu items * config: add ui scope for commits * theme: export button variant creation fn * web: fix only 1 theme showing in theme selector * web: fix navigation item hover & other colors * mobile: update theme * editor: fix toolbar group alignments * editor: set theme provider at app level * theme: use scope name to get current scope * mobile: fix color usage in message card * theme: remove caching * editor: bring back icons in table menus * theme: use zustand to manage theme engine state * web: fix login/signup theming * mobile: fix webpack build * misc: remove ThemeProvider usage * editor: adjust theming and styling of editor toolbar * mobile: refactor * editor: fix toolbar group padding everywhere * web: fix settings sidebar is not scrollable * web: add loading indicator for themes loading * mobile: fix warning * mobile: fix ui issues * web: fix Loader errors on build * theme: add getPreviewColors & validateTheme * theme: fix theme validation * mobile: load theme from file * mobile: fix share extension crash * mobile: rename state * theme: add sourceURL property * theme: refactor theme-engine * web: add support for loading theme from file * web: improve button hover interaction * mobile: fix floating button color * mobile: update theme * mobile: fix border radius of context menu * mobile: set sheet overlay color to theme backdrop * mobile: set sidemenu backdrop to theme backdrop --------- Co-authored-by: Abdullah Atta <abdullahatta@streetwriters.co>
2023-08-01 12:07:21 +05:00
colorScheme: "dark" | "light";
lighTheme: ThemeDefinition;
darkTheme: ThemeDefinition;
2024-03-04 16:10:14 +05:00
markdownShortcuts?: boolean;
2023-12-28 14:35:15 +05:00
appLockHasPasswordSecurity?: boolean;
biometricsAuthEnabled?: boolean;
backgroundSync?: boolean;
2024-02-07 13:03:25 +05:00
applockKeyboardType: "numeric" | "default";
settingsVersion?: number;
backupType: "full" | "partial";
offlineMode?: boolean;
lastFullBackupDate?: number;
2024-08-07 15:07:48 +05:00
serverUrls?: Record<HostId, string>;
defaultSidebarTab: number;
checkForUpdates?: boolean;
defaultLineHeight: number;
imageCompression: "ask-every-time" | "enabled" | "disabled";
keepScreenOn?: boolean;
2022-04-24 05:59:14 +05:00
};
2022-08-30 13:30:11 +05:00
type DimensionsType = {
2022-04-24 05:59:14 +05:00
width: number;
height: number;
};
type Insets = {
top: number;
left: number;
right: number;
bottom: number;
};
export interface SettingStore {
2022-04-24 05:59:14 +05:00
settings: Settings;
fullscreen: boolean;
deviceMode: string | null;
2022-08-30 13:30:11 +05:00
dimensions: DimensionsType;
2022-04-24 05:59:14 +05:00
setSettings: (settings: Settings) => void;
setFullscreen: (fullscreen: boolean) => void;
2024-12-06 10:27:42 +05:00
setDeviceMode: (mode: string | null) => void;
2022-08-30 13:30:11 +05:00
setDimensions: (dimensions: DimensionsType) => void;
2023-12-16 14:58:32 +05:00
isAppLoading: boolean;
setAppLoading: (isAppLoading: boolean) => void;
2022-04-24 05:59:14 +05:00
setSheetKeyboardHandler: (sheetKeyboardHandler: boolean) => void;
sheetKeyboardHandler: boolean;
requestBiometrics: boolean;
setRequestBiometrics: (requestBiometrics: boolean) => void;
appDidEnterBackgroundForAction: boolean;
setAppDidEnterBackgroundForAction: (value: boolean) => void;
insets: Insets;
setInsets: (insets: Insets) => void;
2023-06-05 15:08:40 +05:00
timeFormat: string;
dateFormat: string;
2025-12-30 09:38:18 +05:00
dayFormat: DayFormat;
2026-01-09 11:22:03 +05:00
weekFormat: WeekFormat;
2026-04-09 10:22:25 +05:00
vaultLockAfter: number;
2023-12-28 14:35:15 +05:00
dbPassword?: string;
isOldAppLock: () => boolean;
initialUrl: string | null;
2025-12-30 09:38:18 +05:00
refresh: () => void;
2026-01-24 12:32:12 +05:00
inboxEnabled: boolean;
setInboxEnabled: (inboxEnabled: boolean) => void;
pendingShortcut: ShortcutItem | null;
2022-04-24 05:59:14 +05:00
}
const { width, height } = Dimensions.get("window");
2022-04-24 05:59:14 +05:00
2023-06-12 14:54:32 +05:00
export const defaultSettings: SettingStore["settings"] = {
2024-02-07 13:03:25 +05:00
applockKeyboardType: "numeric",
2024-01-17 12:23:34 +05:00
appLockTimer: 0,
2023-06-12 14:54:32 +05:00
showToolbarOnTop: false,
disableAutoSync: false,
disableRealtimeSync: false,
disableSync: false,
appLockEnabled: false,
backupDirectoryAndroid: null,
offlineMode: false,
2023-06-12 14:54:32 +05:00
showKeyboardOnOpen: false,
fontScale: 1,
forcePortraitOnTablet: false,
useSystemTheme: true,
reminder: "off",
defaultSidebarTab: 0,
encryptedBackup: false,
2023-06-12 14:54:32 +05:00
homepage: "Notes",
sort: "default",
sortOrder: "desc",
screenshotMode: true,
privacyScreen: false,
appLockMode: "none",
notebooksListMode: "normal",
notesListMode: "normal",
searchListMode: "normal",
2023-06-12 14:54:32 +05:00
devMode: false,
notifNotes: false,
pitchBlack: false,
reduceAnimations: false,
rateApp: false,
migrated: false,
introCompleted: Config.isTesting ? true : false,
nextBackupRequestTime: undefined,
lastBackupDate: 0,
2023-06-12 14:54:32 +05:00
userEmailConfirmed: false,
recoveryKeySaved: false,
showBackupCompleteSheet: true,
sessionExpired: false,
version: null,
doubleSpacedLines: true,
reminderNotifications: true,
defaultSnoozeTime: "5",
corsProxy: "https://cors.notesnook.com",
reminderNotificationMode: "urgent",
notificationSound: undefined,
defaultFontFamily: "sans-serif",
2023-08-03 18:24:25 +05:00
defaultFontSize: 16,
global: implement the new theme engine (#2196) * mobile: theme * theme: add theme engine * mobile: migrate app colors to new theme engine * mobile: fixed some colors * mobile: fix colors * mobile: store theme info in store * theme: `ColorsType` -> `Variants` * theme: use explicit return type for `useThemeColors` * theme: add `backdrop` color * mobile: `const colors` -> `const {colors} * theme: add default pitch-black theme * mobile: manage theme state via theme-engine * mobile: add theme scopes * mobile: commit * mobile: fix button width on applock screen * mobile: fix typings * mobile: fix theme definition * web: add partial support for custom themes only context menus & popups are left. * theme: add dialog & sheet scopes * global: sync with master branch and make everything work again * mobile: fix theme-engine usage in editor & app * mobile: fix colors * mobile: fix colors * mobile: cleanup * mobile: fix status bar color incorrect on entering foreground * mobile: fix dark color scheme * web: move emotion theme provider to @notesnook/theme * editor: add support for theme enging * web: adjust hover & focus colors on list item * mobile: migrate share ext to theme engine * mobile: fix editor theme provider * clipper: add support for the new theme engine * mobile: fix statusbar color on switch from bg * misc: fix build * mobile: fix build * misc: fix colors * mobile: fix theme colors * mobile: fix bottom padding * server: add theme server * theme: add previewColors * server: support themes query pagination * mobile: add client from theme server * server: reset cache on sync repo * server: fix types * server: show ip & port on start server * server: theme updates * web: finalize new theme engine on web * editor: fix build * global: fix @emotion/react version to 11.11.1 * editor: update katex patch * web: fix imports * global: fix @trpc/* versions * global: a huge set of changes 1. get rid of ThemeVariant. All variants can now be accessed anywhere. 2. remove unnecessary button variants 3. make buttons more responsive 4. implement themes server * web: add support for theme search and theme switching * global: update lockfiles * mobile: fix error * theme: use vite-plugin-react to start theme server * web: add support for auto updating themes * mobile: update theme selector * mobile: update theme if new verison available * theme: add `isomorphic-fetch` package * global: update lockfiles * web: add theme details dialog * setup: add scope for themes server in bootstrap script * web: add production server url * web: update lockfile * web: update lockfile * mobile: remove `react-native-blob-util` * web: add support for endless scrolling in themes * web: bring back dark/light mode option in settings * web: fix colors in places * theme: add selected variant * global: use single typescript version across the projects * web: fix sort & group options not having submenus * web: apply selected variant where appropriate * ui: use unique id for all menu items * config: add ui scope for commits * theme: export button variant creation fn * web: fix only 1 theme showing in theme selector * web: fix navigation item hover & other colors * mobile: update theme * editor: fix toolbar group alignments * editor: set theme provider at app level * theme: use scope name to get current scope * mobile: fix color usage in message card * theme: remove caching * editor: bring back icons in table menus * theme: use zustand to manage theme engine state * web: fix login/signup theming * mobile: fix webpack build * misc: remove ThemeProvider usage * editor: adjust theming and styling of editor toolbar * mobile: refactor * editor: fix toolbar group padding everywhere * web: fix settings sidebar is not scrollable * web: add loading indicator for themes loading * mobile: fix warning * mobile: fix ui issues * web: fix Loader errors on build * theme: add getPreviewColors & validateTheme * theme: fix theme validation * mobile: load theme from file * mobile: fix share extension crash * mobile: rename state * theme: add sourceURL property * theme: refactor theme-engine * web: add support for loading theme from file * web: improve button hover interaction * mobile: fix floating button color * mobile: update theme * mobile: fix border radius of context menu * mobile: set sheet overlay color to theme backdrop * mobile: set sidemenu backdrop to theme backdrop --------- Co-authored-by: Abdullah Atta <abdullahatta@streetwriters.co>
2023-08-01 12:07:21 +05:00
colorScheme: "light",
lighTheme: ThemeLight,
2024-03-04 16:10:14 +05:00
darkTheme: ThemeDark,
markdownShortcuts: false,
2023-12-28 14:35:15 +05:00
biometricsAuthEnabled: false,
appLockHasPasswordSecurity: false,
backgroundSync: true,
settingsVersion: 0,
backupType: "partial",
fullBackupReminder: "never",
lastFullBackupDate: 0,
checkForUpdates: true,
defaultLineHeight: EDITOR_LINE_HEIGHT.DEFAULT,
imageCompression: "ask-every-time",
keepScreenOn: true
2023-06-12 14:54:32 +05:00
};
export const useSettingStore = create<SettingStore>((set, get) => ({
2023-12-28 14:35:15 +05:00
dbPassword: undefined,
2023-06-12 14:54:32 +05:00
settings: { ...defaultSettings },
2022-04-24 05:59:14 +05:00
sheetKeyboardHandler: true,
fullscreen: false,
2023-07-05 09:51:32 +05:00
deviceMode: null,
2022-04-24 05:59:14 +05:00
dimensions: { width, height },
2023-12-16 14:58:32 +05:00
isAppLoading: true,
setSettings: (settings) => set({ settings }),
setFullscreen: (fullscreen) => set({ fullscreen }),
setDeviceMode: (mode) => set({ deviceMode: mode }),
setDimensions: (dimensions) => set({ dimensions: dimensions }),
2023-12-16 14:58:32 +05:00
setAppLoading: (isAppLoading) => set({ isAppLoading }),
setSheetKeyboardHandler: (sheetKeyboardHandler) =>
set({ sheetKeyboardHandler }),
2022-04-24 05:59:14 +05:00
requestBiometrics: false,
setRequestBiometrics: (requestBiometrics) => set({ requestBiometrics }),
setInsets: (insets) => set({ insets }),
2023-06-05 15:08:40 +05:00
timeFormat: "12-hour",
dateFormat: "DD-MM-YYYY",
2025-12-30 09:38:18 +05:00
dayFormat: "short",
2026-01-09 11:22:03 +05:00
weekFormat: "Sun",
2026-04-09 10:22:25 +05:00
vaultLockAfter: 1000 * 60 * 30,
setAppDidEnterBackgroundForAction: (value: boolean) => {
set({
appDidEnterBackgroundForAction: value
});
},
appDidEnterBackgroundForAction: false,
2023-12-28 14:35:15 +05:00
isOldAppLock: () => {
return (
get().settings.appLockHasPasswordSecurity === undefined &&
get().settings.biometricsAuthEnabled === undefined &&
get().settings.appLockMode !== "none"
);
},
insets: initialWindowMetrics?.insets
? initialWindowMetrics.insets
: { top: 0, right: 0, left: 0, bottom: 0 },
2025-12-30 09:38:18 +05:00
initialUrl: null,
2026-01-24 12:32:12 +05:00
refresh: async () => {
2025-12-30 09:38:18 +05:00
set({
dayFormat: db.settings.getDayFormat(),
timeFormat: db.settings.getTimeFormat(),
dateFormat: db.settings?.getDateFormat(),
2026-01-24 12:32:12 +05:00
weekFormat: db.settings.getWeekFormat(),
2026-04-09 10:22:25 +05:00
vaultLockAfter: db.settings.getVaultLockAfter(),
2026-01-24 12:32:12 +05:00
inboxEnabled: await db.user.hasInboxKeys()
2025-12-30 09:38:18 +05:00
});
2026-01-24 12:32:12 +05:00
},
inboxEnabled: false,
setInboxEnabled: (inboxEnabled) => set({ inboxEnabled }),
pendingShortcut: null
2022-04-24 05:59:14 +05:00
}));