Guest User

Untitled

a guest
Aug 14th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. //
  2. // CustomExtensions.swift
  3. // Pods-AARxSwiftExtensions
  4. //
  5. // Created by Arlind on 8/12/18.
  6. //
  7.  
  8. import UIKit
  9. import RxSwift
  10. import CoreLocation
  11. import RxCocoa
  12.  
  13. extension Reactive where Base: NotificationCenter {
  14. public func notification(_ name: Notification.Name, object: AnyObject? = nil) -> Observable<Notification> {
  15. return Observable.create { notificationObserver in
  16. let observer = self.base.addObserver(forName: name, object: object, queue: nil) { notification in
  17. notificationObserver.onNext(notification)
  18. }
  19. return Disposables.create {
  20. self.base.removeObserver(observer)
  21. }
  22. }
  23. }
  24. }
  25.  
  26. class RxUIScrollViewDelegateProxy: DelegateProxy<UIScrollView,
  27. UIScrollViewDelegate>, DelegateProxyType, UIScrollViewDelegate
  28. {
  29. static func registerKnownImplementations() {
  30. self.register { RxUIScrollViewDelegateProxy(scollView: $0) }
  31. }
  32.  
  33. public weak private(set) var scollView: UIScrollView?
  34. public init(scollView: ParentObject) {
  35. self.scollView = scollView
  36. super.init(parentObject: scollView, delegateProxy:
  37. RxUIScrollViewDelegateProxy.self)
  38. }
  39. }
  40.  
  41. extension Reactive where Base: UIScrollView {
  42. public var delegate: DelegateProxy<UIScrollView,
  43. UIScrollViewDelegate> {
  44. return RxUIScrollViewDelegateProxy.proxy(for: base)
  45. }
  46.  
  47. var contentOffset: Observable<CGPoint> {
  48. return
  49. delegate.methodInvoked(#selector(UIScrollViewDelegate.scrollViewDidScroll(_:)))
  50. .map { obcjects in
  51. let scrollView = obcjects[0] as! UIScrollView
  52. return scrollView.contentOffset
  53. }
  54. }
  55. }
  56.  
  57. extension UIScrollView {
  58. func bindKeyboardEvents() -> Disposable {
  59. let observable =
  60. Observable.from([
  61. NotificationCenter.default.rx.notification(Notification.Name.UIKeyboardWillShow) .map { notification in
  62. let keyboardFrame = notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! CGRect
  63. let keyboardHeight: CGFloat = keyboardFrame.height
  64. return keyboardHeight
  65. },
  66. NotificationCenter.default.rx.notification(Notification.Name.UIKeyboardWillHide) .map { _ in CGFloat(0) }])
  67. .merge()
  68. .observeOn(MainScheduler.instance)
  69.  
  70. return observable.subscribe(onNext: { [weak self] keyboardHeight in
  71. self?.contentInset.bottom = keyboardHeight
  72. self?.scrollIndicatorInsets.bottom = keyboardHeight
  73. })
  74. }
  75.  
  76. func loadNextPageObservable() -> Observable<Void> {
  77. return rx.contentOffset
  78. .skipWhile {
  79. $0.y < self.frame.size.height
  80. }.map { contentOffset -> Bool in
  81. let padding = CGFloat(20.0)
  82. return contentOffset.y + self.frame.size.height + padding > self.contentSize.height
  83. }.distinctUntilChanged().map { shouldLoad in
  84. if shouldLoad {
  85. Void()
  86. }
  87. }
  88. }
  89. }
  90.  
  91. extension Date {
  92. func formattedForView() -> String {
  93. let formatter = DateFormatter()
  94. formatter.dateFormat = "MM/dd/yyyy"
  95. return formatter.string(from: self)
  96. }
  97. }
Add Comment
Please, Sign In to add comment