| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- import { WidgetPersonalConfig, UserPreferences } from './WidgetConfigManager';
- import { ThemeInfo } from './ThemeSyncService';
- /**
- * 对象工具类
- * 提供ArkTS兼容的对象操作方法
- */
- export class ObjectUtils {
- /**
- * 克隆WidgetPersonalConfig对象
- */
- static cloneWidgetConfig(config: WidgetPersonalConfig): WidgetPersonalConfig {
- return {
- showProgress: config.showProgress,
- showCover: config.showCover,
- showAlbum: config.showAlbum,
- showTime: config.showTime,
- theme: config.theme,
- backgroundColor: config.backgroundColor,
- textColor: config.textColor,
- accentColor: config.accentColor,
- buttonSize: config.buttonSize,
- fontSize: config.fontSize,
- borderRadius: config.borderRadius,
- autoUpdate: config.autoUpdate,
- updateInterval: config.updateInterval,
- clickBehavior: config.clickBehavior,
- enableAnimations: config.enableAnimations,
- enableHapticFeedback: config.enableHapticFeedback,
- enableShadow: config.enableShadow
- };
- }
- /**
- * 克隆UserPreferences对象
- */
- static cloneUserPreferences(prefs: UserPreferences): UserPreferences {
- return {
- defaultTheme: prefs.defaultTheme,
- defaultSize: prefs.defaultSize,
- syncWithMainApp: prefs.syncWithMainApp,
- syncThemeColor: prefs.syncThemeColor,
- enableCache: prefs.enableCache,
- cacheExpiry: prefs.cacheExpiry,
- showSongInfo: prefs.showSongInfo,
- showArtistInfo: prefs.showArtistInfo,
- showAlbumCover: prefs.showAlbumCover
- };
- }
- /**
- * 克隆ThemeInfo对象
- */
- static cloneThemeInfo(theme: ThemeInfo): ThemeInfo {
- return {
- theme: theme.theme,
- primaryColor: theme.primaryColor,
- backgroundColor: theme.backgroundColor,
- textColor: theme.textColor,
- accentColor: theme.accentColor,
- isDarkMode: theme.isDarkMode
- };
- }
- /**
- * 合并UserPreferences对象
- */
- static mergeUserPreferences(target: UserPreferences, source: Partial<UserPreferences>): UserPreferences {
- const result = ObjectUtils.cloneUserPreferences(target);
-
- if (source.defaultTheme !== undefined) result.defaultTheme = source.defaultTheme;
- if (source.defaultSize !== undefined) result.defaultSize = source.defaultSize;
- if (source.syncWithMainApp !== undefined) result.syncWithMainApp = source.syncWithMainApp;
- if (source.syncThemeColor !== undefined) result.syncThemeColor = source.syncThemeColor;
- if (source.enableCache !== undefined) result.enableCache = source.enableCache;
- if (source.cacheExpiry !== undefined) result.cacheExpiry = source.cacheExpiry;
- if (source.showSongInfo !== undefined) result.showSongInfo = source.showSongInfo;
- if (source.showArtistInfo !== undefined) result.showArtistInfo = source.showArtistInfo;
- if (source.showAlbumCover !== undefined) result.showAlbumCover = source.showAlbumCover;
-
- return result;
- }
- /**
- * 合并ThemeInfo对象
- */
- static mergeThemeInfo(target: ThemeInfo, source: Partial<ThemeInfo>): ThemeInfo {
- const result = ObjectUtils.cloneThemeInfo(target);
-
- if (source.theme !== undefined) result.theme = source.theme;
- if (source.primaryColor !== undefined) result.primaryColor = source.primaryColor;
- if (source.backgroundColor !== undefined) result.backgroundColor = source.backgroundColor;
- if (source.textColor !== undefined) result.textColor = source.textColor;
- if (source.accentColor !== undefined) result.accentColor = source.accentColor;
- if (source.isDarkMode !== undefined) result.isDarkMode = source.isDarkMode;
-
- return result;
- }
- /**
- * 通用对象克隆方法
- */
- static assign<T>(obj: T): T {
- if (obj === null || typeof obj !== 'object') {
- return obj;
- }
- const cloned: Record<string, Object> = {};
- const keys = Object.keys(obj);
- for (let i = 0; i < keys.length; i++) {
- const key = keys[i];
- const objRecord = obj as Record<string, Object>;
- cloned[key] = objRecord[key];
- }
-
- return cloned as T;
- }
- }
|