Skip to content

Commit 45db46f

Browse files
committed
docs: README.md update
1 parent 0b77167 commit 45db46f

File tree

1 file changed

+101
-7
lines changed

1 file changed

+101
-7
lines changed

README.md

Lines changed: 101 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,17 @@ Swift Declarative Configuration (SDC, for short) is a tiny library, that enables
2626

2727
Functional builder for anything, enables you to modify object instances in a declarative way. Also contains `BuilderProvider` protocol with a computed `builder` property and implements that protocol on `NSObject` type.
2828

29+
- **[FunctionalClosures](./Sources/FunctionalClosures)**
30+
31+
Functional closures allow you to setup functional handlers & datasources, the API may seem a bit strange at the first look, so feel free to ask or discuss anything [here](https://github.com/MakeupStudio/swift-declarative-configuration/issues/1).
32+
2933
- **[DeclarativeConfiguration](./Sources/DeclarativeConfiguration)**
3034

3135
Wraps and exports all the products.
3236

3337
## Basic Usage
3438

35-
### UIKit & No SDC
39+
### No SDC
3640

3741
```swift
3842
class ImageViewController: UIViewController {
@@ -52,7 +56,7 @@ class ImageViewController: UIViewController {
5256
}
5357
```
5458

55-
### UIKit & FunctionalConfigurator
59+
### FunctionalConfigurator
5660

5761
```swift
5862
import FunctionalConfigurator
@@ -75,7 +79,7 @@ class ImageViewController: UIViewController {
7579

7680
**Note:** This way is **recommended**, but remember, that custom types **MUST** implement initializer with no parameters even if the superclass already has it or you will get a crash otherwise.
7781

78-
### UIKit & FunctionalBuilder
82+
### FunctionalBuilder
7983

8084
```swift
8185
import FunctionalBuilder
@@ -96,7 +100,97 @@ class ImageViewController: UIViewController {
96100

97101
Note: This way is recommended too, and it is more **safe**, because it modifies existing objects.
98102

99-
### Other usecases
103+
### FunctionalHandler
104+
105+
### No SDC
106+
107+
**Declaration**
108+
109+
```swift
110+
public class TapGestureRecognizer: UITapGestureRecognizer {
111+
var onTapGesture: ((TapGestureRecognizer) -> Void)?
112+
113+
init() {
114+
super.init(target: nil, action: nil)
115+
commonInit()
116+
}
117+
118+
override public init(target: Any?, action: Selector?) {
119+
super.init(target: target, action: action)
120+
commonInit()
121+
}
122+
123+
private func commonInit() {
124+
self.addTarget(self, action: #selector(handleTap))
125+
}
126+
127+
@objc private func handleTap(_ recognizer: TapGestureRecognizer) {
128+
onTapGesture?(recognizer)
129+
}
130+
}
131+
```
132+
133+
**Usage**
134+
135+
```swift
136+
let tapRecognizer = TapGestureRecognizer()
137+
138+
// handler setup
139+
tapRecognizer.onTapGesture = { recognizer in
140+
// ...
141+
}
142+
143+
// call from the outside
144+
tapRecognizer.onTapGesture?(tapRecognizer)
145+
```
146+
147+
### With SDC
148+
149+
**Declaration**
150+
151+
```swift
152+
public class TapGestureRecognizer: UITapGestureRecognizer {
153+
@FunctionalHandler<TapGestureRecognizer>
154+
var onTapGesture
155+
156+
init() {
157+
super.init(target: nil, action: nil)
158+
commonInit()
159+
}
160+
161+
override public init(target: Any?, action: Selector?) {
162+
super.init(target: target, action: action)
163+
commonInit()
164+
}
165+
166+
private func commonInit() {
167+
self.addTarget(self, action: #selector(handleTap))
168+
}
169+
170+
@objc private func handleTap(_ recognizer: TapGestureRecognizer) {
171+
_onTapGesture(recognizer)
172+
}
173+
}
174+
```
175+
176+
**Usage**
177+
178+
```swift
179+
let tapRecognizer = TapGestureRecognizer()
180+
181+
// handler setup now called as function
182+
tapRecognizer.onTapGesture { recognizer in
183+
// ...
184+
}
185+
186+
// call from the outside now uses propertyWrapper projectedValue API, which is not as straitforward
187+
// and it is nice, because:
188+
// - handlers usually should not be called from the outside
189+
// - you do not lose the ability to call it, but an API tells you that it's kinda private
190+
tapRecognizer.$onTapGesture?(tapRecognizer)
191+
```
192+
193+
### More
100194

101195
#### Builder
102196

@@ -129,11 +223,11 @@ extension CLLocationCoordinate2D: BuilderProvider {}
129223

130224
#### Configurator
131225

132-
> README PLACEHOLDER (Not yet written 😅)
226+
> **Note:** Your NSObject classes **must** implement `init()` to use Configurators. It's a little trade-off for the convenience it brings to your codebase, see [tests](./Tests/DeclarativeConfigurationTests/ConfiguratorTests.swift) for an example.
133227
134-
#### FunctionalHandler
228+
#### FunctionalDataSource
135229

136-
> README PLACEHOLDER (Not yet written 😅)
230+
`FunctionalDataSource` type is very similar to the `FunctionalHandler`, but if `FunctionalHandler<Input>` is kinda `FunctionalDataSource<Input, Void>`, the second one may have different types of an output. Usage is similar, different types are provided just for better semantics.
137231

138232
## Installation
139233

0 commit comments

Comments
 (0)