Permalink
Browse files

Update code for Swift 4.2 / Xcode 10

  • Loading branch information...
freak4pc committed Sep 21, 2018
1 parent eaee342 commit 0c19e9dc1014834d31014b6ac4643dab8a0d1469
Showing with 21 additions and 10 deletions.
  1. +2 −2 Package.resolved
  2. +2 −2 Package.swift
  3. +17 −6 Sources/RxKeyboard/RxKeyboard.swift
View
@@ -6,8 +6,8 @@
"repositoryURL": "https://github.com/ReactiveX/RxSwift.git",
"state": {
"branch": null,
"revision": "3e848781c7756accced855a6317a4c2ff5e8588b",
"version": "4.1.2"
"revision": "0df62b4d562f8620d4b795b18e4adf0b631527a1",
"version": "4.3.1"
}
}
]
View
@@ -1,4 +1,4 @@
// swift-tools-version:4.1
// swift-tools-version:4.2
import PackageDescription
@@ -8,7 +8,7 @@ let package = Package(
.library(name: "RxKeyboard", targets: ["RxKeyboard"]),
],
dependencies: [
.package(url: "https://github.com/ReactiveX/RxSwift.git", .upToNextMajor(from: "4.1.0")),
.package(url: "https://github.com/ReactiveX/RxSwift.git", .upToNextMajor(from: "4.3.0")),
],
targets: [
.target(name: "RxKeyboard", dependencies: ["RxSwift", "RxCocoa"]),
@@ -47,10 +47,21 @@ public class RxKeyboard: NSObject, RxKeyboardType {
private let disposeBag = DisposeBag()
private let panRecognizer = UIPanGestureRecognizer()
// MARK: Initializing
override init() {
#if swift(>=4.2)
let keyboardWillChangeFrame = UIResponder.keyboardWillChangeFrameNotification
let keyboardWillHide = UIResponder.keyboardWillHideNotification
let keyboardFrameEndKey = UIResponder.keyboardFrameEndUserInfoKey
let applicationDidFinishLaunching = UIApplication.didFinishLaunchingNotification
#else
let keyboardWillChangeFrame = NSNotification.Name.UIKeyboardWillChangeFrame
let keyboardWillHide = NSNotification.Name.UIKeyboardWillHide
let keyboardFrameEndKey = UIKeyboardFrameEndUserInfoKey
let applicationDidFinishLaunching = NSNotification.Name.UIApplicationDidFinishLaunching
#endif
let defaultFrame = CGRect(
x: 0,
y: UIScreen.main.bounds.height,
@@ -70,9 +81,9 @@ public class RxKeyboard: NSObject, RxKeyboardType {
super.init()
// keyboard will change frame
let willChangeFrame = NotificationCenter.default.rx.notification(.UIKeyboardWillChangeFrame)
let willChangeFrame = NotificationCenter.default.rx.notification(keyboardWillChangeFrame)
.map { notification -> CGRect in
let rectValue = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue
let rectValue = notification.userInfo?[keyboardFrameEndKey] as? NSValue
return rectValue?.cgRectValue ?? defaultFrame
}
.map { frame -> CGRect in
@@ -85,9 +96,9 @@ public class RxKeyboard: NSObject, RxKeyboardType {
}
// keyboard will hide
let willHide = NotificationCenter.default.rx.notification(.UIKeyboardWillHide)
let willHide = NotificationCenter.default.rx.notification(keyboardWillHide)
.map { notification -> CGRect in
let rectValue = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue
let rectValue = notification.userInfo?[keyboardFrameEndKey] as? NSValue
return rectValue?.cgRectValue ?? defaultFrame
}
.map { frame -> CGRect in
@@ -120,7 +131,7 @@ public class RxKeyboard: NSObject, RxKeyboardType {
// gesture recognizer
self.panRecognizer.delegate = self
NotificationCenter.default.rx.notification(.UIApplicationDidFinishLaunching)
NotificationCenter.default.rx.notification(applicationDidFinishLaunching)
.map { _ in Void() }
.startWith(Void()) // when RxKeyboard is initialized before UIApplication.window is created
.subscribe(onNext: { _ in

0 comments on commit 0c19e9d

Please sign in to comment.