Skip to content

Commit

Permalink
Convert code to Swift 5 (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
alphatroya authored Jul 12, 2019
1 parent ab59540 commit f439ed0
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 86 deletions.
4 changes: 2 additions & 2 deletions KeyboardManager.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down Expand Up @@ -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;
Expand Down
50 changes: 23 additions & 27 deletions Sources/KeyboardManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
19 changes: 9 additions & 10 deletions Tests/Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
])
}

Expand Down
16 changes: 7 additions & 9 deletions Tests/KeyboardManager+NotificationCenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@

import UIKit

import XCTest
@testable import KeyboardManager
import XCTest

class KeyboardManagerNotificationCenterTest: XCTestCase {

private var notificationCenter: NotificationCenterMock!
var keyboardManager: KeyboardManagerProtocol!

Expand Down Expand Up @@ -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
Expand All @@ -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
}
}
Expand Down
33 changes: 16 additions & 17 deletions Tests/KeyboardManager+ScrollView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@
// 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()
let initialInsets = UIEdgeInsets(top: 10, left: 11, bottom: 12, right: 13)
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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
Loading

0 comments on commit f439ed0

Please sign in to comment.