我应该从哪里在干净的架构中呈现 MFMailComposeViewController?

From where should I present an MFMailComposeViewController in a Clean Architecture?

你能就在哪里放置 MFMailComposeViewController 提出一些建议吗?

在非 RxSwift 和非 Clean Architecture 项目中,我会在一些视图控制器中实现它,如下所示:

extension ViewController: MFMailComposeViewControllerDelegate {

    func presentMailComposer() {

        if !MFMailComposeViewController.canSendMail() {
            // TODO: - Handle error here
            return
        }

        DispatchQueue.global(qos: DispatchQoS.QoSClass.userInitiated).async {

            let mailComposeViewController = MFMailComposeViewController()
            mailComposeViewController.mailComposeDelegate = self
            mailComposeViewController.setToRecipients(["mail@example.com"])
            mailComposeViewController.setMessageBody("Message body", isHTML: false)

            DispatchQueue.main.async(execute: {
                self.present(mailComposeViewController, animated: true, completion: nil)
            })

        }

    }

    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
        if result == MFMailComposeResult.failed {
            // TODO: - Handle error here
        }
    }

}

在 Clean Architecture 中,您会将邮件编辑器放在哪里?

你会在 Navigator/Router 上展示这个吗?它毕竟是一个 "Scene",即使我们不一定有一个 Navigator/Router 和一个专用于 MailComposer 的 ViewModel。

有 2 个不同的地方可能会发生错误,我真的认为 Navigator 不应该处理这些地方。

谢谢!

这取决于您决定管理项目的方式。

最终,邮件编写器元素是一个 UI 元素,因此应在 UI 处理 类 中执行呈现它 - 例如您的 VC,某种像你做的扩展等..

在我看来,你可以做的是将你的邮件编写器子类化,并在它完成时从中创建一个完成块响应,然后相应地处理 UI 中的错误,这样它将管理本身(由于它是一个全局控制器,因此拥有一个通用的 VM 是一种代码浪费)。

然后当您呈现邮件编辑器时,您让用户添加完成和失败块/使用来自 Rx 的信号到 return 结果。

Clean Architecture 背后的基本前提是 "business rules",即您的应用程序的逻辑,不依赖于 UI,也不由其执行。相反,您的应用程序的逻辑处于控制之中。

这意味着应用程序的某些逻辑知道用户何时可以发送电子邮件,但不知道如何[=53] =] 发生了。

如果您使用的是 RxSwift,您可以将用户交互视为模型转换。所以你的例子会变成:

func sendMail(recipients: [String], tile: String, message: String, isHTML: Bool) -> Observable<Bool>

以上内容可以作为闭包传递给您的逻辑,也可以嵌入到您的逻辑使用的协议中。


如果您想使用 Robert Martin 的特定结构,那么情况会有所不同,因为您根本不会在模型对象中使用 Rx。 (他建议您的 Interactors &al. 不要依赖外部库。)

在这种情况下,交互器会向演示者发送一条消息,以通过响应模型对象显示电子邮件视图控制器,而控制器会将 success/failure 结果发送回交互器,或者更有可能不同的交互器。

以下是 Bob 叔叔所说的他构建事物的方式:https://camo.githubusercontent.com/c34f4ed0203238af6e43b44544b864dffac6bc08/687474703a2f2f692e696d6775722e636f6d2f576b42414154792e706e67 However in the one iOS Swift app he has publicly presented, he didn't use this structure. https://github.com/unclebob/MACS_GOMOKU


在你的评论之后详细说明,签名确实有效,但它需要一些支持结构...

首先,一个很好但不是绝对必要的部分,我们使视图控制器呈现反应式:

extension Reactive where Base: UIViewController {

    func present(_ viewControllerToPresent: UIViewController, animated: Bool) -> Observable<Void> {
        return Observable.create { observer in
            self.base.present(viewControllerToPresent, animated: animated, completion: {
                observer.onNext()
                observer.onCompleted()
            })
            return Disposables.create()
        }
    }
}

不仅一个视图控制器只能由另一个视图控制器呈现,而且它必须是系统中当前未呈现任何内容的一个视图控制器。我们可以通过从根开始并沿着表示堆栈向上查找来找到该视图控制器:

extension UIViewController {

    static func top() -> UIViewController? {
        var result = UIApplication.shared.delegate.flatMap { [=12=].window??.rootViewController }
        while let child = result?.presentedViewController {
            result = child
        }
        return result
    }
}

现在,我们不再让一些视图控制器符合 MFMailComposeViewControllerDelegate 协议,而是制作了一个专用的 Reactive class。

class MailComposeViewControllerDelegate: NSObject, UINavigationControllerDelegate, MFMailComposeViewControllerDelegate {

    let subject = PublishSubject<MFMailComposeResult>()

    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
        if let error = error {
            subject.onError(error)
        }
        else {
            subject.onNext(result)
        }
    }
}

一旦所有这些都准备就绪,编写 sendMail 函数就很容易了:

func sendMail(recipients: [String], tile: String, message: String, isHTML: Bool) -> Observable<MFMailComposeResult> {
    let delegate = MailComposeViewControllerDelegate()
    let controller = MFMailComposeViewController()
    controller.delegate = delegate
    return UIViewController.top()!.rx.present(controller, animated: true)
        .flatMap { delegate.subject }
}

就像我说的,你应该直接调用这个函数。相反,您应该将它注入将调用它的对象中,以便您可以模拟它进行测试。

同样的模式适用于 UIImagePickerController 甚至 UIAlertController!

您可能会发现我写的这篇文章读起来很有趣。它使用 promises 而不是 Rx,但原理是一样的:https://medium.com/@danielt1263/encapsulating-the-user-in-a-function-ec5e5c02045f