|
|
@@ -0,0 +1,140 @@
|
|
|
+import data_preferences from '@ohos.data.preferences'
|
|
|
+import { common } from '@kit.AbilityKit'
|
|
|
+import Logger from '../util/Logger'
|
|
|
+
|
|
|
+const TAG = 'MusicCardFormStore'
|
|
|
+const STORE_NAME = 'music_card_form_store'
|
|
|
+const FORM_IDS_KEY = 'music_card_form_ids'
|
|
|
+type Preferences = ReturnType<typeof data_preferences.getPreferencesSync>
|
|
|
+
|
|
|
+const formatError = (error: unknown): string => {
|
|
|
+ if (error instanceof Error) {
|
|
|
+ return error.message
|
|
|
+ }
|
|
|
+ return `${error}`
|
|
|
+}
|
|
|
+
|
|
|
+const resolveContext = (context?: common.Context): common.Context | undefined => {
|
|
|
+ if (context) {
|
|
|
+ return context
|
|
|
+ }
|
|
|
+ return AppStorage.get('context') as common.Context | undefined
|
|
|
+}
|
|
|
+
|
|
|
+const resolvePreferences = (context?: common.Context): Preferences | undefined => {
|
|
|
+ const resolvedContext = resolveContext(context)
|
|
|
+ if (!resolvedContext) {
|
|
|
+ Logger.warn(TAG, 'context is undefined')
|
|
|
+ return undefined
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ return data_preferences.getPreferencesSync(resolvedContext, STORE_NAME)
|
|
|
+ } catch (error) {
|
|
|
+ Logger.error(TAG, `getPreferencesSync failed: ${formatError(error)}`)
|
|
|
+ return undefined
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+const normalizeFormId = (value: unknown): number | null => {
|
|
|
+ if (typeof value === 'number' && Number.isFinite(value)) {
|
|
|
+ return value
|
|
|
+ }
|
|
|
+ if (typeof value === 'string') {
|
|
|
+ const parsed = Number(value)
|
|
|
+ if (Number.isFinite(parsed)) {
|
|
|
+ return parsed
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null
|
|
|
+}
|
|
|
+
|
|
|
+const readFormIdsFromPreferences = (preferences: Preferences): number[] => {
|
|
|
+ let raw = ''
|
|
|
+ try {
|
|
|
+ raw = preferences.getSync(FORM_IDS_KEY, '') as string
|
|
|
+ } catch (error) {
|
|
|
+ Logger.error(TAG, `readFormIds failed: ${formatError(error)}`)
|
|
|
+ return []
|
|
|
+ }
|
|
|
+ if (!raw || raw.length === 0) {
|
|
|
+ return []
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ const parsed = JSON.parse(raw) as Array<number | string>
|
|
|
+ if (!Array.isArray(parsed)) {
|
|
|
+ return []
|
|
|
+ }
|
|
|
+ const result: number[] = []
|
|
|
+ for (let i = 0; i < parsed.length; i++) {
|
|
|
+ const normalized = normalizeFormId(parsed[i])
|
|
|
+ if (normalized !== null) {
|
|
|
+ result.push(normalized)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return Array.from(new Set(result))
|
|
|
+ } catch (error) {
|
|
|
+ Logger.error(TAG, `parseFormIds failed: ${formatError(error)}`)
|
|
|
+ return []
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+const writeFormIdsToPreferences = (
|
|
|
+ preferences: Preferences,
|
|
|
+ formIds: number[]
|
|
|
+): void => {
|
|
|
+ const payload = JSON.stringify(formIds ?? [])
|
|
|
+ preferences.putSync(FORM_IDS_KEY, payload)
|
|
|
+ preferences.flushSync()
|
|
|
+}
|
|
|
+
|
|
|
+export class MusicCardFormStore {
|
|
|
+ static readFormIds(context?: common.Context): number[] {
|
|
|
+ const preferences = resolvePreferences(context)
|
|
|
+ if (!preferences) {
|
|
|
+ return []
|
|
|
+ }
|
|
|
+ return readFormIdsFromPreferences(preferences)
|
|
|
+ }
|
|
|
+
|
|
|
+ static addFormId(context: common.Context | undefined, formId: number): void {
|
|
|
+ const preferences = resolvePreferences(context)
|
|
|
+ if (!preferences) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ const normalized = normalizeFormId(formId)
|
|
|
+ if (normalized === null) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ const current = readFormIdsFromPreferences(preferences)
|
|
|
+ if (current.includes(normalized)) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ current.push(normalized)
|
|
|
+ try {
|
|
|
+ writeFormIdsToPreferences(preferences, current)
|
|
|
+ } catch (error) {
|
|
|
+ Logger.error(TAG, `addFormId failed: ${formatError(error)}`)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ static removeFormId(context: common.Context | undefined, formId: number): void {
|
|
|
+ const preferences = resolvePreferences(context)
|
|
|
+ if (!preferences) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ const normalized = normalizeFormId(formId)
|
|
|
+ if (normalized === null) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ const current = readFormIdsFromPreferences(preferences)
|
|
|
+ const next = current.filter((item) => item !== normalized)
|
|
|
+ if (next.length === current.length) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ writeFormIdsToPreferences(preferences, next)
|
|
|
+ } catch (error) {
|
|
|
+ Logger.error(TAG, `removeFormId failed: ${formatError(error)}`)
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|