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

populating cell via viewModel

nayooti 5 жил өмнө
parent
commit
575d911f8e

+ 38 - 46
deltachat-ios/Controller/ChatListController.swift

@@ -49,7 +49,7 @@ class ChatListController: UITableViewController {
         self.showArchive = showArchive
         dcContext.updateDeviceChats()
         super.init(nibName: nil, bundle: nil)
-        viewModel.onChatListUpdate = handleChatListUpdate
+        viewModel.onChatListUpdate = handleChatListUpdate // register listener
     }
 
     required init?(coder _: NSCoder) {
@@ -72,7 +72,7 @@ class ChatListController: UITableViewController {
         }
 
         updateTitle()
-
+        viewModel.refreshData()
         let nc = NotificationCenter.default
         msgChangedObserver = nc.addObserver(
             forName: dcNotificationChanged,
@@ -124,7 +124,7 @@ class ChatListController: UITableViewController {
         }
     }
 
-    // Mark: - configuration
+    // MARK: - configuration
     private func configureTableView() {
         tableView.register(ContactCell.self, forCellReuseIdentifier: chatCellReuseIdentifier)
         tableView.register(ContactCell.self, forCellReuseIdentifier: deadDropCellReuseIdentifier)
@@ -132,6 +132,7 @@ class ChatListController: UITableViewController {
         tableView.rowHeight = 80
     }
 
+    // MARK: - actions
     @objc func didPressNewChat() {
         coordinator?.showNewChatController()
     }
@@ -154,19 +155,6 @@ class ChatListController: UITableViewController {
         tableView.reloadData()
     }
 
-    private func updateTitle() {
-        if RelayHelper.sharedInstance.isForwarding() {
-            title = String.localized("forward_to")
-            if !showArchive {
-                navigationItem.setLeftBarButton(cancelButton, animated: true)
-            }
-        } else {
-            title = showArchive ? String.localized("chat_archived_chats_title") :
-                String.localized("pref_chats")
-            navigationItem.setLeftBarButton(nil, animated: true)
-        }
-    }
-
     override func numberOfSections(in tableView: UITableView) -> Int {
         return viewModel.numberOfSections
     }
@@ -185,8 +173,11 @@ override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexP
         if chatId == DC_CHAT_ID_ARCHIVED_LINK {
             updateArchivedCell() // to make sure archived chats count is always right
             return archiveCell
-        } else if chatId == DC_CHAT_ID_DEADDROP, let deaddropCell = tableView.dequeueReusableCell(withIdentifier: deadDropCellReuseIdentifier, for: indexPath) as? ContactCell {
-            // TODO update
+        } else if
+            chatId == DC_CHAT_ID_DEADDROP,
+            let msgId = viewModel.msgIdFor(row: indexPath.row),
+            let deaddropCell = tableView.dequeueReusableCell(withIdentifier: deadDropCellReuseIdentifier, for: indexPath) as? ContactCell {
+            updateDeaddropCell(deaddropCell, msgId: msgId, cellData: cellData)
             return deaddropCell
         } else if let chatCell = tableView.dequeueReusableCell(withIdentifier: chatCellReuseIdentifier, for: indexPath) as? ContactCell {
         // default chatCell
@@ -339,6 +330,19 @@ override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexP
 
     // MARK: updates
 
+    private func updateTitle() {
+        if RelayHelper.sharedInstance.isForwarding() {
+            title = String.localized("forward_to")
+            if !showArchive {
+                navigationItem.setLeftBarButton(cancelButton, animated: true)
+            }
+        } else {
+            title = showArchive ? String.localized("chat_archived_chats_title") :
+                String.localized("pref_chats")
+            navigationItem.setLeftBarButton(nil, animated: true)
+        }
+    }
+
     func handleChatListUpdate() {
         tableView.reloadData()
     }
@@ -350,16 +354,21 @@ override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexP
         archiveCell.textLabel?.text = title
     }
 
-    func getDeaddropCell(_ tableView: UITableView) -> ContactCell {
-        let deaddropCell: ContactCell
-        if let cell = tableView.dequeueReusableCell(withIdentifier: "DeaddropCell") as? ContactCell {
-            deaddropCell = cell
+    func updateDeaddropCell(_ cell: ContactCell, msgId: Int, cellData: AvatarCellViewModel) {
+        cell.backgroundColor = DcColors.deaddropBackground
+        cell.contentView.backgroundColor = DcColors.deaddropBackground
+
+        cell.updateCell(cellViewModel: cellData)
+/*
+        let contact = DcContact(id: DcMsg(id: msgId).fromContactId)
+        if let img = contact.profileImage {
+            cell.resetBackupImage()
+            cell.setImage(img)
         } else {
-            deaddropCell = ContactCell(style: .default, reuseIdentifier: "DeaddropCell")
+            cell.setBackupImage(name: contact.name, color: contact.color)
         }
-        deaddropCell.backgroundColor = DcColors.deaddropBackground
-        deaddropCell.contentView.backgroundColor = DcColors.deaddropBackground
-        return deaddropCell
+
+ */
     }
 
     func getArchiveCell(_ tableView: UITableView, title: String) -> UITableViewCell {
@@ -391,29 +400,12 @@ override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexP
     private func deleteChat(chatId: Int, animated: Bool) {
 
         if !animated {
-            dcContext.deleteChat(chatId: chatId)
-            self.getChatList()
+            viewModel.deleteChat(chatId: chatId)
+            viewModel.refreshData()
             return
         }
 
-        guard let chatList = chatList else {
-            return
-        }
-
-        // find index of chatId
-        let indexToDelete = Array(0..<chatList.length).filter { chatList.getChatId(index: $0) == chatId }.first
-
-        guard let row = indexToDelete else {
-            return
-        }
-
-        var gclFlags: Int32 = 0
-        if showArchive {
-            gclFlags |= DC_GCL_ARCHIVED_ONLY
-        }
-
-        dcContext.deleteChat(chatId: chatId)
-        self.chatList = dcContext.getChatlist(flags: gclFlags, queryString: nil, queryId: 0)
+        let row = viewModel.deleteChat(chatId: chatId)
         tableView.deleteRows(at: [IndexPath(row: row, section: 0)], with: .fade)
     }
 }

+ 28 - 9
deltachat-ios/ViewModel/ChatListViewModel.swift

@@ -10,12 +10,15 @@ protocol ChatListViewModelProtocol: class, UISearchResultsUpdating {
     func numberOfRowsIn(section: Int) -> Int
     func cellDataFor(section: Int, row: Int) -> AvatarCellViewModel
 
+    func msgIdFor(row: Int) -> Int?
+
     // search related
     var searchActive: Bool { get }
     func beginFiltering()
     func endFiltering()
 
-    func deleteChat(chatId: Int)
+    /// returns ROW of table
+    func deleteChat(chatId: Int) -> Int
     func archiveChat(chatId: Int)
     func refreshData()
 
@@ -37,16 +40,18 @@ class ChatListViewModel: NSObject, ChatListViewModelProtocol {
         self.isArchive = isArchive
         self.dcContext = dcContext
         super.init()
-        updateChatList()
+        updateChatList(notifyListener: true)
     }
 
-    private func updateChatList() {
+    private func updateChatList(notifyListener: Bool) {
         var gclFlags: Int32 = 0
         if isArchive {
             gclFlags |= DC_GCL_ARCHIVED_ONLY
         }
         self.chatList = dcContext.getChatlist(flags: gclFlags, queryString: nil, queryId: 0)
-        onChatListUpdate?()
+        if notifyListener {
+            onChatListUpdate?()
+        }
     }
 
     var numberOfSections: Int {
@@ -71,24 +76,38 @@ class ChatListViewModel: NSObject, ChatListViewModelProtocol {
         return viewModel
     }
 
+    func msgIdFor(row: Int) -> Int? {
+        if searchActive {
+            return nil
+        }
+        return chatList.getMsgId(index: row)
+    }
+
     func refreshData() {
-        updateChatList()
+        updateChatList(notifyListener: true)
     }
 
     func beginFiltering() {
-
+        searchActive = true
     }
 
     func endFiltering() {
-
+        searchActive = false
     }
 
-    func deleteChat(chatId: Int) {
+    func deleteChat(chatId: Int) -> Int {
 
+        // find index of chatId
+        let indexToDelete = Array(0..<chatList.length).filter { chatList.getChatId(index: $0) == chatId }.first
+        dcContext.deleteChat(chatId: chatId)
+        updateChatList(notifyListener: false)
+        safe_assert(indexToDelete != nil)
+        return indexToDelete ?? -1
     }
 
     func archiveChat(chatId: Int) {
-
+        dcContext.archiveChat(chatId: chatId, archive: !self.isArchive)
+        updateChatList(notifyListener: false)
     }
 
     var numberOfArchivedChats: Int {