2021-02-20 12:54:08 +01:00
|
|
|
//
|
|
|
|
// KeyboardResponderService.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by Cirno MainasuK on 2021-2-20.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import Combine
|
|
|
|
|
2021-07-19 11:12:45 +02:00
|
|
|
final public class KeyboardResponderService {
|
2021-02-20 12:54:08 +01:00
|
|
|
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
|
|
|
|
// MARK: - Singleton
|
|
|
|
public static let shared = KeyboardResponderService()
|
|
|
|
|
|
|
|
// output
|
2021-07-19 11:12:45 +02:00
|
|
|
public let isShow = CurrentValueSubject<Bool, Never>(false)
|
|
|
|
public let state = CurrentValueSubject<KeyboardState, Never>(.none)
|
|
|
|
public let endFrame = CurrentValueSubject<CGRect, Never>(.zero)
|
2021-03-12 08:23:28 +01:00
|
|
|
|
2021-02-20 12:54:08 +01:00
|
|
|
private init() {
|
|
|
|
NotificationCenter.default.publisher(for: UIResponder.keyboardWillShowNotification, object: nil)
|
|
|
|
.sink { notification in
|
|
|
|
self.isShow.value = true
|
|
|
|
self.updateInternalStatus(notification: notification)
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
|
|
|
|
NotificationCenter.default.publisher(for: UIResponder.keyboardDidHideNotification, object: nil)
|
|
|
|
.sink { notification in
|
|
|
|
self.isShow.value = false
|
|
|
|
self.updateInternalStatus(notification: notification)
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
|
|
|
|
NotificationCenter.default.publisher(for: UIResponder.keyboardDidChangeFrameNotification, object: nil)
|
|
|
|
.sink { notification in
|
2021-02-22 05:51:35 +01:00
|
|
|
self.updateInternalStatus(notification: notification)
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
NotificationCenter.default.publisher(for: UIResponder.keyboardWillChangeFrameNotification, object: nil)
|
|
|
|
.sink { notification in
|
2021-02-20 12:54:08 +01:00
|
|
|
self.updateInternalStatus(notification: notification)
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension KeyboardResponderService {
|
|
|
|
|
|
|
|
private func updateInternalStatus(notification: Notification) {
|
|
|
|
guard let isLocal = notification.userInfo?[UIWindow.keyboardIsLocalUserInfoKey] as? Bool,
|
|
|
|
let endFrame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-12 08:23:28 +01:00
|
|
|
self.endFrame.value = endFrame
|
|
|
|
|
2021-02-20 12:54:08 +01:00
|
|
|
guard isLocal else {
|
|
|
|
self.state.value = .notLocal
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if floating
|
|
|
|
guard endFrame.width == UIScreen.main.bounds.width else {
|
|
|
|
self.state.value = .floating
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if undock | split
|
|
|
|
let dockMinY = UIScreen.main.bounds.height - endFrame.height
|
|
|
|
if endFrame.minY < dockMinY {
|
|
|
|
self.state.value = .notDock
|
|
|
|
} else {
|
|
|
|
self.state.value = .dock
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension KeyboardResponderService {
|
2021-07-19 11:12:45 +02:00
|
|
|
public enum KeyboardState {
|
2021-02-20 12:54:08 +01:00
|
|
|
case none
|
|
|
|
case notLocal
|
|
|
|
case notDock // undock | split
|
|
|
|
case floating // iPhone size floating
|
|
|
|
case dock
|
|
|
|
}
|
|
|
|
}
|