Browse Source

implement timer to refresh date labels of chat messages

cyberta 5 years ago
parent
commit
744ab25e20
1 changed files with 21 additions and 0 deletions
  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 msgChangedObserver: Any?
     var incomingMsgObserver: Any?
     var incomingMsgObserver: Any?
 
 
+    weak var timer: Timer?
+
     lazy var navBarTap: UITapGestureRecognizer = {
     lazy var navBarTap: UITapGestureRecognizer = {
         UITapGestureRecognizer(target: self, action: #selector(chatProfilePressed))
         UITapGestureRecognizer(target: self, action: #selector(chatProfilePressed))
     }()
     }()
@@ -113,6 +115,23 @@ class ChatViewController: MessagesViewController {
                                        object: nil)
                                        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() {
     private func configureEmptyStateView() {
         view.addSubview(emptyStateView)
         view.addSubview(emptyStateView)
         view.addConstraints([emptyStateView.constraintCenterYTo(view),
         view.addConstraints([emptyStateView.constraintCenterYTo(view),
@@ -185,6 +204,7 @@ class ChatViewController: MessagesViewController {
         dcContext.marknoticedChat(chatId: chatId)
         dcContext.marknoticedChat(chatId: chatId)
         let array = DcArray(arrayPointer: dc_get_fresh_msgs(mailboxPointer))
         let array = DcArray(arrayPointer: dc_get_fresh_msgs(mailboxPointer))
         UIApplication.shared.applicationIconBadgeNumber = array.count
         UIApplication.shared.applicationIconBadgeNumber = array.count
+        startTimer()
     }
     }
 
 
     override func viewWillDisappear(_ animated: Bool) {
     override func viewWillDisappear(_ animated: Bool) {
@@ -206,6 +226,7 @@ class ChatViewController: MessagesViewController {
             nc.removeObserver(incomingMsgObserver)
             nc.removeObserver(incomingMsgObserver)
         }
         }
         audioController.stopAnyOngoingPlaying()
         audioController.stopAnyOngoingPlaying()
+        stopTimer()
     }
     }
 
 
     private func updateTitle(chat: DcChat) {
     private func updateTitle(chat: DcChat) {