Skip to content

Commit

Permalink
Merge pull request #43 from futuredapp/housekeep/add-deprecations
Browse files Browse the repository at this point in the history
Remove StoredSubject and UserDefault wrapper
  • Loading branch information
mkj-is committed Sep 20, 2021
2 parents e1373f5 + affaf41 commit 5538726
Show file tree
Hide file tree
Showing 8 changed files with 10 additions and 463 deletions.
5 changes: 2 additions & 3 deletions FTPropertyWrappers.podspec
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
Pod::Spec.new do |s|
s.name = "FTPropertyWrappers"
s.version = "1.1.0"
s.version = "2.0.0"
s.summary = "Commonly used property wrappers"
s.description = <<-DESC
Property wrappers for common use-cases such as User Defaults, Serialization,
Keychain storage and Observing
Property wrappers for common use-cases such as Serialization and Keychain storage
DESC
s.homepage = "https://github.com/futuredapp/FTPropertyWrappers"
s.license = { :type => "MIT", :file => "LICENSE" }
Expand Down
83 changes: 8 additions & 75 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ When using Swift package manager install using Xcode 11+
or add following line to your dependencies:

```swift
.package(url: "https://github.com/futuredapp/FTPropertyWrappers.git", from: "1.0.0")
.package(url: "https://github.com/futuredapp/FTPropertyWrappers.git", from: "2.0.0")
```
When using CocoaPods add following line to your `Podfile`:

Expand All @@ -23,84 +23,11 @@ pod 'FTPropertyWrappers', '~> 1.0'

The main aim of this package is to provide programmer with access to commonly used features or snippets with as easy API as possible. Runtime efficiency, while important, is not the main focus of this package. As for today, this package contains wrappers for following features:

- `UserDefaults` for storing a value inside Foundation's User Defaults
- `StoredSubject` which is simple multidelegate/observing primitive for Swift
- `Serialized` is naive implementation of property living on a certain thread using Dispatch
- `GenericPassword` and `InternetPassword` are implementation of two classes storable in Keychain, with ability to inspect certain keychain item's attributes

## Usage

### `UserDefaults`

User Defaults property wrapper uses two main approaches for storing data in User Defaults. Primary approach is usage of Plist coders. If a data type can't be encoded using Plist coders, value is passed directly to User Default. When creating property wrapper, you have to provide key. All properties wrapped in this property wrapper have to be optional.

```swift
// Stored via User Default's method
@DefaultsStore(key: "key.for.number") var number: Int?

// Stored as `Data` encoded by Plist coder
struct Person: Codable {
let age: Int
let name: String
}
@DefaultsStore(key: "key.for.person") var person: Person?
```

Data are stored into `UserDefaults.standard` instance as a default. This behavior may be changed, if user provides custom `UserDefaults` instance with custom configuration. The same approach applies for Plist encoder/decoder.

User may provide `defaultValue` during initialization. This value is returned as the property wrapper's value, in case that decoding process failed and/or there is no such value in the store.

```swift
// Stored via User Default's method
@DefaultsStore(key: "key.for.number", defaultValue: 10) var number: Int?

print(number) // Prints: Optional(10)
number = 30
print(number) // Prints: Optional(30)
number = nil
print(number) // Prints: Optional(10)
```

### `StoredSubject`

Stored subject is a simple implementation of observer/listener pattern. Solves a problem,
where a simple delegate is not sufficient and you want to notify more objects. It is designated
for those projects where Combine is not available or other reactive programming frameworks
would be an over-kill.

Suppose we want to observe all the changes to logged in user we need to implement some
object (in this case service) which handles all the user-related logic. We need to expose
the observe method, since the property wrapper is private.

```swift
class UserService {
@StoredSubject var user: User = User()

func observeUser(subscription: @escaping (User) -> Void) -> Disposable {
_user.observe(subscription)
}
}
```

At the place where we want to listen to the changes of the user we need to hold the dispose bag.
In this dispose bag we put the subscription. The changes are received until the dispose bag is retained.

```swift
class LoginController {
let userService: UserService

private let bag = DisposeBag()

init(userService: UserService) {
self.userService = userService

userService.observeUser { user in
...
}.dispose(in: bag)
}
}
```

### `Serialized`

Searialized is a naive implementation of thread local property based on Dispatch (GCD). Special thread is created upon initialization and all read/write operations are performed on the thread. By default, read and write operations are blocking, therefore if you use read and write operation (like += on Int) two blocking operations are dispatched.
Expand Down Expand Up @@ -241,7 +168,13 @@ It appears, that in case of ambiguity, element with the oldest `creationDate` is

## Migration notes

### 0.2.0 ~> 1.0.0
### 2.0.0

`@UserDefaults` was removed in favor of [`@AppStorage`](https://developer.apple.com/documentation/swiftui/appstorage/) in iOS 14+.
`@StoredSubject` was removed in favor of [`CurrentValueSubject`](https://developer.apple.com/documentation/combine/currentvaluesubject) in iOS 13+.

### 1.0.0

During this migration process, code breaking changes were made only to Keychain property wrappers. Other changes were additive. In order to successfully migrate your Keychain related code, you have to take four steps.

1. Change all `KeychainStore`, `CodableKeychainAdapter` or `KeychainAdapter` variables to `@GenericPassword`.
Expand Down
100 changes: 0 additions & 100 deletions Sources/FTPropertyWrappers/StoredSubject.swift

This file was deleted.

71 changes: 0 additions & 71 deletions Sources/FTPropertyWrappers/UserDefault.swift

This file was deleted.

44 changes: 0 additions & 44 deletions Tests/FTPropertyWrappersTests/StoredSubjectsTests.swift

This file was deleted.

0 comments on commit 5538726

Please sign in to comment.