RxSwiftExtensions

Frequently used extensoins in RxSwift

MIT License

Stars
42
Committers
1

RxSwiftExtensions

Frequently used extensions in RxSwift


Dear those who love RxSwift

This library based on questions from KakaoTalk open chat room. Join us if you have some questions about RxSwift or you are interested. We always welcome ๐Ÿ˜€ However, this chat room us operating in South Korea. Therefore, you will need speaking Korean ๐Ÿ‡ฐ๐Ÿ‡ท

์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ์นด์นด์˜คํ†ก ์˜คํ”ˆ ์ฑ„ํŒ…๋ฐฉ์—์„œ ์˜ฌ๋ผ์˜จ ์งˆ๋ฌธ์„ ๋ฐ”ํƒ•์œผ๋กœ ๋งŒ๋“ค์–ด์กŒ์Šต๋‹ˆ๋‹ค. RxSwift์— ๊ด€๋ จํ•˜์—ฌ ์งˆ๋ฌธ์ด ์žˆ๊ฑฐ๋‚˜ ๊ด€์‹ฌ์ด ์žˆ๋‹ค๋ฉด ์–ธ์ œ๋“ ์ง€ ๊ฐ€๋ฒผ์šด ๋งˆ์Œ์œผ๋กœ ์ฐธ์—ฌํ•˜์„ธ์š” ๐Ÿ˜€ ์ด ๋ฐฉ์€ ํ•œ๊ตญ์—์„œ ์šด์˜๋˜๋Š” ๊ฒƒ์ด๋ผ ํ•œ๊ตญ์–ด๋ฅผ ํ•„์š”๋กœ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค... (์ด๊ฑฐ ์ฝ์œผ์‹ค ์ •๋„๋ฉด ๋ฌธ์ œ์—†์„ ๊ฒ๋‹ˆ๋‹ค ๐Ÿ˜‚)

ใ“ใฎใƒฉใ‚คใƒ–ใƒฉใƒชใƒผใฏใ‚ซใ‚ซใ‚ชใƒˆใƒผใ‚ฏใ‚ชใƒผใƒ—ใƒณใƒใƒฃใƒƒใƒˆใƒซใƒผใƒ ใซๆŠ•็จฟใ•ใ‚ŒใŸ่ณชๅ•ใ‚’ๅŸบใซไฝœใ‚‰ใ‚Œใพใ—ใŸใ€‚ RxSwiftใซใคใ„ใฆ่ณชๅ•ใ‚„่ˆˆๅ‘ณใŒใ‚ใ‚Œใฐใ€ใ„ใคใงใ‚‚ๆฐ—่ปฝใซๅ‚ๅŠ ใ—ใฆใใ ใ•ใ„ ๐Ÿ˜€ ใ“ใฎใƒซใƒผใƒ ใฏ้Ÿ“ๅ›ฝใงๅ–ถๆฅญใ•ใ‚Œใฆใ„ใ‚‹ใ‚‚ใฎใชใฎใง้Ÿ“ๅ›ฝ่ชžใ‚’ๅฟ…่ฆใจใ—ใพใ™ ๐Ÿ‡ฐ๐Ÿ‡ท


Functions

