Skip to content

Commit

Permalink
Improved observing typing events.
Browse files Browse the repository at this point in the history
  • Loading branch information
buh authored and b-onc committed Mar 18, 2020
1 parent be93a25 commit a004235
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 29 deletions.
15 changes: 0 additions & 15 deletions Sources/Core/Presenter/Channel Presenter/ChannelPresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public final class ChannelPresenter: Presenter<ChatItem> {
/// Show statuses separators, e.g. Today
public private(set) var showStatuses = true

private var startedTyping = false
let lastMessageAtomic = Atomic<Message>()

/// The last parsed message from WebSocket events.
Expand Down Expand Up @@ -263,21 +262,7 @@ extension ChannelPresenter {
// MARK: - Send Event

extension ChannelPresenter {
/// Send a typing event.
public func sendEvent(isTyping: Bool) -> Observable<Event> {
guard parentMessage == nil && isTyping != startedTyping else {
return .empty()
}

startedTyping = isTyping

return channel
.send(eventType: isTyping ? .typingStart : .typingStop)
.observeOn(MainScheduler.instance)
}

/// Send Read event if the app is active.
///
/// - Returns: an observable completion.
public func markReadIfPossible() -> Observable<Void> {
guard InternetConnection.shared.isAvailable, channel.config.readEventsEnabled else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import RxSwift
import RxCocoa

// MARK: Setup Keyboard Events

extension Reactive where Base: ChatViewController {
var keyboard: Binder<KeyboardNotification> {
return Binder<KeyboardNotification>(base) { chatViewController, keyboardNotification in
Expand Down Expand Up @@ -90,15 +89,14 @@ public extension ChatViewController {
// MARK: Setup

extension ChatViewController {

func createComposerView() -> ComposerView {
let composerView = ComposerView(frame: .zero)
composerView.style = style.composer
return composerView
}

func setupComposerView() {
guard composerView.superview == nil else {
guard composerView.superview == nil, let presenter = channelPresenter else {
return
}

Expand All @@ -112,17 +110,23 @@ extension ChatViewController {
.subscribe(onNext: { [weak self] in self?.showAddFileView() })
.disposed(by: disposeBag)
}

composerView.textView.rx.text
.skip(1)
.unwrap()
.do(onNext: { [weak self] in self?.dispatchCommands(in: $0) })
.filter { [weak self] in !$0.isBlank && (self?.channelPresenter?.channel.config.typingEventsEnabled ?? false) }
.buffer(timeSpan: .seconds(3), count: 1, scheduler: MainScheduler.instance)
.flatMapLatest { [weak self] (input) -> Observable<StreamChatCore.Event> in
self?.channelPresenter?.sendEvent(isTyping: !input.isEmpty) ?? .empty() }
.subscribe()
.disposed(by: disposeBag)

let textViewEvents = composerView.textView.rx.text.skip(1).unwrap().share()

// Dispatch commands from text view.
textViewEvents.subscribe(onNext: { [weak self] in self?.dispatchCommands(in: $0) }).disposed(by: disposeBag)

// Send typing events.
if presenter.channel.config.typingEventsEnabled, presenter.parentMessage == nil {
Observable.merge([textViewEvents.map { _ in true },
textViewEvents.debounce(.seconds(3), scheduler: MainScheduler.instance).map { _ in false }])
.distinctUntilChanged()
.flatMapLatest({ [weak self] isTyping in
self?.channelPresenter?.channel.send(eventType: isTyping ? .typingStart : .typingStop) ?? .empty()
})
.subscribe()
.disposed(by: disposeBag)
}

composerView.sendButton.rx.tap
.subscribe(onNext: { [weak self] in self?.send() })
Expand Down

0 comments on commit a004235

Please sign in to comment.