chore: add keyboardWillShowNotification to KeyboardResponderService

This commit is contained in:
sunxiaojian 2021-02-22 12:51:35 +08:00
parent 6285cb95fa
commit e4989cfe30
2 changed files with 21 additions and 14 deletions

View File

@ -142,7 +142,7 @@ final class MastodonRegisterViewController: UIViewController, NeedsDependency {
let passwordCheckLabel: UILabel = { let passwordCheckLabel: UILabel = {
let label = UILabel() let label = UILabel()
label.numberOfLines = 4 label.numberOfLines = 0
return label return label
}() }()
@ -210,7 +210,7 @@ extension MastodonRegisterViewController {
stackView.axis = .vertical stackView.axis = .vertical
stackView.distribution = .fill stackView.distribution = .fill
stackView.spacing = 40 stackView.spacing = 40
stackView.layoutMargins = UIEdgeInsets(top: 0, left: 4, bottom: 0, right: 4) stackView.layoutMargins = UIEdgeInsets(top: 0, left: 4, bottom: 26, right: 4)
stackView.isLayoutMarginsRelativeArrangement = true stackView.isLayoutMarginsRelativeArrangement = true
stackView.addArrangedSubview(largeTitleLabel) stackView.addArrangedSubview(largeTitleLabel)
stackView.addArrangedSubview(photoView) stackView.addArrangedSubview(photoView)
@ -293,21 +293,19 @@ extension MastodonRegisterViewController {
signUpActivityIndicatorView.centerYAnchor.constraint(equalTo: signUpButton.centerYAnchor), signUpActivityIndicatorView.centerYAnchor.constraint(equalTo: signUpButton.centerYAnchor),
]) ])
Publishers.CombineLatest3( Publishers.CombineLatest(
KeyboardResponderService.shared.isShow.eraseToAnyPublisher(),
KeyboardResponderService.shared.state.eraseToAnyPublisher(), KeyboardResponderService.shared.state.eraseToAnyPublisher(),
KeyboardResponderService.shared.endFrame.eraseToAnyPublisher() KeyboardResponderService.shared.willEndFrame.eraseToAnyPublisher()
) )
.sink(receiveValue: { [weak self] isShow, state, endFrame in .sink(receiveValue: { [weak self] state, endFrame in
guard let self = self else { return } guard let self = self else { return }
guard isShow, state == .dock else { guard state == .dock else {
self.scrollview.contentInset.bottom = 0.0 self.scrollview.contentInset.bottom = 0.0
self.scrollview.verticalScrollIndicatorInsets.bottom = 0.0 self.scrollview.verticalScrollIndicatorInsets.bottom = 0.0
return return
} }
// isShow AND dock state
let contentFrame = self.view.convert(self.scrollview.frame, to: nil) let contentFrame = self.view.convert(self.scrollview.frame, to: nil)
let padding = contentFrame.maxY - endFrame.minY let padding = contentFrame.maxY - endFrame.minY
guard padding > 0 else { guard padding > 0 else {
@ -409,9 +407,10 @@ extension MastodonRegisterViewController: UITextFieldDelegate {
// align to password label when overlap // align to password label when overlap
if textField === passwordTextField, if textField === passwordTextField,
KeyboardResponderService.shared.isShow.value, KeyboardResponderService.shared.isShow.value,
KeyboardResponderService.shared.state.value == .dock { KeyboardResponderService.shared.state.value == .dock
let endFrame = KeyboardResponderService.shared.endFrame.value {
let contentFrame = self.scrollview.convert(self.passwordCheckLabel.frame, to: nil) let endFrame = KeyboardResponderService.shared.willEndFrame.value
let contentFrame = scrollview.convert(passwordCheckLabel.frame, to: nil)
let padding = contentFrame.maxY - endFrame.minY let padding = contentFrame.maxY - endFrame.minY
if padding > 0 { if padding > 0 {
let contentOffsetY = scrollview.contentOffset.y let contentOffsetY = scrollview.contentOffset.y

View File

@ -18,7 +18,8 @@ final class KeyboardResponderService {
// output // output
let isShow = CurrentValueSubject<Bool, Never>(false) let isShow = CurrentValueSubject<Bool, Never>(false)
let state = CurrentValueSubject<KeyboardState, Never>(.none) let state = CurrentValueSubject<KeyboardState, Never>(.none)
let endFrame = CurrentValueSubject<CGRect, Never>(.zero) let didEndFrame = CurrentValueSubject<CGRect, Never>(.zero)
let willEndFrame = CurrentValueSubject<CGRect, Never>(.zero)
private init() { private init() {
NotificationCenter.default.publisher(for: UIResponder.keyboardWillShowNotification, object: nil) NotificationCenter.default.publisher(for: UIResponder.keyboardWillShowNotification, object: nil)
@ -37,6 +38,15 @@ final class KeyboardResponderService {
NotificationCenter.default.publisher(for: UIResponder.keyboardDidChangeFrameNotification, object: nil) NotificationCenter.default.publisher(for: UIResponder.keyboardDidChangeFrameNotification, object: nil)
.sink { notification in .sink { notification in
guard let endFrame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect else { return }
self.didEndFrame.value = endFrame
self.updateInternalStatus(notification: notification)
}
.store(in: &disposeBag)
NotificationCenter.default.publisher(for: UIResponder.keyboardWillChangeFrameNotification, object: nil)
.sink { notification in
guard let endFrame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect else { return }
self.willEndFrame.value = endFrame
self.updateInternalStatus(notification: notification) self.updateInternalStatus(notification: notification)
} }
.store(in: &disposeBag) .store(in: &disposeBag)
@ -52,8 +62,6 @@ extension KeyboardResponderService {
return return
} }
self.endFrame.value = endFrame
guard isLocal else { guard isLocal else {
self.state.value = .notLocal self.state.value = .notLocal
return return