Skip to content

Commit

Permalink
Merge pull request #16 from num42/feature/the_great_refactor
Browse files Browse the repository at this point in the history
Feature/the great refactor
  • Loading branch information
Lutzifer authored Apr 10, 2024
2 parents 1315876 + 0419478 commit 7d5fa34
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 56 deletions.
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
85 changes: 30 additions & 55 deletions README.md
Original file line number Diff line number Diff line change
@@ -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, [email protected]
- Hans-Martin Schuller, [email protected]
- Wolfgang Lutz, [email protected]

David Kraus, [email protected]

Hans-Martin Schuller, [email protected]

Wolfgang Lutz, [email protected]

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
23 changes: 23 additions & 0 deletions Sources/RxUserDefaults/PrivacyInfo.xcprivacy
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSPrivacyTracking</key>
<false/>
<key>NSPrivacyTrackingDomains</key>
<array/>
<key>NSPrivacyCollectedDataTypes</key>
<array/>
<key>NSPrivacyAccessedAPITypes</key>
<array>
<dict>
<key>NSPrivacyAccessedAPIType</key>
<string>NSPrivacyAccessedAPICategoryUserDefaults</string>
<key>NSPrivacyAccessedAPITypeReasons</key>
<array>
<string>C56D.1</string>
</array>
</dict>
</array>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -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)
}
}
36 changes: 36 additions & 0 deletions Sources/RxUserDefaults/SettingTypeProtocol.swift
Original file line number Diff line number Diff line change
@@ -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<SettingType> { get set }

func asObservable() -> Observable<SettingType>
var isSet: Bool { get }
func remove()
var value: SettingType { get }
func setValue(value: SettingType)
}

public extension SettingTypeProtocol {
func asObservable() -> Observable<SettingType> {
setting.asObservable()
}

var isSet: Bool {
setting.isSet
}

func remove() {
setting.remove()
}

var value: SettingType {
setting.value
}

func setValue(value: SettingType) {
setting.value = value
}
}
40 changes: 40 additions & 0 deletions Tests/RxUserDefaultsTests/RxUserDefaultsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 7d5fa34

Please sign in to comment.