Browse Source

streamline createChatByContactId()

B. Petersen 5 years ago
parent
commit
83ffba28aa

+ 1 - 1
deltachat-ios/Controller/ChatViewController.swift

@@ -601,7 +601,7 @@ class ChatViewController: MessagesViewController {
                           actionHandler: { _ in
                             self.dismiss(animated: true, completion: nil)
                             let contactId = self.dcContext.createContact(name: "", email: email)
-                            let chatId = self.dcContext.createChat(contactId: contactId)
+                            let chatId = self.dcContext.createChatByContactId(contactId: contactId)
                             self.coordinator?.showChat(chatId: chatId)})
     }
 

+ 1 - 1
deltachat-ios/Controller/ContactDetailViewController.swift

@@ -268,7 +268,7 @@ extension ContactDetailViewController {
                                       preferredStyle: .safeActionSheet)
         alert.addAction(UIAlertAction(title: String.localized("start_chat"), style: .default, handler: { _ in
             self.dismiss(animated: true, completion: nil)
-            let chatId = Int(dc_create_chat_by_contact_id(mailboxPointer, UInt32(contactId)))
+            let chatId = self.viewModel.context.createChatByContactId(contactId: contactId)
             self.coordinator?.showChat(chatId: chatId)
         }))
         alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: { _ in

+ 2 - 2
deltachat-ios/Controller/EditContactController.swift

@@ -5,8 +5,8 @@ class EditContactController: NewContactController {
     // for editing existing contacts (only
     // the name may be edited, therefore disable
     // the email field)
-    init(contactIdForUpdate: Int) {
-        super.init()
+    init(dcContext: DcContext, contactIdForUpdate: Int) {
+        super.init(dcContext: dcContext)
         title = String.localized("edit_contact")
 
         let contact = DcContact(id: contactIdForUpdate)

+ 5 - 3
deltachat-ios/Controller/NewContactController.swift

@@ -2,6 +2,7 @@ import UIKit
 
 class NewContactController: UITableViewController {
 
+    private let dcContext: DcContext
     weak var coordinator: EditContactCoordinatorProtocol?
     var openChatOnSave = true
 
@@ -27,7 +28,8 @@ class NewContactController: UITableViewController {
     let cells: [UITableViewCell]
 
     // for creating a new contact
-    init() {
+    init(dcContext: DcContext) {
+        self.dcContext = dcContext
         cells = [emailCell, nameCell]
         super.init(style: .grouped)
         emailCell.textField.delegate = self
@@ -74,8 +76,8 @@ class NewContactController: UITableViewController {
     }
 
     @objc func saveContactButtonPressed() {
-        let contactId = dc_create_contact(mailboxPointer, model.name, model.email)
-        let chatId = Int(dc_create_chat_by_contact_id(mailboxPointer, UInt32(contactId)))
+        let contactId = dcContext.createContact(name: model.name, email: model.email)
+        let chatId = dcContext.createChatByContactId(contactId: contactId)
         if openChatOnSave {
             coordinator?.showChat(chatId: chatId)
         } else {

+ 2 - 2
deltachat-ios/Controller/QrViewController.swift

@@ -169,7 +169,7 @@ class QrViewController: UITableViewController, QrCodeReaderDelegate {
             let msg = String.localizedStringWithFormat(String.localized(state==DC_QR_ADDR ? "ask_start_chat_with" : "qrshow_x_verified"), nameAndAddress)
             let alert = UIAlertController(title: msg, message: nil, preferredStyle: .alert)
             alert.addAction(UIAlertAction(title: String.localized("start_chat"), style: .default, handler: { _ in
-                let chatId = self.dcContext.createChat(contactId: qrParsed.id)
+                let chatId = self.dcContext.createChatByContactId(contactId: qrParsed.id)
                 self.coordinator?.showChat(chatId: chatId)
             }))
             alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .default, handler: nil))
@@ -311,7 +311,7 @@ class QrViewController: UITableViewController, QrCodeReaderDelegate {
     }
 
     func displayNewChat(contactId: Int) {
-        let chatId = dc_create_chat_by_contact_id(mailboxPointer, UInt32(contactId))
+        let chatId = dcContext.createChatByContactId(contactId: contactId)
         let chatVC = ChatViewController(dcContext: dcContext, chatId: Int(chatId))
 
         chatVC.hidesBottomBarWhenPushed = true

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

@@ -310,7 +310,7 @@ class NewChatCoordinator: Coordinator {
     }
 
     func showNewContactController() {
-        let newContactController = NewContactController()
+        let newContactController = NewContactController(dcContext: dcContext)
         let coordinator = EditContactCoordinator(dcContext: dcContext, navigationController: navigationController)
         childCoordinators.append(coordinator)
         newContactController.coordinator = coordinator
@@ -318,7 +318,7 @@ class NewChatCoordinator: Coordinator {
     }
 
     func showNewChat(contactId: Int) {
-        let chatId = dc_create_chat_by_contact_id(mailboxPointer, UInt32(contactId))
+        let chatId = dcContext.createChatByContactId(contactId: contactId)
         showChat(chatId: Int(chatId))
     }
 
@@ -358,7 +358,7 @@ class GroupChatDetailCoordinator: Coordinator {
     }
 
     func showSingleChatEdit(contactId: Int) {
-        let editContactController = EditContactController(contactIdForUpdate: contactId)
+        let editContactController = EditContactController(dcContext: dcContext, contactIdForUpdate: contactId)
         let coordinator = EditContactCoordinator(dcContext: dcContext, navigationController: navigationController)
         childCoordinators.append(coordinator)
         editContactController.coordinator = coordinator
@@ -549,7 +549,7 @@ class AddGroupMembersCoordinator: Coordinator {
     }
 
     func showNewContactController() {
-        let newContactController = NewContactController()
+        let newContactController = NewContactController(dcContext: dcContext)
         newContactController.openChatOnSave = false
         let coordinator = EditContactCoordinator(dcContext: dcContext, navigationController: navigationController)
         childCoordinators.append(coordinator)
@@ -643,7 +643,7 @@ class ContactDetailCoordinator: Coordinator, ContactDetailCoordinatorProtocol {
     }
 
     func showEditContact(contactId: Int) {
-        let editContactController = EditContactController(contactIdForUpdate: contactId)
+        let editContactController = EditContactController(dcContext: dcContext, contactIdForUpdate: contactId)
         let coordinator = EditContactCoordinator(dcContext: dcContext, navigationController: navigationController)
         childCoordinators.append(coordinator)
         editContactController.coordinator = coordinator

+ 5 - 1
deltachat-ios/DC/Wrapper.swift

@@ -55,10 +55,14 @@ class DcContext {
     }
 
     @discardableResult
-    func createChat(contactId: Int) -> Int {
+    func createChatByContactId(contactId: Int) -> Int {
         return Int(dc_create_chat_by_contact_id(contextPointer, UInt32(contactId)))
     }
 
+    func getChatIdByContactId(contactId: Int) -> Int {
+        return Int(dc_get_chat_id_by_contact_id(contextPointer, UInt32(contactId)))
+    }
+
     func createGroupChat(verified: Bool, name: String) -> Int {
         return Int(dc_create_group_chat(contextPointer, verified ? 1 : 0, name))
     }

+ 1 - 0
deltachat-ios/ViewModel/ContactDetailViewModel.swift

@@ -1,6 +1,7 @@
 import UIKit
 
 protocol ContactDetailViewModelProtocol {
+    var context: DcContext { get }
     var contactId: Int { get }
     var contact: DcContact { get }
     var numberOfSections: Int { get }