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

cell actions now use viewModel

nayooti 5 жил өмнө
parent
commit
e0d620355f

+ 95 - 61
deltachat-ios/Controller/ChatListController.swift

@@ -200,70 +200,62 @@ class ChatListController: UITableViewController {
     }
 
     override func tableView(_: UITableView, didSelectRowAt indexPath: IndexPath) {
-        /*
-         let row = indexPath.row
-         guard let chatList = chatList else { return }
-         let chatId = chatList.getChatId(index: row)
-         if chatId == DC_CHAT_ID_DEADDROP {
-         let msgId = chatList.getMsgId(index: row)
-         let dcMsg = DcMsg(id: msgId)
-         let dcContact = DcContact(id: dcMsg.fromContactId)
-         let title = String.localizedStringWithFormat(String.localized("ask_start_chat_with"), dcContact.nameNAddr)
-         let alert = UIAlertController(title: title, message: nil, preferredStyle: .safeActionSheet)
-         alert.addAction(UIAlertAction(title: String.localized("start_chat"), style: .default, handler: { _ in
-         let chat = dcMsg.createChat()
-         self.coordinator?.showChat(chatId: chat.id)
-         }))
-         alert.addAction(UIAlertAction(title: String.localized("not_now"), style: .default, handler: { _ in
-         dcContact.marknoticed()
-         }))
-         alert.addAction(UIAlertAction(title: String.localized("menu_block_contact"), style: .destructive, handler: { _ in
-         dcContact.block()
-         }))
-         alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel))
-         present(alert, animated: true, completion: nil)
-         } else if chatId == DC_CHAT_ID_ARCHIVED_LINK {
-         coordinator?.showArchive()
-         } else {
-         coordinator?.showChat(chatId: chatId)
-         }
-         */
+        let cellData = viewModel.cellDataFor(section: indexPath.section, row: indexPath.row)
+        switch cellData.type {
+        case .CHAT(let chatData):
+            let chatId = chatData.chatId
+            if chatId == DC_CHAT_ID_DEADDROP {
+                guard let msgId = viewModel.msgIdFor(row: indexPath.row) else {
+                    return
+                }
+                showDeaddropRequestAlert(msgId: msgId)
+            } else if chatId == DC_CHAT_ID_ARCHIVED_LINK {
+                coordinator?.showArchive()
+            } else {
+                coordinator?.showChat(chatId: chatId)
+            }
+        case .CONTACT(let contactData):
+            let contactId = contactData.contactId
+            self.askToChatWith(contactId: contactId)
+        }
     }
 
     override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
-        /*
-         let row = indexPath.row
-         guard let chatList = chatList else {
-         return []
-         }
-
-         let chatId = chatList.getChatId(index: row)
-         if chatId==DC_CHAT_ID_ARCHIVED_LINK || chatId==DC_CHAT_ID_DEADDROP {
-         return []
-         // returning nil may result in a default delete action,
-         // see https://forums.developer.apple.com/thread/115030
-         }
-
-         let archive = UITableViewRowAction(style: .destructive, title: String.localized(showArchive ? "unarchive" : "archive")) { [unowned self] _, _ in
-         self.dcContext.archiveChat(chatId: chatId, archive: !self.showArchive)
-         }
-         archive.backgroundColor = UIColor.lightGray
-
-         let chat = dcContext.getChat(chatId: chatId)
-         let pinned = chat.visibility==DC_CHAT_VISIBILITY_PINNED
-         let pin = UITableViewRowAction(style: .destructive, title: String.localized(pinned ? "unpin" : "pin")) { [unowned self] _, _ in
-         self.dcContext.setChatVisibility(chatId: chatId, visibility: pinned ? DC_CHAT_VISIBILITY_NORMAL : DC_CHAT_VISIBILITY_PINNED)
-         }
-         pin.backgroundColor = UIColor.systemGreen
-
-         let delete = UITableViewRowAction(style: .normal, title: String.localized("delete")) { [unowned self] _, _ in
-         self.showDeleteChatConfirmationAlert(chatId: chatId)
-         }
-         delete.backgroundColor = UIColor.systemRed
-
-         return [archive, pin, delete]
-         */
-        return []
+
+        if viewModel.searchActive {
+            return []
+        }
+
+        guard let chatId = viewModel.chatIdFor(section: indexPath.section, row: indexPath.row) else {
+            return []
+        }
+
+        if chatId==DC_CHAT_ID_ARCHIVED_LINK || chatId==DC_CHAT_ID_DEADDROP {
+            return []
+            // returning nil may result in a default delete action,
+            // see https://forums.developer.apple.com/thread/115030
+        }
+        let archiveActionTitle: String = String.localized(viewModel.isArchive ? "unarchive" : "archive")
+
+        let archive = UITableViewRowAction(style: .destructive, title: archiveActionTitle) { [unowned self] _, _ in
+            self.viewModel.archiveChat(chatId: chatId)
+            self.updateArchivedCell()
+        }
+        archive.backgroundColor = UIColor.lightGray
+
+        let chat = DcChat(id: chatId)
+        let pinned = chat.visibility==DC_CHAT_VISIBILITY_PINNED
+        let pin = UITableViewRowAction(style: .destructive, title: String.localized(pinned ? "unpin" : "pin")) { [unowned self] _, _ in
+            self.viewModel.pinChat(chatId: chat.id)
+        }
+        pin.backgroundColor = UIColor.systemGreen
+
+        let delete = UITableViewRowAction(style: .normal, title: String.localized("delete")) { [unowned self] _, _ in
+            self.showDeleteChatConfirmationAlert(chatId: chatId)
+        }
+        delete.backgroundColor = UIColor.systemRed
+
+        return [archive, pin, delete]
     }
 
     // MARK: updates
