-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce subscription observation token (#51)
- Loading branch information
1 parent
7320615
commit c0fb1f8
Showing
17 changed files
with
735 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--swiftversion 5.2 | ||
--header "\nCopyright © {created.year} Alexey Korolev <alphatroya@gmail.com>\n//" | ||
--header "\nMIT License\n\nCopyright (c) {created.year} Alexey Korolev\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the "Software"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\nTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n" | ||
--maxwidth 140 | ||
--wraparguments before-first | ||
--wrapcollections before-first |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// | ||
// MIT License | ||
// | ||
// Copyright (c) 2021 Alexey Korolev | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the Software), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
// | ||
|
||
import UIKit | ||
|
||
/// Keyboard transition metadata object | ||
public enum KeyboardManagerEvent { | ||
/// UIKeyboardWillShow notification case event | ||
case willShow(KeyboardManagerEvent.Data) | ||
|
||
/// UIKeyboardDidShow notification case event | ||
case didShow(KeyboardManagerEvent.Data) | ||
|
||
/// UIKeyboardWillHide notification case event | ||
case willHide(KeyboardManagerEvent.Data) | ||
|
||
/// UIKeyboardDidHide notification case event | ||
case didHide(KeyboardManagerEvent.Data) | ||
|
||
/// UIKeyboardWillChangeFrame notification case event | ||
case willFrameChange(KeyboardManagerEvent.Data) | ||
|
||
/// UIKeyboardDidChangeFrame notification case event | ||
case didFrameChange(KeyboardManagerEvent.Data) | ||
|
||
/// `UIKeyboardFrameBeginUserInfoKey` and `UIKeyboardFrameEndUserInfoKey` values | ||
public struct Frame { | ||
/// Begin transition keyboard frame | ||
public var begin: CGRect | ||
|
||
/// Final transition keyboard frame | ||
public var end: CGRect | ||
} | ||
|
||
/// Notification `userInfo` metadata info | ||
public struct Data { | ||
/// Keyboard frames | ||
public var frame: Frame | ||
|
||
/// Animation curve value | ||
public var animationCurve: Int | ||
|
||
/// Transition animation duration value | ||
public var animationDuration: Double | ||
|
||
/// `UIKeyboardIsLocalUserInfoKey` `userInfo` value | ||
public var isLocal: Bool | ||
|
||
static func null() -> Data { | ||
let frame = Frame(begin: CGRect.zero, end: CGRect.zero) | ||
return Data(frame: frame, animationCurve: 0, animationDuration: 0.0, isLocal: false) | ||
} | ||
} | ||
|
||
var data: KeyboardManagerEvent.Data { | ||
switch self { | ||
case let .willShow(data), | ||
let .didShow(data), | ||
let .willHide(data), | ||
let .didHide(data), | ||
let .willFrameChange(data), | ||
let .didFrameChange(data): | ||
return data | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// | ||
// MIT License | ||
// | ||
// Copyright (c) 2021 Alexey Korolev | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the Software), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
// | ||
|
||
import Foundation | ||
|
||
/// Keyboard's notification observing closure | ||
public typealias KeyboardManagerEventClosure = (KeyboardManagerEvent) -> Void |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// | ||
// MIT License | ||
// | ||
// Copyright (c) 2021 Alexey Korolev | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the Software), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
// | ||
|
||
import UIKit | ||
|
||
/// Token instance that internally store subscription, cancels subscription on deallocation | ||
public final class KeyboardObserverToken { | ||
init(keyboardManager: KeyboardManager) { | ||
self.keyboardManager = keyboardManager | ||
} | ||
|
||
private var keyboardManager: KeyboardManager | ||
} | ||
|
||
/// Keyboard observer is a namespace for subscription static methods | ||
public enum KeyboardObserver { | ||
/** | ||
Add observer closure for observing keyboard events | ||
- Parameters: | ||
- notificationCenter: notification center to observe notifications | ||
- observer: closure for observing events | ||
- Returns: observer token that store subscription | ||
*/ | ||
public static func addObserver( | ||
_ notificationCenter: NotificationCenter = .default, | ||
_ observer: @escaping KeyboardManagerEventClosure | ||
) -> KeyboardObserverToken { | ||
let keyboardManager = KeyboardManager(notificationCenter: notificationCenter) | ||
keyboardManager.addEventClosure(observer) | ||
return KeyboardObserverToken(keyboardManager: keyboardManager) | ||
} | ||
|
||
/** | ||
Automatically adjusts view's bottom constraint offset after receiving keyboard's notifications | ||
|
||
- Parameters: | ||
- _ notificationCenter: notification center to observe notifications | ||
- superview: parent view for adjusted constraints | ||
- bottomConstraint: current bottom constraint instance | ||
- bottomOffset: minimal preserved constraint offset value | ||
- safeAreaInsets: safe area generator for compensate offset for view controllers with tabbar | ||
- animated: should changes be animated | ||
- Returns: observer token that store subscription | ||
*/ | ||
public static func addObserver( | ||
_ notificationCenter: NotificationCenter = .default, | ||
superview: UIView, | ||
bottomConstraint: NSLayoutConstraint, | ||
bottomOffset: CGFloat = 0.0, | ||
safeAreaInsets: @escaping () -> UIEdgeInsets = { UIEdgeInsets.zero }, | ||
animated: Bool = false | ||
) -> KeyboardObserverToken { | ||
let keyboardManager = KeyboardManager(notificationCenter: notificationCenter) | ||
keyboardManager.bindToKeyboardNotifications( | ||
superview: superview, | ||
bottomConstraint: bottomConstraint, | ||
bottomOffset: bottomOffset, | ||
safeAreaInsets: safeAreaInsets, | ||
animated: animated | ||
) | ||
return KeyboardObserverToken(keyboardManager: keyboardManager) | ||
} | ||
|
||
/** | ||
Automatically adjusts scrollView's contentInset property with animation after receiving keyboard's notifications | ||
- Parameters: | ||
- _ notificationCenter: notification center to observe notifications | ||
- scrollView: scroll view instance | ||
- Returns: observer token that store subscription | ||
*/ | ||
public static func addObserver( | ||
_ notificationCenter: NotificationCenter = .default, | ||
scrollView: UIScrollView | ||
) -> KeyboardObserverToken { | ||
let keyboardManager = KeyboardManager(notificationCenter: notificationCenter) | ||
keyboardManager.bindToKeyboardNotifications( | ||
scrollView: scrollView | ||
) | ||
return KeyboardObserverToken(keyboardManager: keyboardManager) | ||
} | ||
} |
Oops, something went wrong.