diff --git a/src/reactivetypes/Reactive+UIButton.swift b/src/reactivetypes/Reactive+UIButton.swift index 363a998..835c341 100644 --- a/src/reactivetypes/Reactive+UIButton.swift +++ b/src/reactivetypes/Reactive+UIButton.swift @@ -1,9 +1,58 @@ -// -// Reactive+UIButton.swift -// Pods -// -// Created by Jeff Verkoeyen on 5/11/17. -// -// +/* + Copyright 2017-present The Material Motion Authors. All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + */ import Foundation +import UIKit + +/** + A reactive button target exposes streams for certain button events. + */ +public final class ReactiveButtonTarget: NSObject { + public init(_ button: UIButton) { + super.init() + + button.addTarget(self, action: #selector(didEnterEvent), + for: [.touchDown, .touchDragInside]) + button.addTarget(self, action: #selector(didExitEvent), + for: [.touchUpInside, .touchUpOutside, .touchDragOutside]) + } + + // MARK: Streams + + /** + Emits true when the button should be highlighted and false when it should not. + */ + public var didHighlight: MotionObservable { + return MotionObservable { observer in + self.didHighlightObservers.append(observer) + return { + if let index = self.didHighlightObservers.index(where: { $0 === observer }) { + self.didHighlightObservers.remove(at: index) + } + } + } + } + + func didEnterEvent(_ button: UIButton) { + didHighlightObservers.forEach { $0.next(true) } + } + func didExitEvent(_ button: UIButton) { + didHighlightObservers.forEach { $0.next(false) } + } + private var didHighlightObservers: [MotionObserver] = [] + + public let metadata = Metadata("Button target") +}