From f439ed05bf77aedd34c2e1285b8e2b6158d30b81 Mon Sep 17 00:00:00 2001 From: Alexey Korolev Date: Fri, 12 Jul 2019 13:50:14 +0300 Subject: [PATCH] Convert code to Swift 5 (#32) --- KeyboardManager.xcodeproj/project.pbxproj | 4 +- Sources/KeyboardManager.swift | 50 +++++++++---------- Tests/Helpers.swift | 19 ++++--- .../KeyboardManager+NotificationCenter.swift | 16 +++--- Tests/KeyboardManager+ScrollView.swift | 33 ++++++------ Tests/KeyboardManagerTests.swift | 40 +++++++-------- project.yml | 2 +- 7 files changed, 78 insertions(+), 86 deletions(-) diff --git a/KeyboardManager.xcodeproj/project.pbxproj b/KeyboardManager.xcodeproj/project.pbxproj index 77caf8c..1823fa9 100644 --- a/KeyboardManager.xcodeproj/project.pbxproj +++ b/KeyboardManager.xcodeproj/project.pbxproj @@ -291,7 +291,7 @@ SDKROOT = iphoneos; SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; - SWIFT_VERSION = 4.0; + SWIFT_VERSION = 5.0; }; name = Debug; }; @@ -366,7 +366,7 @@ PRODUCT_NAME = "$(TARGET_NAME)"; SDKROOT = iphoneos; SWIFT_COMPILATION_MODE = wholemodule; - SWIFT_VERSION = 4.0; + SWIFT_VERSION = 5.0; VALIDATE_PRODUCT = YES; }; name = Release; diff --git a/Sources/KeyboardManager.swift b/Sources/KeyboardManager.swift index 785a42d..983f141 100644 --- a/Sources/KeyboardManager.swift +++ b/Sources/KeyboardManager.swift @@ -153,34 +153,28 @@ public final class KeyboardManager { notificationCenter.addObserver(self, selector: #selector(keyboardWillShow(_:)), - name: Notification.Name.UIKeyboardWillShow, - object: nil - ) + name: UIResponder.keyboardWillShowNotification, + object: nil) notificationCenter.addObserver(self, selector: #selector(keyboardDidShow(_:)), - name: Notification.Name.UIKeyboardDidShow, - object: nil - ) + name: UIResponder.keyboardDidShowNotification, + object: nil) notificationCenter.addObserver(self, selector: #selector(keyboardWillHide(_:)), - name: Notification.Name.UIKeyboardWillHide, - object: nil - ) + name: UIResponder.keyboardWillHideNotification, + object: nil) notificationCenter.addObserver(self, selector: #selector(keyboardDidHide(_:)), - name: Notification.Name.UIKeyboardDidHide, - object: nil - ) + name: UIResponder.keyboardDidHideNotification, + object: nil) notificationCenter.addObserver(self, selector: #selector(keyboardWillChangeFrame(_:)), - name: Notification.Name.UIKeyboardWillChangeFrame, - object: nil - ) + name: UIResponder.keyboardWillChangeFrameNotification, + object: nil) notificationCenter.addObserver(self, selector: #selector(keyboardDidChangeFrame(_:)), - name: Notification.Name.UIKeyboardDidChangeFrame, - object: nil - ) + name: UIResponder.keyboardDidChangeFrameNotification, + object: nil) } deinit { @@ -225,11 +219,11 @@ public final class KeyboardManager { } private func extractData(from notification: Notification) -> KeyboardManagerEvent.Data { - guard let endFrame = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue, - let beginFrame = notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue, - let curve = notification.userInfo?[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber, - let isLocal = notification.userInfo?[UIKeyboardIsLocalUserInfoKey] as? NSNumber, - let duration = notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber else { + guard let endFrame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue, + let beginFrame = notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue, + let curve = notification.userInfo?[UIResponder.keyboardAnimationCurveUserInfoKey] as? NSNumber, + let isLocal = notification.userInfo?[UIResponder.keyboardIsLocalUserInfoKey] as? NSNumber, + let duration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? NSNumber else { return KeyboardManagerEvent.Data.null() } let frame = KeyboardManagerEvent.Frame(begin: beginFrame.cgRectValue, end: endFrame.cgRectValue) @@ -285,18 +279,20 @@ extension KeyboardManager: KeyboardManagerProtocol { UIView.animateKeyframes( withDuration: data.animationDuration, delay: 0, - options: UIViewKeyframeAnimationOptions(rawValue: UInt(data.animationCurve)), + options: UIView.KeyframeAnimationOptions(rawValue: UInt(data.animationCurve)), animations: { scrollView.contentInset.bottom = initialInset.bottom + data.frame.end.size.height - }) + } + ) case let .willHide(data): UIView.animateKeyframes( withDuration: data.animationDuration, delay: 0, - options: UIViewKeyframeAnimationOptions(rawValue: UInt(data.animationCurve)), + options: UIView.KeyframeAnimationOptions(rawValue: UInt(data.animationCurve)), animations: { scrollView.contentInset.bottom = initialInset.bottom - }) + } + ) default: break } diff --git a/Tests/Helpers.swift b/Tests/Helpers.swift index ec6593d..33fdbe6 100644 --- a/Tests/Helpers.swift +++ b/Tests/Helpers.swift @@ -3,25 +3,24 @@ // Copyright (c) 2017 Alexey Korolev. All rights reserved. // -import UIKit import KeyboardManager +import UIKit extension KeyboardManagerTests { - func postTestNotification(name: Notification.Name) { notificationCenter.post(name: name, object: nil, userInfo: [ - UIKeyboardFrameEndUserInfoKey: NSValue(cgRect: endFrame), - UIKeyboardFrameBeginUserInfoKey: NSValue(cgRect: beginFrame), - UIKeyboardAnimationDurationUserInfoKey: animationDuration, - UIKeyboardIsLocalUserInfoKey: isLocal, - UIKeyboardAnimationCurveUserInfoKey: curve, + UIResponder.keyboardFrameEndUserInfoKey: NSValue(cgRect: endFrame), + UIResponder.keyboardFrameBeginUserInfoKey: NSValue(cgRect: beginFrame), + UIResponder.keyboardAnimationDurationUserInfoKey: animationDuration, + UIResponder.keyboardIsLocalUserInfoKey: isLocal, + UIResponder.keyboardAnimationCurveUserInfoKey: curve, ]) } func postWrongTestNotification() { - notificationCenter.post(name: Notification.Name.UIKeyboardDidShow, object: nil, userInfo: [ - UIKeyboardFrameEndUserInfoKey: NSValue(cgRect: endFrame), - UIKeyboardAnimationCurveUserInfoKey: 10, + notificationCenter.post(name: UIResponder.keyboardDidShowNotification, object: nil, userInfo: [ + UIResponder.keyboardFrameEndUserInfoKey: NSValue(cgRect: endFrame), + UIResponder.keyboardAnimationCurveUserInfoKey: 10, ]) } diff --git a/Tests/KeyboardManager+NotificationCenter.swift b/Tests/KeyboardManager+NotificationCenter.swift index a8389e4..23d1543 100644 --- a/Tests/KeyboardManager+NotificationCenter.swift +++ b/Tests/KeyboardManager+NotificationCenter.swift @@ -5,11 +5,10 @@ import UIKit -import XCTest @testable import KeyboardManager +import XCTest class KeyboardManagerNotificationCenterTest: XCTestCase { - private var notificationCenter: NotificationCenterMock! var keyboardManager: KeyboardManagerProtocol! @@ -57,7 +56,6 @@ class KeyboardManagerNotificationCenterTest: XCTestCase { // swiftlint:disable force_unwrapping private class NotificationCenterMock: NotificationCenter { - var isWillShow: Bool = false var isDidShow: Bool = false var isWillHide: Bool = false @@ -67,17 +65,17 @@ private class NotificationCenterMock: NotificationCenter { var isUnsubscribed: Bool = false override func addObserver(_: Any, selector _: Selector, name aName: NSNotification.Name?, object _: Any?) { - if case Notification.Name.UIKeyboardWillShow = aName! { + if case UIResponder.keyboardWillShowNotification = aName! { isWillShow = true - } else if case Notification.Name.UIKeyboardDidShow = aName! { + } else if case UIResponder.keyboardDidShowNotification = aName! { isDidShow = true - } else if case Notification.Name.UIKeyboardWillHide = aName! { + } else if case UIResponder.keyboardWillHideNotification = aName! { isWillHide = true - } else if case Notification.Name.UIKeyboardDidHide = aName! { + } else if case UIResponder.keyboardDidHideNotification = aName! { isDidHide = true - } else if case Notification.Name.UIKeyboardWillChangeFrame = aName! { + } else if case UIResponder.keyboardWillChangeFrameNotification = aName! { isWillChangeFrame = true - } else if case Notification.Name.UIKeyboardDidChangeFrame = aName! { + } else if case UIResponder.keyboardDidChangeFrameNotification = aName! { isDidChangeFrame = true } } diff --git a/Tests/KeyboardManager+ScrollView.swift b/Tests/KeyboardManager+ScrollView.swift index f891228..d63f19d 100644 --- a/Tests/KeyboardManager+ScrollView.swift +++ b/Tests/KeyboardManager+ScrollView.swift @@ -3,12 +3,11 @@ // Copyright (c) 2017 Alexey Korolev. All rights reserved. // -import XCTest -import UIKit @testable import KeyboardManager +import UIKit +import XCTest extension KeyboardManagerTests { - func testScrollViewInsetAdjustingAfterKeyboardAppear() { // GIVEN let scrollView = UIScrollView() @@ -16,7 +15,7 @@ extension KeyboardManagerTests { scrollView.contentInset = initialInsets // WHEN keyboardManager.bindToKeyboardNotifications(scrollView: scrollView) - postTestNotification(name: Notification.Name.UIKeyboardWillShow) + postTestNotification(name: UIResponder.keyboardWillShowNotification) // THEN XCTAssertEqual(scrollView.contentInset.bottom, initialInsets.bottom + endFrame.height) } @@ -28,7 +27,7 @@ extension KeyboardManagerTests { scrollView.contentInset = initialInsets // WHEN keyboardManager.bindToKeyboardNotifications(scrollView: scrollView) - postTestNotification(name: Notification.Name.UIKeyboardWillChangeFrame) + postTestNotification(name: UIResponder.keyboardWillChangeFrameNotification) // THEN XCTAssertEqual(scrollView.contentInset.bottom, initialInsets.bottom + endFrame.height) } @@ -40,8 +39,8 @@ extension KeyboardManagerTests { scrollView.contentInset = initialInsets // WHEN keyboardManager.bindToKeyboardNotifications(scrollView: scrollView) - postTestNotification(name: Notification.Name.UIKeyboardWillShow) - postTestNotification(name: Notification.Name.UIKeyboardWillShow) + postTestNotification(name: UIResponder.keyboardWillShowNotification) + postTestNotification(name: UIResponder.keyboardWillShowNotification) // THEN XCTAssertEqual(scrollView.contentInset.bottom, initialInsets.bottom + endFrame.height) } @@ -53,8 +52,8 @@ extension KeyboardManagerTests { scrollView.contentInset = initialInsets // WHEN keyboardManager.bindToKeyboardNotifications(scrollView: scrollView) - postTestNotification(name: Notification.Name.UIKeyboardWillShow) - postTestNotification(name: Notification.Name.UIKeyboardWillChangeFrame) + postTestNotification(name: UIResponder.keyboardWillShowNotification) + postTestNotification(name: UIResponder.keyboardWillChangeFrameNotification) // THEN XCTAssertEqual(scrollView.contentInset.bottom, initialInsets.bottom + endFrame.height) } @@ -66,8 +65,8 @@ extension KeyboardManagerTests { scrollView.contentInset = initialInsets // WHEN keyboardManager.bindToKeyboardNotifications(scrollView: scrollView) - postTestNotification(name: Notification.Name.UIKeyboardWillShow) - postTestNotification(name: Notification.Name.UIKeyboardWillHide) + postTestNotification(name: UIResponder.keyboardWillShowNotification) + postTestNotification(name: UIResponder.keyboardWillHideNotification) // THEN XCTAssertEqual(scrollView.contentInset, initialInsets) } @@ -79,9 +78,9 @@ extension KeyboardManagerTests { scrollView.contentInset = initialInsets // WHEN keyboardManager.bindToKeyboardNotifications(scrollView: scrollView) - postTestNotification(name: Notification.Name.UIKeyboardWillShow) - postTestNotification(name: Notification.Name.UIKeyboardWillHide) - postTestNotification(name: Notification.Name.UIKeyboardWillHide) + postTestNotification(name: UIResponder.keyboardWillShowNotification) + postTestNotification(name: UIResponder.keyboardWillHideNotification) + postTestNotification(name: UIResponder.keyboardWillHideNotification) // THEN XCTAssertEqual(scrollView.contentInset, initialInsets) } @@ -93,7 +92,7 @@ extension KeyboardManagerTests { scrollView.contentInset = initialInsets // WHEN keyboardManager.bindToKeyboardNotifications(scrollView: scrollView) - postTestNotification(name: Notification.Name.UIKeyboardDidShow) + postTestNotification(name: UIResponder.keyboardDidShowNotification) // THEN XCTAssertEqual(scrollView.contentInset, initialInsets) } @@ -105,7 +104,7 @@ extension KeyboardManagerTests { scrollView.contentInset = initialInsets // WHEN keyboardManager.bindToKeyboardNotifications(scrollView: scrollView) - postTestNotification(name: Notification.Name.UIKeyboardDidChangeFrame) + postTestNotification(name: UIResponder.keyboardDidChangeFrameNotification) // THEN XCTAssertEqual(scrollView.contentInset, initialInsets) } @@ -117,7 +116,7 @@ extension KeyboardManagerTests { scrollView.contentInset = initialInsets // WHEN keyboardManager.bindToKeyboardNotifications(scrollView: scrollView) - postTestNotification(name: Notification.Name.UIKeyboardWillHide) + postTestNotification(name: UIResponder.keyboardWillHideNotification) // THEN XCTAssertEqual(scrollView.contentInset, initialInsets) } diff --git a/Tests/KeyboardManagerTests.swift b/Tests/KeyboardManagerTests.swift index e3251d8..833ee23 100644 --- a/Tests/KeyboardManagerTests.swift +++ b/Tests/KeyboardManagerTests.swift @@ -40,7 +40,7 @@ class KeyboardManagerTests: XCTestCase { isTriggered = true } } - postTestNotification(name: Notification.Name.UIKeyboardWillShow) + postTestNotification(name: UIResponder.keyboardWillShowNotification) XCTAssertTrue(isTriggered) } @@ -52,7 +52,7 @@ class KeyboardManagerTests: XCTestCase { isTriggered = true } } - postTestNotification(name: Notification.Name.UIKeyboardDidShow) + postTestNotification(name: UIResponder.keyboardDidShowNotification) XCTAssertTrue(isTriggered) } @@ -64,7 +64,7 @@ class KeyboardManagerTests: XCTestCase { isTriggered = true } } - postTestNotification(name: Notification.Name.UIKeyboardWillHide) + postTestNotification(name: UIResponder.keyboardWillHideNotification) XCTAssertTrue(isTriggered) } @@ -76,7 +76,7 @@ class KeyboardManagerTests: XCTestCase { isTriggered = true } } - postTestNotification(name: Notification.Name.UIKeyboardDidHide) + postTestNotification(name: UIResponder.keyboardDidHideNotification) XCTAssertTrue(isTriggered) } @@ -88,7 +88,7 @@ class KeyboardManagerTests: XCTestCase { isTriggered = true } } - postTestNotification(name: Notification.Name.UIKeyboardWillChangeFrame) + postTestNotification(name: UIResponder.keyboardWillChangeFrameNotification) XCTAssertTrue(isTriggered) } @@ -100,7 +100,7 @@ class KeyboardManagerTests: XCTestCase { isTriggered = true } } - postTestNotification(name: Notification.Name.UIKeyboardDidChangeFrame) + postTestNotification(name: UIResponder.keyboardDidChangeFrameNotification) XCTAssertTrue(isTriggered) } @@ -124,7 +124,7 @@ class KeyboardManagerTests: XCTestCase { XCTAssertTrue(self.compare(lhs: data, rhs: nullObject)) expectation.fulfill() } - notificationCenter.post(name: Notification.Name.UIKeyboardDidShow, object: nil) + notificationCenter.post(name: UIResponder.keyboardDidShowNotification, object: nil) waitForExpectations(timeout: 5) } @@ -137,7 +137,7 @@ class KeyboardManagerTests: XCTestCase { let bottomOffset: CGFloat = 20.0 // WHEN keyboardManager.bindToKeyboardNotifications(superview: view, bottomConstraint: bottomConstrain, bottomOffset: bottomOffset) - postTestNotification(name: Notification.Name.UIKeyboardWillShow) + postTestNotification(name: UIResponder.keyboardWillShowNotification) // THEN XCTAssertEqual(bottomConstrain.constant, -endFrame.height) } @@ -151,7 +151,7 @@ class KeyboardManagerTests: XCTestCase { let bottomOffset: CGFloat = 20.0 // WHEN keyboardManager.bindToKeyboardNotifications(superview: view, bottomConstraint: bottomConstrain, bottomOffset: bottomOffset) - postTestNotification(name: Notification.Name.UIKeyboardWillChangeFrame) + postTestNotification(name: UIResponder.keyboardWillChangeFrameNotification) // THEN XCTAssertEqual(bottomConstrain.constant, -endFrame.height) } @@ -165,9 +165,9 @@ class KeyboardManagerTests: XCTestCase { let bottomOffset: CGFloat = 20.0 // WHEN keyboardManager.bindToKeyboardNotifications(superview: view, bottomConstraint: bottomConstrain, bottomOffset: bottomOffset) - postTestNotification(name: Notification.Name.UIKeyboardWillShow) - postTestNotification(name: Notification.Name.UIKeyboardWillShow) - postTestNotification(name: Notification.Name.UIKeyboardWillShow) + postTestNotification(name: UIResponder.keyboardWillShowNotification) + postTestNotification(name: UIResponder.keyboardWillShowNotification) + postTestNotification(name: UIResponder.keyboardWillShowNotification) // THEN XCTAssertEqual(bottomConstrain.constant, -endFrame.height) } @@ -181,7 +181,7 @@ class KeyboardManagerTests: XCTestCase { let bottomOffset: CGFloat = 20.0 // WHEN keyboardManager.bindToKeyboardNotifications(superview: view, bottomConstraint: bottomConstrain, bottomOffset: bottomOffset) - postTestNotification(name: Notification.Name.UIKeyboardWillHide) + postTestNotification(name: UIResponder.keyboardWillHideNotification) // THEN XCTAssertEqual(bottomConstrain.constant, -bottomOffset) } @@ -195,9 +195,9 @@ class KeyboardManagerTests: XCTestCase { let bottomOffset: CGFloat = 20.0 // WHEN keyboardManager.bindToKeyboardNotifications(superview: view, bottomConstraint: bottomConstrain, bottomOffset: bottomOffset) - postTestNotification(name: Notification.Name.UIKeyboardWillHide) - postTestNotification(name: Notification.Name.UIKeyboardWillHide) - postTestNotification(name: Notification.Name.UIKeyboardWillHide) + postTestNotification(name: UIResponder.keyboardWillHideNotification) + postTestNotification(name: UIResponder.keyboardWillHideNotification) + postTestNotification(name: UIResponder.keyboardWillHideNotification) // THEN XCTAssertEqual(bottomConstrain.constant, -bottomOffset) } @@ -211,7 +211,7 @@ class KeyboardManagerTests: XCTestCase { let bottomOffset: CGFloat = 20.0 // WHEN keyboardManager.bindToKeyboardNotifications(superview: view, bottomConstraint: bottomConstrain, bottomOffset: bottomOffset) - postTestNotification(name: Notification.Name.UIKeyboardDidShow) + postTestNotification(name: UIResponder.keyboardDidShowNotification) // THEN XCTAssertEqual(bottomConstrain.constant, 0) } @@ -225,7 +225,7 @@ class KeyboardManagerTests: XCTestCase { let bottomOffset: CGFloat = 20.0 // WHEN keyboardManager.bindToKeyboardNotifications(superview: view, bottomConstraint: bottomConstrain, bottomOffset: bottomOffset) - postTestNotification(name: Notification.Name.UIKeyboardDidHide) + postTestNotification(name: UIResponder.keyboardDidHideNotification) // THEN XCTAssertEqual(bottomConstrain.constant, 0) } @@ -239,7 +239,7 @@ class KeyboardManagerTests: XCTestCase { let bottomOffset: CGFloat = 20.0 // WHEN keyboardManager.bindToKeyboardNotifications(superview: view, bottomConstraint: bottomConstrain, bottomOffset: bottomOffset) - postTestNotification(name: Notification.Name.UIKeyboardDidChangeFrame) + postTestNotification(name: UIResponder.keyboardDidChangeFrameNotification) // THEN XCTAssertEqual(bottomConstrain.constant, 0) } @@ -259,7 +259,7 @@ class KeyboardManagerTests: XCTestCase { // WHEN keyboardManager.bindToKeyboardNotifications(scrollView: scrollView) keyboardManager.bindToKeyboardNotifications(superview: view, bottomConstraint: bottomConstrain, bottomOffset: bottomOffset) - postTestNotification(name: Notification.Name.UIKeyboardWillShow) + postTestNotification(name: UIResponder.keyboardWillShowNotification) // THEN XCTAssertEqual(scrollView.contentInset.bottom, initialInsets.bottom + endFrame.height) XCTAssertEqual(bottomConstrain.constant, -endFrame.height) diff --git a/project.yml b/project.yml index 18856d3..83c09fa 100644 --- a/project.yml +++ b/project.yml @@ -11,7 +11,7 @@ configs: settings: base: CURRENT_PROJECT_VERSION: 1 - SWIFT_VERSION: 4.0 + SWIFT_VERSION: 5.0 configs: Debug: OTHER_SWIFT_FLAGS: -D DEBUG