Merge pull request #287 from TouchInstinct/feature/AsyncOperation
feat: add AsyncOperation - generic subclass of Operation with chaining and result observation support
This commit is contained in:
commit
05c3273fd8
|
|
@ -1,5 +1,8 @@
|
|||
# Changelog
|
||||
|
||||
### 1.8.0
|
||||
- **Add**: `TIFoundationUtils.AsyncOperation` - generic subclass of Operation with chaining and result observation support
|
||||
|
||||
### 1.7.0
|
||||
- **Add**: `TINetworking` - Swagger-frendly networking layer helpers
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
Pod::Spec.new do |s|
|
||||
s.name = "LeadKit"
|
||||
s.version = "1.7.0"
|
||||
s.version = "1.8.0"
|
||||
s.summary = "iOS framework with a bunch of tools for rapid development"
|
||||
s.homepage = "https://github.com/TouchInstinct/LeadKit"
|
||||
s.license = "Apache License, Version 2.0"
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ let package = Package(
|
|||
|
||||
// MARK: - Utils
|
||||
.target(name: "TISwiftUtils", path: "TISwiftUtils/Sources"),
|
||||
.target(name: "TIFoundationUtils", dependencies: ["TISwiftUtils"], path: "TIFoundationUtils/Sources"),
|
||||
.target(name: "TIFoundationUtils", dependencies: ["TISwiftUtils"], path: "TIFoundationUtils"),
|
||||
.target(name: "TIKeychainUtils", dependencies: ["TIFoundationUtils", "KeychainAccess"], path: "TIKeychainUtils/Sources"),
|
||||
.target(name: "TITableKitUtils", dependencies: ["TIUIElements", "TableKit"], path: "TITableKitUtils/Sources"),
|
||||
.target(name: "TINetworking", dependencies: ["TISwiftUtils", "Alamofire"], path: "TINetworking/Sources"),
|
||||
|
|
|
|||
|
|
@ -0,0 +1,30 @@
|
|||
import Foundation
|
||||
import TIFoundationUtils
|
||||
|
||||
let operationQueue = OperationQueue()
|
||||
operationQueue.maxConcurrentOperationCount = 1
|
||||
|
||||
ClosureAsyncOperation<Int, Error> { completion in
|
||||
DispatchQueue.global().asyncAfter(deadline: .now() + .seconds(1)) {
|
||||
completion(.success(1))
|
||||
}
|
||||
}
|
||||
.map { $0 * 2 }
|
||||
.observe(onSuccess: { result in
|
||||
debugPrint("Async operation one has finished with \(result)")
|
||||
})
|
||||
.add(to: operationQueue)
|
||||
|
||||
ClosureAsyncOperation<Int, Error> { completion in
|
||||
DispatchQueue.global().asyncAfter(deadline: .now() + .seconds(1)) {
|
||||
completion(.success(2))
|
||||
}
|
||||
}
|
||||
.map { $0 * 2 }
|
||||
.observe(onSuccess: { result in
|
||||
debugPrint("Async operation two has finished with \(result)")
|
||||
})
|
||||
.add(to: operationQueue)
|
||||
|
||||
// "Async operation one has finished with 2"
|
||||
// "Async operation two has finished with 4"
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
|
||||
<timeline fileName='timeline.xctimeline'/>
|
||||
</playground>
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
# AsyncOperation
|
||||
|
||||
Generic subclass of Operation with chaining and result observation support.
|
||||
|
||||
## Examples
|
||||
|
||||
### Serial queue of async operations
|
||||
|
||||
Creating serial queue of async operations
|
||||
|
||||
```swift
|
||||
import Foundation
|
||||
import TIFoundationUtils
|
||||
|
||||
let operationQueue = OperationQueue()
|
||||
operationQueue.maxConcurrentOperationCount = 1
|
||||
|
||||
ClosureAsyncOperation<Int, Error> { completion in
|
||||
DispatchQueue.global().asyncAfter(deadline: .now() + .seconds(1)) {
|
||||
completion(.success(1))
|
||||
}
|
||||
}
|
||||
.map { $0 * 2 }
|
||||
.observe(onSuccess: { result in
|
||||
debugPrint("Async operation one has finished with \(result)")
|
||||
})
|
||||
.add(to: operationQueue)
|
||||
|
||||
ClosureAsyncOperation<Int, Error> { completion in
|
||||
DispatchQueue.global().asyncAfter(deadline: .now() + .seconds(1)) {
|
||||
completion(.success(2))
|
||||
}
|
||||
}
|
||||
.map { $0 * 2 }
|
||||
.observe(onSuccess: { result in
|
||||
debugPrint("Async operation two has finished with \(result)")
|
||||
})
|
||||
.add(to: operationQueue)
|
||||
|
||||
// "Async operation one has finished with 2"
|
||||
// "Async operation two has finished with 4"
|
||||
```
|
||||
|
|
@ -0,0 +1,67 @@
|
|||
//
|
||||
// Copyright (c) 2022 Touch Instinct
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the Software), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
private final class MapAsyncOperation<Output, Failure: Error>: AsyncOperation<Output, Failure> {
|
||||
private var dependencyObservation: NSKeyValueObservation?
|
||||
|
||||
public init<DependencyOutput, DependencyFailure: Error>(dependency: AsyncOperation<DependencyOutput, DependencyFailure>,
|
||||
mapOutput: @escaping (DependencyOutput) -> Result<Output, Failure>,
|
||||
mapFailure: @escaping (DependencyFailure) -> Failure) {
|
||||
|
||||
super.init()
|
||||
|
||||
dependencyObservation = dependency.subscribe { [weak self] in
|
||||
switch mapOutput($0) {
|
||||
case let .success(result):
|
||||
self?.handle(result: result)
|
||||
|
||||
case let .failure(error):
|
||||
self?.handle(error: error)
|
||||
}
|
||||
} onFailure: { [weak self] in
|
||||
self?.handle(error: mapFailure($0))
|
||||
}
|
||||
|
||||
addDependency(dependency) // keeps strong reference to dependency as well
|
||||
|
||||
state = .isReady
|
||||
}
|
||||
}
|
||||
|
||||
public extension AsyncOperation {
|
||||
func map<NewOutput, NewFailure: Error>(mapOutput: @escaping (Output) -> Result<NewOutput, NewFailure>,
|
||||
mapFailure: @escaping (Failure) -> NewFailure)
|
||||
-> AsyncOperation<NewOutput, NewFailure> {
|
||||
|
||||
MapAsyncOperation(dependency: self,
|
||||
mapOutput: mapOutput,
|
||||
mapFailure: mapFailure)
|
||||
}
|
||||
|
||||
func map<NewOutput>(mapOutput: @escaping (Output) -> NewOutput) -> AsyncOperation<NewOutput, Failure> {
|
||||
MapAsyncOperation(dependency: self,
|
||||
mapOutput: { .success(mapOutput($0)) },
|
||||
mapFailure: { $0 })
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,54 @@
|
|||
//
|
||||
// Copyright (c) 2022 Touch Instinct
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the Software), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
private final class ClosureObserverOperation<Output, Failure: Error>: AsyncOperation<Output, Failure> {
|
||||
private var dependencyObservation: NSKeyValueObservation?
|
||||
|
||||
public init(dependency: AsyncOperation<Output, Failure>,
|
||||
onSuccess: ((Output) -> Void)? = nil,
|
||||
onFailure: ((Failure) -> Void)? = nil) {
|
||||
|
||||
super.init()
|
||||
|
||||
dependencyObservation = dependency.subscribe { [weak self] in
|
||||
onSuccess?($0)
|
||||
self?.handle(result: $0)
|
||||
} onFailure: { [weak self] in
|
||||
onFailure?($0)
|
||||
self?.handle(error: $0)
|
||||
}
|
||||
|
||||
addDependency(dependency) // keeps strong reference to dependency as well
|
||||
|
||||
state = .isReady
|
||||
}
|
||||
}
|
||||
|
||||
public extension AsyncOperation {
|
||||
func observe(onSuccess: ((Output) -> Void)? = nil,
|
||||
onFailure: ((Failure) -> Void)? = nil) -> AsyncOperation<Output, Failure> {
|
||||
|
||||
ClosureObserverOperation(dependency: self, onSuccess: onSuccess, onFailure: onFailure)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,116 @@
|
|||
//
|
||||
// Copyright (c) 2022 Touch Instinct
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the Software), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
open class AsyncOperation<Output, Failure: Error>: Operation {
|
||||
|
||||
open var result: Result<Output, Failure>?
|
||||
|
||||
var state: State? = nil { // isReady == false
|
||||
// KVO support
|
||||
willSet {
|
||||
willChangeValue(forKey: newValue.orReady.rawValue)
|
||||
willChangeValue(forKey: state.orReady.rawValue)
|
||||
}
|
||||
|
||||
didSet {
|
||||
didChangeValue(forKey: state.orReady.rawValue)
|
||||
didChangeValue(forKey: oldValue.orReady.rawValue)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Operation override
|
||||
|
||||
open override var isCancelled: Bool {
|
||||
state == .isCancelled
|
||||
}
|
||||
|
||||
open override var isExecuting: Bool {
|
||||
state == .isExecuting
|
||||
}
|
||||
|
||||
open override var isFinished: Bool {
|
||||
state == .isFinished
|
||||
}
|
||||
|
||||
open override var isReady: Bool {
|
||||
state == .isReady
|
||||
}
|
||||
|
||||
open override var isAsynchronous: Bool {
|
||||
true
|
||||
}
|
||||
|
||||
open override func start() {
|
||||
state = .isExecuting
|
||||
|
||||
if result != nil {
|
||||
state = .isFinished
|
||||
}
|
||||
}
|
||||
|
||||
open override func cancel() {
|
||||
state = .isCancelled
|
||||
}
|
||||
|
||||
// MARK: - Methods for subclass override
|
||||
|
||||
open func handle(result: Output) {
|
||||
self.result = .success(result)
|
||||
|
||||
state = .isFinished
|
||||
}
|
||||
|
||||
open func handle(error: Failure) {
|
||||
self.result = .failure(error)
|
||||
|
||||
state = .isFinished
|
||||
}
|
||||
|
||||
// MARK: - Completion observation
|
||||
|
||||
public func subscribe(onSuccess: ((Output) -> Void)? = nil,
|
||||
onFailure: ((Failure) -> Void)? = nil) -> NSKeyValueObservation {
|
||||
|
||||
observe(\.isFinished, options: [.new]) { object, change in
|
||||
if let isFinished = change.newValue, isFinished {
|
||||
switch object.result {
|
||||
case let .success(result)?:
|
||||
onSuccess?(result)
|
||||
|
||||
case let .failure(failure)?:
|
||||
onFailure?(failure)
|
||||
|
||||
default:
|
||||
assertionFailure("Got nil result from operation when isFinished was true!")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private extension Optional where Wrapped == Operation.State {
|
||||
var orReady: Wrapped {
|
||||
self ?? .isReady
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
//
|
||||
// Copyright (c) 2022 Touch Instinct
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the Software), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
//
|
||||
|
||||
public final class ClosureAsyncOperation<Output, Failure: Error>: AsyncOperation<Output, Failure> {
|
||||
public typealias TaskClosure = (@escaping (Result<Output, Failure>) -> Void) -> Void
|
||||
|
||||
private let taskClosure: TaskClosure
|
||||
|
||||
public init(taskClosure: @escaping TaskClosure) {
|
||||
self.taskClosure = taskClosure
|
||||
|
||||
super.init()
|
||||
|
||||
self.state = .isReady
|
||||
}
|
||||
|
||||
public override func start() {
|
||||
super.start()
|
||||
|
||||
taskClosure { [weak self] in
|
||||
switch $0 {
|
||||
case let .success(result):
|
||||
self?.handle(result: result)
|
||||
|
||||
case let .failure(error):
|
||||
self?.handle(error: error)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
//
|
||||
// Copyright (c) 2022 Touch Instinct
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the Software), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
public extension Operation {
|
||||
var flattenDependencies: [Operation] {
|
||||
dependencies.flatMap { $0.dependencies } + dependencies
|
||||
}
|
||||
|
||||
func add(to operationQueue: OperationQueue, waitUntilFinished: Bool = false) {
|
||||
operationQueue.addOperations(flattenDependencies + [self], waitUntilFinished: waitUntilFinished)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
//
|
||||
// Copyright (c) 2022 Touch Instinct
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the Software), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
public extension Operation {
|
||||
enum State: String {
|
||||
case isCancelled
|
||||
case isExecuting
|
||||
case isFinished
|
||||
case isReady
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
import Foundation
|
||||
import TIFoundationUtils
|
||||
|
||||
struct ProfileInfo: Codable {
|
||||
let userName: String
|
||||
}
|
||||
|
||||
extension StorageKey {
|
||||
static var profileKey: StorageKey<ProfileInfo> {
|
||||
.init(rawValue: "profileKey")
|
||||
}
|
||||
|
||||
static var onboardingFinishedKey: StorageKey<Bool> {
|
||||
.init(rawValue: "onboardingFinishedKey")
|
||||
}
|
||||
}
|
||||
|
||||
var defaults = UserDefaults.standard
|
||||
defaults[.profileKey] = ProfileInfo(userName: "John Appleseed")
|
||||
//defaults[.profileKey] = "Agent Smith" // this will threat compile error:
|
||||
// Cannot assign value of type 'String' to subscript of type 'ProfileInfo'
|
||||
|
||||
final class ViewModel {
|
||||
@UserDefaultsCodableBackingStore(key: .profileKey, codableKeyValueStorage: .standard)
|
||||
var profile: ProfileInfo?
|
||||
|
||||
// This will threat compile error:
|
||||
// Cannot convert value of type 'BackingStore<UserDefaults, Bool?>' to specified type 'ProfileInfo?'
|
||||
// @UserDefaultsCodableBackingStore(key: .onboardingFinishedKey, codableKeyValueStorage: .standard)
|
||||
// var wrongKeyProfile: ProfileInfo?
|
||||
|
||||
// For primitive types we can't use default json decoder/encoder
|
||||
@UserDefaultsCodableBackingStore(key: .onboardingFinishedKey,
|
||||
codableKeyValueStorage: .standard,
|
||||
decoder: UnarchiverKeyValueDecoder(),
|
||||
encoder: ArchiverKeyValueEncoder())
|
||||
var onboardingFinished = false
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
|
||||
<timeline fileName='timeline.xctimeline'/>
|
||||
</playground>
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
# CodableKeyValueStorage
|
||||
|
||||
Storage that can get and set codable objects by the key.
|
||||
|
||||
Implementations: `UserDefaults`
|
||||
|
||||
## Example
|
||||
|
||||
```swift
|
||||
struct ProfileInfo: Codable {
|
||||
let userName: String
|
||||
}
|
||||
```
|
||||
|
||||
Keys:
|
||||
|
||||
```swift
|
||||
extension StorageKey {
|
||||
static var profileKey: StorageKey<ProfileInfo> {
|
||||
.init(rawValue: "profileKey")
|
||||
}
|
||||
|
||||
static var onboardingFinishedKey: StorageKey<Bool> {
|
||||
.init(rawValue: "onboardingFinishedKey")
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Subscript example
|
||||
|
||||
```swift
|
||||
var defaults = UserDefaults.standard
|
||||
defaults[.profileKey] = ProfileInfo(userName: "John Appleseed")
|
||||
defaults[.profileKey] = "Agent Smith" // this will threat compile error:
|
||||
// Cannot assign value of type 'String' to subscript of type 'ProfileInfo'
|
||||
```
|
||||
### @propertyWrapper example
|
||||
|
||||
```swift
|
||||
final class ViewModel {
|
||||
@UserDefaultsCodableBackingStore(key: .profileKey, codableKeyValueStorage: .standard)
|
||||
var profile: ProfileInfo?
|
||||
|
||||
// This will threat compile error:
|
||||
// Cannot convert value of type 'BackingStore<UserDefaults, Bool?>' to specified type 'ProfileInfo?'
|
||||
@UserDefaultsCodableBackingStore(key: .onboardingFinishedKey, codableKeyValueStorage: .standard)
|
||||
var wrongKeyProfile: ProfileInfo?
|
||||
|
||||
// For primitive types we can't use default json decoder/encoder
|
||||
@UserDefaultsCodableBackingStore(key: .onboardingFinishedKey,
|
||||
codableKeyValueStorage: .standard,
|
||||
decoder: UnarchiverKeyValueDecoder(),
|
||||
encoder: ArchiverKeyValueEncoder())
|
||||
var onboardingFinished = false
|
||||
}
|
||||
```
|
||||
|
|
@ -3,61 +3,6 @@
|
|||
Set of helpers for Foundation framework classes.
|
||||
|
||||
* [TITimer](Sources/TITimer) - pretty timer
|
||||
* [CodableKeyValueStorage](#codablekeyvaluestorage)
|
||||
|
||||
## CodableKeyValueStorage
|
||||
|
||||
Storage that can get and set codable objects by the key.
|
||||
|
||||
Implementations: `UserDefaults`
|
||||
|
||||
### Example
|
||||
|
||||
```swift
|
||||
struct ProfileInfo: Codable {
|
||||
let userName: String
|
||||
}
|
||||
```
|
||||
|
||||
Keys:
|
||||
|
||||
```swift
|
||||
extension StorageKey {
|
||||
static var profileKey: StorageKey<ProfileInfo> {
|
||||
.init(rawValue: "profileKey")
|
||||
}
|
||||
|
||||
static var onboardingFinishedKey: StorageKey<Bool> {
|
||||
.init(rawValue: "onboardingFinishedKey")
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### Subscript example
|
||||
|
||||
```swift
|
||||
var defaults = UserDefaults.standard
|
||||
defaults[.profileKey] = ProfileInfo(userName: "John Appleseed")
|
||||
defaults[.profileKey] = "Agent Smith" // this will threat compile error:
|
||||
// Cannot assign value of type 'String' to subscript of type 'ProfileInfo'
|
||||
```
|
||||
#### @propertyWrapper example
|
||||
|
||||
```swift
|
||||
final class ViewModel {
|
||||
@UserDefaultsCodableBackingStore(key: .profileKey, codableKeyValueStorage: .standard)
|
||||
var profile: ProfileInfo?
|
||||
|
||||
// This will threat compile error:
|
||||
// Cannot convert value of type 'BackingStore<UserDefaults, Bool?>' to specified type 'ProfileInfo?'
|
||||
@UserDefaultsCodableBackingStore(key: .onboardingFinishedKey, codableKeyValueStorage: .standard)
|
||||
var wrongKeyProfile: ProfileInfo?
|
||||
|
||||
// For primitive types we can't use default json decoder/encoder
|
||||
@UserDefaultsCodableBackingStore(key: .onboardingFinishedKey,
|
||||
codableKeyValueStorage: .standard,
|
||||
decoder: UnarchiverKeyValueDecoder(),
|
||||
encoder: ArchiverKeyValueEncoder())
|
||||
var onboardingFinished = false
|
||||
}
|
||||
```
|
||||
* [AsyncOperation](AsyncOperation) - generic subclass of Operation with chaining and result observation support
|
||||
* [CodableKeyValueStorage](CodableKeyValueStorage) - key-value storage for Codable types + UserDefaults implementation
|
||||
* [UserDefaultsBackingStore](UserDefaultsBackingStore) - BackingStore property wrapper for UserDefaults
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
Pod::Spec.new do |s|
|
||||
s.name = 'TIFoundationUtils'
|
||||
s.version = '1.7.0'
|
||||
s.version = '1.8.0'
|
||||
s.summary = 'Set of helpers for Foundation framework classes.'
|
||||
s.homepage = 'https://github.com/TouchInstinct/LeadKit/tree/' + s.version.to_s + '/' + s.name
|
||||
s.license = { :type => 'MIT', :file => 'LICENSE' }
|
||||
|
|
|
|||
|
|
@ -0,0 +1,16 @@
|
|||
import Foundation
|
||||
import TIFoundationUtils
|
||||
|
||||
extension StorageKey {
|
||||
static var onboardingFinishedKey: StorageKey<Bool> {
|
||||
.init(rawValue: "onboardingFinishedKey")
|
||||
}
|
||||
}
|
||||
|
||||
final class ViewModel {
|
||||
@UserDefaultsBackingStore(key: .onboardingFinishedKey,
|
||||
userDefaultsStorage: .standard,
|
||||
getClosure: { $0.bool(forKey: $1) },
|
||||
setClosure: { $0.set($1, forKey: $2) })
|
||||
var hasFinishedOnboarding = false // default value if nothing was stored in defaults
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
|
||||
<timeline fileName='timeline.xctimeline'/>
|
||||
</playground>
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
# UserDefaultsBackingStore
|
||||
|
||||
BackingStore property wrapper for UserDefaults
|
||||
|
||||
## Usage example
|
||||
|
||||
```swift
|
||||
extension StorageKey {
|
||||
static var onboardingFinishedKey: StorageKey<Bool> {
|
||||
.init(rawValue: "onboardingFinishedKey")
|
||||
}
|
||||
}
|
||||
|
||||
final class ViewModel {
|
||||
@UserDefaultsBackingStore(key: .onboardingFinishedKey,
|
||||
userDefaultsStorage: .standard,
|
||||
getClosure: { $0.bool(forKey: $1) },
|
||||
setClosure: { $0.set($1, forKey: $2) })
|
||||
var hasFinishedOnboarding = false // default value if nothing was stored in defaults
|
||||
}
|
||||
```
|
||||
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
Pod::Spec.new do |s|
|
||||
s.name = 'TIKeychainUtils'
|
||||
s.version = '1.7.0'
|
||||
s.version = '1.8.0'
|
||||
s.summary = 'Set of helpers for Keychain classes.'
|
||||
s.homepage = 'https://github.com/TouchInstinct/LeadKit/tree/' + s.version.to_s + '/' + s.name
|
||||
s.license = { :type => 'MIT', :file => 'LICENSE' }
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
Pod::Spec.new do |s|
|
||||
s.name = 'TINetworking'
|
||||
s.version = '1.7.0'
|
||||
s.version = '1.8.0'
|
||||
s.summary = 'Swagger-frendly networking layer helpers.'
|
||||
s.homepage = 'https://github.com/TouchInstinct/LeadKit/tree/' + s.version.to_s + '/' + s.name
|
||||
s.license = { :type => 'MIT', :file => 'LICENSE' }
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
Pod::Spec.new do |s|
|
||||
s.name = 'TISwiftUtils'
|
||||
s.version = '1.7.0'
|
||||
s.version = '1.8.0'
|
||||
s.summary = 'Bunch of useful helpers for Swift development.'
|
||||
s.homepage = 'https://github.com/TouchInstinct/LeadKit/tree/' + s.version.to_s + '/' + s.name
|
||||
s.license = { :type => 'MIT', :file => 'LICENSE' }
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
Pod::Spec.new do |s|
|
||||
s.name = 'TITableKitUtils'
|
||||
s.version = '1.7.0'
|
||||
s.version = '1.8.0'
|
||||
s.summary = 'Set of helpers for TableKit classes.'
|
||||
s.homepage = 'https://github.com/TouchInstinct/LeadKit/tree/' + s.version.to_s + '/' + s.name
|
||||
s.license = { :type => 'MIT', :file => 'LICENSE' }
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
Pod::Spec.new do |s|
|
||||
s.name = 'TITransitions'
|
||||
s.version = '1.7.0'
|
||||
s.version = '1.8.0'
|
||||
s.summary = 'Set of custom transitions to present controller. '
|
||||
s.homepage = 'https://github.com/TouchInstinct/LeadKit/tree/' + s.version.to_s + '/' + s.name
|
||||
s.license = { :type => 'MIT', :file => 'LICENSE' }
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
Pod::Spec.new do |s|
|
||||
s.name = 'TIUIElements'
|
||||
s.version = '1.7.0'
|
||||
s.version = '1.8.0'
|
||||
s.summary = 'Bunch of useful protocols and views.'
|
||||
s.homepage = 'https://github.com/TouchInstinct/LeadKit/tree/' + s.version.to_s + '/' + s.name
|
||||
s.license = { :type => 'MIT', :file => 'LICENSE' }
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
Pod::Spec.new do |s|
|
||||
s.name = 'TIUIKitCore'
|
||||
s.version = '1.7.0'
|
||||
s.version = '1.8.0'
|
||||
s.summary = 'Core UI elements: protocols, views and helpers.'
|
||||
s.homepage = 'https://github.com/TouchInstinct/LeadKit/tree/' + s.version.to_s + '/' + s.name
|
||||
s.license = { :type => 'MIT', :file => 'LICENSE' }
|
||||
|
|
|
|||
Loading…
Reference in New Issue