Przeglądaj źródła

always create chats from DcContext

cyberta 5 lat temu
rodzic
commit
7f57864b2f

+ 9 - 7
deltachat-ios/Controller/ChatListController.swift

@@ -3,6 +3,7 @@ import UIKit
 class ChatListController: UITableViewController {
 class ChatListController: UITableViewController {
     weak var coordinator: ChatListCoordinator?
     weak var coordinator: ChatListCoordinator?
     let viewModel: ChatListViewModelProtocol
     let viewModel: ChatListViewModelProtocol
+    let dcContext: DcContext
 
 
     private let chatCellReuseIdentifier = "chat_cell"
     private let chatCellReuseIdentifier = "chat_cell"
     private let deadDropCellReuseIdentifier = "deaddrop_cell"
     private let deadDropCellReuseIdentifier = "deaddrop_cell"
@@ -41,8 +42,9 @@ class ChatListController: UITableViewController {
         return cell
         return cell
     }
     }
 
 
-    init(viewModel: ChatListViewModelProtocol) {
+    init(dcContext: DcContext, viewModel: ChatListViewModelProtocol) {
         self.viewModel = viewModel
         self.viewModel = viewModel
+        self.dcContext = dcContext
         if viewModel.isArchive {
         if viewModel.isArchive {
             super.init(nibName: nil, bundle: nil)
             super.init(nibName: nil, bundle: nil)
         } else {
         } else {
@@ -175,21 +177,21 @@ class ChatListController: UITableViewController {
             guard let deaddropCell = tableView.dequeueReusableCell(withIdentifier: deadDropCellReuseIdentifier, for: indexPath) as? ContactCell else {
             guard let deaddropCell = tableView.dequeueReusableCell(withIdentifier: deadDropCellReuseIdentifier, for: indexPath) as? ContactCell else {
                 break
                 break
             }
             }
-            deaddropCell.updateCell(cellViewModel: cellData)
+            deaddropCell.updateCell(dcContext: dcContext, cellViewModel: cellData)
             return deaddropCell
             return deaddropCell
         case .chat(let chatData):
         case .chat(let chatData):
             let chatId = chatData.chatId
             let chatId = chatData.chatId
             if chatId == DC_CHAT_ID_ARCHIVED_LINK {
             if chatId == DC_CHAT_ID_ARCHIVED_LINK {
-                return getArchiveCell(title: DcChat(id: chatId).name)
+                return getArchiveCell(title: dcContext.getChat(chatId: chatId).name)
             } else if let chatCell = tableView.dequeueReusableCell(withIdentifier: chatCellReuseIdentifier, for: indexPath) as? ContactCell {
             } else if let chatCell = tableView.dequeueReusableCell(withIdentifier: chatCellReuseIdentifier, for: indexPath) as? ContactCell {
                 // default chatCell
                 // default chatCell
-                chatCell.updateCell(cellViewModel: cellData)
+                chatCell.updateCell(dcContext: dcContext, cellViewModel: cellData)
                 return chatCell
                 return chatCell
             }
             }
         case .contact:
         case .contact:
             safe_assert(viewModel.searchActive)
             safe_assert(viewModel.searchActive)
             if let contactCell = tableView.dequeueReusableCell(withIdentifier: contactCellReuseIdentifier, for: indexPath) as? ContactCell {
             if let contactCell = tableView.dequeueReusableCell(withIdentifier: contactCellReuseIdentifier, for: indexPath) as? ContactCell {
-                contactCell.updateCell(cellViewModel: cellData)
+                contactCell.updateCell(dcContext: dcContext, cellViewModel: cellData)
                 return contactCell
                 return contactCell
             }
             }
         }
         }
@@ -247,7 +249,7 @@ class ChatListController: UITableViewController {
         }
         }
         archiveAction.backgroundColor = UIColor.lightGray
         archiveAction.backgroundColor = UIColor.lightGray
 
 
-        let chat = DcChat(id: chatId)
+        let chat = dcContext.getChat(chatId: chatId)
         let pinned = chat.visibility==DC_CHAT_VISIBILITY_PINNED
         let pinned = chat.visibility==DC_CHAT_VISIBILITY_PINNED
         let pinAction = UITableViewRowAction(style: .destructive, title: String.localized(pinned ? "unpin" : "pin")) { [unowned self] _, _ in
         let pinAction = UITableViewRowAction(style: .destructive, title: String.localized(pinned ? "unpin" : "pin")) { [unowned self] _, _ in
             self.viewModel.pinChatToggle(chatId: chat.id)
             self.viewModel.pinChatToggle(chatId: chat.id)
@@ -313,7 +315,7 @@ class ChatListController: UITableViewController {
         let title = String.localizedStringWithFormat(String.localized("ask_start_chat_with"), dcContact.nameNAddr)
         let title = String.localizedStringWithFormat(String.localized("ask_start_chat_with"), dcContact.nameNAddr)
         let alert = UIAlertController(title: title, message: nil, preferredStyle: .safeActionSheet)
         let alert = UIAlertController(title: title, message: nil, preferredStyle: .safeActionSheet)
         alert.addAction(UIAlertAction(title: String.localized("start_chat"), style: .default, handler: { _ in
         alert.addAction(UIAlertAction(title: String.localized("start_chat"), style: .default, handler: { _ in
-            let chat = dcMsg.createChat()
+            let chat = self.dcContext.createChatByMessageId(msgId)
             self.coordinator?.showChat(chatId: chat.id)
             self.coordinator?.showChat(chatId: chat.id)
         }))
         }))
         alert.addAction(UIAlertAction(title: String.localized("not_now"), style: .default, handler: { _ in
         alert.addAction(UIAlertAction(title: String.localized("not_now"), style: .default, handler: { _ in

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

@@ -81,7 +81,7 @@ class ChatViewController: MessagesViewController {
     }
     }
 
 
     init(dcContext: DcContext, chatId: Int) {
     init(dcContext: DcContext, chatId: Int) {
-        let dcChat = DcChat(id: chatId)
+        let dcChat = dcContext.getChat(chatId: chatId)
         self.dcContext = dcContext
         self.dcContext = dcContext
         self.chatId = chatId
         self.chatId = chatId
         self.disableWriting = !dcChat.canSend
         self.disableWriting = !dcChat.canSend
@@ -148,7 +148,7 @@ class ChatViewController: MessagesViewController {
         navigationController?.navigationBar.addGestureRecognizer(navBarTap)
         navigationController?.navigationBar.addGestureRecognizer(navBarTap)
 
 
         if showCustomNavBar {
         if showCustomNavBar {
-            updateTitle(chat: DcChat(id: chatId))
+            updateTitle(chat: dcContext.getChat(chatId: chatId))
         }
         }
 
 
         configureMessageMenu()
         configureMessageMenu()
@@ -172,7 +172,7 @@ class ChatViewController: MessagesViewController {
                     }
                     }
                 }
                 }
                 if self.showCustomNavBar {
                 if self.showCustomNavBar {
-                    self.updateTitle(chat: DcChat(id: self.chatId))
+                    self.updateTitle(chat: self.dcContext.getChat(chatId: self.chatId))
                 }
                 }
             }
             }
         }
         }
@@ -241,7 +241,7 @@ class ChatViewController: MessagesViewController {
                 }
                 }
             },
             },
             completion: { _ in
             completion: { _ in
-                self.updateTitle(chat: DcChat(id: self.chatId))
+                self.updateTitle(chat: self.dcContext.getChat(chatId: self.chatId))
                 self.messagesCollectionView.reloadDataAndKeepOffset()
                 self.messagesCollectionView.reloadDataAndKeepOffset()
                 if lastSectionVisibleBeforeTransition {
                 if lastSectionVisibleBeforeTransition {
                     self.messagesCollectionView.scrollToBottom(animated: false)
                     self.messagesCollectionView.scrollToBottom(animated: false)
@@ -323,7 +323,7 @@ class ChatViewController: MessagesViewController {
 
 
     private func showEmptyStateView(_ show: Bool) {
     private func showEmptyStateView(_ show: Bool) {
         if show {
         if show {
-            let dcChat = DcChat(id: chatId)
+            let dcChat = dcContext.getChat(chatId: chatId)
             if chatId == DC_CHAT_ID_DEADDROP {
             if chatId == DC_CHAT_ID_DEADDROP {
                 if dcContext.showEmails != DC_SHOW_EMAILS_ALL {
                 if dcContext.showEmails != DC_SHOW_EMAILS_ALL {
                     emptyStateView.text = String.localized("chat_no_contact_requests")
                     emptyStateView.text = String.localized("chat_no_contact_requests")
@@ -640,7 +640,7 @@ class ChatViewController: MessagesViewController {
     }
     }
 
 
     private func askToForwardMessage() {
     private func askToForwardMessage() {
-        let chat = DcChat(id: self.chatId)
+        let chat = dcContext.getChat(chatId: self.chatId)
         if chat.isSelfTalk {
         if chat.isSelfTalk {
             RelayHelper.sharedInstance.forward(to: self.chatId)
             RelayHelper.sharedInstance.forward(to: self.chatId)
         } else {
         } else {
@@ -1242,7 +1242,7 @@ extension ChatViewController: MessageCellDelegate {
     @objc func didTapAvatar(in cell: MessageCollectionViewCell) {
     @objc func didTapAvatar(in cell: MessageCollectionViewCell) {
         if let indexPath = messagesCollectionView.indexPath(for: cell) {
         if let indexPath = messagesCollectionView.indexPath(for: cell) {
             let message = messageList[indexPath.section]
             let message = messageList[indexPath.section]
-            let chat = DcChat(id: chatId)
+            let chat = dcContext.getChat(chatId: chatId)
             coordinator?.showContactDetail(of: message.fromContact.id, in: chat.chatType, chatId: chatId)
             coordinator?.showContactDetail(of: message.fromContact.id, in: chat.chatType, chatId: chatId)
         }
         }
     }
     }

+ 4 - 4
deltachat-ios/Controller/GroupChatDetailViewController.swift

@@ -107,7 +107,7 @@ class GroupChatDetailViewController: UIViewController {
 
 
     init(chatId: Int, context: DcContext) {
     init(chatId: Int, context: DcContext) {
         self.context = context
         self.context = context
-        chat = DcChat(id: chatId)
+        chat = context.getChat(chatId: chatId)
         super.init(nibName: nil, bundle: nil)
         super.init(nibName: nil, bundle: nil)
         setupSubviews()
         setupSubviews()
     }
     }
@@ -137,7 +137,7 @@ class GroupChatDetailViewController: UIViewController {
     override func viewWillAppear(_ animated: Bool) {
     override func viewWillAppear(_ animated: Bool) {
         super.viewWillAppear(animated)
         super.viewWillAppear(animated)
         //update chat object, maybe chat name was edited
         //update chat object, maybe chat name was edited
-        chat = DcChat(id: chat.id)
+        chat = context.getChat(chatId: chat.id)
         updateGroupMembers()
         updateGroupMembers()
         tableView.reloadData() // to display updates
         tableView.reloadData() // to display updates
         editBarButtonItem.isEnabled = currentUser != nil
         editBarButtonItem.isEnabled = currentUser != nil
@@ -176,7 +176,7 @@ class GroupChatDetailViewController: UIViewController {
         } else {
         } else {
             self.navigationController?.popToRootViewController(animated: false)
             self.navigationController?.popToRootViewController(animated: false)
         }
         }
-        self.chat = DcChat(id: chat.id)
+        self.chat = context.getChat(chatId: chat.id)
      }
      }
 
 
     private func getGroupMemberIdFor(_ row: Int) -> Int {
     private func getGroupMemberIdFor(_ row: Int) -> Int {
@@ -259,7 +259,7 @@ extension GroupChatDetailViewController: UITableViewDelegate, UITableViewDataSou
                 chatId: context.getChatIdByContactId(contactId)
                 chatId: context.getChatIdByContactId(contactId)
             )
             )
             let cellViewModel = ContactCellViewModel(contactData: cellData)
             let cellViewModel = ContactCellViewModel(contactData: cellData)
-            contactCell.updateCell(cellViewModel: cellViewModel)
+            contactCell.updateCell(dcContext: context, cellViewModel: cellViewModel)
             return contactCell
             return contactCell
         case .chatActions:
         case .chatActions:
             if row == chatActionsRowArchiveChat {
             if row == chatActionsRowArchiveChat {

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

@@ -81,7 +81,7 @@ class AddGroupMembersViewController: GroupMembersViewController {
 
 
     private lazy var chat: DcChat? = {
     private lazy var chat: DcChat? = {
         if let chatId = chatId {
         if let chatId = chatId {
-            return DcChat(id: chatId)
+            return coordinator?.dcContext.getChat(chatId: chatId)
         }
         }
         return nil
         return nil
     }()
     }()

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

@@ -47,7 +47,7 @@ class MailboxViewController: ChatViewController {
             let title = String.localizedStringWithFormat(String.localized("ask_start_chat_with"), dcContact.nameNAddr)
             let title = String.localizedStringWithFormat(String.localized("ask_start_chat_with"), dcContact.nameNAddr)
             let alert = UIAlertController(title: title, message: nil, preferredStyle: .safeActionSheet)
             let alert = UIAlertController(title: title, message: nil, preferredStyle: .safeActionSheet)
             alert.addAction(UIAlertAction(title: String.localized("start_chat"), style: .default, handler: { _ in
             alert.addAction(UIAlertAction(title: String.localized("start_chat"), style: .default, handler: { _ in
-                let chat = message.createChat()
+                let chat = self.dcContext.createChatByMessageId(message.id)
                 self.coordinator?.showChat(chatId: chat.id)
                 self.coordinator?.showChat(chatId: chat.id)
             }))
             }))
             alert.addAction(UIAlertAction(title: String.localized("menu_block_contact"), style: .destructive, handler: { _ in
             alert.addAction(UIAlertAction(title: String.localized("menu_block_contact"), style: .destructive, handler: { _ in

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

@@ -186,7 +186,7 @@ class NewChatViewController: UITableViewController {
                 let cell = tableView.dequeueReusableCell(withIdentifier: "contactCell", for: indexPath)
                 let cell = tableView.dequeueReusableCell(withIdentifier: "contactCell", for: indexPath)
                 if let contactCell = cell as? ContactCell {
                 if let contactCell = cell as? ContactCell {
                     let contactCellViewModel = self.contactViewModelBy(row: indexPath.row)
                     let contactCellViewModel = self.contactViewModelBy(row: indexPath.row)
-                    contactCell.updateCell(cellViewModel: contactCellViewModel)
+                    contactCell.updateCell(dcContext: dcContext, cellViewModel: contactCellViewModel)
                 }
                 }
                 return cell
                 return cell
             } else {
             } else {
@@ -201,7 +201,7 @@ class NewChatViewController: UITableViewController {
             let cell = tableView.dequeueReusableCell(withIdentifier: "contactCell", for: indexPath)
             let cell = tableView.dequeueReusableCell(withIdentifier: "contactCell", for: indexPath)
             if let contactCell = cell as? ContactCell {
             if let contactCell = cell as? ContactCell {
                 let contactCellViewModel = self.contactViewModelBy(row: indexPath.row)
                 let contactCellViewModel = self.contactViewModelBy(row: indexPath.row)
-                contactCell.updateCell(cellViewModel: contactCellViewModel)
+                contactCell.updateCell(dcContext: dcContext, cellViewModel: contactCellViewModel)
             }
             }
             return cell
             return cell
         }
         }

+ 3 - 3
deltachat-ios/Controller/NewGroupController.swift

@@ -258,7 +258,7 @@ class NewGroupController: UITableViewController, MediaPickerDelegate {
         //swipe by delete
         //swipe by delete
         if section == sectionGroupMembers, groupContactIds[row] != DC_CONTACT_ID_SELF {
         if section == sectionGroupMembers, groupContactIds[row] != DC_CONTACT_ID_SELF {
             let delete = UITableViewRowAction(style: .destructive, title: String.localized("remove_desktop")) { [unowned self] _, indexPath in
             let delete = UITableViewRowAction(style: .destructive, title: String.localized("remove_desktop")) { [unowned self] _, indexPath in
-                if self.groupChatId != 0, DcChat(id: self.groupChatId).contactIds.contains(self.groupContactIds[row]) {
+                if self.groupChatId != 0, self.dcContext.getChat(chatId: self.groupChatId).contactIds.contains(self.groupContactIds[row]) {
                     let success = self.dcContext.removeContactFromChat(chatId: self.groupChatId, contactId: self.groupContactIds[row])
                     let success = self.dcContext.removeContactFromChat(chatId: self.groupChatId, contactId: self.groupContactIds[row])
                     if success {
                     if success {
                         self.removeGroupContactFromList(at: indexPath)
                         self.removeGroupContactFromList(at: indexPath)
@@ -315,7 +315,7 @@ class NewGroupController: UITableViewController, MediaPickerDelegate {
     }
     }
 
 
     func updateGroupContactIdsOnQRCodeInvite() {
     func updateGroupContactIdsOnQRCodeInvite() {
-        for contactId in DcChat(id: groupChatId).contactIds {
+        for contactId in dcContext.getChat(chatId: groupChatId).contactIds {
             contactIdsForGroup.insert(contactId)
             contactIdsForGroup.insert(contactId)
         }
         }
         groupContactIds = Array(contactIdsForGroup)
         groupContactIds = Array(contactIdsForGroup)
@@ -325,7 +325,7 @@ class NewGroupController: UITableViewController, MediaPickerDelegate {
     func updateGroupContactIdsOnListSelection(_ members: Set<Int>) {
     func updateGroupContactIdsOnListSelection(_ members: Set<Int>) {
         if groupChatId != 0 {
         if groupChatId != 0 {
             var members = members
             var members = members
-            for contactId in DcChat(id: groupChatId).contactIds {
+            for contactId in dcContext.getChat(chatId: groupChatId).contactIds {
                 members.insert(contactId)
                 members.insert(contactId)
             }
             }
         }
         }

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

@@ -106,7 +106,7 @@ class QrInviteViewController: UITableViewController {
     private func createDescriptionView() -> UIView {
     private func createDescriptionView() -> UIView {
         let label = UILabel.init()
         let label = UILabel.init()
         label.translatesAutoresizingMaskIntoConstraints = false
         label.translatesAutoresizingMaskIntoConstraints = false
-        let dcChat = DcChat(id: chatId)
+        let dcChat = dcContext.getChat(chatId: chatId)
         if !dcChat.name.isEmpty {
         if !dcChat.name.isEmpty {
             label.text = String.localizedStringWithFormat(String.localized("qrshow_join_group_hint"), dcChat.name)
             label.text = String.localizedStringWithFormat(String.localized("qrshow_join_group_hint"), dcChat.name)
         }
         }

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

@@ -47,7 +47,7 @@ class AppCoordinator: NSObject, Coordinator {
 
 
     private lazy var chatListController: UIViewController = {
     private lazy var chatListController: UIViewController = {
         let viewModel = ChatListViewModel(dcContext: dcContext, isArchive: false)
         let viewModel = ChatListViewModel(dcContext: dcContext, isArchive: false)
-        let controller = ChatListController(viewModel: viewModel)
+        let controller = ChatListController(dcContext: dcContext, viewModel: viewModel)
         let nav = UINavigationController(rootViewController: controller)
         let nav = UINavigationController(rootViewController: controller)
         let settingsImage = UIImage(named: "ic_chat")
         let settingsImage = UIImage(named: "ic_chat")
         nav.tabBarItem = UITabBarItem(title: String.localized("pref_chats"), image: settingsImage, tag: chatsTab)
         nav.tabBarItem = UITabBarItem(title: String.localized("pref_chats"), image: settingsImage, tag: chatsTab)
@@ -195,7 +195,7 @@ class ChatListCoordinator: Coordinator {
 
 
     func showArchive() {
     func showArchive() {
         let viewModel = ChatListViewModel(dcContext: dcContext, isArchive: true)
         let viewModel = ChatListViewModel(dcContext: dcContext, isArchive: true)
-        let controller = ChatListController(viewModel: viewModel)
+        let controller = ChatListController(dcContext: dcContext, viewModel: viewModel)
         let coordinator = ChatListCoordinator(dcContext: dcContext, navigationController: navigationController)
         let coordinator = ChatListCoordinator(dcContext: dcContext, navigationController: navigationController)
         childCoordinators.append(coordinator)
         childCoordinators.append(coordinator)
         controller.coordinator = coordinator
         controller.coordinator = coordinator
@@ -450,7 +450,7 @@ class GroupChatDetailCoordinator: Coordinator {
         func showArchive() {
         func showArchive() {
             self.navigationController.popToRootViewController(animated: false) // in main ChatList now
             self.navigationController.popToRootViewController(animated: false) // in main ChatList now
             let viewModel = ChatListViewModel(dcContext: dcContext, isArchive: true)
             let viewModel = ChatListViewModel(dcContext: dcContext, isArchive: true)
-            let controller = ChatListController(viewModel: viewModel)
+            let controller = ChatListController(dcContext: dcContext, viewModel: viewModel)
             let coordinator = ChatListCoordinator(dcContext: dcContext, navigationController: navigationController)
             let coordinator = ChatListCoordinator(dcContext: dcContext, navigationController: navigationController)
             childCoordinators.append(coordinator)
             childCoordinators.append(coordinator)
             controller.coordinator = coordinator
             controller.coordinator = coordinator
@@ -460,7 +460,7 @@ class GroupChatDetailCoordinator: Coordinator {
         CATransaction.begin()
         CATransaction.begin()
         CATransaction.setCompletionBlock(notifyToDeleteChat)
         CATransaction.setCompletionBlock(notifyToDeleteChat)
 
 
-        let chat = DcChat(id: chatId)
+        let chat = dcContext.getChat(chatId: chatId)
         if chat.isArchived {
         if chat.isArchived {
             showArchive()
             showArchive()
         } else {
         } else {
@@ -491,7 +491,7 @@ class ChatViewCoordinator: NSObject, Coordinator {
     }
     }
 
 
     func showChatDetail(chatId: Int) {
     func showChatDetail(chatId: Int) {
-        let chat = DcChat(id: chatId)
+        let chat = dcContext.getChat(chatId: chatId)
         switch chat.chatType {
         switch chat.chatType {
         case .SINGLE:
         case .SINGLE:
             if let contactId = chat.contactIds.first {
             if let contactId = chat.contactIds.first {
@@ -719,7 +719,7 @@ class ContactDetailCoordinator: Coordinator, ContactDetailCoordinatorProtocol {
         func showArchive() {
         func showArchive() {
             self.navigationController.popToRootViewController(animated: false) // in main ChatList now
             self.navigationController.popToRootViewController(animated: false) // in main ChatList now
             let viewModel = ChatListViewModel(dcContext: dcContext, isArchive: true)
             let viewModel = ChatListViewModel(dcContext: dcContext, isArchive: true)
-            let controller = ChatListController(viewModel: viewModel)
+            let controller = ChatListController(dcContext: dcContext, viewModel: viewModel)
             let coordinator = ChatListCoordinator(dcContext: dcContext, navigationController: navigationController)
             let coordinator = ChatListCoordinator(dcContext: dcContext, navigationController: navigationController)
             childCoordinators.append(coordinator)
             childCoordinators.append(coordinator)
             controller.coordinator = coordinator
             controller.coordinator = coordinator
@@ -729,7 +729,7 @@ class ContactDetailCoordinator: Coordinator, ContactDetailCoordinatorProtocol {
         CATransaction.begin()
         CATransaction.begin()
         CATransaction.setCompletionBlock(notifyToDeleteChat)
         CATransaction.setCompletionBlock(notifyToDeleteChat)
 
 
-        let chat = DcChat(id: chatId)
+        let chat = dcContext.getChat(chatId: chatId)
         if chat.isArchived {
         if chat.isArchived {
             showArchive()
             showArchive()
         } else {
         } else {

+ 16 - 19
deltachat-ios/DC/Wrapper.swift

@@ -3,7 +3,7 @@ import UIKit
 import AVFoundation
 import AVFoundation
 
 
 class DcContext {
 class DcContext {
-    let contextPointer: OpaquePointer?
+    let contextPointer: OpaquePointer
 
 
     init() {
     init() {
         var version = ""
         var version = ""
@@ -27,20 +27,20 @@ class DcContext {
     }
     }
 
 
     func getContacts(flags: Int32, queryString: String? = nil) -> [Int] {
     func getContacts(flags: Int32, queryString: String? = nil) -> [Int] {
-        let cContacts = dc_get_contacts(self.contextPointer, UInt32(flags), queryString)
+        let cContacts = dc_get_contacts(contextPointer, UInt32(flags), queryString)
         return Utils.copyAndFreeArray(inputArray: cContacts)
         return Utils.copyAndFreeArray(inputArray: cContacts)
     }
     }
 
 
     func addContacts(contactString: String) {
     func addContacts(contactString: String) {
-        dc_add_address_book(mailboxPointer, contactString)
+        dc_add_address_book(contextPointer, contactString)
     }
     }
 
 
     func getChat(chatId: Int) -> DcChat {
     func getChat(chatId: Int) -> DcChat {
-        return DcChat(id: chatId)
+        return DcChat(contextPointer, id: chatId)
     }
     }
 
 
     func getChatIdByContactId(_ contactId: Int) -> Int? {
     func getChatIdByContactId(_ contactId: Int) -> Int? {
-        let chatId = dc_get_chat_id_by_contact_id(self.contextPointer, UInt32(contactId))
+        let chatId = dc_get_chat_id_by_contact_id(contextPointer, UInt32(contactId))
         if chatId == 0 {
         if chatId == 0 {
             return nil
             return nil
         } else {
         } else {
@@ -48,6 +48,11 @@ class DcContext {
         }
         }
     }
     }
 
 
+    func createChatByMessageId(_ messageId: Int) -> DcChat {
+        let chatId = dc_create_chat_by_msg_id(contextPointer, UInt32(messageId))
+        return DcChat(contextPointer, id: Int(chatId))
+    }
+
     func getChatlist(flags: Int32, queryString: String?, queryId: Int) -> DcChatlist {
     func getChatlist(flags: Int32, queryString: String?, queryId: Int) -> DcChatlist {
         let chatlistPointer = dc_get_chatlist(contextPointer, flags, queryString, UInt32(queryId))
         let chatlistPointer = dc_get_chatlist(contextPointer, flags, queryString, UInt32(queryId))
         let chatlist = DcChatlist(chatListPointer: chatlistPointer)
         let chatlist = DcChatlist(chatListPointer: chatlistPointer)
@@ -271,18 +276,12 @@ class DcContext {
         return messageIds
         return messageIds
     }
     }
 
 
-    // it is fine to use existing functionality of DcConfig,
-    // however, as DcConfig uses a global pointer,
-    // new functionality should be added to DcContext.
-
     // also, there is no much worth in adding a separate function or so
     // also, there is no much worth in adding a separate function or so
     // for each config option - esp. if they are just forwarded to the core
     // for each config option - esp. if they are just forwarded to the core
     // and set/get only at one line of code each.
     // and set/get only at one line of code each.
     // this adds a complexity that can be avoided -
     // this adds a complexity that can be avoided -
     // and makes grep harder as these names are typically named following different guidelines.
     // and makes grep harder as these names are typically named following different guidelines.
 
 
-
-
     var displayname: String? {
     var displayname: String? {
         set { setConfig("displayname", newValue) }
         set { setConfig("displayname", newValue) }
         get { return getConfig("displayname") }
         get { return getConfig("displayname") }
@@ -466,11 +465,13 @@ class DcChatlist {
 
 
 class DcChat {
 class DcChat {
     var chatPointer: OpaquePointer?
     var chatPointer: OpaquePointer?
+    var contextPointer: OpaquePointer?
 
 
     // use DcContext.getChat() instead of calling the constructor directly
     // use DcContext.getChat() instead of calling the constructor directly
-    init(id: Int) {
-        if let p = dc_get_chat(mailboxPointer, UInt32(id)) {
+    init(_ contextPointer: OpaquePointer, id: Int) {
+        if let p = dc_get_chat(contextPointer, UInt32(id)) {
             chatPointer = p
             chatPointer = p
+            self.contextPointer = contextPointer
         } else {
         } else {
             fatalError("Invalid chatID opened \(id)")
             fatalError("Invalid chatID opened \(id)")
         }
         }
@@ -478,6 +479,7 @@ class DcChat {
 
 
     deinit {
     deinit {
         dc_chat_unref(chatPointer)
         dc_chat_unref(chatPointer)
+        self.contextPointer = nil
     }
     }
 
 
     var id: Int {
     var id: Int {
@@ -537,7 +539,7 @@ class DcChat {
     }
     }
 
 
     var contactIds: [Int] {
     var contactIds: [Int] {
-        return Utils.copyAndFreeArray(inputArray: dc_get_chat_contacts(mailboxPointer, UInt32(id)))
+        return Utils.copyAndFreeArray(inputArray: dc_get_chat_contacts(contextPointer, UInt32(id)))
     }
     }
 
 
     lazy var profileImage: UIImage? = { [unowned self] in
     lazy var profileImage: UIImage? = { [unowned self] in
@@ -891,11 +893,6 @@ class DcMsg: MessageType {
         return dc_msg_get_showpadlock(messagePointer) == 1
         return dc_msg_get_showpadlock(messagePointer) == 1
     }
     }
 
 
-    func createChat() -> DcChat {
-        let chatId = dc_create_chat_by_msg_id(mailboxPointer, UInt32(id))
-        return DcChat(id: Int(chatId))
-    }
-
     func sendInChat(id: Int) {
     func sendInChat(id: Int) {
         dc_send_msg(mailboxPointer, UInt32(id), messagePointer)
         dc_send_msg(mailboxPointer, UInt32(id), messagePointer)
     }
     }

+ 2 - 2
deltachat-ios/View/ContactCell.swift

@@ -231,7 +231,7 @@ class ContactCell: UITableViewCell {
     }
     }
 
 
     // use this update-method to update cell in cellForRowAt whenever it is possible - other set-methods will be set private in progress
     // use this update-method to update cell in cellForRowAt whenever it is possible - other set-methods will be set private in progress
-    func updateCell(cellViewModel: AvatarCellViewModel) {
+    func updateCell(dcContext: DcContext, cellViewModel: AvatarCellViewModel) {
 
 
         // subtitle
         // subtitle
         subtitleLabel.attributedText = cellViewModel.subtitle.boldAt(indexes: cellViewModel.subtitleHighlightIndexes, fontSize: subtitleLabel.font.pointSize)
         subtitleLabel.attributedText = cellViewModel.subtitle.boldAt(indexes: cellViewModel.subtitleHighlightIndexes, fontSize: subtitleLabel.font.pointSize)
@@ -252,7 +252,7 @@ class ContactCell: UITableViewCell {
             titleLabel.attributedText = cellViewModel.title.boldAt(indexes: cellViewModel.titleHighlightIndexes, fontSize: titleLabel.font.pointSize)
             titleLabel.attributedText = cellViewModel.title.boldAt(indexes: cellViewModel.titleHighlightIndexes, fontSize: titleLabel.font.pointSize)
 
 
         case .chat(let chatData):
         case .chat(let chatData):
-            let chat = DcChat(id: chatData.chatId)
+            let chat = dcContext.getChat(chatId: chatData.chatId)
 
 
             // text bold if chat contains unread messages - otherwise hightlight search results if needed
             // text bold if chat contains unread messages - otherwise hightlight search results if needed
             if chatData.unreadMessages > 0 {
             if chatData.unreadMessages > 0 {

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

@@ -200,7 +200,7 @@ private extension ChatListViewModel {
 
 
 
 
         if let msgId = msgIdFor(row: index), chatId == DC_CHAT_ID_DEADDROP {
         if let msgId = msgIdFor(row: index), chatId == DC_CHAT_ID_DEADDROP {
-            return ChatCellViewModel(dearddropCellData: DeaddropCellData(chatId: chatId, msgId: msgId, summary: summary))
+            return ChatCellViewModel(dcContext: dcContext, deaddropCellData: DeaddropCellData(chatId: chatId, msgId: msgId, summary: summary))
         }
         }
 
 
         let chat = dcContext.getChat(chatId: chatId)
         let chat = dcContext.getChat(chatId: chatId)
@@ -213,6 +213,7 @@ private extension ChatListViewModel {
         }
         }
 
 
         let viewModel = ChatCellViewModel(
         let viewModel = ChatCellViewModel(
+            dcContext: dcContext,
             chatData: ChatCellData(
             chatData: ChatCellData(
                 chatId: chatId,
                 chatId: chatId,
                 summary: summary,
                 summary: summary,
@@ -226,11 +227,12 @@ private extension ChatListViewModel {
     func makeMessageCellViewModel(msgId: Int) -> AvatarCellViewModel {
     func makeMessageCellViewModel(msgId: Int) -> AvatarCellViewModel {
         let msg: DcMsg = DcMsg(id: msgId)
         let msg: DcMsg = DcMsg(id: msgId)
         let chatId: Int = msg.chatId
         let chatId: Int = msg.chatId
-        let chat: DcChat = DcChat(id: chatId)
+        let chat: DcChat = dcContext.getChat(chatId: chatId)
         let summary: DcLot = msg.summary(chat: chat)
         let summary: DcLot = msg.summary(chat: chat)
         let unreadMessages = dcContext.getUnreadMessages(chatId: chatId)
         let unreadMessages = dcContext.getUnreadMessages(chatId: chatId)
 
 
         let viewModel = ChatCellViewModel(
         let viewModel = ChatCellViewModel(
+            dcContext: dcContext,
             chatData: ChatCellData(
             chatData: ChatCellData(
                 chatId: chatId,
                 chatId: chatId,
                 summary: summary,
                 summary: summary,

+ 4 - 4
deltachat-ios/ViewModel/ContactCellViewModel.swift

@@ -84,19 +84,19 @@ class ChatCellViewModel: AvatarCellViewModel {
     var titleHighlightIndexes: [Int]
     var titleHighlightIndexes: [Int]
     var subtitleHighlightIndexes: [Int]
     var subtitleHighlightIndexes: [Int]
 
 
-    init(chatData: ChatCellData, titleHighlightIndexes: [Int] = [], subtitleHighlightIndexes: [Int] = []) {
+    init(dcContext: DcContext, chatData: ChatCellData, titleHighlightIndexes: [Int] = [], subtitleHighlightIndexes: [Int] = []) {
         self.type = CellModel.chat(chatData)
         self.type = CellModel.chat(chatData)
         self.titleHighlightIndexes = titleHighlightIndexes
         self.titleHighlightIndexes = titleHighlightIndexes
         self.subtitleHighlightIndexes = subtitleHighlightIndexes
         self.subtitleHighlightIndexes = subtitleHighlightIndexes
         self.summary = chatData.summary
         self.summary = chatData.summary
-        self.chat = DcChat(id: chatData.chatId)
+        self.chat = dcContext.getChat(chatId: chatData.chatId)
     }
     }
 
 
-    init(dearddropCellData cellData: DeaddropCellData) {
+    init(dcContext: DcContext, deaddropCellData cellData: DeaddropCellData) {
         self.type = CellModel.deaddrop(cellData)
         self.type = CellModel.deaddrop(cellData)
         self.titleHighlightIndexes = []
         self.titleHighlightIndexes = []
         self.subtitleHighlightIndexes = []
         self.subtitleHighlightIndexes = []
-        self.chat = DcChat(id: cellData.chatId)
+        self.chat = dcContext.getChat(chatId: cellData.chatId)
         self.summary = cellData.summary
         self.summary = cellData.summary
     }
     }
 }
 }

+ 3 - 3
deltachat-ios/ViewModel/ContactDetailViewModel.swift

@@ -88,7 +88,7 @@ class ContactDetailViewModel: ContactDetailViewModelProtocol {
            // safe_fatalError("This is a ContactDetail view with no chat id")
            // safe_fatalError("This is a ContactDetail view with no chat id")
             return false
             return false
         }
         }
-        return DcChat(id: chatId).isArchived
+        return context.getChat(chatId: chatId).isArchived
     }
     }
 
 
     var numberOfSections: Int {
     var numberOfSections: Int {
@@ -116,8 +116,8 @@ class ContactDetailViewModel: ContactDetailViewModelProtocol {
         let unreadMessages = context.getUnreadMessages(chatId: chatId)
         let unreadMessages = context.getUnreadMessages(chatId: chatId)
 
 
         let cellData = ChatCellData(chatId: chatId, summary: summary, unreadMessages: unreadMessages)
         let cellData = ChatCellData(chatId: chatId, summary: summary, unreadMessages: unreadMessages)
-        let cellViewModel = ChatCellViewModel(chatData: cellData)
-        cell.updateCell(cellViewModel: cellViewModel)
+        let cellViewModel = ChatCellViewModel(dcContext: context, chatData: cellData)
+        cell.updateCell(dcContext: context, cellViewModel: cellViewModel)
     }
     }
 
 
     func titleFor(section: Int) -> String? {
     func titleFor(section: Int) -> String? {