Swiftpack.co -  VergeGroup/UserDefaultsSnapshot as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
VergeGroup/UserDefaultsSnapshot
πŸ’½ A library that enables us to create a snapshot of the values which UserDefaults manages.
.package(url: "https://github.com/VergeGroup/UserDefaultsSnapshot.git", from: "1.2.0")

UserDefaultsSnapshot

This library enables us to create a snapshot of the values which UserDefaults manages.
The snapshot means an immutable data model, that could be super effective to embed into the state of state-management.

swift5.3 Tests cocoapods

Overview

Creates a schema of the snapshot from UserDefaults

This object is just a schema that projects the keys which the UserDefaults manages.
So there are no namespaces, please carefully on naming the key.

final class MyDefaults: UserDefaultsObject {
  @Property(key: "count") var count = 0
  @OptionalProperty(key: "name") var name: String?
}

πŸ’Ž As mention that above, this object is just a schema like a proxy to access UserDefaults (technically UserDefaults dictionary represented)
The specified key would be used to read and write directly.
This means we can start and stop using this library anytime!
And no needs to projects all of the keys on UserDefaults.
We only project the keys which we want to put on the snapshot.
And we can create multiple schemas for each use-case.

Attributes

  • Property - A non optional value property, returns the initialized value if UserDefautls returns nil.
  • OptionalProperty - A optional value property

Creates a persistent-store

let userDefaults = UserDefaults.init("your_userdefaults")!
let persistentStore = UserDefaultsPersistentStore<MyDefaults>(userDefaults: userDefaults)

Writing the value over UserDefaultsPersistentStore

Thanks to creating a schema, we can modify the value with type-safely.

persistentStore.write { d in
  d.name = "John"
}

XCTAssertEqual(userDefaults.string(forKey: "name"), "john") // βœ…

Reading the value from persitent-store

Using a snapshot to read the value which UserDefaults manages.
And the snapshot reads the backing dictionary represented by UserDefaults creates.

Same as writing, thanks to creating a schema, we can read the value with type-safely.

let snaphot: UserDefaultsSnapshot<MyDefaults> = persistentStore.makeSnapshot()

XCTAssertEqual(store.makeSnapshot().name, "John") // βœ…

Subscribing the snapshot each UserDefaults updates

UserDefaultsPersistentStore publishes new snapshot each receiving the notification that indicates UserDefaults changed.
With this, it provides sinkSnapshot method.

let token = store.sinkSnapshot { snapshot in
  // Receives initial snapshot and every time UserDefaults updated.
}

Integrating with Verge

Verge is a state-management library.

A snapshot is a reference type, but it's an immutable data model.
It can be embedded in the value type such as a state of something like a store in state-management.

struct MyState {

  // βœ… Embed a snapshot here.
  var defaults: UserDefaultsSnapshot<MyDefaults>
  
  // πŸ’‘ We can add any computed property to munipulate the value and provides.
  var localizedName: String {
    defaults.name + "+something"
  }
}

let persistentStore: UserDefaultsPersistentStore<MyDefaults>

let store: MyStore<MyState, Never> = .init(initialState: .init(defaults: persistentStore.makeSnapshot())

let token = store.sinkSnapshot { [weak store] snapshot in
  // βœ… Updates a snapshot every updates.
  store?.commit {
    $0.defaults = snapshot
  }  
}

Use store

let store: MyStore<MyState, Never>

store.sinkState { state in 

  state.ifChanged(\.localizedName) { value in 
    print(value) // "John+something"
  }
  
}

Installations

Import a module with following installation methods.

import UserDefaultsSnapshotLib

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate Alamofire into your Xcode project using CocoaPods, specify it in your Podfile:

pod 'UserDefaultsSnapshotLib'

SwiftPM

The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler. It is in early development, but Alamofire does support its use on supported platforms.

Once you have your Swift package set up, adding Alamofire as a dependency is as easy as adding it to the dependencies value of your Package.swift.

dependencies: [
  .package(url: "https://github.com/VergeGroup/UserDefaultsSnapshot.git", .upToNextMajor(from: "1.0.0"))
]

Author

πŸ‡―πŸ‡΅ Muukii (Hiroshi Kimura)

License

UserDefaultsSnapshot is released under the MIT license.

GitHub

link
Stars: 12
Last commit: 4 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Release Notes

1.2.0
4 weeks ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API