STDevRxExt contains some extension functions for RxSwift and RxCocoa which makes our live easy.

Overview

STDevRxExt

CI Status Platform Cocoapods SPM compatible codecov License

Example

To run the Example.playground, clone the repo, and run pod install from the Example directory first.

Requirements

  • iOS 9.0+
  • tvOS 9.0+
  • macOS 10.10+
  • watchOS 3.0+
  • Swift 5.0+
  • Xcode 11+

Installation

CocoaPods

STDevRxExt is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'STDevRxExt'
Swift Package Manager

You can use The Swift Package Manager to install STDevRxExt by adding the proper description to your Package.swift file:

import PackageDescription

let package = Package(
    name: "YOUR_PROJECT_NAME",
    targets: [],
    dependencies: [
        .package(url: "https://github.com/STDevTM/STDevRxExt.git", from: "1.0.0")
    ]
)

Next, add STDevRxExt to your targets dependencies like so:

.target(
    name: "YOUR_TARGET_NAME",
    dependencies: [
        "STDevRxExt",
    ]
),

Then run swift package update.

List of All Extensions

Filter Extensions

Allow only true elements from Observable<Bool>:

let disposeBag = DisposeBag()

Observable.of(true, false, false, true, true)
    .allowTrue()
    .subscribe(onNext: { print($0) })
    .disposed(by: disposeBag)

Output will be:

true
true
true

You can use allowTrue on Bool? as well. In this case nil elements will be ignored:

let disposeBag = DisposeBag()

Observable.of(true, false, nil, true, nil, true)
    .allowTrue()
    .subscribe(onNext: { print($0) })
    .disposed(by: disposeBag)

Output will be:

true
true
true

If you prefer to allow nil elements as well then you can use allowTrueOrNil like this:

let disposeBag = DisposeBag()

Observable.of(true, false, nil, true, nil, true, false)
    .allowTrueOrNil()
    .subscribe(onNext: { print($0) })
    .disposed(by: disposeBag)

Output will be:

true
true
true
true
true

Map Extensions

You can map every element in sequence with provided value.

let disposeBag = DisposeBag()

Observable.of(1, 5, 7, 8)
    .map(to: "ping")
    .subscribe(onNext: { print($0) })
    .disposed(by: disposeBag)

Output will be:

ping
ping
ping
ping

You can map every element in sequence by procided Key Path.

let disposeBag = DisposeBag()

   let observable = Observable.of(
            Book(title: "Twenty Thousand Leagues Under the Sea", author: Author("Jules", "Verne")),
            Book(title: "Hamlet", author: Author("William", "Shakespeare")),
            Book(title: "Hearts of Three", author: Author("Jack", "London"))
        )

    observable
        .map(at: \.title)
        .subscribe(onNext: { print($0) })
        .disposed(by: disposeBag)

    observable
        .map(at: \.author.firstName)
        .subscribe(onNext: { print($0) })
        .disposed(by: disposeBag)

Output will be:

Twenty Thousand Leagues Under the Sea
Hamlet
Hearts of Three

Jules
William
Jack

Cast Extensions

You can do downcast elements in sequence using cast(to:).

let disposeBag = DisposeBag()

Observable<CustomStringConvertible>.of("1", "5", "7", "8")
    .cast(to: String.self)
    .subscribe(onNext: { print($0) })
    .disposed(by: disposeBag)

Output will be:

Optional("1")
Optional("5")
Optional("7")
Optional("8")

In order to do force downcast use forceCast(to:) liek this:

let disposeBag = DisposeBag()

Observable<CustomStringConvertible>.of("1", "5", "7", "8")
    .forceCast(to: String.self)
    .subscribe(onNext: { print($0) })
    .disposed(by: disposeBag)

Output will be:

1
5
7
8

In case of downcast exception it will return Observable.error(RxCastError.castFailed).

Allow extension functions can be used on Driver as well, except forceCast(to:).

Other Extensions

Sometimes we need to update some subject or observer on each next event of Observable or Driver. For example:

request
    .do(onNext: { [weak self] _ in
        self?.inProgress.onNext(true)
    })
    .flatMap {
        service.doRequest($0)
    }
    .do(onNext: { [weak self] _ in
        self?.inProgress.onNext(false)
    })
    .subscribe(onNext: { response in
        dump(response)
    })
    .disposed(by: disposeBag)

You can use update(_:with:) method for shorting code like this:

request
    .update(inProgress, with: true)
    .flatMap {
        service.doRequest($0)
    }
    .update(inProgress, with: false)
    .subscribe(onNext: { response in
        dump(response)
    })
    .disposed(by: disposeBag)

Author

Tigran Hambardzumyan, [email protected]

Support

Feel free to open issues with any suggestions, bug reports, feature requests, questions.

Let us know!

We’d be really happy if you sent us links to your projects where you use our component. Just send an email to [email protected] And do let us know if you have any questions or suggestion.

License

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

You might also like...
A library that makes defines your style sheet like a breeze.

Atelier A library that makes defines your style sheet like a breeze. Requirements iOS 13.0+ Xcode 12.0+ Swift 5.3+ Installation You can add Atelier to

SDWebImageMockPlugin makes possible the creation of snapshot testing with views using SDWebImage to configure images
SDWebImageMockPlugin makes possible the creation of snapshot testing with views using SDWebImage to configure images

