Skip to content

num42/RxUserDefaults

Repository files navigation

RxUserDefaults

License

Overview

RxUserDefaults is a reactive solution for managing user defaults, inspired by rx-preferences with type handling influenced by wrap and unbox.

Usage

To create a setting, initialize the class using its constructor:

let settings = RxSettings(userDefaults: userDefaults)
let setting = settings.setting(key: "INSERT_KEY", defaultValue: "DEFAULT")

The arguments are self-explanatory.

Supported Types:

  • Array (with types supported by UserDefaults)
  • Bool
  • Codable (using JSON Decoder)
  • Date (as ISO8601 String)
  • Double
  • Enum (enum must conform to the RxSettingEnum protocol)
  • Int
  • Set
  • String
  • UUID

Functions available:

// Retrieve the value
let val = setting.value

// Set the value
setting.value = val

// Check if the value is saved (note: the default value is not automatically saved)
setting.isSet

// Delete the value
setting.remove()

// Provides a hot observable that triggers on every change and starts with the current value (or default value)
setting.asObservable()

Storage Layer

If you prefer not to use UserDefaults as a storage layer, you can implement your own by confirming to the StorageLayer Protocol.

Warnings & TODOs

The goal is to support all types that UserDefaults supports (e.g., Dictionary, URL). For now, you can expand the library to more types by conforming to the RxSettingCompatible protocol. However, note that persisting types not supported by UserDefaults will fail silently.

Installation

RxUserDefaults is available through SPM.

Requirements

Authors

License

Licensed under the Apache License, Version 2.0. See the License for details.