ObjectUtils.ets 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import { WidgetPersonalConfig, UserPreferences } from './WidgetConfigManager';
  2. import { ThemeInfo } from './ThemeSyncService';
  3. /**
  4. * 对象工具类
  5. * 提供ArkTS兼容的对象操作方法
  6. */
  7. export class ObjectUtils {
  8. /**
  9. * 克隆WidgetPersonalConfig对象
  10. */
  11. static cloneWidgetConfig(config: WidgetPersonalConfig): WidgetPersonalConfig {
  12. return {
  13. showProgress: config.showProgress,
  14. showCover: config.showCover,
  15. showAlbum: config.showAlbum,
  16. showTime: config.showTime,
  17. theme: config.theme,
  18. backgroundColor: config.backgroundColor,
  19. textColor: config.textColor,
  20. accentColor: config.accentColor,
  21. buttonSize: config.buttonSize,
  22. fontSize: config.fontSize,
  23. borderRadius: config.borderRadius,
  24. autoUpdate: config.autoUpdate,
  25. updateInterval: config.updateInterval,
  26. clickBehavior: config.clickBehavior,
  27. enableAnimations: config.enableAnimations,
  28. enableHapticFeedback: config.enableHapticFeedback,
  29. enableShadow: config.enableShadow
  30. };
  31. }
  32. /**
  33. * 克隆UserPreferences对象
  34. */
  35. static cloneUserPreferences(prefs: UserPreferences): UserPreferences {
  36. return {
  37. defaultTheme: prefs.defaultTheme,
  38. defaultSize: prefs.defaultSize,
  39. syncWithMainApp: prefs.syncWithMainApp,
  40. syncThemeColor: prefs.syncThemeColor,
  41. enableCache: prefs.enableCache,
  42. cacheExpiry: prefs.cacheExpiry,
  43. showSongInfo: prefs.showSongInfo,
  44. showArtistInfo: prefs.showArtistInfo,
  45. showAlbumCover: prefs.showAlbumCover
  46. };
  47. }
  48. /**
  49. * 克隆ThemeInfo对象
  50. */
  51. static cloneThemeInfo(theme: ThemeInfo): ThemeInfo {
  52. return {
  53. theme: theme.theme,
  54. primaryColor: theme.primaryColor,
  55. backgroundColor: theme.backgroundColor,
  56. textColor: theme.textColor,
  57. accentColor: theme.accentColor,
  58. isDarkMode: theme.isDarkMode
  59. };
  60. }
  61. /**
  62. * 合并UserPreferences对象
  63. */
  64. static mergeUserPreferences(target: UserPreferences, source: Partial<UserPreferences>): UserPreferences {
  65. const result = ObjectUtils.cloneUserPreferences(target);
  66. if (source.defaultTheme !== undefined) result.defaultTheme = source.defaultTheme;
  67. if (source.defaultSize !== undefined) result.defaultSize = source.defaultSize;
  68. if (source.syncWithMainApp !== undefined) result.syncWithMainApp = source.syncWithMainApp;
  69. if (source.syncThemeColor !== undefined) result.syncThemeColor = source.syncThemeColor;
  70. if (source.enableCache !== undefined) result.enableCache = source.enableCache;
  71. if (source.cacheExpiry !== undefined) result.cacheExpiry = source.cacheExpiry;
  72. if (source.showSongInfo !== undefined) result.showSongInfo = source.showSongInfo;
  73. if (source.showArtistInfo !== undefined) result.showArtistInfo = source.showArtistInfo;
  74. if (source.showAlbumCover !== undefined) result.showAlbumCover = source.showAlbumCover;
  75. return result;
  76. }
  77. /**
  78. * 合并ThemeInfo对象
  79. */
  80. static mergeThemeInfo(target: ThemeInfo, source: Partial<ThemeInfo>): ThemeInfo {
  81. const result = ObjectUtils.cloneThemeInfo(target);
  82. if (source.theme !== undefined) result.theme = source.theme;
  83. if (source.primaryColor !== undefined) result.primaryColor = source.primaryColor;
  84. if (source.backgroundColor !== undefined) result.backgroundColor = source.backgroundColor;
  85. if (source.textColor !== undefined) result.textColor = source.textColor;
  86. if (source.accentColor !== undefined) result.accentColor = source.accentColor;
  87. if (source.isDarkMode !== undefined) result.isDarkMode = source.isDarkMode;
  88. return result;
  89. }
  90. /**
  91. * 通用对象克隆方法
  92. */
  93. static assign<T>(obj: T): T {
  94. if (obj === null || typeof obj !== 'object') {
  95. return obj;
  96. }
  97. const cloned: Record<string, Object> = {};
  98. const keys = Object.keys(obj);
  99. for (let i = 0; i < keys.length; i++) {
  100. const key = keys[i];
  101. const objRecord = obj as Record<string, Object>;
  102. cloned[key] = objRecord[key];
  103. }
  104. return cloned as T;
  105. }
  106. }