SDWebImageMockPlugin makes possible the creation of snapshot testing with views using SDWebImage to configure images.

XcodeColorSense - An Xcode plugin that makes working with color easier.
XcodeColorSense - An Xcode plugin that makes working with color easier.

XcodeColorSense An Xcode plugin that makes working with color easier. Inspired by ColorSense-for-Xcode with extra care for Hex color Features Show col

Media view which subclasses UIImageView, and can display & load images, videos, GIFs, and audio and from the web, and has functionality to minimize from fullscreen, as well as show GIF previews for videos.
Media view which subclasses UIImageView, and can display & load images, videos, GIFs, and audio and from the web, and has functionality to minimize from fullscreen, as well as show GIF previews for videos.

I've built out the Swift version of this library! Screenshots Description ABMediaView can display images, videos, as well as now GIFs and Audio! It su

An image download extension of the image view written in Swift for iOS, tvOS and macOS.
An image download extension of the image view written in Swift for iOS, tvOS and macOS.

Moa, an image downloader written in Swift for iOS, tvOS and macOS Moa is an image download library written in Swift. It allows to download and show an

SwiftColorArt is a demo application that includes Swift files with all classes and extension necessary to create a font color schema matching to an image
SwiftColorArt is a demo application that includes Swift files with all classes and extension necessary to create a font color schema matching to an image

SwiftColorArt SwiftColorArt is a demo application that includes Swift files with all classes and extension necessary to create a font color schema mat

📦  An extension that generates letter-based avatars/placeholders
📦 An extension that generates letter-based avatars/placeholders

LetterAvatarKit LetterAvatarKit provides an UIImage extension for generating letter-based avatars/placeholders. There are a few images showing what yo

 SwiftGif - A small UIImage extension with gif support.
SwiftGif - A small UIImage extension with gif support.

SwiftGif - A small UIImage extension with gif support.

Siri Shortcuts extension for calculating NN-based image hash.

NNHash Siri Shortcuts extension for calculating NN-based image hash. Based on nhcalc.

Comments
  • Release 1.0.0

    Release 1.0.0

    Adding Swift Package Manager Support Adding tvOS, macOS, watchOS Support Adding Unit Tests Update GitHub action workflows Remove filterIfNil extension Bumped version name to 1.0.0

    opened by hamtiko 0
  • Add emptyIfNil extension for optional arrays

    Add emptyIfNil extension for optional arrays

    For Observable types with Optional Array elements it will be nice to have emptyIfNil extension which will return empty array if the element is nil and will return non-optional Array.

    enhancement 
    opened by tigran-stdev 0
Releases(1.1.0)
Owner
STDev
STDev is a full-stack web and mobile app design and development company founded about 10 years ago.
STDev
TRex 🦖 TRex makes OCR easy and accessible on a Mac

TRex ?? TRex makes OCR easy and accessible on a Mac. But what is OCR anyway? Imagine you have a PDF file or a Web page where you can't select the text

Ameba Labs 700 Dec 23, 2022
iOS Framework that makes it easy to preview images on any UIImageView.

AZImagePreview iOS Framework that makes it easy to preview images on any UIImageView. Screenshots Installation: Cocoa Pods: pod 'AZImagePreview' Swift

Antonio Zaitoun 25 Dec 11, 2022
ColorKit makes it easy to find the dominant colors of an image

ColorKit ColorKit is your companion to work with colors on iOS. Features Installation Sample Project Contributing License Features Dominant Colors Col

Boris Emorine 570 Jan 5, 2023
An extension to SnapshotTesting which allows you to create HEIC images

?? SnapshotTestingHEIC An extension to SnapshotTesting which allows you to create HEIC images. The benefit of using HEIC instead of PNG is that it can

Aleksei Kakoulin 5 Dec 4, 2022
An app show your Live Photo and export as GIF.

LivelyGIFs Show your Live Photo and export as GIF. Demo HighLights Do not use Pod or Cathage to install 3rd party library Simple logic, new learner fr

Xue Yu 95 Jun 24, 2022
Live rendering of UIImageView properties in Interface Builder

TJProfileImage Live rendering of componet’s properties in Interface Builder Features Dashed border Solid border Round corner Circle image Requirements

Tejas Ardeshna 32 Feb 28, 2022
IOS UIImage processing functions using the vDSP/Accellerate framework for speed.

UIImage Image Processing extensions using the vDSP/Accelerate framework.

null 372 Sep 1, 2022
Extract Metal functions from .metallib files.

Metal Library Archive MetalLibraryArchive is a product of reverse-engineering Apple's metallib file format. You can use MetalLibraryArchive to get the

Yu Ao 57 Dec 3, 2022
Morphi provides some additional shapes for SwiftUI.

Morphi - Μορφ Morphi provides some additional shapes for SwiftUI. Triangle Parallelogram(topLeftAngle) Polygon(sides) RoundedPolygon(sides, cornerRadi

Eric Marchand 66 Jun 2, 2022
Makes dealing with images buttery smooth.

ImageButter Image viewer for iOS that supports WebP. What is WebP? Find out more here. You can find more about why we created this here. Features Anim

Dollar Shave Club 397 Nov 19, 2022