{"id":22282691,"url":"https://github.com/vergegroup/userdefaultssnapshot","last_synced_at":"2025-07-28T21:31:54.834Z","repository":{"id":45403802,"uuid":"324564568","full_name":"VergeGroup/UserDefaultsSnapshot","owner":"VergeGroup","description":"💽 A library that enables us to create a snapshot of the values which UserDefaults manages.","archived":false,"fork":false,"pushed_at":"2024-07-01T03:52:31.000Z","size":59,"stargazers_count":15,"open_issues_count":1,"forks_count":3,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-11-08T07:19:37.940Z","etag":null,"topics":["ios","userdefaults"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/VergeGroup.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-12-26T13:48:35.000Z","updated_at":"2024-08-30T05:00:59.000Z","dependencies_parsed_at":"2022-07-18T08:31:02.687Z","dependency_job_id":null,"html_url":"https://github.com/VergeGroup/UserDefaultsSnapshot","commit_stats":{"total_commits":22,"total_committers":2,"mean_commits":11.0,"dds":"0.045454545454545414","last_synced_commit":"2778214145d6813f1e58d6293589d0c58bd9f349"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VergeGroup%2FUserDefaultsSnapshot","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VergeGroup%2FUserDefaultsSnapshot/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VergeGroup%2FUserDefaultsSnapshot/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VergeGroup%2FUserDefaultsSnapshot/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/VergeGroup","download_url":"https://codeload.github.com/VergeGroup/UserDefaultsSnapshot/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227956777,"owners_count":17847108,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ios","userdefaults"],"created_at":"2024-12-03T16:35:06.133Z","updated_at":"2024-12-03T16:35:08.678Z","avatar_url":"https://github.com/VergeGroup.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# UserDefaultsSnapshot\n\nThis library enables us to create a snapshot of the values which UserDefaults manages.  \nThe snapshot means an immutable data model, that could be super effective to embed into the state of state-management.\n\n## Overview\n\n### Creates a schema of the snapshot from UserDefaults\n\nThis object is just a schema that projects the keys which the UserDefaults manages.  \nSo there are no namespaces, please carefully on naming the key.\n\n```swift\nfinal class MyDefaults: UserDefaultsObject {\n  @Property(key: \"count\") var count = 0\n  @OptionalProperty(key: \"name\") var name: String?\n}\n```\n\n\u003e 💎\nAs mention that above, this object is just a schema like a proxy to access UserDefaults (technically UserDefaults dictionary represented)  \nThe specified key would be used to read and write directly.  \nThis means we can start and stop using this library anytime!  \nAnd no needs to projects all of the keys on UserDefaults.  \nWe only project the keys which we want to put on the snapshot.  \nAnd we can create multiple schemas for each use-case.\n\n#### Attributes\n\n* `Property` - A non optional value property, returns the initialized value if UserDefautls returns nil.\n* `OptionalProperty` - A optional value property\n\n\u003cimg width=400px src=\"https://user-images.githubusercontent.com/1888355/103155348-6a81bb00-47e2-11eb-9925-8002ecba0dd0.png\" /\u003e\n\n### Creates a persistent-store\n\n```swift\nlet userDefaults = UserDefaults.init(\"your_userdefaults\")!\nlet persistentStore = UserDefaultsPersistentStore\u003cMyDefaults\u003e(userDefaults: userDefaults)\n```\n\n### Writing the value over `UserDefaultsPersistentStore`\n\nThanks to creating a schema, we can modify the value with type-safely.  \n\n```swift\npersistentStore.write { d in\n  d.name = \"John\"\n}\n\nXCTAssertEqual(userDefaults.string(forKey: \"name\"), \"john\") // ✅\n```\n\n## Reading the value from persitent-store\n\nUsing a snapshot to read the value which UserDefaults manages.  \nAnd the snapshot reads the backing dictionary represented by UserDefaults creates.\n\nSame as writing, thanks to creating a schema, we can read the value with type-safely.  \n\n```swift\nlet snaphot: UserDefaultsSnapshot\u003cMyDefaults\u003e = persistentStore.makeSnapshot()\n\nXCTAssertEqual(snaphot.name, \"John\") // ✅\n```\n\n### Subscribing the snapshot each UserDefaults updates\n\n`UserDefaultsPersistentStore` publishes new snapshot each receiving the notification that indicates UserDefaults changed.  \nWith this, it provides `sinkSnapshot` method.\n\n```swift\nlet token = persistentStore.sinkSnapshot { snapshot in\n  // Receives initial snapshot and every time UserDefaults updated.\n}\n```\n\n### Integrating with Verge\n\n[**Verge**](https://github.com/VergeGroup/Verge) is a state-management library.\n\nA snapshot is a reference type, but it's an immutable data model.  \nIt can be embedded in the value type such as a state of something like a store in state-management.\n\n```swift\nstruct MyState {\n\n  // ✅ Embed a snapshot here.\n  var defaults: UserDefaultsSnapshot\u003cMyDefaults\u003e\n  \n  // 💡 We can add any computed property to munipulate the value and provides.\n  var localizedName: String {\n    defaults.name + \"+something\"\n  }\n}\n\nlet persistentStore: UserDefaultsPersistentStore\u003cMyDefaults\u003e\n\nlet store: MyStore\u003cMyState, Never\u003e = .init(initialState: .init(defaults: persistentStore.makeSnapshot())\n\nlet token = persistentStore.sinkSnapshot { [weak store] snapshot in\n  // ✅ Updates a snapshot every updates.\n  store?.commit {\n    $0.defaults = snapshot\n  }  \n}\n```\n\nUse store\n```swift\nlet store: MyStore\u003cMyState, Never\u003e\n\nstore.sinkState { state in \n\n  state.ifChanged(\\.localizedName) { value in \n    print(value) // \"John+something\"\n  }\n  \n}\n\n```\n\n## Installations\n\nImport a module with following installation methods.\n\n```swift\nimport UserDefaultsSnapshotLib\n```\n\n**CocoaPods**\n\n[CocoaPods](https://cocoapods.org) 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`:\n\n\n```ruby\npod 'UserDefaultsSnapshotLib'\n```\n\n**SwiftPM**\n\nThe [Swift Package Manager](https://swift.org/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.\n\nOnce 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`.\n\n```swift\ndependencies: [\n  .package(url: \"https://github.com/VergeGroup/UserDefaultsSnapshot.git\", .upToNextMajor(from: \"1.0.0\"))\n]\n```\n\n## Author\n\n[🇯🇵 Muukii (Hiroshi Kimura)](https://github.com/muukii)\n\n## License\n\nUserDefaultsSnapshot is released under the MIT license.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvergegroup%2Fuserdefaultssnapshot","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvergegroup%2Fuserdefaultssnapshot","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvergegroup%2Fuserdefaultssnapshot/lists"}