CombineOperators
Description
This repo includes operators for Combine, almost complete re-implementation of RxCocoa, RxViewController, RxGestures and RxKeyboard libraries and some additional features.
Example
import UIKit
import Combine
import CombineCocoa
import CombineOperators
final class SomeViewModel {
let title = CurrentValueSubject<String, Never>("Title")
let icon = CurrentValueSubject<UIImage?, Never>(nil)
let color = CurrentValueSubject<UIColor, Never>(UIColor.white)
let bool = CurrentValueSubject<Bool, Never>(false)
...
}
class ViewController: UIViewController {
@IBOutlet private weak var titleLabel: UILabel!
@IBOutlet private weak var iconView: UIImageView!
@IBOutlet private weak var switchView: UISwitch!
let viewModel = SomeViewModel()
...
private func configureSubscriptions() {
viewModel.title ==> titleLabel.cb.text
viewModel.iconView ==> iconView.cb.image
viewModel.bool ==> switchView.cb.isOn
viewModel.color ==> (self, ViewController.setTint)
//or viewModel.color ==> cb.weak(method: ViewController.setTint)
//or viewModel.color ==> {[weak self] in self?.setTint(color: $0) }
}
private func setTint(color: UIColor) {
...
}
...
}Usage
- Operator
=>
-
From
PublishertoSubscriber, creates a subscription:intPublisher => intSubscriber
- From `Publisher` to `Subject`, creates a subscription and returns `Cancellable`:
```swift
let Cancellable = intPublisher => intSubject
- From
CancellabletoDisposeBag:
someCancellable => cancellableSet
somePublisher => someSubject => cancellableSet- From
PublishertoScheduler, returnsAnyPublisher<Output, Failure>:
somePublisher => DispatchQueue.main => someSubscriber- From
Publisherto(Output) -> Void:
somePublisher => { print($0) }- From
Publisherto@autoescaping () -> Void:
somePublisher => print("action")All operators casts output-input types and errors types where possible
- Operator
==>
Drive Publisher to Subscriber on main queue:
intPublisher ==> intSubscriber => cancellableSet-
Operators
=>>and==>>replaces.removeDublicates() -
CancellableBuilderandMergeBuilder,CombineLatestBuilder- result builders -
Some features:
UIView.cboperators:isVisible,willAppear,isOnScreen,didAppear,movedToWindow,frame,frameOnWindow, etc.skipNil()operatoror(Bool), .toggle(), !operators for boolean sequences- use
+and+=operator for merging publishers, creating Cancellables, etc interval(...)withLast() -> AnyPublisher<(previous: Output?, current: Output), Failure>.mp-@dynamicMemberLookupmapperasResult() -> AnyPublisher<Result<Output, Failure>, Never>nilIfEmptyisEmptyisNilisNilOrEmptyappend(...)smooth(...)methods to smooth changes, example: sequence[0, 1]turns to[0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1]onValue, onFailure, onFinished, onSubscribe, onCancel, onRequestwrappers onhandleEvents(...)operatorguard()cb.isFirstResponderUIStackView().cb.update(...)UIView().cb.transform.scale(), .rotation(), .translation()
Installation
CombineOperators is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'CombineOperators/CombineCocoa'and run pod update from the podfile directory first.
Create a Package.swift file.
// swift-tools-version:5.0
import PackageDescription
let package = Package(
name: "SomeProject",
dependencies: [
.package(url: "https://github.com/dankinsoid/CombineOperators.git", from: "1.73.0")
],
targets: [
.target(name: "SomeProject", dependencies: ["CombineOperators"])
]
)$ swift buildAuthor
Voidilov, [email protected]
License
CombineOperators is available under the MIT license. See the LICENSE file for more info.