Pārlūkot izejas kodu

remove ChatViewCoordinator and MailboxCoordinator

B. Petersen 5 gadi atpakaļ
vecāks
revīzija
6485469ba4

+ 0 - 2
deltachat-ios/Controller/ChatListController.swift

@@ -371,8 +371,6 @@ class ChatListController: UITableViewController {
     func showChat(chatId: Int) {
         if let navigationController = self.parent as? UINavigationController {
             let chatVC = ChatViewController(dcContext: dcContext, chatId: chatId)
-            let coordinator = ChatViewCoordinator(dcContext: dcContext, navigationController: navigationController, chatId: chatId)
-            chatVC.coordinator = coordinator
             navigationController.pushViewController(chatVC, animated: true)
         }
     }

+ 85 - 15
deltachat-ios/Controller/ChatViewController.swift

@@ -39,13 +39,9 @@ extension ChatViewController: MediaPickerDelegate {
 }
 
 class ChatViewController: MessagesViewController {
-
     var dcContext: DcContext
-    weak var coordinator: ChatViewCoordinator?
-
     let outgoingAvatarOverlap: CGFloat = 17.5
     let loadCount = 30
-
     let chatId: Int
     let refreshControl = UIRefreshControl()
     var messageList: [DcMsg] = []
@@ -67,6 +63,14 @@ class ChatViewController: MessagesViewController {
     var showCustomNavBar = true
     var previewView: UIView?
 
+    private lazy var mediaPicker: MediaPicker? = {
+        if let navigationController = self.parent as? UINavigationController {
+            return MediaPicker(navigationController: navigationController)
+        } else {
+            return nil
+        }
+    }()
+
     var emptyStateView: PaddingLabel = {
         let view =  PaddingLabel()
         view.backgroundColor = DcColors.systemMessageBackgroundColor
@@ -498,7 +502,9 @@ class ChatViewController: MessagesViewController {
     }
 
     @objc private func chatProfilePressed() {
-        coordinator?.showChatDetail(chatId: chatId)
+        if chatId == DC_CHAT_ID_DEADDROP {
+            showChatDetail(chatId: chatId)
+        }
     }
 
     // MARK: - UICollectionViewDataSource
@@ -576,7 +582,7 @@ class ChatViewController: MessagesViewController {
         case NSSelectorFromString("messageForward:"):
             let msg = messageList[indexPath.section]
             RelayHelper.sharedInstance.setForwardMessage(messageId: msg.id)
-            coordinator?.navigateBack()
+            navigateBack()
         default:
             super.collectionView(collectionView, performAction: action, forItemAt: indexPath, withSender: sender)
         }
@@ -599,14 +605,14 @@ class ChatViewController: MessagesViewController {
         if dcContext.getChatIdByContactId(contactId: contactId) != 0 {
             self.dismiss(animated: true, completion: nil)
             let chatId = self.dcContext.createChatByContactId(contactId: contactId)
-            self.coordinator?.showChat(chatId: chatId)
+            self.showChat(chatId: chatId)
         } else {
             confirmationAlert(title: String.localizedStringWithFormat(String.localized("ask_start_chat_with"), email),
                               actionTitle: String.localized("start_chat"),
                               actionHandler: { _ in
                                 self.dismiss(animated: true, completion: nil)
                                 let chatId = self.dcContext.createChatByContactId(contactId: contactId)
-                                self.coordinator?.showChat(chatId: chatId)})
+                                self.showChat(chatId: chatId)})
         }
     }
 
@@ -629,7 +635,71 @@ class ChatViewController: MessagesViewController {
                             self.dismiss(animated: true, completion: nil)},
                           cancelHandler: { _ in
                             self.dismiss(animated: false, completion: nil)
-                            self.coordinator?.navigateBack()})
+                            self.navigateBack()})
+        }
+    }
+
+    //  MARK: - coordinator
+    func navigateBack() {
+        if let navigationController = self.parent as? UINavigationController {
+            navigationController.popViewController(animated: true)
+        }
+    }
+
+    func showChatDetail(chatId: Int) {
+        if let navigationController = self.parent as? UINavigationController {
+            let chat = dcContext.getChat(chatId: chatId)
+            switch chat.chatType {
+            case .SINGLE:
+                if let contactId = chat.contactIds.first {
+                    let viewModel = ContactDetailViewModel(contactId: contactId, chatId: chatId, context: dcContext)
+                    let contactDetailController = ContactDetailViewController(viewModel: viewModel)
+                    navigationController.pushViewController(contactDetailController, animated: true)
+                }
+            case .GROUP, .VERIFIEDGROUP:
+                let groupChatDetailViewController = GroupChatDetailViewController(chatId: chatId, dcContext: dcContext)
+                navigationController.pushViewController(groupChatDetailViewController, animated: true)
+            }
+        }
+    }
+
+    func showContactDetail(of contactId: Int, in chatOfType: ChatType, chatId: Int?) {
+        if let navigationController = self.parent as? UINavigationController {
+            let viewModel = ContactDetailViewModel(contactId: contactId, chatId: chatId, context: dcContext )
+            let contactDetailController = ContactDetailViewController(viewModel: viewModel)
+            navigationController.pushViewController(contactDetailController, animated: true)
+        }
+    }
+
+    func showChat(chatId: Int) {
+        if let navigationController = self.parent as? UINavigationController, let appDelegate = UIApplication.shared.delegate as? AppDelegate {
+            navigationController.popToRootViewController(animated: false)
+            appDelegate.appCoordinator.showChat(chatId: chatId)
+        }
+    }
+
+    func showDocumentLibrary(delegate: MediaPickerDelegate) {
+        mediaPicker?.showDocumentLibrary(delegate: delegate)
+    }
+
+    func showVoiceMessageRecorder(delegate: MediaPickerDelegate) {
+        mediaPicker?.showVoiceRecorder(delegate: delegate)
+    }
+
+    func showCameraViewController(delegate: MediaPickerDelegate) {
+        mediaPicker?.showCamera(delegate: delegate, allowCropping: false)
+    }
+
+    func showPhotoVideoLibrary(delegate: MediaPickerDelegate) {
+        mediaPicker?.showPhotoVideoLibrary(delegate: delegate)
+    }
+
+    func showMediaGallery(currentIndex: Int, mediaUrls urls: [URL]) {
+        if let navigationController = self.parent as? UINavigationController {
+            let betterPreviewController = PreviewController(currentIndex: currentIndex, urls: urls)
+            let nav = UINavigationController(rootViewController: betterPreviewController)
+            nav.modalPresentationStyle = .fullScreen
+            navigationController.present(nav, animated: true)
         }
     }
 }