Functions Detail

    • Observe KVO by keyPath

      KVO is an Objective-C mechanism. That means that it wasn't built with type safety in mind. This project tries to solve some of the problems.

      UIView()
        .observe(\.bounds, , options: [.initial, .new], retainSelf: true)
        .subscribe({ print($0) })
      
    • Observe KVO by keyPath

      rx.observeWeakly has somewhat slower than rx.observe because it has to handle object deallocation in case of weak references.

      UIView()
        .observeWeakly(\.bounds, , options: [.initial, .new], retainSelf: true)
        .subscribe({ print($0) })
      
  • Define Section Data easily in tableView or collectionView

    Define follow

    enum TestSectionData {
        case section1
        case section2([Value])
    }
    
    extension TestSectionData: SectionDataType {
    
        typealias Item = Value
    
        var items: [TestSectionData.Value] {
            switch self {
            case .section1: return [.string("Section 1")]
            case let .section2(value): return value
            }
        }
    
        enum Value {
            case string(String)
            case int(Int)
        }
    }
    

    You can use it follow

    let section = sections[indexPath.section].items[indexPath.row]
    
    switch section {
      case let .int(value):
        cell.set(value)
        return cell
      case let .string(value):
        cell.set(value)
        return cell
    }
    
    • Observe bounds in UIView

      UIView().rx.bounds.subscribe({ print($0) })
      
    • Observe center in UIView

      UIView().rx.center.subscribe({ print($0) })
      
    • Binder textColor in UILabel

      Observable<UIColor?>.just(UIColor.white).bind(to: UILabel().rx.textColor)
      
    • Binder textColor in UITextView

      Observable<UIColor?>.just(UIColor.white).bind(to: UITextView().rx.textColor)
      
    • Observe contentSize in UIScrollView

      UIScrollView().rx.contentSize.subscribe({ print($0) })
      
    • Observe scrollable of vertical (bounds.height < contentSize.height) in UIScrollView

      UIScrollView().rx.scrollableVertical.subscribe({ print($0) })
      
    • Observe scrollable of horizontal (bounds.width < contentSize.width) in UIScrollView

      UIScrollView().rx.scrollableHorizontal.subscribe({ print($0) })
      
    • Register UITableViewCell easily

      Following sample will be register with "Cell" (UITableViewCell.Identifier).

      UITableView().register(cell: Cell.self)
      

      Also you can define Identifier like follow

      UITableView().register(cell: Cell.self, forCellReuseIdentifier: "CustomIdentifier")
      
    • Register UITableViewCell using NIB easily

      Following sample will be register with "Cell" (UITableViewCell.Identifier).

      UITableView().register(nibCell: Cell.self)
      

      Also you can define Identifier like follow

      UITableView().register(nibCell: Cell.self, forCellReuseIdentifier: "CustomIdentifier")
      
    • Register UITableViewHeaderFooterView easily

      FolloFollowing sample will be register with "Cell" (UITableViewHeaderFooterView.Identifier).

      UITableView().register(cell: Cell.self)
      

      Also you can define Identifier like follow

      UITableView().register(cell: Cell.self, forCellReuseIdentifier: "CustomIdentifier")
      
    • Register UITableViewCell using NIB easily

      FolloFollowing sample will be register with "Cell" (UITableViewHeaderFooterView.Identifier).

      UITableView().register(cell: Cell.self)
      

      Also you can define Identifier like follow

      UITableView().register(cell: Cell.self, forCellReuseIdentifier: "CustomIdentifier")
      
    • Dequeue UICollectionViewCell easily

      Following sample will be dequeue with "Cell" (UICollectionReusableView.Identifier).

      let cell: Cell? = UITableView().dequeue(Cell.self)
      
    • Dequeue UICollectionViewCell easily

      Following sample will be dequeue with "Cell" (UICollectionReusableView.Identifier).

      let cell: Cell? = UITableView().dequeue(Cell.self, indexPath: IndexPath)
      
    • Dequeue UITableViewHeaderFooterView easily

      let cell: Cell? = UITableView().dequeue(Cell.self)
      
    • Bind items by rx

      Observable.just(["0", "1", "2", "3"])
          .bind(to: tableView.rx.items(cell: Cell.self)) { row, item, cell in
      
          }
      
    • Define Identifier by class name

      class MyCell: UITableViewCell {
      
      }
      
      MyCell.Identifier == "MyCell"
      
    • Define Identifier by class name

      class MyCell: UITableViewHeaderFooterView {
      
      }
      
      MyCell.Identifier == "MyCell"
      
    • Register UICollectionViewCell easily

      Following sample will be register with "Cell" (UICollectionReusableView.Identifier).

      UICollectionView().register(cell: Cell.self)
      

      Also you can define Identifier like follow

      UICollectionView().register(cell: Cell.self, forCellWithReuseIdentifier: "CustomIdentifier")
      
    • Register UICollectionViewCell using NIB easily

      Following sample will be register with "Cell" (UICollectionReusableView.Identifier).

      UICollectionView().register(nibCell: Cell.self)
      

      Also you can define Identifier like follow

      UICollectionView().register(nibCell: Cell.self, forCellWithReuseIdentifier: "CustomIdentifier")
      
    • Register UICollectionReusableView for SupplementaryViewOfKind

      UICollectionView().register(cell: Cell.self, forSupplementaryViewOfKind: .header)
      
      UICollectionView().register(nibCell: Cell.self, forSupplementaryViewOfKind: .footer)
      
    • Dequeue UICollectionViewCell easily

      Following sample will be dequeue with "Cell" (UICollectionReusableView.Identifier).

      let cell: Cell? = UICollectionView.dequeue(Cell.self, for: IndexPath)
      
    • DeDequeue UICollectionReusableView for SupplementaryViewOfKind easily

      let cell: Cell? = UICollectionView.dequeue(Cell.self, ofKind: .header, for: IndexPath)
      
    • Bind items by rx

      Observable.just(["0", "1", "2", "3"])
          .bind(to: collectionView.rx.items(cell: Cell.self)) { row, item, cell in
      
          }
      
    • Define Identifier by class name

      class MyCell: UICollectionReusableView {
      
      }
      
      MyCell.Identifier == "MyCell"
      
    • UIViewController().rx.viewDidLoad.subscribe({ print($0) })
      
    • UIViewController().rx.viewWillAppear.subscribe({ print($0) })
      
    • UIViewController().rx.viewDidAppear.subscribe({ print($0) })
      
    • UIViewController().rx.viewWillDisappear.subscribe({ print($0) })
      
    • UIViewController().rx.viewDidDisappear.subscribe({ print($0) })
      
    • UIViewController().rx.viewWillLayoutSubviews.subscribe({ print($0) })
      
    • UIViewController().rx.viewDidLayoutSubviews.subscribe({ print($0) })
      
    • UIViewController().rx.willMove(toParentViewController: parentViewController).subscribe({ print($0) })
      
    • UIViewController().rx.didMove(toParentViewController: parentViewController).subscribe({ print($0) })
      
    • UIViewController().rx.didReceiveMemoryWarning.subscribe({ print($0) })
      
    • UIViewController().rx.isVisible.subscribe({ print($0) })
      
    • UIViewController().rx.isDismissing.subscribe({ print($0) })
      
    • Extend withLatestFrom for support multi parameters

      let a = Observable.just("A")
      let b = Observable.just("B")
      let c = Observable.just("C")
      
      Observable().withLatestFrom(a, b, c)
          .subscribe(onNext: { (a, b, c) in
              ...
          })
      
    • Extend withLatestFromAndSelf for combining parameters with self

      let a = Observable.just("A")
      let b = Observable.just("B")
      let c = Observable.just("C")
      
      Observable().withLatestFromAndSelf(a, b, c)
          .subscribe(onNext: { (a, b, c) in
              ...
          })
      
    • Can bind multiple ObserverType in one line

      let publisher = PublishSubject<String>()
      
      let observers: [PublishSubject<String>] = [...]
      
      let disposable = publisher.bind(to: observers)
      
    • Can bind optional type if ovserver is nil, it will ignore subscribe

      let publisher = PublishSubject<String>()
      
      let observer: PublishSubject<String>? = nil
      
      let disposable = publisher.bind(to: observer)
      
    • Extend withLatestFrom for support multi parameters

      let a = SharedSequenceConvertibleType.just("A")
      let b = SharedSequenceConvertibleType.just("B")
      let c = SharedSequenceConvertibleType.just("C")
      
      SharedSequenceConvertibleType().withLatestFrom(a, b, c)
          .subscribe(onNext: { (a, b, c) in
              ...
          })
      
    • Extend withLatestFromAndSelf for combining parameters with self

      let a = SharedSequenceConvertibleType.just("A")
      let b = SharedSequenceConvertibleType.just("B")
      let c = SharedSequenceConvertibleType.just("C")
      
      SharedSequenceConvertibleType().withLatestFromAndSelf(a, b, c)
          .subscribe(onNext: { (a, b, c) in
              ...
          })
      

Installation


Used pods

Author

tokijh

License

RxSwiftExtensions is available under the MIT License See the LICENSE file for more info.