Browse Source

implement timer to refresh date labels of chat messages

cyberta 5 năm trước cách đây
mục cha
commit
744ab25e20
1 tập tin đã thay đổi với 21 bổ sung0 xóa
  1. 21 0
      deltachat-ios/Controller/ChatViewController.swift

+ 21 - 0
deltachat-ios/Controller/ChatViewController.swift

@@ -47,6 +47,8 @@ class ChatViewController: MessagesViewController {
     var msgChangedObserver: Any?
     var incomingMsgObserver: Any?
 
+    weak var timer: Timer?
+
     lazy var navBarTap: UITapGestureRecognizer = {
         UITapGestureRecognizer(target: self, action: #selector(chatProfilePressed))
     }()
@@ -113,6 +115,23 @@ class ChatViewController: MessagesViewController {
                                        object: nil)
     }
 
+    private func startTimer() {
+        timer?.invalidate()
+        timer = Timer.scheduledTimer(withTimeInterval: 60, repeats: true) { [weak self] _ in
+            //reload table
+            DispatchQueue.main.async {
+                guard let self = self else { return }
+                self.messageList = self.getMessageIds(self.messageList.count)
+                self.messagesCollectionView.reloadDataAndKeepOffset()
+                self.refreshControl.endRefreshing()
+            }
+        }
+    }
+
+    private func stopTimer() {
+        timer?.invalidate()
+    }
+
     private func configureEmptyStateView() {
         view.addSubview(emptyStateView)
         view.addConstraints([emptyStateView.constraintCenterYTo(view),
@@ -185,6 +204,7 @@ class ChatViewController: MessagesViewController {
         dcContext.marknoticedChat(chatId: chatId)
         let array = DcArray(arrayPointer: dc_get_fresh_msgs(mailboxPointer))
         UIApplication.shared.applicationIconBadgeNumber = array.count
+        startTimer()
     }
 
     override func viewWillDisappear(_ animated: Bool) {
@@ -206,6 +226,7 @@ class ChatViewController: MessagesViewController {
             nc.removeObserver(incomingMsgObserver)
         }
         audioController.stopAnyOngoingPlaying()
+        stopTimer()
     }
 
     private func updateTitle(chat: DcChat) {