Przeglądaj źródła

scroll to last unseen message

cyberta 4 lat temu
rodzic
commit
96bcce48e4

+ 5 - 0
DcCore/DcCore/DC/Wrapper.swift

@@ -27,6 +27,7 @@ public class DcContext {
         return .dcContext
     }
 
+    // TODO: remove count and from parameters if we don't use it
     public func getMessageIds(chatId: Int, count: Int? = nil, from: Int? = nil) -> [Int] {
         let start = CFAbsoluteTimeGetCurrent()
         let cMessageIds = getChatMessages(chatId: chatId)
@@ -249,6 +250,10 @@ public class DcContext {
         return DcArray(arrayPointer: dc_get_fresh_msgs(contextPointer))
     }
 
+    public func getFreshMessagesCount(chatId: Int) -> Int {
+        return Int(dc_get_fresh_msg_cnt(contextPointer, UInt32(chatId)))
+    }
+
     public func markSeenMessages(messageIds: [UInt32]) {
         messageIds.withUnsafeBufferPointer { ptr in
             dc_markseen_msgs(contextPointer, ptr.baseAddress, Int32(ptr.count))

+ 19 - 1
deltachat-ios/Chat/ChatViewController.swift

@@ -338,7 +338,7 @@ class ChatViewController: UITableViewController {
                 UIView.animate(withDuration: 0.1, delay: 0, options: .allowAnimatedContent, animations: { [weak self] in
                     guard let self = self else { return }
                     if self.isInitial {
-                        self.scrollToBottom(animated: false)
+                        self.scrollToLastUnseenMessage()
                     }
                 }, completion: { [weak self] finished in
                     if finished {
@@ -793,6 +793,24 @@ class ChatViewController: UITableViewController {
         }
     }
 
+    private func scrollToLastUnseenMessage() {
+        DispatchQueue.main.async { [weak self] in
+            guard let self = self else { return }
+            let freshMsgsCount = self.dcContext.getFreshMessagesCount(chatId: self.chatId)
+            if self.messageIds.count >= freshMsgsCount {
+                let index = self.messageIds.count - freshMsgsCount - 1
+                let indexPath = IndexPath(row: index, section: 0)
+                self.tableView.scrollToRow(at: indexPath, at: .top, animated: false)
+            } else {
+                // scroll to bottom
+                let numberOfRows = self.tableView.numberOfRows(inSection: 0)
+                if numberOfRows > 0 {
+                    self.tableView.scrollToRow(at: IndexPath(row: numberOfRows - 1, section: 0), at: .bottom, animated: false)
+                }
+            }
+        }
+    }
+
     private func scrollToMessage(msgId: Int, animated: Bool = true) {
         DispatchQueue.main.async { [weak self] in
             guard let self = self else { return }