Skip to content

Commit

Permalink
Merge pull request #10 from fumito-ito/property-wapper
Browse files Browse the repository at this point in the history
support Property wappers
  • Loading branch information
fumito-ito authored Jan 6, 2022
2 parents d8dbaa0 + 20f4685 commit 391a958
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,31 @@ extension Data: RemoteConfigSerializable {
```
Also, take a look at our source code or tests to see more examples of bridges. If you find yourself confused with all these bridges, please create an issue and we will figure something out.

## Property Wrappers

SwiftyRemoteConfig provides property wrappers for Swift 5.1! The property wrapper, `@SwiftyRemoteConfig`, provides an option to use it with key path.

_Note: This propety wrappers only `read` support. You can set new value to the property, but any changes will be reflected to remote config value_

### usage

Given keys:

```swift
extension RemoteConfigKeys {
var userColorScheme: RemoteConfigKey<String> { .init("userColorScheme", defaultValue: "default") }
}
```

You can declare a Settings struct:

```swift
struct Settings {
@SwiftyRemoteConfig(keyPath: \.userColorScheme)
var userColorScheme: String
}
```

## KeyPath dynamicMemberLookup

SwiftyRemoteConfig makes KeyPath dynamicMemberLookpu usable in Swift 5.1.
Expand Down
33 changes: 33 additions & 0 deletions Sources/SwiftyRemoteConfig/PropertyWrappers.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// PropertyWrappers.swift
//
//
// Created by 伊藤史 on 2022/01/07.
//

#if swift(>=5.1)
@propertyWrapper
public final class SwiftyRemoteConfig<T: RemoteConfigSerializable> where T.T == T {

public let key: RemoteConfigKey<T>

public var wrappedValue: T {
get {
return RemoteConfigs[self.key]
}
}

public init<KeyStore>(
keyPath: KeyPath<KeyStore, RemoteConfigKey<T>>,
adapter: RemoteConfigAdapter<KeyStore>
) {
self.key = adapter.keyStore[keyPath: keyPath]
}

public init(
keyPath: KeyPath<RemoteConfigKeys, RemoteConfigKey<T>>
) {
self.key = RemoteConfigs.keyStore[keyPath: keyPath]
}
}
#endif
2 changes: 1 addition & 1 deletion SwiftyRemoteConfig.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |spec|
spec.name = "SwiftyRemoteConfig"
spec.version = "0.0.4"
spec.version = "0.0.5"
spec.summary = "Modern Swift API for FirebaseRemoteConfig"

spec.description = <<-DESC
Expand Down

0 comments on commit 391a958

Please sign in to comment.