diff --git a/Package.swift b/Package.swift
index 9810d96..15e11c6 100644
--- a/Package.swift
+++ b/Package.swift
@@ -26,7 +26,8 @@ let package = Package(
dependencies: [
.product(name: "RxSwift", package: "RxSwift"),
.product(name: "RxCocoa", package: "RxSwift")
- ]
+ ],
+ resources: [.copy("PrivacyInfo.xcprivacy")]
),
.testTarget(
name: "RxUserDefaultsTests",
diff --git a/README.md b/README.md
index 02de52d..996e22c 100644
--- a/README.md
+++ b/README.md
@@ -1,95 +1,70 @@
# RxUserDefaults
-[![CI Status](http://img.shields.io/travis/num42/RxUserDefaults.svg?style=flat)](https://travis-ci.org/num42/RxUserDefaults)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
-
-Overview
--------
-Reactive UserDefaults, inspired by [rx-preferences].
-
-Typehandling was inspired by [wrap] and [unbox]
-
+## 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 use the constructor of that class:
+To create a setting, initialize the class using its constructor:
```swift
let settings = RxSettings(userDefaults: userDefaults)
let setting = settings.setting(key: "INSERT_KEY", defaultValue: "DEFAULT")
```
-The arguments should be self explanatory.
+The arguments are self-explanatory.
-Currently only following Types are supported:
-- String
-- Int
+Supported Types:
+
+- Array (with types supported by UserDefaults)
- Bool
-- Array (only with the types supported by the UserDefaults)
-- Enum (but enum must conform to the RxSettingEnum protocol)
+- Codable (using JSON Decoder)
+- Date (as ISO8601 String)
+- Double
+- Enum (enum must conform to the RxSettingEnum protocol)
+- Int
+- Set
+- String
+- UUID
-Functions you can use:
+Functions available:
```swift
-// gets the value
+// Retrieve the value
let val = setting.value
-// sets the value
+// Set the value
setting.value = val
-// check if value is saved (note: the default value is not automatically saved)
+// Check if the value is saved (note: the default value is not automatically saved)
setting.isSet
-// deletes the value
+// Delete the value
setting.remove()
-// provides a hot observable that triggers on every change
-// and starts with the current value (or default value)
+// Provides a hot observable that triggers on every change and starts with the current value (or default value)
setting.asObservable()
-
```
## Storage Layer
-If you do not want to use the UserDefaults as a storage layer you can implement your own.
-To do this you have to confirm to the StorageLayer Protocol.
+If you prefer not to use UserDefaults as a storage layer, you can implement your own by confirming to the StorageLayer Protocol.
## Warnings & TODOs
-I want to support everything that the UserDefaults support (e.g. Dictionary, URL ...).
-For now you can expand the library to more types by conforming to the RxSettingCompatible protocol.
-But note that persisting types that are not supported by the UserDefaults will fail silently.
+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
+RxUserDefaults is available through SPM.
## Requirements
-
- [RxSwift](https://github.com/ReactiveX/RxSwift)
## Authors
+- David Kraus, kraus.david.dev@gmail.com
+- Hans-Martin Schuller, hm.schuller@gmail.com
+- Wolfgang Lutz, wolfgang@lutz-wiesent.de
-David Kraus, kraus.david.dev@gmail.com
-
-Hans-Martin Schuller, hm.schuller@gmail.com
-
-Wolfgang Lutz, wolfgang@lutz-wiesent.de
-
-License
--------
-
- Copyright 2018 Number42 GmbH
-
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
-
+## License
+Licensed under the Apache License, Version 2.0. See the [License](https://opensource.org/licenses/Apache-2.0) for details.
[rx-preferences]: https://github.com/f2prateek/rx-preferences
[wrap]: https://github.com/JohnSundell/Wrap
-[unbox]: https://github.com/JohnSundell/Unbox
+[unbox]: https://github.com/JohnSundell/Unbox
\ No newline at end of file
diff --git a/Sources/RxUserDefaults/PrivacyInfo.xcprivacy b/Sources/RxUserDefaults/PrivacyInfo.xcprivacy
new file mode 100644
index 0000000..4636fcd
--- /dev/null
+++ b/Sources/RxUserDefaults/PrivacyInfo.xcprivacy
@@ -0,0 +1,23 @@
+
+
+
+
+ NSPrivacyTracking
+
+ NSPrivacyTrackingDomains
+
+ NSPrivacyCollectedDataTypes
+
+ NSPrivacyAccessedAPITypes
+
+
+ NSPrivacyAccessedAPIType
+ NSPrivacyAccessedAPICategoryUserDefaults
+ NSPrivacyAccessedAPITypeReasons
+
+ C56D.1
+
+
+
+
+
diff --git a/Sources/RxUserDefaults/RxSettingCompatible/Codable+RxSettingsCompatible.swift b/Sources/RxUserDefaults/RxSettingCompatible/Codable+RxSettingsCompatible.swift
new file mode 100644
index 0000000..78458b3
--- /dev/null
+++ b/Sources/RxUserDefaults/RxSettingCompatible/Codable+RxSettingsCompatible.swift
@@ -0,0 +1,11 @@
+import Foundation
+
+public extension RxSettingCompatible where Self: Codable {
+ static func fromPersistedValue(value: Any) -> Self {
+ try! JSONDecoder().decode(Self.self, from: value as! Data)
+ }
+
+ func toPersistedValue() -> Any {
+ try! JSONEncoder().encode(self)
+ }
+}
diff --git a/Sources/RxUserDefaults/SettingTypeProtocol.swift b/Sources/RxUserDefaults/SettingTypeProtocol.swift
new file mode 100644
index 0000000..c10aa00
--- /dev/null
+++ b/Sources/RxUserDefaults/SettingTypeProtocol.swift
@@ -0,0 +1,36 @@
+import RxSwift
+
+// A protocol that is used by the https://github.com/num42/swift-macro-autosettingtype macro
+public protocol SettingTypeProtocol {
+ associatedtype SettingType: RxSettingCompatible
+
+ var setting: Setting { get set }
+
+ func asObservable() -> Observable
+ var isSet: Bool { get }
+ func remove()
+ var value: SettingType { get }
+ func setValue(value: SettingType)
+}
+
+public extension SettingTypeProtocol {
+ func asObservable() -> Observable {
+ setting.asObservable()
+ }
+
+ var isSet: Bool {
+ setting.isSet
+ }
+
+ func remove() {
+ setting.remove()
+ }
+
+ var value: SettingType {
+ setting.value
+ }
+
+ func setValue(value: SettingType) {
+ setting.value = value
+ }
+}
diff --git a/Tests/RxUserDefaultsTests/RxUserDefaultsTests.swift b/Tests/RxUserDefaultsTests/RxUserDefaultsTests.swift
index 767757c..7887cbe 100644
--- a/Tests/RxUserDefaultsTests/RxUserDefaultsTests.swift
+++ b/Tests/RxUserDefaultsTests/RxUserDefaultsTests.swift
@@ -46,6 +46,46 @@ class RxUserDefaultsTests: XCTestCase {
XCTAssert(!setting.isSet)
}
+ func testCodableSetting() {
+ struct ExampleCodable: Codable, Equatable, RxSettingCompatible {
+ let aStringProperty: String
+ let anIntProperty: Int
+ }
+
+ let defaultValue = ExampleCodable(
+ aStringProperty: "bla",
+ anIntProperty: 0
+ )
+
+ let setting = settings.setting(
+ key: "codable_test",
+ defaultValue: defaultValue
+ )
+
+ // first test default value
+ XCTAssertEqual(setting.value, defaultValue)
+ // test that setting is not persisted
+ XCTAssert(!setting.isSet)
+
+ let anotherValue = ExampleCodable(
+ aStringProperty: "blubb",
+ anIntProperty: 1
+ )
+
+ // set the value
+ setting.value = anotherValue
+
+ // check if value is present
+ XCTAssertEqual(setting.value, anotherValue)
+ XCTAssert(setting.isSet)
+
+ // remove value
+ setting.remove()
+
+ // check that value was removed
+ XCTAssert(!setting.isSet)
+ }
+
func testIntSetting() {
let setting = settings.setting(key: "int_test", defaultValue: 42)