KeyboardReadable.swift 711 B

123456789101112131415161718192021222324252627282930
  1. //
  2. // KeyboardReadable.swift
  3. // Chat
  4. //
  5. // Created by Sergey Tarasov on 25.07.2022.
  6. //
  7. import Combine
  8. import UIKit
  9. /// Publisher to read keyboard changes.
  10. protocol KeyboardReadable {
  11. var keyboardPublisher: AnyPublisher<Bool, Never> { get }
  12. }
  13. extension KeyboardReadable {
  14. var keyboardPublisher: AnyPublisher<Bool, Never> {
  15. Publishers.Merge(
  16. NotificationCenter.default
  17. .publisher(for: UIResponder.keyboardWillShowNotification)
  18. .map { _ in true },
  19. NotificationCenter.default
  20. .publisher(for: UIResponder.keyboardWillHideNotification)
  21. .map { _ in false }
  22. )
  23. .eraseToAnyPublisher()
  24. }
  25. }