Properly managing Ionic user preferences using the “Preferences API” of @capacitor

In every modern app, some sort of state is managed across app restarts. One such example are user preferences. Suppose you have a settings page in your application where users can check/uncheck preferences.

You would mostly have an interface like the following:

interface UserPreferences {
  optionOne: boolean;
  optionTwo: boolean;
  ...
  optionN: boolean
}

I am now trying to provide a global state variable that stores and serves the user preferences. For that, I have created an Angular service with a signal of type UserPreferences:

@Injectable({
  providedIn: 'root'
})
export class PreferencesService {
  public preferences = signal({ optionOne: true, optionTwo: false, ..., optionN: false } as UserPreferences);
}

Displaying that data in a component is also rather easy:

<ion-checkbox 
  [ngModel]="service.preferences().optionOne"
  (ngModelChange)="service.preferences().optionOne = $event"
></ion-checkbox>

<ion-checkbox 
  [ngModel]="service.preferences().optionTwo"
  (ngModelChange)="service.preferences().optionTwo = $event"
></ion-checkbox>

<ion-checkbox 
  [ngModel]="service.preferences().optionN"
  (ngModelChange)="service.preferences().optionN = $event"
></ion-checkbox>

With this, it is possible to update the global object.

However, the data is only stored in-memory. To persist this data and make the user preferences usable across several app restarts, one has to use some sort of storage. One such storage solution would be the PreferencesAPI of @capacitor, which is an async API. (Please note that using window.localStorage is not safe as webview storage might get deleted by the mobile operating system)

Issue 1

Loading the preferences from the storage and supplying them to the signal is not possible easily, as await can only be used in async functions. Meaning, something like this is not possible:

import { Preferences } from '@capacitor/preferences';

@Injectable({
  providedIn: 'root'
})
export class PreferencesService {
  public preferences = signal(await this.getPreferences());

  async getPreferences(): Promise<UserPreferences> {
    const { value } = await Preferences.get({ key: this.storageKey });
    if (value) {
      return JSON.parse(value);
    }
    const newPreferences: UserPreferences = {
      optionOne: true,
      optionTwo: false,
      optionN: true
    };
    await this.store(newPreferences);
    return newPreferences;
  }
}

Omitting the await also does not work, as the signal’s type would be of Promise<UserPreferences>, which is tedious to deal with.

Issue 2

When setting the value in the template via ngModelChange, I would like automatically call a function that stores the changed UserPreferences with the PreferencesAPI.

Possible, but not favoured solution

A workaround for both issues would be creating a signal for every user setting instead of relying on an object.

Something like this:

@Injectable({
  providedIn: 'root'
})
export class PreferencesService {
  public optionOne = signal(true);
  public optionTwo = signal(true);
  public optionN = signal(false);

  constructor() {
    effect(async () => {
      Preferences.set({ key: 'optionOne', value: JSON.stringify(this.optionOne()) });
      ...
    });
  }
}

And when starting the application, the values of the signals could be provided inside the app.component.ts file.

But with every new option a new signal instance variable has to be declared and in the effect hook the call to Preferences.set(...) has to be done, which is rather cumbersome.
I would prefer the initial approach with the usage of an object UserPreferences.

Is there any way this can be achieved? What is the best practice regarding this issue?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật