Giter Site home page Giter Site logo

userdefaultswrapper's Introduction

UserDefaultsWrapper

Swift

A Swift library that makes it easy to use UserDefaults.

Usage

import UserDefaultsWrapper

enum ColorType: String, Codable {
    case red
    case blue
    case green
    case black
    case white
}

// 1. Define preferences class that inherits `KeyValueStoreCoordinator`.
// 2. Add properties with property wrapper `@Stored`.
final class Preferences: KeyValueStoreCoordinator {
    @Stored("intNum")
    var intNum: Int = 3
    
    @Stored("optIntNum")
    var optIntNum: Int?
    
    @Stored("str")
    var str: String = "hello"
    
    @Stored("encrypted_secretText")
    var secretText: String = "encrypted information"
    
    @Stored("rect")
    var rect: CGRect = CGRect(x: 1, y: 2, width: 3, height: 4)
    
    @Stored("colors")
    var colors: [ColorType] = [.blue, .black, .green]
    
    @Stored("dataModificationDate")
    var dataModificationDate: Date?
    
    static let standard: Preferences = .init(
        store: UserDefaultsStore(
            defaults: .standard,
            valueCoder: CryptoValueCoderDecorator( // Encrypt using ChaChaPoly
                valueCoder: JSONValueCoder(),
                symmetricKey: KeyChain.someSymmetricKey,
                encryptWhere: { $0.hasPrefix("encrypted_") })))
    
    static let inMemory: Preferences = .init(store: InMemoryStore())
}

// 3. Just use it.
let preferences = Preferences.standard

preferences.intNum = 5
assert(preferences.intNum == 5)
assert(UserDefaults.standard.integer(forKey: "intNum") == 5)

preferences.rect.size = CGSize(width: 5, height: 6)
assert(preferences.rect.size == CGSize(width: 5, height: 6))

preferences.$rect.sink { print("\($0)") }

// 4. Add `KeyValueLookup` conformance for more features.
extension Preferences: KeyValueLookup {}

preferences.removeStoredValue(for: \.intNum)
assert(UserDefaults.standard.object(forKey: "intNum") == nil)
assert(preferences.intNum == 3) // Default value

For more example usage, see the sample app in Samples folder.

Install

Swift Package Manager

.package(url: "https://github.com/nearfri/UserDefaultsWrapper", from: "0.9.0")

License

Preferences is released under the MIT license. See LICENSE for more information.

userdefaultswrapper's People

Contributors

nearfri avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.