0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Swift】UIColorPickerViewControllerをRxSwiftに対応した

Last updated at Posted at 2022-11-06

はじめに

1ヶ月前くらいからRxSwiftを学習し始めまして、最近はRxSwiftを使って個人開発をしています。
UIColorPickerViewControllerがRxSwiftに対応していなかったのでDelegateをRxSwiftで使えるようにしてみました。

何をするか

colorPickerViewControllerDidSelectColorcolorPickerViewControllerDidFinishをRxSwiftで使えるようにする

extension ViewController: UIColorPickerViewControllerDelegate {
    func colorPickerViewControllerDidSelectColor(_ viewController: UIColorPickerViewController) {
    }

    func colorPickerViewController(_ viewController: UIColorPickerViewController, didSelect color: UIColor, continuously: Bool) {
    }
}

実装

import RxCocoa
import RxSwift
import UIKit

public extension Reactive where Base: UIColorPickerViewController {
    var didFinish: Observable<Void> {
        return RxUIColorPickerViewProxy.proxy(for: base)
            .didFinishSubject
            .asObservable()
    }

    var didSelectColor: Observable<(UIColor, Bool)> {
        return RxUIColorPickerViewProxy.proxy(for: base)
            .didSelectColorSubject
            .asObservable()
    }
}

public typealias UIColorPickerDelegate = UIColorPickerViewControllerDelegate

extension UIColorPickerViewController: HasDelegate {
    public typealias delegate = UIColorPickerDelegate
}

class RxUIColorPickerViewProxy: DelegateProxy<UIColorPickerViewController, UIColorPickerDelegate>, DelegateProxyType, UIColorPickerViewControllerDelegate {
    public init(colorPicker: UIColorPickerViewController) {
        super.init(parentObject: colorPicker, delegateProxy: RxUIColorPickerViewProxy.self)
    }

    // MARK: DelegateProxyType

    static func registerKnownImplementations() {
        register { RxUIColorPickerViewProxy(colorPicker: $0) }
    }

    static func currentDelegate(for object: UIColorPickerViewController) -> UIColorPickerViewControllerDelegate? {
        return object.delegate
    }

    static func setCurrentDelegate(_ delegate: UIColorPickerViewControllerDelegate?, to object: UIColorPickerViewController) {
        object.delegate = delegate
    }

    // MARK: Proxy Subject

    internal lazy var didFinishSubject = PublishSubject<Void>()
    internal lazy var didSelectColorSubject = PublishSubject<(UIColor, Bool)>()

    // MARK: UIColorPickerViewControllerDelegate

    func colorPickerViewControllerDidFinish(_: UIColorPickerViewController) {
        didFinishSubject.onNext(())
    }

    func colorPickerViewController(_: UIColorPickerViewController, didSelect color: UIColor, continuously: Bool) {
        didSelectColorSubject.onNext((color, continuously))
    }

    // MARK: Completed

    deinit {
        self.didFinishSubject.onCompleted()
        self.didSelectColorSubject.onCompleted()
    }
}

使い方

colorPicker.rx.didFinish
    .subscribe(onNext: { _ in
        print("終了しました")
    })
    .disposed(by: disposeBag)

colorPicker.rx.didSelectColor
    .subscribe(onNext: { [weak self] color, continuously in
        print("色を選択しました")
    })
    .disposed(by: disposeBag)

おわり

RxSwiftはめっちゃ便利!!
でも、これからはCombineかな?

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?