Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alisoftware/functionalvcdemo
Demo for the Functional ViewControllers concept in RxSwift
https://github.com/alisoftware/functionalvcdemo
Last synced: about 1 month ago
JSON representation
Demo for the Functional ViewControllers concept in RxSwift
- Host: GitHub
- URL: https://github.com/alisoftware/functionalvcdemo
- Owner: AliSoftware
- Created: 2016-11-14T22:42:15.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2016-11-15T00:55:05.000Z (over 8 years ago)
- Last Synced: 2025-01-05T08:51:19.710Z (about 2 months ago)
- Language: Swift
- Size: 260 KB
- Stars: 8
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# FunctionalVCDemo
This project is a sample app demonstrating the concept of Functional ViewControllers using `RxSwift`.
## Concept
A Functional ViewController is a ViewController that can be used as a function `func show(input: T) -> Observable`. It will display the ViewController to gather data from the user, wait for the user to validate, then emit a `.next` even on the returned `Observable` once validated.
## Advantages
This pattern is useful to integrate a ViewContoller is a functional workflow, abstracting away the fact that the ViewController is a UI component and instead treating it as an asynchronous function getting inputs and asynchronously returning an output.
## Concept example
Imagine you have some Functional VCs that can be exposed in those functions:
* `func pickImage() -> Observable`
* `func askTitle() -> Observable`
* `func askNote() -> Observable`Each function presents a UIViewController, waits for the user to interact and validate (e.g. `pickImage` will present an `UIImagePickerController` and wait for the user to select an image), and asynchronously return the result (in case of `pickImage`, that's the `UIImage` obvously).
With this you can easily chain those functional VCs usng classic RxSwift reactive chains:
```swift
struct ImageDescription {
let image: UIImage?
let name: String?
let note: Int?
}var imageDesc: ImageDescription()
self.pickImage()
.flatMap({ image in
imageDesc.image = image
self.askTitle()
})
.flatMap({ title in
imageDesc.title = title
self.askNote()
})
.subscribe(onNext: { note in
imageDesc.note = note
print(imageDesc)
})
.addDisposableTo(disposeBag)
```This allows to read the workflow like a chain of steps happening one after the other, hiding away the complexity of asynchronicity.