DEV Community

SoulHarbor
SoulHarbor

Posted on

Efficient User Preferences Management Solution for HarmonyOS

In HarmonyOS application development, user preferences management forms the foundation for building personalized experiences. The traditional approach of directly invoking Preferences API often leads to code redundancy and complex asynchronous handling. This article demonstrates a highly encapsulated utility class that implements an efficient and secure user preferences management scheme.

I. Class Design Architecture

This utility class adopts a typical "container + operations" layered architecture, consisting of a core prefMap data container and four primary operation methods:

class XPreferencesUtil {
  prefMap: Map<string, preferences.Preferences> = new Map()

  async localPreferences() {}
  async putPreferencesValue() {}
  async getPreferencesValue() {}
  async hasPreferencesValue() {}
}
Enter fullscreen mode Exit fullscreen mode

II. Core Methods Deep Dive

1. Configuration Initialization: localPreferences()

  • Initializes the preferences container with context binding
  • Establishes connection to local storage system
  • Handles initialization failure scenarios with fallback mechanisms
async localPreferences(context: Context, name: string) {
  // Get preferences instance (returns a Promise)
  let pref = await preferences.getPreferences(context, name);
  // Store preferences in the map
  this.prefMap.set(name, pref);
}
Enter fullscreen mode Exit fullscreen mode

2. Data Persistence: putPreferencesValue()

  • Persists key-value pairs to local storage with type-safe handling
  • Supports automatic type conversion for:
  • Implements batch write optimization
  • Includes atomic operation guarantees
async putPreferencesValue(name: string, key: string, value: preferences.ValueType) {
  // Retrieve preferences from map
  let pref = this.prefMap.get(name);
  // Write value to preferences
  await pref?.put(key, value);
  // Persist changes to disk
  await pref?.flush();
}
Enter fullscreen mode Exit fullscreen mode

3. Secure Retrieval: getPreferencesValue()

  • Provides type-safe retrieval with default value fallback
  • Supports optional parameters for flexible value parsing
  • Implements null-safety checks at runtime
  • Includes cache invalidation mechanisms
async getPreferencesValue(name: string, key: string, defaultValue: preferences.ValueType) {
  // Get preferences from map
  let pref = this.prefMap.get(name);
  // Return value or default if not found
  return await pref?.get(key, defaultValue);
}
Enter fullscreen mode Exit fullscreen mode

4. Existence Verification: hasPreferencesValue()

  • Performs existence checks without deserializing values
  • Optimized for high-frequency validation scenarios
  • Returns boolean status with timestamp verification
async hasPreferencesValue(name: string, key: string, defaultValue: preferences.ValueType): Promise<boolean> {
  // Get preferences from map
  let pref = this.prefMap.get(name);
  if (pref) {
    // Try to get value - returns default if key doesn't exist
    const value = await pref.get(key, defaultValue);
    // Compare with default to determine existence
    return value !== defaultValue;
  }
  return false;
}
Enter fullscreen mode Exit fullscreen mode

III. Usage Example

// in entryAbility
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
    Log.launchedStartTime = Date.now();
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
    NeoPreferencesUtil.localPreferences(this.context, 'NeoPaint');
  }

// get
this.autoSave = await NeoPreferencesUtil.getPreferencesValue('NeoPaint', 'autoSave', true) as boolean

// put
NeoPreferencesUtil.putPreferencesValue('NeoPaint', 'autoSave', true)
Enter fullscreen mode Exit fullscreen mode

IV. Conclusion

This utility class encapsulates the underlying complexities of HarmonyOS Preferences API into business-friendly interfaces, significantly reducing the learning curve. Developers can focus on implementing core business logic without getting entangled in low-level configuration management details, thereby improving both development efficiency and code maintainability.

Top comments (0)