Created
October 7, 2019 12:28
-
-
Save pimnijman/bb45172116576c767da3c6a405ca5eaf to your computer and use it in GitHub Desktop.
ReactiveExtension to create a bidirectional binding with a CosmosView rating control
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
// | |
// ReactiveExtensionsCosmos.swift | |
// Route.nl | |
// | |
// Created by Pim Nijman on 07/09/2019. | |
// Copyright © 2019 Falk. All rights reserved. | |
// | |
import Bond | |
import ReactiveKit | |
import Cosmos // https://github.com/evgenyneu/Cosmos | |
extension ReactiveExtensions where Base: CosmosView { | |
public var rating: DynamicSubject<Double> { | |
return dynamicSubject( | |
signal: didTouchCosmosSignal, | |
get: { $0.rating }, | |
set: { $0.rating = $1 } | |
) | |
} | |
public var didTouchCosmosSignal: SafeSignal<Void> { | |
let base = self.base | |
return Signal { [weak base] observer in | |
guard let base = base else { | |
observer.completed() | |
return NonDisposable.instance | |
} | |
base.didTouchCosmos = { _ in | |
observer.next(()) | |
} | |
return BlockDisposable { | |
base.didTouchCosmos = nil | |
} | |
}.take(until: base.deallocated) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment