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 /** 歌词 */ 61 lyric: { 62 showStatusBarLyric: boolean; 63 topPercent: number; 64 leftPercent: number; 65 align: number; 66 color: string; 67 backgroundColor: string; 68 widthPercent: number; 69 fontSize: number; 70 // 详情页的字体大小 71 detailFontSize: number; 72 // 自动搜索歌词 73 autoSearchLyric: boolean; 74 }; 75 76 /** 主题 */ 77 theme: { 78 background: string; 79 backgroundOpacity: number; 80 backgroundBlur: number; 81 colors: CustomizedColors; 82 customColors?: CustomizedColors; 83 followSystem: boolean; 84 selectedTheme: string; 85 }; 86 87 backup: { 88 resumeMode: 'append' | 'overwrite'; 89 }; 90 91 plugin: { 92 subscribeUrl: string; 93 }; 94 webdav: { 95 url: string; 96 username: string; 97 password: string; 98 }; 99 }; 100 status: { 101 music: { 102 /** 当前的音乐 */ 103 track: IMusic.IMusicItem; 104 /** 进度 */ 105 progress: number; 106 /** 模式 */ 107 repeatMode: string; 108 /** 列表 */ 109 musicQueue: IMusic.IMusicItem[]; 110 /** 速度 */ 111 rate: number; 112 }; 113 app: { 114 /** 跳过特定版本 */ 115 skipVersion: string; 116 }; 117 }; 118} 119 120type FilterType<T, R = never> = T extends Record<string | number, any> 121 ? { 122 [P in keyof T]: T[P] extends ExceptionType ? R : T[P]; 123 } 124 : never; 125 126type KeyPaths< 127 T extends object, 128 Root extends boolean = true, 129 R = FilterType<T, ''>, 130 K extends keyof R = keyof R, 131> = K extends string | number 132 ? 133 | (Root extends true ? `${K}` : `.${K}`) 134 | (R[K] extends Record<string | number, any> 135 ? `${Root extends true ? `${K}` : `.${K}`}${KeyPaths< 136 R[K], 137 false 138 >}` 139 : never) 140 : never; 141 142type KeyPathValue<T extends object, K extends string> = T extends Record< 143 string | number, 144 any 145> 146 ? K extends `${infer S}.${infer R}` 147 ? KeyPathValue<T[S], R> 148 : T[K] 149 : never; 150 151type KeyPathsObj< 152 T extends object, 153 K extends string = KeyPaths<T>, 154> = T extends Record<string | number, any> 155 ? { 156 [R in K]: KeyPathValue<T, R>; 157 } 158 : never; 159 160type DeepPartial<T> = { 161 [K in keyof T]?: T[K] extends Record<string | number, any> 162 ? T[K] extends ExceptionType 163 ? T[K] 164 : DeepPartial<T[K]> 165 : T[K]; 166}; 167 168export type IConfigPaths = KeyPaths<IConfig>; 169type PartialConfig = DeepPartial<IConfig> | null; 170type IConfigPathsObj = KeyPathsObj<DeepPartial<IConfig>, IConfigPaths>; 171 172let config: PartialConfig = null; 173/** 初始化config */ 174async function setup() { 175 config = (await getStorage('local-config')) ?? {}; 176 // await checkValidPath(['setting.theme.background']); 177 notify(); 178} 179 180/** 设置config */ 181async function setConfig<T extends IConfigPaths>( 182 key: T, 183 value: IConfigPathsObj[T], 184 shouldNotify = true, 185) { 186 if (config === null) { 187 return; 188 } 189 const keys = key.split('.'); 190 191 const result = produce(config, draft => { 192 draft[keys[0] as keyof IConfig] = draft[keys[0] as keyof IConfig] ?? {}; 193 let conf: any = draft[keys[0] as keyof IConfig]; 194 for (let i = 1; i < keys.length - 1; ++i) { 195 if (!conf?.[keys[i]]) { 196 conf[keys[i]] = {}; 197 } 198 conf = conf[keys[i]]; 199 } 200 conf[keys[keys.length - 1]] = value; 201 return draft; 202 }); 203 204 setStorage('local-config', result); 205 config = result; 206 if (shouldNotify) { 207 notify(); 208 } 209} 210 211// todo: 获取兜底 212/** 获取config */ 213function getConfig(): PartialConfig; 214function getConfig<T extends IConfigPaths>(key: T): IConfigPathsObj[T]; 215function getConfig(key?: string) { 216 let result: any = config; 217 if (key && config) { 218 result = getPathValue(config, key); 219 } 220 221 return result; 222} 223 224/** 通过path获取值 */ 225function getPathValue(obj: Record<string, any>, path: string) { 226 const keys = path.split('.'); 227 let tmp = obj; 228 for (let i = 0; i < keys.length; ++i) { 229 tmp = tmp?.[keys[i]]; 230 } 231 return tmp; 232} 233 234/** 同步hook */ 235const notifyCbs = new Set<() => void>(); 236function notify() { 237 notifyCbs.forEach(_ => _?.()); 238} 239 240/** hook */ 241function useConfig(): PartialConfig; 242function useConfig<T extends IConfigPaths>(key: T): IConfigPathsObj[T]; 243function useConfig(key?: string) { 244 // TODO: 应该有性能损失 245 const [_cfg, _setCfg] = useState<PartialConfig>(config); 246 function setCfg() { 247 _setCfg(config); 248 } 249 useEffect(() => { 250 notifyCbs.add(setCfg); 251 return () => { 252 notifyCbs.delete(setCfg); 253 }; 254 }, []); 255 256 if (key) { 257 return _cfg ? getPathValue(_cfg, key) : undefined; 258 } else { 259 return _cfg; 260 } 261} 262 263const Config = { 264 get: getConfig, 265 set: setConfig, 266 useConfig, 267 setup, 268}; 269 270export default Config; 271