Last active
March 17, 2019 17:23
-
-
Save Ankit-Aggarwal/ac0086e051d7513e0764eb2d63629397 to your computer and use it in GitHub Desktop.
Adding additional feature to Reactive Struct and BindingTargetProvider present in Reactive Swift
This file contains 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+Extension.swift | |
// | |
// Created by Ankit Aggarwal on 03/01/17. | |
// Copyright © 2017 Ankit Aggarwal. All rights reserved. | |
// | |
// Explanation can be found in my medium blog here : | |
import UIKit | |
import ReactiveSwift | |
import Result | |
extension Reactive where Base: NSObjectProtocol { | |
func makeBindingTarget<U>(_ key: StaticString = #function, action: @escaping (Base, U) -> Void) -> BindingTarget<U> { | |
return BindingTarget(on: UIScheduler(), lifetime: lifetime) { [weak base = self.base] value in | |
if let base = base { | |
action(base, value) | |
} | |
} | |
} | |
} | |
extension Reactive where Base: UIView { | |
public var showLoader: BindingTarget<Bool> { | |
return makeBindingTarget(action: { (view, showLoader) in | |
if showLoader { | |
view.showLoader() | |
} else { | |
view.hideLoader() | |
} | |
}) | |
} | |
} | |
extension Optional where Wrapped == BindingTargetProvider { | |
@discardableResult | |
public static func <~ | |
<Source: BindingSource, Target: BindingTargetProvider> | |
(provider1: Target?, source: Source) -> Disposable? | |
where Source.Value == Target.Value, Source.Error == NoError { | |
guard let provider = provider1 else { return nil } | |
return provider <~ source | |
} | |
@discardableResult | |
public static func <~ | |
<Source: BindingSource, Target: BindingTargetProvider> | |
(provider1: Target?, source: Source) -> Disposable? | |
where Target.Value == Source.Value?, Source.Error == NoError { | |
guard let provider = provider1 else { return nil } | |
return provider <~ source | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment