Эх сурвалжийг харах

markseen messages only if app is in foreground and chat view is the top view controller

cyberta 4 жил өмнө
parent
commit
54d0edd258

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

@@ -250,6 +250,7 @@ public class DcContext {
     }
     }
 
 
     public func markSeenMessages(messageIds: [UInt32]) {
     public func markSeenMessages(messageIds: [UInt32]) {
+        logger?.debug("markseen messages: \(messageIds)")
         messageIds.withUnsafeBufferPointer { ptr in
         messageIds.withUnsafeBufferPointer { ptr in
             dc_markseen_msgs(contextPointer, ptr.baseAddress, Int32(ptr.count))
             dc_markseen_msgs(contextPointer, ptr.baseAddress, Int32(ptr.count))
         }
         }

+ 28 - 9
deltachat-ios/Chat/ChatViewController.swift

@@ -24,6 +24,7 @@ class ChatViewController: UITableViewController {
     var isDismissing = false
     var isDismissing = false
     var isInitial = true
     var isInitial = true
     var ignoreInputBarChange = false
     var ignoreInputBarChange = false
+    private var isVisibleToUser: Bool = false
 
 
     lazy var isGroupChat: Bool = {
     lazy var isGroupChat: Bool = {
         return dcContext.getChat(chatId: chatId).isGroup
         return dcContext.getChat(chatId: chatId).isGroup
@@ -420,8 +421,8 @@ class ChatViewController: UITableViewController {
             guard let self = self else { return }
             guard let self = self else { return }
             self.dcContext.marknoticedChat(chatId: self.chatId)
             self.dcContext.marknoticedChat(chatId: self.chatId)
         }
         }
-        startTimer()
-        markSeenMessagesInVisibleArea()
+        
+        handleUserVisibility(isVisible: true)
     }
     }
 
 
     override func viewWillDisappear(_ animated: Bool) {
     override func viewWillDisappear(_ animated: Bool) {
@@ -437,8 +438,8 @@ class ChatViewController: UITableViewController {
         isDismissing = false
         isDismissing = false
         ignoreInputBarChange = true
         ignoreInputBarChange = true
         AppStateRestorer.shared.resetLastActiveChat()
         AppStateRestorer.shared.resetLastActiveChat()
-        draft.save(context: dcContext)
-        stopTimer()
+        handleUserVisibility(isVisible: false)
+        
         let nc = NotificationCenter.default
         let nc = NotificationCenter.default
         if let msgChangedObserver = self.msgChangedObserver {
         if let msgChangedObserver = self.msgChangedObserver {
             nc.removeObserver(msgChangedObserver)
             nc.removeObserver(msgChangedObserver)
@@ -484,12 +485,23 @@ class ChatViewController: UITableViewController {
 
 
     @objc func applicationDidBecomeActive(_ notification: NSNotification) {
     @objc func applicationDidBecomeActive(_ notification: NSNotification) {
         if navigationController?.visibleViewController == self {
         if navigationController?.visibleViewController == self {
-            startTimer()
+            handleUserVisibility(isVisible: true)
         }
         }
     }
     }
 
 
     @objc func applicationWillResignActive(_ notification: NSNotification) {
     @objc func applicationWillResignActive(_ notification: NSNotification) {
         if navigationController?.visibleViewController == self {
         if navigationController?.visibleViewController == self {
+            handleUserVisibility(isVisible: false)
+        }
+    }
+    
+    func handleUserVisibility(isVisible: Bool) {
+        if isVisible {
+            isVisibleToUser = true
+            startTimer()
+            markSeenMessagesInVisibleArea()
+        } else {
+            isVisibleToUser = false
             stopTimer()
             stopTimer()
             draft.save(context: dcContext)
             draft.save(context: dcContext)
         }
         }
@@ -1085,8 +1097,10 @@ class ChatViewController: UITableViewController {
 
 
     func updateMessage(_ messageId: Int) {
     func updateMessage(_ messageId: Int) {
         if messageIds.firstIndex(where: { $0 == messageId }) != nil {
         if messageIds.firstIndex(where: { $0 == messageId }) != nil {
-            DispatchQueue.global(qos: .background).async { [weak self] in
-                self?.dcContext.markSeenMessages(messageIds: [UInt32(messageId)])
+            if isVisibleToUser {
+                DispatchQueue.global(qos: .background).async { [weak self] in
+                    self?.dcContext.markSeenMessages(messageIds: [UInt32(messageId)])
+                }
             }
             }
             let wasLastSectionVisible = self.isLastRowVisible()
             let wasLastSectionVisible = self.isLastRowVisible()
             reloadData()
             reloadData()
@@ -1102,8 +1116,13 @@ class ChatViewController: UITableViewController {
     }
     }
 
 
     func insertMessage(_ message: DcMsg) {
     func insertMessage(_ message: DcMsg) {
-        DispatchQueue.global(qos: .background).async { [weak self] in
-            self?.dcContext.markSeenMessages(messageIds: [UInt32(message.id)])
+        if isVisibleToUser {
+            logger.debug("markseen messages - message is visible to user")
+            DispatchQueue.global(qos: .background).async { [weak self] in
+                self?.dcContext.markSeenMessages(messageIds: [UInt32(message.id)])
+            }
+        } else {
+            logger.debug("NO markseen messages - message is NOT visible to user")
         }
         }
 
 
         let wasLastSectionVisible = isLastRowVisible()
         let wasLastSectionVisible = isLastRowVisible()