Last active
July 29, 2018 07:39
-
-
Save huguesbr/31603b1f74d135ed3221f7ada9e206ff to your computer and use it in GitHub Desktop.
Reactive proxy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Reactive.swift | |
// RxSwift | |
// | |
// Created by Yury Korolev on 5/2/16. | |
// Copyright © 2016 Krunoslav Zaher. All rights reserved. | |
// | |
/** | |
Use `Reactive` proxy as customization point for constrained protocol extensions. | |
General pattern would be: | |
// 1. Extend Reactive protocol with constrain on Base | |
// Read as: Reactive Extension where Base is a SomeType | |
extension Reactive where Base: SomeType { | |
// 2. Put any specific reactive extension for SomeType here | |
} | |
With this approach we can have more specialized methods and properties using | |
`Base` and not just specialized on common base type. | |
*/ | |
public struct Reactive<Base> { | |
/// Base object to extend. | |
public let base: Base | |
/// Creates extensions with base object. | |
/// | |
/// - parameter base: Base object. | |
public init(_ base: Base) { | |
self.base = base | |
} | |
} | |
/// A type that has reactive extensions. | |
public protocol ReactiveCompatible { | |
/// Extended type | |
associatedtype CompatibleType | |
/// Reactive extensions. | |
static var rx: Reactive<CompatibleType>.Type { get set } | |
/// Reactive extensions. | |
var rx: Reactive<CompatibleType> { get set } | |
} | |
extension ReactiveCompatible { | |
/// Reactive extensions. | |
public static var rx: Reactive<Self>.Type { | |
get { | |
return Reactive<Self>.self | |
} | |
set { | |
// this enables using Reactive to "mutate" base type | |
} | |
} | |
/// Reactive extensions. | |
public var rx: Reactive<Self> { | |
get { | |
return Reactive(self) | |
} | |
set { | |
// this enables using Reactive to "mutate" base object | |
} | |
} | |
} | |
import class Foundation.NSObject | |
/// Extend NSObject with `rx` proxy. | |
extension NSObject: ReactiveCompatible { } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension ObservableType { | |
public func retryPromptOn(_ vc: UIViewController) -> Observable<E> { | |
return self.retryWhen { (error: Observable<Error>) -> Observable<Void> in | |
return vc.rx.retryPrompt(error: error) | |
} | |
} | |
} | |
extension Reactive where Base: UIViewController { | |
func retryPrompt(error: Observable<Error>) -> Observable<Void> { | |
let retry = PublishSubject<Void>() | |
error.subscribe { (error: Event<Error>) in | |
let alert = UIAlertController(title: error.element?.localizedDescription, message: "Retry?", preferredStyle: .alert) | |
alert.addAction(UIAlertAction(title: "No", style: .cancel) { _ in | |
retry.onError(error.element!) | |
}) | |
alert.addAction(UIAlertAction(title: "Retry", style: .default) { _ in | |
retry.onNext() | |
}) | |
self.base.present(alert, animated: true) | |
} | |
return retry | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment