Skip to content

Commit

Permalink
Refine handle event
Browse files Browse the repository at this point in the history
  • Loading branch information
zetbaitsu committed Jun 16, 2017
1 parent 96695ee commit 82bba7c
Showing 1 changed file with 14 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public class QiscusChatPresenter extends QiscusPresenter<QiscusChatPresenter.Vie

public QiscusChatPresenter(View view, QiscusChatRoom room) {
super(view);
if (!EventBus.getDefault().isRegistered(this)) {
EventBus.getDefault().register(this);
}
this.room = room;
this.currentTopicId = room.getLastTopicId();
qiscusAccount = Qiscus.getQiscusAccount();
Expand All @@ -72,9 +75,6 @@ public QiscusChatPresenter(View view, QiscusChatRoom room) {
updateReadState();
listenRoomTask = this::listenRoomEvent;
QiscusAndroidUtil.runOnUIThread(listenRoomTask, 1000);
if (!EventBus.getDefault().isRegistered(this)) {
EventBus.getDefault().register(this);
}
}

private void doInIo(Runnable runnable) {
Expand Down Expand Up @@ -536,23 +536,21 @@ private void handleEvent(QiscusChatRoomEvent event) {
if (event.getTopicId() == currentTopicId) {
switch (event.getEvent()) {
case TYPING:
view.onUserTyping(event.getUser(), event.isTyping());
QiscusAndroidUtil.runOnUIThread(() -> view.onUserTyping(event.getUser(), event.isTyping()));
break;
case DELIVERED:
QiscusComment deliveredComment = Qiscus.getDataStore()
.getComment(event.getCommentId(), event.getCommentUniqueId());
if (deliveredComment != null) {
deliveredComment.setId(event.getCommentId());
updateLastDeliveredComment(deliveredComment);
doInIo(() -> {
if (QiscusComment.STATE_DELIVERED > deliveredComment.getState()) {
deliveredComment.setState(QiscusComment.STATE_DELIVERED);
Qiscus.getDataStore().update(deliveredComment);
}
});
if (QiscusComment.STATE_DELIVERED > deliveredComment.getState()) {
deliveredComment.setState(QiscusComment.STATE_DELIVERED);
Qiscus.getDataStore().update(deliveredComment);
}
} else {
lastDeliveredCommentId.set(event.getCommentId());
view.updateLastDeliveredComment(lastDeliveredCommentId.get());
QiscusAndroidUtil.runOnUIThread(() -> view.updateLastDeliveredComment(lastDeliveredCommentId.get()));
}
break;
case READ:
Expand All @@ -561,16 +559,14 @@ private void handleEvent(QiscusChatRoomEvent event) {
if (readComment != null) {
readComment.setId(event.getCommentId());
updateLastReadComment(readComment);
doInIo(() -> {
if (QiscusComment.STATE_READ > readComment.getState()) {
readComment.setState(QiscusComment.STATE_READ);
Qiscus.getDataStore().update(readComment);
}
});
if (QiscusComment.STATE_READ > readComment.getState()) {
readComment.setState(QiscusComment.STATE_READ);
Qiscus.getDataStore().update(readComment);
}
} else {
lastReadCommentId.set(event.getCommentId());
lastDeliveredCommentId.set(lastReadCommentId.get());
view.updateLastReadComment(lastReadCommentId.get());
QiscusAndroidUtil.runOnUIThread(() -> view.updateLastReadComment(lastReadCommentId.get()));
}
break;
}
Expand Down

0 comments on commit 82bba7c

Please sign in to comment.