Skip to content

Commit

Permalink
simplify message list updates (#2052)
Browse files Browse the repository at this point in the history
the existing code has bugs wrt not-appearing messages (#1691)
and too many appearing messags (#1716 shows reactions temporarily as bubbles)
  • Loading branch information
r10s authored Jan 26, 2024
1 parent 26aae7e commit fee574f
Showing 1 changed file with 24 additions and 69 deletions.
93 changes: 24 additions & 69 deletions deltachat-ios/Chat/ChatViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -553,39 +553,42 @@ class ChatViewController: UITableViewController, UITableViewDropDelegate {
object: nil,
queue: OperationQueue.main
) { [weak self] notification in
guard let self = self, let id = notification.userInfo?["message_id"] as? Int else { return }
if self.dcChat.canSend, id > 0 {
let msg = self.dcContext.getMessage(id: id)
if msg.isInfo,
let parent = msg.parent,
parent.type == DC_MSG_WEBXDC {
self.refreshMessages()
} else {
self.updateMessage(msg)
guard let self, let ui = notification.userInfo else { return }
let chatId = ui["chat_id"] as? Int ?? 0
if chatId == 0 || chatId == self.chatId {
let messageId = ui["message_id"] as? Int ?? 0
if messageId > 0 {
let msg = self.dcContext.getMessage(id: messageId)
if msg.state == DC_STATE_OUT_DRAFT && msg.type == DC_MSG_WEBXDC {
draft.draftMsg = msg
configureDraftArea(draft: draft, animated: false)
return
}
}
} else {
self.refreshMessages()
refreshMessages()
updateTitle()
DispatchQueue.main.async {
self.updateScrollDownButtonVisibility()
}
markSeenMessagesInVisibleArea()
}
self.updateTitle()
}

incomingMsgObserver = nc.addObserver(
forName: eventIncomingMsg,
object: nil, queue: OperationQueue.main
) { [weak self] notification in
guard let self = self, let ui = notification.userInfo else { return }
if self.chatId == ui["chat_id"] as? Int {
if let id = ui["message_id"] as? Int {
if id > 0 {
self.insertMessage(self.dcContext.getMessage(id: id))
} else {
logger.debug(">>> messageId \(id) is not > 0, message not inserted")
}
guard let self, let ui = notification.userInfo else { return }
let chatId = ui["chat_id"] as? Int ?? 0
if chatId == 0 || chatId == self.chatId {
let wasLastSectionScrolledToBottom = isLastRowScrolledToBottom()
refreshMessages()
updateTitle()
if wasLastSectionScrolledToBottom {
scrollToBottom(animated: true)
}
self.updateTitle()
updateScrollDownButtonVisibility()
markSeenMessagesInVisibleArea()
}
}

Expand Down Expand Up @@ -933,14 +936,6 @@ class ChatViewController: UITableViewController, UITableViewDropDelegate {
}
}
}

func markSeenMessage(id: Int) {
if isVisibleToUser {
DispatchQueue.global(qos: .background).async { [weak self] in
self?.dcContext.markSeenMessages(messageIds: [UInt32(id)])
}
}
}

override func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
return tableView.cellForRow(at: indexPath) as? SelectableCell != nil
Expand Down Expand Up @@ -1689,46 +1684,6 @@ class ChatViewController: UITableViewController, UITableViewDropDelegate {
}
}

private func updateMessage(_ msg: DcMsg) {
if messageIds.firstIndex(of: msg.id) != nil {
reloadData()
} else {
// new outgoing message
if msg.state != DC_STATE_OUT_DRAFT,
msg.chatId == chatId {
logger.debug(">>> updateMessage: outgoing message \(msg.id)")
if let newMsgMarkerIndex = messageIds.firstIndex(of: Int(DC_MSG_ID_MARKER1)) {
messageIds.remove(at: newMsgMarkerIndex)
}
insertMessage(msg)
} else if msg.type == DC_MSG_WEBXDC,
msg.chatId == chatId {
// webxdc draft got updated
draft.draftMsg = msg
configureDraftArea(draft: draft, animated: false)
} else {
logger.debug(">>> updateMessage: unhandled message \(msg.id) - msg.chatId: \(msg.chatId) vs. chatId: \(chatId) - msg.state: \(msg.state)")
}
}
}

private func insertMessage(_ message: DcMsg) {
logger.debug(">>> insertMessage \(message.id)")
markSeenMessage(id: message.id)
let wasLastSectionScrolledToBottom = isLastRowScrolledToBottom()
messageIds.append(message.id)
emptyStateView.isHidden = true

reloadData()
if UIAccessibility.isVoiceOverRunning && !message.isFromCurrentSender {
scrollToBottom(animated: false, focusOnVoiceOver: true)
} else if wasLastSectionScrolledToBottom || message.isFromCurrentSender {
scrollToBottom(animated: true)
} else {
updateScrollDownButtonVisibility()
}
}

private func sendTextMessage(text: String, quoteMessage: DcMsg?) {
DispatchQueue.global().async { [weak self] in
guard let self = self else { return }
Expand Down

0 comments on commit fee574f

Please sign in to comment.