@@ -1119,19 +1189,19 @@ extension ChatViewController: MessagesLayoutDelegate {
     }
 
     private func documentActionPressed(_ action: UIAlertAction) {
-        coordinator?.showDocumentLibrary(delegate: self)
+        showDocumentLibrary(delegate: self)
     }
 
     private func voiceMessageButtonPressed(_ action: UIAlertAction) {
-        coordinator?.showVoiceMessageRecorder(delegate: self)
+        showVoiceMessageRecorder(delegate: self)
     }
 
     private func cameraButtonPressed(_ action: UIAlertAction) {
-        coordinator?.showCameraViewController(delegate: self)
+        showCameraViewController(delegate: self)
     }
 
     private func galleryButtonPressed(_ action: UIAlertAction) {
-        coordinator?.showPhotoVideoLibrary(delegate: self)
+        showPhotoVideoLibrary(delegate: self)
     }
 
     private func locationStreamingButtonPressed(_ action: UIAlertAction) {
@@ -1181,7 +1251,7 @@ extension ChatViewController: MessageCellDelegate {
 
                 // these are the files user will be able to swipe trough
                 let mediaUrls: [URL] = previousUrls + [url] + nextUrls
-                coordinator?.showMediaGallery(currentIndex: previousUrls.count, mediaUrls: mediaUrls)
+                showMediaGallery(currentIndex: previousUrls.count, mediaUrls: mediaUrls)
             }
         }
     }
@@ -1229,7 +1299,7 @@ extension ChatViewController: MessageCellDelegate {
         if let indexPath = messagesCollectionView.indexPath(for: cell) {
             let message = messageList[indexPath.section]
             let chat = dcContext.getChat(chatId: chatId)
-            coordinator?.showContactDetail(of: message.fromContact.id, in: chat.chatType, chatId: chatId)
+            showContactDetail(of: message.fromContact.id, in: chat.chatType, chatId: chatId)
         }
     }
 

+ 0 - 2
deltachat-ios/Controller/ContactDetailViewController.swift

@@ -282,8 +282,6 @@ class ContactDetailViewController: UITableViewController {
     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)
         }

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

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

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

