Skip to content

Commit 09a6ca7

Browse files
authored
Merge pull request #16 from RxSwiftCommunity/willshowvisibleheight-fix
Fix willShowVisibleHeight to emit value only when keyboard is shown from the bottom
2 parents 4615ebc + d0d555e commit 09a6ca7

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

Sources/RxKeyboard.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,12 @@ public class RxKeyboard: NSObject {
4949
let frameVariable = Variable<CGRect>(defaultFrame)
5050
self.frame = frameVariable.asDriver().distinctUntilChanged()
5151
self.visibleHeight = self.frame.map { UIScreen.main.bounds.height - $0.origin.y }
52-
self.willShowVisibleHeight = NotificationCenter.default.rx.notification(.UIKeyboardWillShow)
53-
.map { notification -> CGFloat in
54-
let rectValue = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue
55-
let keyboardFrame = rectValue?.cgRectValue ?? defaultFrame
56-
return UIScreen.main.bounds.height - keyboardFrame.origin.y
52+
self.willShowVisibleHeight = self.visibleHeight
53+
.scan((visibleHeight: 0, isShowing: false)) { lastState, newVisibleHeight in
54+
return (visibleHeight: newVisibleHeight, isShowing: lastState.visibleHeight == 0)
5755
}
58-
.asDriver { _ in .empty() }
56+
.filter { state in state.isShowing }
57+
.map { state in state.visibleHeight }
5958

6059
super.init()
6160

0 commit comments

Comments
 (0)