Persistent App Settings

Problem

There is a need for a uniform way to read configuration data as well as write configuration values at runtime.

Solution

Writable configuration from Uno.Extensions.Configuration provides you an interface for using and updating local persistent data.

Create a new record

public record AppConfig
{
    public string? Title { get; init; }
    public bool? IsDark { get; init; }
    public bool? Notification { get; init; }
    public string? AccentColor { get; init; }
}

Add to your IConfigBuilder

Use Section<T>() inside UseConfiguration. You can chain multiple configs.

.UseConfiguration(configure: configBuilder =>
    configBuilder
        .EmbeddedSource<App>()
        .Section<AppConfig>()
        .Section<Credentials>()
        .Section<SearchHistory>()
)

Get and Update the value

UserService.cs

  1. Inject IWritableOptions<AppConfig> in the constructor.
public class UserService(
    ChefsApiClient client,
    IWritableOptions<AppConfig> chefAppOptions,
    IWritableOptions<Credentials> credentialOptions)
    : IUserService
  1. Implement the logic to read and write to the configuration.
public async ValueTask<AppConfig> GetSettings(CancellationToken ct)
    => chefAppOptions.Value;
 public async Task SetSettings(AppConfig chefSettings, CancellationToken ct)
 {
    var settings = new AppConfig
    {
    Title = chefSettings.Title,
    IsDark = chefSettings.IsDark,
    Notification = chefSettings.Notification,
    AccentColor = chefSettings.AccentColor,
    };

    await chefAppOptions.UpdateAsync(_ => settings);
 }

Source Code

Documentation