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