Created
August 14, 2018 08:49
-
-
Save Arrlindii/f21ce40da5e6f62414af12698c1e20d1 to your computer and use it in GitHub Desktop.
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
| // | |
| // CustomExtensions.swift | |
| // Pods-AARxSwiftExtensions | |
| // | |
| // Created by Arlind on 8/12/18. | |
| // | |
| import UIKit | |
| import RxSwift | |
| import CoreLocation | |
| import RxCocoa | |
| extension Reactive where Base: NotificationCenter { | |
| public func notification(_ name: Notification.Name, object: AnyObject? = nil) -> Observable<Notification> { | |
| return Observable.create { notificationObserver in | |
| let observer = self.base.addObserver(forName: name, object: object, queue: nil) { notification in | |
| notificationObserver.onNext(notification) | |
| } | |
| return Disposables.create { | |
| self.base.removeObserver(observer) | |
| } | |
| } | |
| } | |
| } | |
| class RxUIScrollViewDelegateProxy: DelegateProxy<UIScrollView, | |
| UIScrollViewDelegate>, DelegateProxyType, UIScrollViewDelegate | |
| { | |
| static func registerKnownImplementations() { | |
| self.register { RxUIScrollViewDelegateProxy(scollView: $0) } | |
| } | |
| public weak private(set) var scollView: UIScrollView? | |
| public init(scollView: ParentObject) { | |
| self.scollView = scollView | |
| super.init(parentObject: scollView, delegateProxy: | |
| RxUIScrollViewDelegateProxy.self) | |
| } | |
| } | |
| extension Reactive where Base: UIScrollView { | |
| public var delegate: DelegateProxy<UIScrollView, | |
| UIScrollViewDelegate> { | |
| return RxUIScrollViewDelegateProxy.proxy(for: base) | |
| } | |
| var contentOffset: Observable<CGPoint> { | |
| return | |
| delegate.methodInvoked(#selector(UIScrollViewDelegate.scrollViewDidScroll(_:))) | |
| .map { obcjects in | |
| let scrollView = obcjects[0] as! UIScrollView | |
| return scrollView.contentOffset | |
| } | |
| } | |
| } | |
| extension UIScrollView { | |
| func bindKeyboardEvents() -> Disposable { | |
| let observable = | |
| Observable.from([ | |
| NotificationCenter.default.rx.notification(Notification.Name.UIKeyboardWillShow) .map { notification in | |
| let keyboardFrame = notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! CGRect | |
| let keyboardHeight: CGFloat = keyboardFrame.height | |
| return keyboardHeight | |
| }, | |
| NotificationCenter.default.rx.notification(Notification.Name.UIKeyboardWillHide) .map { _ in CGFloat(0) }]) | |
| .merge() | |
| .observeOn(MainScheduler.instance) | |
| return observable.subscribe(onNext: { [weak self] keyboardHeight in | |
| self?.contentInset.bottom = keyboardHeight | |
| self?.scrollIndicatorInsets.bottom = keyboardHeight | |
| }) | |
| } | |
| func loadNextPageObservable() -> Observable<Void> { | |
| return rx.contentOffset | |
| .skipWhile { | |
| $0.y < self.frame.size.height | |
| }.map { contentOffset -> Bool in | |
| let padding = CGFloat(20.0) | |
| return contentOffset.y + self.frame.size.height + padding > self.contentSize.height | |
| }.distinctUntilChanged().map { shouldLoad in | |
| if shouldLoad { | |
| Void() | |
| } | |
| } | |
| } | |
| } | |
| extension Date { | |
| func formattedForView() -> String { | |
| let formatter = DateFormatter() | |
| formatter.dateFormat = "MM/dd/yyyy" | |
| return formatter.string(from: self) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment