KeyboardVisible.swift (742B)
1 // 2 // KeyboardVisible.swift 3 // damus 4 // 5 // Created by William Casarin on 2023-05-05. 6 // 7 8 import Foundation 9 import Combine 10 import UIKit 11 12 13 /// Publisher to read keyboard changes. 14 protocol KeyboardReadable { 15 var keyboardPublisher: AnyPublisher<Bool, Never> { get } 16 } 17 18 extension KeyboardReadable { 19 var keyboardPublisher: AnyPublisher<Bool, Never> { 20 Publishers.Merge( 21 NotificationCenter.default 22 .publisher(for: UIResponder.keyboardWillShowNotification) 23 .map { _ in true }, 24 25 NotificationCenter.default 26 .publisher(for: UIResponder.keyboardWillHideNotification) 27 .map { _ in false } 28 ) 29 .eraseToAnyPublisher() 30 } 31 }