浏览代码

Merge pull request #1083 from deltachat/chatlist_update

Chatlist update
cyBerta 4 年之前
父节点
当前提交
a375d4f07c
共有 1 个文件被更改,包括 60 次插入1 次删除
  1. 60 1
      deltachat-ios/Controller/ChatListController.swift

+ 60 - 1
deltachat-ios/Controller/ChatListController.swift

@@ -13,6 +13,10 @@ class ChatListController: UITableViewController {
     private var msgsNoticedObserver: Any?
     private var incomingMsgObserver: Any?
     private var viewChatObserver: Any?
+    private var foregroundObserver: Any?
+    private var backgroundObserver: Any?
+
+    private weak var timer: Timer?
 
     private lazy var searchController: UISearchController = {
         let searchController = UISearchController(searchResultsController: nil)
@@ -97,7 +101,11 @@ class ChatListController: UITableViewController {
             quitSearch(animated: false)
             tableView.scrollToTop()
         }
-
+    }
+    
+    override func viewDidAppear(_ animated: Bool) {
+        super.viewDidAppear(animated)
+        startTimer()
         let nc = NotificationCenter.default
         msgChangedObserver = nc.addObserver(
             forName: dcNotificationChanged,
@@ -127,10 +135,19 @@ class ChatListController: UITableViewController {
                     self?.showChat(chatId: chatId)
                 }
         }
+        foregroundObserver = nc.addObserver(self,
+                                            selector: #selector(applicationDidBecomeActive(_:)),
+                                            name: UIApplication.didBecomeActiveNotification,
+                                            object: nil)
+        backgroundObserver = nc.addObserver(self,
+                                            selector: #selector(applicationWillResignActive(_:)),
+                                            name: UIApplication.willResignActiveNotification,
+                                            object: nil)
     }
 
     override func viewDidDisappear(_ animated: Bool) {
         super.viewDidDisappear(animated)
+        stopTimer()
 
         let nc = NotificationCenter.default
         if let msgChangedObserver = self.msgChangedObserver {
@@ -142,7 +159,17 @@ class ChatListController: UITableViewController {
         if let viewChatObserver = self.viewChatObserver {
             nc.removeObserver(viewChatObserver)
         }
+        if let msgsNoticedObserver = self.msgsNoticedObserver {
+            nc.removeObserver(msgsNoticedObserver)
+        }
+        if let foregroundObserver = self.foregroundObserver {
+            nc.removeObserver(foregroundObserver)
+        }
+        if let backgroundObserver = self.backgroundObserver {
+            nc.removeObserver(backgroundObserver)
+        }
     }
+    
     // MARK: - setup
     private func setupSubviews() {
         emptySearchStateLabel.addCenteredTo(parentView: view)
@@ -156,6 +183,20 @@ class ChatListController: UITableViewController {
         tableView.rowHeight = ContactCell.cellHeight
     }
 
+    
+    @objc func applicationDidBecomeActive(_ notification: NSNotification) {
+        if navigationController?.visibleViewController == self {
+            startTimer()
+            viewModel.refreshData()
+        }
+    }
+
+    @objc func applicationWillResignActive(_ notification: NSNotification) {
+        if navigationController?.visibleViewController == self {
+            stopTimer()
+        }
+    }
+    
     // MARK: - actions
     @objc func didPressNewChat() {
         showNewChatController()
@@ -321,6 +362,24 @@ class ChatListController: UITableViewController {
             emptySearchStateLabel.isHidden = true
         }
     }
+    
+    private func startTimer() {
+        // check if the timer is not yet started
+        if !(timer?.isValid ?? false) {
+            timer = Timer.scheduledTimer(withTimeInterval: 60, repeats: true) { [weak self] _ in
+                DispatchQueue.main.async { [weak self] in
+                    self?.viewModel.refreshData()
+                }
+            }
+        }
+    }
+    
+    private func stopTimer() {
+        // check if the timer is not already stopped
+        if timer?.isValid ?? false {
+            timer?.invalidate()
+        }
+    }
 
     // MARK: - alerts
     private func showDeleteChatConfirmationAlert(chatId: Int) {