-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from num42/feature/the_great_refactor
Feature/the great refactor
- Loading branch information
Showing
6 changed files
with
142 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
11 changes: 11 additions & 0 deletions
11
Sources/RxUserDefaults/RxSettingCompatible/Codable+RxSettingsCompatible.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters