https://github.com/magi82/rxviewbinder
RxViewBinder is a one-way architecture framework using Reactive.
https://github.com/magi82/rxviewbinder
architecture reactive rx rxswift swift
Last synced: about 1 year ago
JSON representation
RxViewBinder is a one-way architecture framework using Reactive.
- Host: GitHub
- URL: https://github.com/magi82/rxviewbinder
- Owner: magi82
- License: mit
- Created: 2018-03-16T13:28:37.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2020-07-07T15:38:07.000Z (almost 6 years ago)
- Last Synced: 2024-04-26T15:01:54.393Z (about 2 years ago)
- Topics: architecture, reactive, rx, rxswift, swift
- Language: Swift
- Homepage:
- Size: 62.5 KB
- Stars: 32
- Watchers: 4
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# RxViewBinder

[](http://cocoapods.org/pods/RxViewBinder)
[](http://cocoapods.org/pods/RxViewBinder)
[](https://github.com/Carthage/Carthage)
[](http://cocoapods.org/pods/RxViewBinder)
RxViewBinder is a simple one-way architecture.
Simple and easy to implement. :sunny:
It is implemented as a reactive extension.
## Flow

## Usage (ViewBindable)
- Create a ViewBinder class that implements ViewBindable.
Command, Action, State must be implemented.
Command is enum type.
Action, State is structure type.
*important!!*
You need to bind the action and state in the constructor of the state structure.
```swift
final class SampleViewBinder: ViewBindable {
enum Command {
case fetch
}
struct Action {
let value: PublishRelay = PublishRelay()
}
struct State {
let value: Driver
init(action: Action) {
// Action and state binding
value = action.value.asDriver(onErrorJustReturn: "")
}
}
let action = Action()
lazy var state = State(action: self.action)
}
```
- implements a binding method that accepts a command stream and sends the stream to action.
When changing the state of ui, only action is used.
state is used only when the view receives the state of ui.
```swift
func binding(command: Command) {
switch command {
case .fetch:
Observable.just("test")
.bind(to: action.value)
.disposed(by: self.disposeBag)
}
}
```
- Or you can simply send the stream without creating an observer.
```swift
func binding(command: Command) {
switch command {
case .fetch:
action.value.accept("test")
}
}
```
## Usage (BindView)
- Implement the BindView protocol on the view controller.
It injects the view binder at initialization.
```swift
final class ViewController: UIViewController, BindView {
typealias ViewBinder = SampleViewBinder
init(viewBinder: ViewBinder) {
defer { self.viewBinder = viewBinder }
super.init(nibName: nil, bundle: nil)
}
}
```
- If you are using a storyboard, you have to inject it in a different way.
```swift
let vc = ViewController()
vc.viewBinder = SampleViewBinder()
```
or
```swift
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.viewBinder = ViewBinder()
}
```
- Implements the command and state methods.
```swift
func command(viewBinder: ViewBinder) {
self.rx.methodInvoked(#selector(UIViewController.viewDidLoad))
.map { _ in ViewBinder.Command.fetch }
.bind(to: viewBinder.command)
.disposed(by: self.disposeBag)
}
func state(viewBinder: ViewBinder) {
viewBinder.state
.value
.drive(onNext: { print($0) })
.disposed(by: self.disposeBag)
}
```
## Requirements
- Swift 5.0+
- iOS 9.0+
## Installation
- **For iOS 9+ projects** with [CocoaPods](https://cocoapods.org):
```ruby
pod 'RxViewBinder', '~> 2.0.0'
```
- **For iOS 9+ projects** with [Carthage](https://github.com/Carthage/Carthage):
```ruby
github "magi82/RxViewBinder" ~> 2.0.0
```
## Author
magi82, devmagi82@gmail.com
## License
**RxViewBinder** is available under the MIT license. See the [LICENSE](LICENSE) file for more info.