@@ -356,8 +356,6 @@ class NewChatViewController: UITableViewController {
     func showChat(chatId: Int) {
         if let navigationController = self.parent as? UINavigationController {
             let chatViewController = ChatViewController(dcContext: dcContext, chatId: chatId)
-            let coordinator = ChatViewCoordinator(dcContext: dcContext, navigationController: navigationController, chatId: chatId)
-            chatViewController.coordinator = coordinator
             navigationController.pushViewController(chatViewController, animated: true)
             navigationController.viewControllers.remove(at: 1)
         }

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

@@ -120,9 +120,6 @@ class NewContactController: UITableViewController {
     func showChat(chatId: Int) {
         let chatViewController = ChatViewController(dcContext: dcContext, chatId: chatId)
         if let navigationController = self.parent as? UINavigationController {
-            let coordinator = ChatViewCoordinator(dcContext: dcContext, navigationController: navigationController, chatId: chatId)
-            coordinator.chatViewController = chatViewController
-            chatViewController.coordinator = coordinator
             navigationController.popToRootViewController(animated: false)
             navigationController.pushViewController(chatViewController, animated: true)
         }

+ 0 - 2
deltachat-ios/Controller/NewGroupController.swift

@@ -352,8 +352,6 @@ class NewGroupController: UITableViewController, MediaPickerDelegate {
     func showGroupChat(chatId: Int) {
         if let navigationController = self.parent as? UINavigationController {
             let chatViewController = ChatViewController(dcContext: dcContext, chatId: chatId)
-            let coordinator = ChatViewCoordinator(dcContext: dcContext, navigationController: navigationController, chatId: chatId)
-            chatViewController.coordinator = coordinator
             navigationController.popToRootViewController(animated: false)
             navigationController.pushViewController(chatViewController, animated: true)
         }

+ 0 - 2
deltachat-ios/Controller/SettingsController.swift

@@ -477,8 +477,6 @@ internal final class SettingsViewController: UITableViewController {
     func showContactRequests() {
         if let navigationController = self.parent as? UINavigationController {
             let deaddropViewController = MailboxViewController(dcContext: dcContext, chatId: Int(DC_CHAT_ID_DEADDROP))
-            let deaddropCoordinator = MailboxCoordinator(dcContext: dcContext, navigationController: navigationController)
-            deaddropViewController.coordinator = deaddropCoordinator
             navigationController.pushViewController(deaddropViewController, animated: true)
         }
     }

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

@@ -184,29 +184,6 @@ extension AppCoordinator: WelcomeCoordinator {
     }
 }
 
-// since mailbox and chatView -tab both use ChatViewController we want to be able to assign different functionality via coordinators -> therefore we override unneeded functions such as showChatDetail -> maybe find better solution in longterm
-class MailboxCoordinator: ChatViewCoordinator {
-
-    init(dcContext: DcContext, navigationController: UINavigationController) {
-        super.init(dcContext: dcContext, navigationController: navigationController, chatId: -1)
-    }
-
-    override func showChatDetail(chatId _: Int) {
-        // ignore for now
-    }
-
-    override func showCameraViewController(delegate: MediaPickerDelegate) {
-        // ignore
-    }
-
-    override func showChat(chatId: Int) {
-        if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
-            navigationController.popToRootViewController(animated: false)
-            appDelegate.appCoordinator.showChat(chatId: chatId)
-        }
-    }
-}
-
 // MARK: - EditSettingsCoordinator
 class EditSettingsCoordinator: Coordinator {
     var dcContext: DcContext
@@ -228,81 +205,6 @@ class EditSettingsCoordinator: Coordinator {
     }
 }
 
-// MARK: - ChatViewCoordinator
-class ChatViewCoordinator: NSObject, Coordinator {
-    var dcContext: DcContext
-    let navigationController: UINavigationController
-    let chatId: Int
-    var chatViewController: ChatViewController!
-
-    var childCoordinators: [Coordinator] = []
-    let mediaPicker: MediaPicker
-
-    init(dcContext: DcContext, navigationController: UINavigationController, chatId: Int) {
-        self.dcContext = dcContext
-        self.navigationController = navigationController
-        self.chatId = chatId
-        self.mediaPicker = MediaPicker(navigationController: self.navigationController)
-    }
-
-    func navigateBack() {
-        navigationController.popViewController(animated: true)
-    }
-
-    func showChatDetail(chatId: Int) {
-        let chat = dcContext.getChat(chatId: chatId)
-        switch chat.chatType {
-        case .SINGLE:
-            if let contactId = chat.contactIds.first {
-                let viewModel = ContactDetailViewModel(contactId: contactId, chatId: chatId, context: dcContext)
-                let contactDetailController = ContactDetailViewController(viewModel: viewModel)
-                navigationController.pushViewController(contactDetailController, animated: true)
-            }
-        case .GROUP, .VERIFIEDGROUP:
-            let groupChatDetailViewController = GroupChatDetailViewController(chatId: chatId, dcContext: dcContext)
-            navigationController.pushViewController(groupChatDetailViewController, animated: true)
-        }
-    }
-
-    func showContactDetail(of contactId: Int, in chatOfType: ChatType, chatId: Int?) {
-        let viewModel = ContactDetailViewModel(contactId: contactId, chatId: chatId, context: dcContext )
-        let contactDetailController = ContactDetailViewController(viewModel: viewModel)
-        navigationController.pushViewController(contactDetailController, animated: true)
-    }
-
-    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 showDocumentLibrary(delegate: MediaPickerDelegate) {
-        mediaPicker.showDocumentLibrary(delegate: delegate)
-    }
-
-    func showVoiceMessageRecorder(delegate: MediaPickerDelegate) {
-        mediaPicker.showVoiceRecorder(delegate: delegate)
-    }
-
-    func showCameraViewController(delegate: MediaPickerDelegate) {
-        mediaPicker.showCamera(delegate: delegate, allowCropping: false)
-    }
-
-    func showPhotoVideoLibrary(delegate: MediaPickerDelegate) {
-        mediaPicker.showPhotoVideoLibrary(delegate: delegate)
-    }
-
-    func showMediaGallery(currentIndex: Int, mediaUrls urls: [URL]) {
-        let betterPreviewController = PreviewController(currentIndex: currentIndex, urls: urls)
-        let nav = UINavigationController(rootViewController: betterPreviewController)
-        nav.modalPresentationStyle = .fullScreen
-        navigationController.present(nav, animated: true)
-    }
-}
-
 /*
  boilerplate - I tend to remove that interface (cyberta)
  */