From c2403a4bf1ba39eaddcceac986c9b0252b15c1b7 Mon Sep 17 00:00:00 2001 From: Ian Ynda-Hummel Date: Tue, 2 May 2017 15:04:46 -0400 Subject: [PATCH] Factor out interactive pan gesture to into separate subspec The interactive behavior relies on UIApplication, which is not accessible from extensions. This change allows for projects to import only the non-interactive components to allow for use in extensions. By default, all subspecs in a podspec are used so existing projects will remain unchanged. --- RxKeyboard.podspec | 9 ++++++- RxKeyboard.xcodeproj/project.pbxproj | 4 ++++ Sources/RxKeyboard.swift | 35 ++++++++++++++++++---------- Sources/RxKeyboardInteractive.swift | 24 +++++++++++++++++++ 4 files changed, 59 insertions(+), 13 deletions(-) create mode 100644 Sources/RxKeyboardInteractive.swift diff --git a/RxKeyboard.podspec b/RxKeyboard.podspec index 6a20a01..73afad4 100644 --- a/RxKeyboard.podspec +++ b/RxKeyboard.podspec @@ -7,13 +7,20 @@ Pod::Spec.new do |s| s.author = { 'Suyeol Jeon' => 'devxoul@gmail.com' } s.source = { :git => 'https://github.com/RxSwiftCommunity/RxKeyboard.git', :tag => s.version.to_s } - s.source_files = 'Sources/*.swift' s.frameworks = 'UIKit', 'Foundation' s.requires_arc = true s.dependency 'RxSwift', '>= 3.0' s.dependency 'RxCocoa', '>= 3.0' + s.subspec 'Core' do |core| + core.source_files = 'Sources/RxKeyboard.swift' + end + + s.subspec 'Interactive' do |interactive| + interactive.source_files = 'Sources/RxKeyboardInteractive.swift' + end + s.ios.deployment_target = '8.0' s.pod_target_xcconfig = { diff --git a/RxKeyboard.xcodeproj/project.pbxproj b/RxKeyboard.xcodeproj/project.pbxproj index 4145ca3..d232c8e 100644 --- a/RxKeyboard.xcodeproj/project.pbxproj +++ b/RxKeyboard.xcodeproj/project.pbxproj @@ -11,6 +11,7 @@ 0310C6771DA9535200BDA512 /* RxKeyboard.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0310C6761DA9535200BDA512 /* RxKeyboard.swift */; }; 03CE448E1E35AAE5006DD6E9 /* RxCocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 034E7CA31DAD548900CFC398 /* RxCocoa.framework */; }; 03CE448F1E35AAE5006DD6E9 /* RxSwift.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 034E7CA41DAD548900CFC398 /* RxSwift.framework */; }; + 407629F61EB906710051197B /* RxKeyboardInteractive.swift in Sources */ = {isa = PBXBuildFile; fileRef = 407629F51EB906710051197B /* RxKeyboardInteractive.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -22,6 +23,7 @@ 034E7CA41DAD548900CFC398 /* RxSwift.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = RxSwift.framework; path = Carthage/Build/iOS/RxSwift.framework; sourceTree = ""; }; 034E7CA51DAD548900CFC398 /* RxTests.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = RxTests.framework; path = Carthage/Build/iOS/RxTests.framework; sourceTree = ""; }; 03A258CB1DA94EC50035A85B /* RxKeyboard.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = RxKeyboard.framework; sourceTree = BUILT_PRODUCTS_DIR; }; + 407629F51EB906710051197B /* RxKeyboardInteractive.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RxKeyboardInteractive.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -42,6 +44,7 @@ children = ( 0310C6711DA9525200BDA512 /* RxKeyboard.h */, 0310C6761DA9535200BDA512 /* RxKeyboard.swift */, + 407629F51EB906710051197B /* RxKeyboardInteractive.swift */, ); path = Sources; sourceTree = ""; @@ -167,6 +170,7 @@ buildActionMask = 2147483647; files = ( 0310C6771DA9535200BDA512 /* RxKeyboard.swift in Sources */, + 407629F61EB906710051197B /* RxKeyboardInteractive.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Sources/RxKeyboard.swift b/Sources/RxKeyboard.swift index ff16d85..50941c6 100644 --- a/Sources/RxKeyboard.swift +++ b/Sources/RxKeyboard.swift @@ -30,11 +30,10 @@ public class RxKeyboard: NSObject { /// useful when adjusting scroll view content offset. public let willShowVisibleHeight: Driver + // MARK: Internal - // MARK: Private - - fileprivate let disposeBag = DisposeBag() - fileprivate let panRecognizer = UIPanGestureRecognizer() + internal let disposeBag = DisposeBag() + internal let panRecognizer = UIPanGestureRecognizer() // MARK: Initializing @@ -93,7 +92,7 @@ public class RxKeyboard: NSObject { .withLatestFrom(frameVariable.asObservable()) { ($0, $1) } .flatMap { (gestureRecognizer, frame) -> Observable in guard case .changed = gestureRecognizer.state, - let window = UIApplication.shared.windows.first, + let window = gestureRecognizer.view, frame.origin.y < UIScreen.main.bounds.height else { return .empty() } let origin = gestureRecognizer.location(in: window) @@ -109,13 +108,7 @@ public class RxKeyboard: NSObject { // gesture recognizer self.panRecognizer.delegate = self - NotificationCenter.default.rx.notification(.UIApplicationDidFinishLaunching) - .map { _ in Void() } - .startWith(Void()) // when RxKeyboard is initialized before UIApplication.window is created - .subscribe(onNext: { _ in - UIApplication.shared.windows.first?.addGestureRecognizer(self.panRecognizer) - }) - .addDisposableTo(self.disposeBag) + self.attachPanRecognizer() } } @@ -149,3 +142,21 @@ extension RxKeyboard: UIGestureRecognizerDelegate { } } + +// MARK: - Interactive + +internal protocol RxKeyboardInteractive { + + var panRecognizer: UIPanGestureRecognizer { get } + var disposeBag: DisposeBag { get } + func attachPanRecognizer() + +} + +internal extension RxKeyboardInteractive { + + func attachPanRecognizer() {} + +} + +extension RxKeyboard: RxKeyboardInteractive {} diff --git a/Sources/RxKeyboardInteractive.swift b/Sources/RxKeyboardInteractive.swift new file mode 100644 index 0000000..b7eff49 --- /dev/null +++ b/Sources/RxKeyboardInteractive.swift @@ -0,0 +1,24 @@ +// +// RxKeyboardInteractive.swift +// RxKeyboard +// +// Created by Ian Ynda-Hummel on 5/2/17. +// Copyright © 2017 Suyeol Jeon. All rights reserved. +// + +import UIKit + +import RxCocoa +import RxSwift + +internal extension RxKeyboard { + internal func attachPanRecognizer() { + NotificationCenter.default.rx.notification(.UIApplicationDidFinishLaunching) + .map { _ in Void() } + .startWith(Void()) // when RxKeyboard is initialized before UIApplication.window is created + .subscribe(onNext: { _ in + UIApplication.shared.windows.first?.addGestureRecognizer(self.panRecognizer) + }) + .addDisposableTo(self.disposeBag) + } +}