@@ -322,6 +314,7 @@ class ChatListController: UITableViewController {
         return archiveCell
     }
 
+    // MARK: - alerts
     private func showDeleteChatConfirmationAlert(chatId: Int) {
         let alert = UIAlertController(
             title: nil,
@@ -335,6 +328,45 @@ class ChatListController: UITableViewController {
         self.present(alert, animated: true, completion: nil)
     }
 
+    private func showDeaddropRequestAlert(msgId: Int) {
+        let dcMsg = DcMsg(id: msgId)
+        let dcContact = DcContact(id: dcMsg.fromContactId)
+        let title = String.localizedStringWithFormat(String.localized("ask_start_chat_with"), dcContact.nameNAddr)
+        let alert = UIAlertController(title: title, message: nil, preferredStyle: .safeActionSheet)
+        alert.addAction(UIAlertAction(title: String.localized("start_chat"), style: .default, handler: { _ in
+            let chat = dcMsg.createChat()
+            self.coordinator?.showChat(chatId: chat.id)
+        }))
+        alert.addAction(UIAlertAction(title: String.localized("not_now"), style: .default, handler: { _ in
+            dcContact.marknoticed()
+        }))
+        alert.addAction(UIAlertAction(title: String.localized("menu_block_contact"), style: .destructive, handler: { _ in
+            dcContact.block()
+        }))
+        alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel))
+        present(alert, animated: true, completion: nil)
+    }
+
+    private func askToChatWith(contactId: Int) {
+        let dcContact = DcContact(id: contactId)
+        let alert = UIAlertController(
+            title: String.localizedStringWithFormat(String.localized("ask_start_chat_with"), dcContact.nameNAddr),
+            message: nil,
+            preferredStyle: .safeActionSheet)
+        alert.addAction(UIAlertAction(
+            title: String.localized("start_chat"),
+            style: .default,
+            handler: { _ in
+                self.coordinator?.showNewChat(contactId: contactId)
+        }))
+        alert.addAction(UIAlertAction(
+            title: String.localized("cancel"),
+            style: .cancel,
+            handler: { _ in
+        }))
+        self.present(alert, animated: true, completion: nil)
+    }
+
     private func deleteChat(chatId: Int, animated: Bool) {
 
         if !animated {
@@ -348,6 +380,8 @@ class ChatListController: UITableViewController {
     }
 }
 
+
+// MARK
 extension ChatListController: UISearchBarDelegate {
     func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
         viewModel.beginFiltering()

+ 5 - 0
deltachat-ios/Coordinator/AppCoordinator.swift

@@ -198,6 +198,11 @@ class ChatListCoordinator: Coordinator {
         controller.coordinator = coordinator
         navigationController.pushViewController(controller, animated: true)
     }
+
+    func showNewChat(contactId: Int) {
+        let chatId = dc_create_chat_by_contact_id(mailboxPointer, UInt32(contactId))
+        showChat(chatId: Int(chatId))
+    }
 }
 
 class SettingsCoordinator: Coordinator {

+ 21 - 2
deltachat-ios/ViewModel/ChatListViewModel.swift

@@ -11,7 +11,7 @@ protocol ChatListViewModelProtocol: class, UISearchResultsUpdating {
     func cellDataFor(section: Int, row: Int) -> AvatarCellViewModel
 
     func msgIdFor(row: Int) -> Int?
-
+    func chatIdFor(section: Int, row: Int) -> Int? // to differentiate betweeen deaddrop / archive / default
     // search related
     var searchActive: Bool { get }
     func beginFiltering()
@@ -20,6 +20,7 @@ protocol ChatListViewModelProtocol: class, UISearchResultsUpdating {
     /// returns ROW of table
     func deleteChat(chatId: Int) -> Int
     func archiveChat(chatId: Int)
+    func pinChat(chatId: Int)
     func refreshData()
 
     var numberOfArchivedChats: Int { get }
@@ -77,6 +78,16 @@ class ChatListViewModel: NSObject, ChatListViewModelProtocol {
         return viewModel
     }
 
+    func chatIdFor(section: Int, row: Int) -> Int? {
+        let cellData = cellDataFor(section: section, row: row)
+        switch cellData.type {
+        case .CHAT(let data):
+            return data.chatId
+        case .CONTACT:
+            return nil
+        }
+    }
+
     func msgIdFor(row: Int) -> Int? {
         if searchActive {
             return nil
@@ -111,8 +122,16 @@ class ChatListViewModel: NSObject, ChatListViewModelProtocol {
         updateChatList(notifyListener: false)
     }
 
+    func pinChat(chatId: Int) {
+        let chat = DcChat(id: chatId)
+        let pinned = chat.visibility==DC_CHAT_VISIBILITY_PINNED
+        self.dcContext.setChatVisibility(chatId: chatId, visibility: pinned ? DC_CHAT_VISIBILITY_NORMAL : DC_CHAT_VISIBILITY_PINNED)
+        updateChatList(notifyListener: false)
+    }
+
     var numberOfArchivedChats: Int {
-        return 0
+        let chatList = dcContext.getChatlist(flags: DC_GCL_ARCHIVED_ONLY, queryString: nil, queryId: 0)
+        return chatList.length
     }
 
 }