|
| 1 | +# Swift Declarative Configuration |
| 2 | + |
| 3 | +[](https://swift.org/download/) [](https://swift.org/package-manager/) [](https://twitter.com/maximkrouk) |
| 4 | + |
| 5 | +Swift Declarative Configuration (SDC, for short) is a tiny library, that enables you to configure your objects in a declarative, consistent and understandable way, with ergonomics in mind. It can be used to configure any objects on any platform, including server-side-swift. |
| 6 | + |
| 7 | +## Products |
| 8 | + |
| 9 | +- **[FunctionalModification](./Sources/FunctionalModification)** |
| 10 | + |
| 11 | + Provides modification functions for copying and modifying immutable stuff. It is useful for self-configuring objects like builder, when modificating methods should return modified `self` |
| 12 | + |
| 13 | +- **[FunctionalKeyPath](./Sources/FunctionalKeyPath)** & **[CasePaths](https://github.com/pointfreeco/swift-case-paths)** |
| 14 | + |
| 15 | + KeyPath functional wrappers, one is generalized and the other is for enums. [CasePath is a dependency](https://github.com/pointfreeco/swift-case-paths). |
| 16 | + |
| 17 | +- **[FunctionalConfigurator](./Sources/FunctionalConfigurator)** |
| 18 | + |
| 19 | + Funtional configurator for anything, enables you to specify modification of an object and to apply the modification later. |
| 20 | + |
| 21 | +- **[FunctionalBuilder](./Sources/FunctionalBuilder)** |
| 22 | + |
| 23 | + 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. |
| 24 | + |
| 25 | +- **[DeclarativeConfiguration](./Sources/DeclarativeConfiguration)** |
| 26 | + |
| 27 | + Wraps and exports all the products. |
| 28 | + |
| 29 | +## Basic Usage |
| 30 | + |
| 31 | +### UIKit & FunctionalConfigurator |
| 32 | + |
| 33 | +Maybe it worth to make another abstraction over configurator for UI setup, but for example I'll be using pure version. |
| 34 | + |
| 35 | +```swift |
| 36 | +import FunctionalConfigurator |
| 37 | + |
| 38 | +class ImageViewController: UIViewController { |
| 39 | + enum StyleSheet { |
| 40 | + static let imageView = Configurator<UIImageView> |
| 41 | + .contentMode(.scaleAspectFit) |
| 42 | + .backgroundColor(.black) |
| 43 | + .layer.masksToBounds(true) |
| 44 | + .layer.cornerRadius(10) |
| 45 | + } |
| 46 | + |
| 47 | + let imageView: UIImageView = .init() |
| 48 | + |
| 49 | + override func loadView() { |
| 50 | + self.view = imageView |
| 51 | + } |
| 52 | + |
| 53 | + override func viewDidLoad() { |
| 54 | + super.viewDidLoad() |
| 55 | + StyleSheet.imageView.configure(imageView) |
| 56 | + } |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +### UIKit & FunctionalBuilder |
| 61 | +```swift |
| 62 | +import FunctionalBuilder |
| 63 | + |
| 64 | +class ImageViewController: UIViewController { |
| 65 | + let imageView = UIImageView().builder |
| 66 | + .contentMode(.scaleAspectFit) |
| 67 | + .backgroundColor(.black) |
| 68 | + .layer.masksToBounds(true) |
| 69 | + .layer.cornerRadius(10) |
| 70 | + .build() |
| 71 | + |
| 72 | + override func loadView() { |
| 73 | + self.view = imageView |
| 74 | + } |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +### Modification |
| 79 | + |
| 80 | +```swift |
| 81 | +import FunctionalModification |
| 82 | + |
| 83 | +struct MyModel { |
| 84 | + var value1 = 0 |
| 85 | + init() {} |
| 86 | +} |
| 87 | + |
| 88 | +let model_0 = MyModel() |
| 89 | +let model_1 = modification(of: model_0) { $0.value = 1 } |
| 90 | + |
| 91 | +import UIKit |
| 92 | + |
| 93 | +extension UIView { |
| 94 | + @discardableResult |
| 95 | + func cornerRadius(_ value: CGFloat) -> Self { |
| 96 | + modification(of: self) { view in |
| 97 | + view.layer.cornerRadius = value |
| 98 | + view.layer.masksToBounds = true |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +## Installation |
| 105 | + |
| 106 | +### Basic |
| 107 | + |
| 108 | +You can add DeclarativeConfiguration to an Xcode project by adding it as a package dependency. |
| 109 | + |
| 110 | +1. From the **File** menu, select **Swift Packages › Add Package Dependency…** |
| 111 | +2. Enter "https://github.com/makeupstudio/swift-declarative-configuration" into the package repository URL text field |
| 112 | +3. Choose products you need to link them to your project. |
| 113 | + |
| 114 | +### Recommended |
| 115 | + |
| 116 | +If you use SwiftPM for your project, you can add DeclarativeConfiguration to your package file. Also my advice will be to use SSH. |
| 117 | + |
| 118 | +```swift |
| 119 | +.package( |
| 120 | + url: "[email protected]:makeupstudio/swift-declarative-configuration.git", |
| 121 | + from: "0.0.1" |
| 122 | +) |
| 123 | +``` |
| 124 | + |
| 125 | +Do not forget about target dependencies: |
| 126 | + |
| 127 | +```swift |
| 128 | +.product( |
| 129 | + name: "DeclarativeConfiguration", |
| 130 | + package: "swift-declarative-configuration" |
| 131 | +) |
| 132 | +``` |
| 133 | + |
| 134 | +## License |
| 135 | + |
| 136 | +This library is released under the MIT license. See [LICENSE](./LICENSE) for details. |
0 commit comments