Răsfoiți Sursa

remove ContactDetailCoordinator

B. Petersen 5 ani în urmă
părinte
comite
0d75ee657f

+ 89 - 10
deltachat-ios/Controller/ContactDetailViewController.swift

@@ -1,9 +1,10 @@
 import UIKit
+import DcCore
 
 // this is also used as ChatDetail for SingleChats
 class ContactDetailViewController: UITableViewController {
-    weak var coordinator: ContactDetailCoordinator?
     private let viewModel: ContactDetailViewModelProtocol
+    private var previewController: PreviewController?
 
     private lazy var headerCell: ContactDetailHeader = {
         let cell = ContactDetailHeader()
@@ -150,7 +151,7 @@ class ContactDetailViewController: UITableViewController {
             chatWith(contactId: contactId)
         case .sharedChats:
             let chatId = viewModel.getSharedChatIdAt(indexPath: indexPath)
-            coordinator?.showChat(chatId: chatId)
+            showChat(chatId: chatId)
         }
     }
 
@@ -200,9 +201,9 @@ class ContactDetailViewController: UITableViewController {
         let action = viewModel.attachmentActionFor(row: index)
         switch action {
         case .documents:
-            coordinator?.showDocuments()
+            showDocuments()
         case .gallery:
-            coordinator?.showGallery()
+            showGallery()
         }
     }
 
@@ -222,12 +223,11 @@ class ContactDetailViewController: UITableViewController {
 
 
     @objc private func editButtonPressed() {
-        coordinator?.showEditContact(contactId: viewModel.contactId)
+        showEditContact(contactId: viewModel.contactId)
     }
-}
 
-// MARK: alerts
-extension ContactDetailViewController {
+    // MARK: alerts
+
     private func showDeleteChatConfirmationAlert() {
         let alert = UIAlertController(
             title: nil,
@@ -235,7 +235,7 @@ extension ContactDetailViewController {
             preferredStyle: .safeActionSheet
         )
         alert.addAction(UIAlertAction(title: String.localized("menu_delete_chat"), style: .destructive, handler: { _ in
-            self.coordinator?.deleteChat()
+            self.deleteChat()
         }))
         alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
         self.present(alert, animated: true, completion: nil)
@@ -273,7 +273,86 @@ extension ContactDetailViewController {
 
     private func chatWith(contactId: Int) {
         let chatId = self.viewModel.context.createChatByContactId(contactId: contactId)
-        self.coordinator?.showChat(chatId: chatId)
+        self.showChat(chatId: chatId)
+    }
+
+
+    // MARK: - coordinator
+
+    func showChat(chatId: Int) {
+        if let navigationController = self.parent as? UINavigationController {
+            let chatViewController = ChatViewController(dcContext: viewModel.context, chatId: chatId)
+            let coordinator = ChatViewCoordinator(dcContext: viewModel.context, navigationController: navigationController, chatId: chatId)
+            chatViewController.coordinator = coordinator
+            navigationController.popToRootViewController(animated: false)
+            navigationController.pushViewController(chatViewController, animated: true)
+        }
+    }
+
+    func showEditContact(contactId: Int) {
+        if let navigationController = self.parent as? UINavigationController {
+            let editContactController = EditContactController(dcContext: viewModel.context, contactIdForUpdate: contactId)
+            navigationController.pushViewController(editContactController, animated: true)
+        }
+    }
+
+    func showDocuments() {
+        presentPreview(for: DC_MSG_FILE, messageType2: DC_MSG_AUDIO, messageType3: 0)
+    }
+
+    func showGallery() {
+        presentPreview(for: DC_MSG_IMAGE, messageType2: DC_MSG_GIF, messageType3: DC_MSG_VIDEO)
+    }
+
+    private func presentPreview(for messageType: Int32, messageType2: Int32, messageType3: Int32) {
+        guard let chatId = viewModel.chatId else { return }
+        let messageIds = viewModel.context.getChatMedia(chatId: chatId, messageType: messageType, messageType2: messageType2, messageType3: messageType3)
+        var mediaUrls: [URL] = []
+        for messageId in messageIds {
+            let message = DcMsg.init(id: messageId)
+            if let url = message.fileURL {
+                mediaUrls.insert(url, at: 0)
+            }
+        }
+        previewController = PreviewController(currentIndex: 0, urls: mediaUrls)
+        if let navigationController = self.parent as? UINavigationController, let previewController = previewController {
+            navigationController.pushViewController(previewController, animated: true)
+        }
+    }
+
+
+    func deleteChat() {
+        guard let chatId = viewModel.chatId, let navigationController = self.parent as? UINavigationController else {
+            return
+        }
+
+        /*
+        app will navigate to chatlist or archive and delete the chat there
+        notify chatList/archiveList to delete chat AFTER is is visible
+        */
+        func notifyToDeleteChat() {
+            NotificationCenter.default.post(name: dcNotificationChatDeletedInChatDetail, object: nil, userInfo: ["chat_id": chatId])
+        }
+
+        func showArchive() {
+            navigationController.popToRootViewController(animated: false) // in main ChatList now
+            let chatlistVM = ChatListViewModel(dcContext: viewModel.context, isArchive: true)
+            let controller = ChatListController(dcContext: viewModel.context, viewModel: chatlistVM)
+            let coordinator = ChatListCoordinator(dcContext: viewModel.context, navigationController: navigationController)
+            controller.coordinator = coordinator
+            navigationController.pushViewController(controller, animated: false)
+        }
+
+        CATransaction.begin()
+        CATransaction.setCompletionBlock(notifyToDeleteChat)
+
+        let chat = viewModel.context.getChat(chatId: chatId)
+        if chat.isArchived {
+            showArchive()
+        } else {
+            navigationController.popToRootViewController(animated: true) // in main ChatList now
+        }
+        CATransaction.commit()
     }
 
 }

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

@@ -443,9 +443,6 @@ class NewChatCoordinator: Coordinator {
     func showContactDetail(contactId: Int) {
         let viewModel = ContactDetailViewModel(contactId: contactId, chatId: nil, context: dcContext)
         let contactDetailController = ContactDetailViewController(viewModel: viewModel)
-        let coordinator = ContactDetailCoordinator(dcContext: dcContext, chatId: nil, navigationController: navigationController)
-        childCoordinators.append(coordinator)
-        contactDetailController.coordinator = coordinator
         navigationController.pushViewController(contactDetailController, animated: true)
     }
     
@@ -495,9 +492,6 @@ class GroupChatDetailCoordinator: Coordinator {
     func showContactDetail(of contactId: Int) {
         let viewModel = ContactDetailViewModel(contactId: contactId, chatId: nil, context: dcContext)
         let contactDetailController = ContactDetailViewController(viewModel: viewModel)
-        let coordinator = ContactDetailCoordinator(dcContext: dcContext, chatId: nil, navigationController: navigationController)
-        childCoordinators.append(coordinator)
-        contactDetailController.coordinator = coordinator
         navigationController.pushViewController(contactDetailController, animated: true)
     }
 
@@ -584,9 +578,6 @@ class ChatViewCoordinator: NSObject, Coordinator {
             if let contactId = chat.contactIds.first {
                 let viewModel = ContactDetailViewModel(contactId: contactId, chatId: chatId, context: dcContext)
                 let contactDetailController = ContactDetailViewController(viewModel: viewModel)
-                let coordinator = ContactDetailCoordinator(dcContext: dcContext, chatId: chatId, navigationController: navigationController)
-                childCoordinators.append(coordinator)
-                contactDetailController.coordinator = coordinator
                 navigationController.pushViewController(contactDetailController, animated: true)
             }
         case .GROUP, .VERIFIEDGROUP:
@@ -601,9 +592,6 @@ class ChatViewCoordinator: NSObject, Coordinator {
     func showContactDetail(of contactId: Int, in chatOfType: ChatType, chatId: Int?) {
         let viewModel = ContactDetailViewModel(contactId: contactId, chatId: chatId, context: dcContext )
         let contactDetailController = ContactDetailViewController(viewModel: viewModel)
-        let coordinator = ContactDetailCoordinator(dcContext: dcContext, chatId: chatId, navigationController: navigationController)
-        childCoordinators.append(coordinator)
-        contactDetailController.coordinator = coordinator
         navigationController.pushViewController(contactDetailController, animated: true)
     }
 
@@ -733,97 +721,6 @@ class NewGroupCoordinator: Coordinator {
     }
 }
 
-// MARK: - ContactDetailCoordinator
-class ContactDetailCoordinator: Coordinator {
-    var dcContext: DcContext
-    let navigationController: UINavigationController
-    var previewController: PreviewController?
-    let chatId: Int?
-
-    private var childCoordinators: [Coordinator] = []
-
-    init(dcContext: DcContext, chatId: Int?, navigationController: UINavigationController) {
-        self.chatId = chatId
-        self.dcContext = dcContext
-        self.navigationController = navigationController
-    }
-
-    func showChat(chatId: Int) {
-        let chatViewController = ChatViewController(dcContext: dcContext, chatId: chatId)
-        let coordinator = ChatViewCoordinator(dcContext: dcContext, navigationController: navigationController, chatId: chatId)
-        childCoordinators.append(coordinator)
-        chatViewController.coordinator = coordinator
-        navigationController.popToRootViewController(animated: false)
-        navigationController.pushViewController(chatViewController, animated: true)
-    }
-
-    func showEditContact(contactId: Int) {
-        let editContactController = EditContactController(dcContext: dcContext, contactIdForUpdate: contactId)
-        navigationController.pushViewController(editContactController, animated: true)
-    }
-
-    func showDocuments() {
-        presentPreview(for: DC_MSG_FILE, messageType2: DC_MSG_AUDIO, messageType3: 0)
-    }
-
-    func showGallery() {
-        presentPreview(for: DC_MSG_IMAGE, messageType2: DC_MSG_GIF, messageType3: DC_MSG_VIDEO)
-    }
-
-    private func presentPreview(for messageType: Int32, messageType2: Int32, messageType3: Int32) {
-        guard let chatId = self.chatId else { return }
-        let messageIds = dcContext.getChatMedia(chatId: chatId, messageType: messageType, messageType2: messageType2, messageType3: messageType3)
-        var mediaUrls: [URL] = []
-        for messageId in messageIds {
-            let message = DcMsg.init(id: messageId)
-            if let url = message.fileURL {
-                mediaUrls.insert(url, at: 0)
-            }
-        }
-        previewController = PreviewController(currentIndex: 0, urls: mediaUrls)
-        if let previewController = previewController {
-            navigationController.pushViewController(previewController, animated: true)
-        }
-    }
-
-
-    func deleteChat() {
-        guard let chatId = chatId else {
-            return
-        }
-
-        /*
-        app will navigate to chatlist or archive and delete the chat there
-        notify chatList/archiveList to delete chat AFTER is is visible
-        */
-        func notifyToDeleteChat() {
-            NotificationCenter.default.post(name: dcNotificationChatDeletedInChatDetail, object: nil, userInfo: ["chat_id": chatId])
-        }
-
-        func showArchive() {
-            self.navigationController.popToRootViewController(animated: false) // in main ChatList now
-            let viewModel = ChatListViewModel(dcContext: dcContext, isArchive: true)
-            let controller = ChatListController(dcContext: dcContext, viewModel: viewModel)
-            let coordinator = ChatListCoordinator(dcContext: dcContext, navigationController: navigationController)
-            childCoordinators.append(coordinator)
-            controller.coordinator = coordinator
-            navigationController.pushViewController(controller, animated: false)
-        }
-
-        CATransaction.begin()
-        CATransaction.setCompletionBlock(notifyToDeleteChat)
-
-        let chat = dcContext.getChat(chatId: chatId)
-        if chat.isArchived {
-            showArchive()
-        } else {
-            self.navigationController.popToRootViewController(animated: true) // in main ChatList now
-        }
-        CATransaction.commit()
-    }
-
-}
-
 // MARK: - EditGroupCoordinator
 class EditGroupCoordinator: Coordinator {
     let navigationController: UINavigationController

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

@@ -4,6 +4,7 @@ import DcCore
 protocol ContactDetailViewModelProtocol {
     var context: DcContext { get }
     var contactId: Int { get }
+    var chatId: Int? { get }
     var contact: DcContact { get }
     var numberOfSections: Int { get }
     var chatIsArchived: Bool { get }
@@ -45,7 +46,7 @@ class ContactDetailViewModel: ContactDetailViewModelProtocol {
         return DcContact(id: contactId)
     }
 
-    private let chatId: Int?
+    internal let chatId: Int?
     private let sharedChats: DcChatlist
     private var sections: [ProfileSections] = []
     private var chatActions: [ChatAction] = [] // chatDetail: archive, block, delete - else: block