Skip to content

Factor out interactive pan gesture to into separate subspec #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion RxKeyboard.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@ Pod::Spec.new do |s|
s.author = { 'Suyeol Jeon' => '[email protected]' }
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 = {
Expand Down
4 changes: 4 additions & 0 deletions RxKeyboard.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -22,6 +23,7 @@
034E7CA41DAD548900CFC398 /* RxSwift.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = RxSwift.framework; path = Carthage/Build/iOS/RxSwift.framework; sourceTree = "<group>"; };
034E7CA51DAD548900CFC398 /* RxTests.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = RxTests.framework; path = Carthage/Build/iOS/RxTests.framework; sourceTree = "<group>"; };
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 = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand All @@ -42,6 +44,7 @@
children = (
0310C6711DA9525200BDA512 /* RxKeyboard.h */,
0310C6761DA9535200BDA512 /* RxKeyboard.swift */,
407629F51EB906710051197B /* RxKeyboardInteractive.swift */,
);
path = Sources;
sourceTree = "<group>";
Expand Down Expand Up @@ -167,6 +170,7 @@
buildActionMask = 2147483647;
files = (
0310C6771DA9535200BDA512 /* RxKeyboard.swift in Sources */,
407629F61EB906710051197B /* RxKeyboardInteractive.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
35 changes: 23 additions & 12 deletions Sources/RxKeyboard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@ public class RxKeyboard: NSObject {
/// useful when adjusting scroll view content offset.
public let willShowVisibleHeight: Driver<CGFloat>

// MARK: Internal

// MARK: Private

fileprivate let disposeBag = DisposeBag()
fileprivate let panRecognizer = UIPanGestureRecognizer()
internal let disposeBag = DisposeBag()
internal let panRecognizer = UIPanGestureRecognizer()


// MARK: Initializing
Expand Down Expand Up @@ -93,7 +92,7 @@ public class RxKeyboard: NSObject {
.withLatestFrom(frameVariable.asObservable()) { ($0, $1) }
.flatMap { (gestureRecognizer, frame) -> Observable<CGRect> 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)
Expand All @@ -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()
}

}
Expand Down Expand Up @@ -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 {}
24 changes: 24 additions & 0 deletions Sources/RxKeyboardInteractive.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}