85 lines
3.0 KiB
Swift
85 lines
3.0 KiB
Swift
//
|
|
// CommonKeyboard.swift
|
|
// Wallet
|
|
//
|
|
// Created by Igor on 23.03.2021.
|
|
// Copyright © 2021 AM. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
public struct KeyboardInfo {
|
|
public let frameBegin: CGRect
|
|
public let frameEnd: CGRect
|
|
public let isLocal: Bool
|
|
public let animationCurve: Int
|
|
public let duration: TimeInterval
|
|
}
|
|
|
|
public extension KeyboardInfo {
|
|
init(notification: Notification) {
|
|
frameBegin = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue ?? CGRect.zero
|
|
frameEnd = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue ?? CGRect.zero
|
|
isLocal = (notification.userInfo?[UIResponder.keyboardIsLocalUserInfoKey] as? Bool) ?? true
|
|
animationCurve = (notification.userInfo?[UIResponder.keyboardAnimationCurveUserInfoKey] as? Int) ?? 0
|
|
duration = (notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? TimeInterval) ?? 0
|
|
}
|
|
}
|
|
|
|
enum KeyboardConnectionType: String {
|
|
case willShow
|
|
case didShow
|
|
case willHide
|
|
case didHide
|
|
case willChangeFrame
|
|
case didChangeFrame
|
|
fileprivate var notification: NSNotification.Name {
|
|
([
|
|
.willShow: UIResponder.keyboardWillShowNotification,
|
|
.didShow: UIResponder.keyboardDidShowNotification,
|
|
.willHide: UIResponder.keyboardWillHideNotification,
|
|
.didHide: UIResponder.keyboardDidHideNotification,
|
|
.willChangeFrame: UIResponder.keyboardWillChangeFrameNotification,
|
|
.didChangeFrame: UIResponder.keyboardDidChangeFrameNotification
|
|
] as [KeyboardConnectionType: NSNotification.Name])[self]!
|
|
}
|
|
}
|
|
|
|
extension UIViewController {
|
|
private struct Keyboard {
|
|
static var values = "values"
|
|
}
|
|
private var keyboard: [KeyboardConnectionType: AnyObject]? {
|
|
get { Common.Runtime.object(self, key: &Keyboard.values) }
|
|
set { Common.Runtime.set(self, value: newValue, key: &Keyboard.values) }
|
|
}
|
|
func connectKeyboard(action: KeyboardConnectionType, _ handler: ((KeyboardInfo) -> Void)?) {
|
|
let value = Notification.subscribe(name: action.notification) { (ntf) in
|
|
handler?(KeyboardInfo(notification: ntf))
|
|
}
|
|
if keyboard == nil {
|
|
keyboard = [action: value]
|
|
} else {
|
|
keyboard?[action] = value
|
|
}
|
|
}
|
|
func disconnectKeyboard() {
|
|
disconnectKeyboard(action: .willShow)
|
|
disconnectKeyboard(action: .willHide)
|
|
disconnectKeyboard(action: .didShow)
|
|
disconnectKeyboard(action: .didHide)
|
|
disconnectKeyboard(action: .willChangeFrame)
|
|
disconnectKeyboard(action: .didChangeFrame)
|
|
}
|
|
func disconnectKeyboard(action: KeyboardConnectionType) {
|
|
guard let object = keyboard?[action] else { return }
|
|
Notification.unsubscribe(observer: object)
|
|
keyboard?.removeValue(forKey: action)
|
|
}
|
|
|
|
var appDelegate: AppDelegate {
|
|
// swiftlint:disable force_cast
|
|
return UIApplication.shared.delegate as! AppDelegate
|
|
}
|
|
}
|