|
| 1 | + |
| 2 | + |
| 3 | +<br/> |
| 4 | + |
| 5 | +[](#Required) |
| 6 | +[](#Development%20Progress) |
| 7 | +[](#Development%20Progress) |
| 8 | + |
| 9 | +# RxSwiftUIKit |
| 10 | + |
| 11 | +**RxSwiftUIKit** is an extension of [SwiftUIKit](https://github.com/sonla58/SwiftUIKit). Combine with [RxSwift](https://github.com/ReactiveX/RxSwift) and [Carbon](https://github.com/ra1028/Carbon), tt gives us the ability to build the tableView or collectionView using the declarative way. |
| 12 | + |
| 13 | +<img src="Assets/demo.jpeg" align="right" width="300px" hspace="10px" vspace="0px"> |
| 14 | + |
| 15 | +```swift |
| 16 | +ListView(style: .plain, reloadTriggers: [self.items.map { _ in Void() }]) { |
| 17 | + InstanceComponent(identifier: 0) { (_) in |
| 18 | + ZStackView { |
| 19 | + UILabel() |
| 20 | + .dx.text("This screen was build by `InstanceComponent` in `ListView`. Check `ViewController` class for more infomation.") |
| 21 | + .dx.font(UIFont.systemFont(ofSize: 14, weight: .regular)) |
| 22 | + .dx.numberOfLines(0) |
| 23 | + .dx.textAlignment(NSTextAlignment.center) |
| 24 | + .stickingToParentEdges(left: 16, right: 16, top: 14, bottom: 14) |
| 25 | + } |
| 26 | + .dx.style(SimpleCard()) |
| 27 | + .fillingParent(insets: (22, 22, 14, 14)) |
| 28 | + } |
| 29 | + |
| 30 | + Group(of: self.items.value) { (item: Item) in |
| 31 | + InstanceComponent(identifier: item.0) { (i) in |
| 32 | + ZStackView { |
| 33 | + UILabel() |
| 34 | + .dx.text(i) |
| 35 | + .stickingToParentEdges(left: 20, right: .greaterThanOrEqualTo(20), top: 12, bottom: 12) |
| 36 | + UIButton() |
| 37 | + .dx.useRx(withUnretained: self, disposeBag: self.disposeBag, reactiveBlock: { (owner, base: UIButton) in |
| 38 | + base.rx.tap |
| 39 | + .bind { (_) in |
| 40 | + item.1() |
| 41 | + } |
| 42 | + }) |
| 43 | + .fillingParent() |
| 44 | + } |
| 45 | + .fillingParent() |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +# Requirement |
| 52 | + |
| 53 | +- iOS 11+ |
| 54 | +- XCode 11+ |
| 55 | +- Swift 5.3+ |
| 56 | + |
| 57 | +# Installation |
| 58 | + |
| 59 | +## CocoaPods |
| 60 | + |
| 61 | +Add following line to your project's Podfile |
| 62 | +```ruby |
| 63 | +pod 'RxSwiftUIKit', '1.0.0' |
| 64 | +``` |
| 65 | +Run `pod install` to install SwiftUIKit |
| 66 | + |
| 67 | +## Source Code |
| 68 | + |
| 69 | +Drop all files in folder `./RxSwiftUIKit/Source` to your project or download this repo and drag `RxSwiftUIKit.xcodeproj` to your project with necessary files then link your app with `SwiftUIKit` framework munualy |
| 70 | + |
| 71 | +# Usage |
| 72 | + |
| 73 | +## Layout |
| 74 | + |
| 75 | +To build layout with Declarative way using LayoutBuilder, read [SwiftUIKit](https://github.com/sonla58/SwiftUIKit) |
| 76 | + |
| 77 | +## Component |
| 78 | + |
| 79 | +**Component** is a unit of the UI in [Carbon](https://github.com/ra1028/Carbon) to build UITableView or UICollectionView. Please read more document of Carbon here: [Carbon Document]([Carbon](https://github.com/ra1028/Carbon)) |
| 80 | + |
| 81 | +Build Component |
| 82 | + |
| 83 | +```swift |
| 84 | +struct BasicComponemt: Component { |
| 85 | + let title: String |
| 86 | + let onTap: () -> Void |
| 87 | + |
| 88 | + func renderContent() -> BasicView { |
| 89 | + BasicView() |
| 90 | + } |
| 91 | + |
| 92 | + func render(in content: BasicView) { |
| 93 | + content.title = title |
| 94 | + content.onTap = onTap |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +class BasicView: UI.View { |
| 99 | + |
| 100 | + let title: String |
| 101 | + let onTap: () -> Void = { } |
| 102 | + |
| 103 | + override var subviewsLayout: SomeView { |
| 104 | + ZStackView { |
| 105 | + UILabel() |
| 106 | + .dx.text(title) |
| 107 | + .stickingToParentEdges(left: 20, right: .greaterThanOrEqualTo(20), top: 12, bottom: 12) |
| 108 | + UIButton() |
| 109 | + .dx.useRx(withUnretained: self, |
| 110 | + disposeBag: self.disposeBag, |
| 111 | + reactiveBlock: { (owner, base: UIButton) in |
| 112 | + base.rx.tap |
| 113 | + .bind { (_) in |
| 114 | + owner.onTap() |
| 115 | + } |
| 116 | + }) |
| 117 | + .fillingParent() |
| 118 | + } |
| 119 | + .fillingParent() |
| 120 | + } |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +or you can use `InstanceComponent` to build basic component with LayoutBuilder |
| 125 | + |
| 126 | +```swift |
| 127 | +InstanceComponent(identifier: id) { (id) in |
| 128 | + ZStackView { |
| 129 | + UILabel() |
| 130 | + .dx.text(id) |
| 131 | + .stickingToParentEdges(left: 20, right: .greaterThanOrEqualTo(20), top: 12, bottom: 12) |
| 132 | + UIButton() |
| 133 | + .dx.useRx(withUnretained: self, |
| 134 | + disposeBag: self.disposeBag, |
| 135 | + reactiveBlock: { (owner, base: UIButton) in |
| 136 | + base.rx.tap |
| 137 | + .bind { (_) in |
| 138 | + print(id) |
| 139 | + } |
| 140 | + }) |
| 141 | + .fillingParent() |
| 142 | + } |
| 143 | + .fillingParent() |
| 144 | +} |
| 145 | +``` |
| 146 | + |
| 147 | +## ListView |
| 148 | + |
| 149 | +```swift |
| 150 | +let list = BehaviorRelay<[String]>(value: [ |
| 151 | + "Bún Đậu", |
| 152 | + "Phở", |
| 153 | + "Mắm tôm" |
| 154 | +]) |
| 155 | + |
| 156 | +ListView(style: .plain, reloadTriggers: [self.list.map { _ in Void() }]) { |
| 157 | + Group(list) { item in |
| 158 | + BasicComponent(title: item) { |
| 159 | + print(item) |
| 160 | + } |
| 161 | + } |
| 162 | +} |
| 163 | +.fillingParent() |
| 164 | +``` |
| 165 | + |
| 166 | +`reloadTriggers` is an array of `Observable<Void>`, when one of triggers receive `next` event, listView will be reload data automatically. |
| 167 | + |
| 168 | +## GridView |
| 169 | + |
| 170 | +```swift |
| 171 | +let list = BehaviorRelay<[String]>(value: [ |
| 172 | + "Bún Đậu", |
| 173 | + "Phở", |
| 174 | + "Mắm tôm" |
| 175 | +]) |
| 176 | + |
| 177 | +GridView(reloadTriggers: [self.list.map { _ in Void() }]) { |
| 178 | + Group(list) { item in |
| 179 | + BasicComponent(title: item) { |
| 180 | + print(item) |
| 181 | + } |
| 182 | + } |
| 183 | +} |
| 184 | +.fillingParent() |
| 185 | +``` |
| 186 | + |
| 187 | +This is a basic example of usage GridView with default layout: `UICollectionViewFlowLayout` and default `Renderer`. Read Complex example in [Carbon](https://github.com/ra1028/Carbon) |
0 commit comments