Skip to content

HyperfocusDisordered/CombineOperators

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CombineOperators

CI Status Version License Platform

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

  1. Operator =>
  • From Publisher to Subscriber, creates a subscription:

    intPublisher => intSubscriber

- From `Publisher` to `Subject`, creates a subscription and returns `Cancellable`:

```swift
  let Cancellable = intPublisher => intSubject
  • From Cancellable to DisposeBag:
someCancellable => cancellableSet
somePublisher => someSubject => cancellableSet
  • From Publisher to Scheduler, returns AnyPublisher<Output, Failure>:
somePublisher => DispatchQueue.main => someSubscriber
  • From Publisher to (Output) -> Void:
somePublisher => { print($0) }
  • From Publisher to @autoescaping () -> Void:
somePublisher => print("action")

All operators casts output-input types and errors types where possible

  1. Operator ==>

Drive Publisher to Subscriber on main queue:

intPublisher ==> intSubscriber => cancellableSet
  1. Operators =>> and ==>> replaces .removeDublicates()

  2. CancellableBuilder and MergeBuilder, CombineLatestBuilder - result builders

  3. Some features:

  • UIView.cb operators: isVisible, willAppear, isOnScreen, didAppear, movedToWindow, frame, frameOnWindow, etc.
  • skipNil() operator
  • or(Bool), .toggle(), ! operators for boolean sequences
  • use + and += operator for merging publishers, creating Cancellables, etc
  • interval(...)
  • withLast() -> AnyPublisher<(previous: Output?, current: Output), Failure>
  • .mp - @dynamicMemberLookup mapper
  • asResult() -> AnyPublisher<Result<Output, Failure>, Never>
  • nilIfEmpty
  • isEmpty
  • isNil
  • isNilOrEmpty
  • append(...)
  • 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, onRequest wrappers on handleEvents(...) operator
  • guard()
  • cb.isFirstResponder
  • UIStackView().cb.update(...)
  • UIView().cb.transform.scale(), .rotation(), .translation()

Installation

  1. CocoaPods

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.

  1. Swift Package Manager

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.81.0")
    ],
  targets: [
    .target(name: "SomeProject", dependencies: ["CombineOperators"])
    ]
)
$ swift build

Author

Voidilov, [email protected]

License

CombineOperators is available under the MIT license. See the LICENSE file for more info.

About

Combine operators

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 99.7%
  • Objective-C 0.3%