xref: /MusicFree/src/core/config.ts (revision 316c909695af8978fd54ffb10af86d9214d66fef)
1// import {Quality} from '@/constants/commonConst';
2import {CustomizedColors} from '@/hooks/useColors';
3import {getStorage, setStorage} from '@/utils/storage';
4import produce from 'immer';
5import {useEffect, useState} from 'react';
6
7type ExceptionType = IMusic.IMusicItem | IMusic.IMusicItem[] | IMusic.IQuality;
8interface IConfig {
9    setting: {
10        basic: {
11            autoPlayWhenAppStart: boolean;
12            /** 使用移动网络播放 */
13            useCelluarNetworkPlay: boolean;
14            /** 使用移动网络下载 */
15            useCelluarNetworkDownload: boolean;
16            /** 最大同时下载 */
17            maxDownload: number | string;
18            /** 播放歌曲行为 */
19            clickMusicInSearch: '播放歌曲' | '播放歌曲并替换播放列表';
20            /** 点击专辑单曲 */
21            clickMusicInAlbum: '播放专辑' | '播放单曲';
22            /** 下载文件夹 */
23            downloadPath: string;
24            /** 同时播放 */
25            notInterrupt: boolean;
26            /** 打断时 */
27            tempRemoteDuck: '暂停' | '降低音量';
28            /** 播放错误时自动停止 */
29            autoStopWhenError: boolean;
30            /** 插件缓存策略 todo */
31            pluginCacheControl: string;
32            /** 最大音乐缓存 */
33            maxCacheSize: number;
34            /** 默认播放音质 */
35            defaultPlayQuality: IMusic.IQualityKey;
36            /** 音质顺序 */
37            playQualityOrder: 'asc' | 'desc';
38            /** 默认下载音质 */
39            defaultDownloadQuality: IMusic.IQualityKey;
40            /** 下载音质顺序 */
41            downloadQualityOrder: 'asc' | 'desc';
42            /** 歌曲详情页 */
43            musicDetailDefault: 'album' | 'lyric';
44            /** 歌曲详情页常亮 */
45            musicDetailAwake: boolean;
46            debug: {
47                errorLog: boolean;
48                traceLog: boolean;
49                devLog: boolean;
50            };
51            /** 最大历史记录条目 */
52            maxHistoryLen: number;
53            /** 启动时自动更新插件 */
54            autoUpdatePlugin: boolean;
55            // 不检查插件版本号
56            notCheckPluginVersion: boolean;
57            /** 关联歌词方式 */
58            associateLyricType: 'input' | 'search';
59            // 是否展示退出按钮
60            showExitOnNotification: boolean;
61        };
62        /** 歌词 */
63        lyric: {
64            showStatusBarLyric: boolean;
65            topPercent: number;
66            leftPercent: number;
67            align: number;
68            color: string;
69            backgroundColor: string;
70            widthPercent: number;
71            fontSize: number;
72            // 详情页的字体大小
73            detailFontSize: number;
74            // 自动搜索歌词
75            autoSearchLyric: boolean;
76        };
77
78        /** 主题 */
79        theme: {
80            background: string;
81            backgroundOpacity: number;
82            backgroundBlur: number;
83            colors: CustomizedColors;
84            customColors?: CustomizedColors;
85            followSystem: boolean;
86            selectedTheme: string;
87        };
88
89        backup: {
90            resumeMode: 'append' | 'overwrite';
91        };
92
93        plugin: {
94            subscribeUrl: string;
95        };
96        webdav: {
97            url: string;
98            username: string;
99            password: string;
100        };
101    };
102    status: {
103        music: {
104            /** 当前的音乐 */
105            track: IMusic.IMusicItem;
106            /** 进度 */
107            progress: number;
108            /** 模式 */
109            repeatMode: string;
110            /** 列表 */
111            musicQueue: IMusic.IMusicItem[];
112            /** 速度 */
113            rate: number;
114        };
115        app: {
116            /** 跳过特定版本 */
117            skipVersion: string;
118        };
119    };
120}
121
122type FilterType<T, R = never> = T extends Record<string | number, any>
123    ? {
124          [P in keyof T]: T[P] extends ExceptionType ? R : T[P];
125      }
126    : never;
127
128type KeyPaths<
129    T extends object,
130    Root extends boolean = true,
131    R = FilterType<T, ''>,
132    K extends keyof R = keyof R,
133> = K extends string | number
134    ?
135          | (Root extends true ? `${K}` : `.${K}`)
136          | (R[K] extends Record<string | number, any>
137                ? `${Root extends true ? `${K}` : `.${K}`}${KeyPaths<
138                      R[K],
139                      false
140                  >}`
141                : never)
142    : never;
143
144type KeyPathValue<T extends object, K extends string> = T extends Record<
145    string | number,
146    any
147>
148    ? K extends `${infer S}.${infer R}`
149        ? KeyPathValue<T[S], R>
150        : T[K]
151    : never;
152
153type KeyPathsObj<
154    T extends object,
155    K extends string = KeyPaths<T>,
156> = T extends Record<string | number, any>
157    ? {
158          [R in K]: KeyPathValue<T, R>;
159      }
160    : never;
161
162type DeepPartial<T> = {
163    [K in keyof T]?: T[K] extends Record<string | number, any>
164        ? T[K] extends ExceptionType
165            ? T[K]
166            : DeepPartial<T[K]>
167        : T[K];
168};
169
170export type IConfigPaths = KeyPaths<IConfig>;
171type PartialConfig = DeepPartial<IConfig> | null;
172type IConfigPathsObj = KeyPathsObj<DeepPartial<IConfig>, IConfigPaths>;
173
174let config: PartialConfig = null;
175/** 初始化config */
176async function setup() {
177    config = (await getStorage('local-config')) ?? {};
178    // await checkValidPath(['setting.theme.background']);
179    notify();
180}
181
182/** 设置config */
183async function setConfig<T extends IConfigPaths>(
184    key: T,
185    value: IConfigPathsObj[T],
186    shouldNotify = true,
187) {
188    if (config === null) {
189        return;
190    }
191    const keys = key.split('.');
192
193    const result = produce(config, draft => {
194        draft[keys[0] as keyof IConfig] = draft[keys[0] as keyof IConfig] ?? {};
195        let conf: any = draft[keys[0] as keyof IConfig];
196        for (let i = 1; i < keys.length - 1; ++i) {
197            if (!conf?.[keys[i]]) {
198                conf[keys[i]] = {};
199            }
200            conf = conf[keys[i]];
201        }
202        conf[keys[keys.length - 1]] = value;
203        return draft;
204    });
205
206    setStorage('local-config', result);
207    config = result;
208    if (shouldNotify) {
209        notify();
210    }
211}
212
213// todo: 获取兜底
214/** 获取config */
215function getConfig(): PartialConfig;
216function getConfig<T extends IConfigPaths>(key: T): IConfigPathsObj[T];
217function getConfig(key?: string) {
218    let result: any = config;
219    if (key && config) {
220        result = getPathValue(config, key);
221    }
222
223    return result;
224}
225
226/** 通过path获取值 */
227function getPathValue(obj: Record<string, any>, path: string) {
228    const keys = path.split('.');
229    let tmp = obj;
230    for (let i = 0; i < keys.length; ++i) {
231        tmp = tmp?.[keys[i]];
232    }
233    return tmp;
234}
235
236/** 同步hook */
237const notifyCbs = new Set<() => void>();
238function notify() {
239    notifyCbs.forEach(_ => _?.());
240}
241
242/** hook */
243function useConfig(): PartialConfig;
244function useConfig<T extends IConfigPaths>(key: T): IConfigPathsObj[T];
245function useConfig(key?: string) {
246    // TODO: 应该有性能损失
247    const [_cfg, _setCfg] = useState<PartialConfig>(config);
248    function setCfg() {
249        _setCfg(config);
250    }
251    useEffect(() => {
252        notifyCbs.add(setCfg);
253        return () => {
254            notifyCbs.delete(setCfg);
255        };
256    }, []);
257
258    if (key) {
259        return _cfg ? getPathValue(_cfg, key) : undefined;
260    } else {
261        return _cfg;
262    }
263}
264
265const Config = {
266    get: getConfig,
267    set: setConfig,
268    useConfig,
269    setup,
270};
271
272export default Config;
273