Skip to content

Commit 4f7cffc

Browse files
committed
Fixed ComposerView and keyboard events crashes.
Fixed the ComposerView position for embedded ChatViewController.
1 parent e52d07c commit 4f7cffc

File tree

4 files changed

+28
-23
lines changed

4 files changed

+28
-23
lines changed

Sources/UI/Extensions/Rx/RxKeyboard.swift renamed to Sources/UI/Extensions/Keyboard.swift

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// RxKeyboard.swift
2+
// Keyboard.swift
33
// StreamChat
44
//
55
// Created by Alexey Bukhtin on 04/06/2019.
@@ -11,23 +11,20 @@ import RxCocoa
1111
import RxSwift
1212
import RxGesture
1313

14-
extension Reactive where Base: NotificationCenter {
14+
struct Keyboard {
1515

16-
var keyboard: Driver<KeyboardNotification> {
17-
return Observable.merge(windowPan, keyboardNotifications)
18-
.distinctUntilChanged()
19-
.asDriver(onErrorJustReturn: KeyboardNotification(.init(name: UIResponder.keyboardWillChangeFrameNotification)))
20-
}
21-
22-
private var keyboardNotifications: Observable<KeyboardNotification> {
23-
return Observable.merge(notification(UIResponder.keyboardWillChangeFrameNotification),
24-
notification(UIResponder.keyboardWillHideNotification),
25-
notification(UIResponder.keyboardWillShowNotification))
26-
.map { KeyboardNotification($0) }
27-
}
16+
static let shared = Self()
17+
let notification: Driver<KeyboardNotification>
2818

29-
private var windowPan: Observable<KeyboardNotification> {
30-
return notification(UIApplication.didFinishLaunchingNotification)
19+
init() {
20+
let keyboardNotifications: Observable<KeyboardNotification> =
21+
Observable.merge(NotificationCenter.default.rx.notification(UIResponder.keyboardWillChangeFrameNotification),
22+
NotificationCenter.default.rx.notification(UIResponder.keyboardWillHideNotification),
23+
NotificationCenter.default.rx.notification(UIResponder.keyboardWillShowNotification))
24+
.map { KeyboardNotification($0) }
25+
26+
let windowPan: Observable<KeyboardNotification> =
27+
NotificationCenter.default.rx.notification(UIApplication.didFinishLaunchingNotification)
3128
.void()
3229
.startWith(Void())
3330
.flatMapLatest({ _ -> Observable<UIPanGestureRecognizer> in
@@ -40,6 +37,10 @@ extension Reactive where Base: NotificationCenter {
4037
.withLatestFrom(keyboardNotifications) { ($0, $1) }
4138
.filter { $1.height > 0 }
4239
.compactMap { KeyboardNotification(panGesture: $0, with: $1) }
40+
41+
notification = Observable.merge(windowPan, keyboardNotifications)
42+
.distinctUntilChanged()
43+
.asDriver(onErrorJustReturn: KeyboardNotification(.init(name: UIResponder.keyboardWillChangeFrameNotification)))
4344
}
4445
}
4546

Sources/UI/User Story/Chat/Chat View Controller/ChatViewController+Composer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ extension ChatViewController {
128128
.subscribe(onNext: { [weak self] in self?.send() })
129129
.disposed(by: disposeBag)
130130

131-
NotificationCenter.default.rx.keyboard
131+
Keyboard.shared.notification
132132
.filter { $0.isHidden }
133133
.drive(onNext: { [weak self] _ in self?.showCommands(show: false) })
134134
.disposed(by: disposeBag)

Sources/UI/User Story/Chat/Chat View Controller/ChatViewController.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,14 @@ open class ChatViewController: ViewController, UITableViewDataSource, UITableVie
3737

3838
/// A composer view.
3939
public private(set) lazy var composerView = createComposerView()
40-
private(set) lazy var initialSafeAreaBottom: CGFloat = view.safeAreaInsets.bottom
4140
var keyboardIsVisible = false
4241
lazy var keyboardHandler = setupKeyboard()
4342

43+
private(set) lazy var initialSafeAreaBottom: CGFloat = {
44+
let bottom = view.safeAreaInsets.bottom
45+
return bottom > 0 ? bottom : (parent?.view.safeAreaInsets.bottom ?? 0)
46+
}()
47+
4448
/// Attachments file types for thw composer view.
4549
public lazy var composerAddFileTypes = defaultComposerAddFileTypes
4650

@@ -129,7 +133,7 @@ open class ChatViewController: ViewController, UITableViewDataSource, UITableVie
129133
changesEnabled = true
130134
setupFooterUpdates()
131135

132-
NotificationCenter.default.rx.keyboard.drive(keyboardHandler).disposed(by: disposeBag)
136+
Keyboard.shared.notification.drive(keyboardHandler).disposed(by: disposeBag)
133137
}
134138

135139
open override func viewDidAppear(_ animated: Bool) {

StreamChat.xcodeproj/project.pbxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@
175175
8AD5EDB922E9BA98005CFAC9 /* String+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8AD5ECCC22E9BA39005CFAC9 /* String+Extensions.swift */; };
176176
8AD5EDBA22E9BA98005CFAC9 /* String+FileType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8AD5ECCD22E9BA39005CFAC9 /* String+FileType.swift */; };
177177
8AD5EDBC22E9BA98005CFAC9 /* RxGesture+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8AD5ECD022E9BA39005CFAC9 /* RxGesture+Extensions.swift */; };
178-
8AD5EDBD22E9BA98005CFAC9 /* RxKeyboard.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8AD5ECD122E9BA39005CFAC9 /* RxKeyboard.swift */; };
178+
8AD5EDBD22E9BA98005CFAC9 /* Keyboard.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8AD5ECD122E9BA39005CFAC9 /* Keyboard.swift */; };
179179
8AD5EDBE22E9BA98005CFAC9 /* RxDocumentPickerViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8AD5ECD222E9BA39005CFAC9 /* RxDocumentPickerViewController.swift */; };
180180
8AD5EDC722E9BAC5005CFAC9 /* RxAppState.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8AD5EDBF22E9BAC5005CFAC9 /* RxAppState.framework */; };
181181
8AD5EDC822E9BAC5005CFAC9 /* RxRelay.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8AD5EDC022E9BAC5005CFAC9 /* RxRelay.framework */; };
@@ -296,7 +296,7 @@
296296
8AD5ECCD22E9BA39005CFAC9 /* String+FileType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+FileType.swift"; sourceTree = "<group>"; };
297297
8AD5ECCE22E9BA39005CFAC9 /* Date+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Date+Extensions.swift"; sourceTree = "<group>"; };
298298
8AD5ECD022E9BA39005CFAC9 /* RxGesture+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "RxGesture+Extensions.swift"; sourceTree = "<group>"; };
299-
8AD5ECD122E9BA39005CFAC9 /* RxKeyboard.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RxKeyboard.swift; sourceTree = "<group>"; };
299+
8AD5ECD122E9BA39005CFAC9 /* Keyboard.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Keyboard.swift; sourceTree = "<group>"; };
300300
8AD5ECD222E9BA39005CFAC9 /* RxDocumentPickerViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RxDocumentPickerViewController.swift; sourceTree = "<group>"; };
301301
8AD5ECD322E9BA39005CFAC9 /* Chat.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Chat.xcassets; sourceTree = "<group>"; };
302302
8AD5ECD522E9BA39005CFAC9 /* ChatItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatItem.swift; sourceTree = "<group>"; };
@@ -682,6 +682,7 @@
682682
8AD5ECC922E9BA39005CFAC9 /* CharacterSet+Extensions.swift */,
683683
8AD5ECCA22E9BA39005CFAC9 /* Bundle+Extensions.swift */,
684684
8AE61E692379D3AE00A585BA /* DataDetector.swift */,
685+
8AD5ECD122E9BA39005CFAC9 /* Keyboard.swift */,
685686
8A2BE6082334E0ED00D6E684 /* EscapeBridgingUI.swift */,
686687
);
687688
path = Extensions;
@@ -755,7 +756,6 @@
755756
isa = PBXGroup;
756757
children = (
757758
8AD5ECD022E9BA39005CFAC9 /* RxGesture+Extensions.swift */,
758-
8AD5ECD122E9BA39005CFAC9 /* RxKeyboard.swift */,
759759
8AD5ECD222E9BA39005CFAC9 /* RxDocumentPickerViewController.swift */,
760760
);
761761
path = Rx;
@@ -1253,7 +1253,7 @@
12531253
8AD5ED8D22E9BA98005CFAC9 /* ReactionsView.swift in Sources */,
12541254
8AD5EDB922E9BA98005CFAC9 /* String+Extensions.swift in Sources */,
12551255
8AD5ED7C22E9BA98005CFAC9 /* ComposerView+Images.swift in Sources */,
1256-
8AD5EDBD22E9BA98005CFAC9 /* RxKeyboard.swift in Sources */,
1256+
8AD5EDBD22E9BA98005CFAC9 /* Keyboard.swift in Sources */,
12571257
8AD5ED8222E9BA98005CFAC9 /* ComposerFileView.swift in Sources */,
12581258
8AD54F1C231E91E500364657 /* ViewController.swift in Sources */,
12591259
8AE61E6D237AE25600A585BA /* DataDetector.swift in Sources */,

0 commit comments

Comments
 (0)