2

我正在阅读有关 VIPER 的内容,我的理解是 - 通常一个 viewController 与一位演示者相关,而一位演示者与一位交互者交谈。

但是,如果我们有主详细信息页面或列表详细信息页面怎么办。要显示项目列表,我将有一个控制器/演示者来显示列表,而另一个控制器/演示者来显示详细信息。并且 FetchList 和 FetchDetail 应该属于同一个交互器。

如果这两个演示者与这个交互者通信,他们将不得不实现 FetchList 和 FetchDetail 方法。这两种方法的实现之一将是空的。

4

2 回答 2

2

您应该有两个独立的 VIPER 模块:MainItems 和DetailedItems。

阅读这篇文章(https://www.ckl.io/blog/best-practices-viper-architecture),了解如何使用委托在 VIPER 模块之间发送数据。注意FetchListFetchDetail应该属于不同的交互器:

// 1. Declare which messages can be sent to the delegate

// ProductScreenDelegate.swift
protocol ProductScreenDelegate {
//Add arguments if you need to send some information
    func onProductScreenDismissed()
    func onProductSelected(_ product: Product?)
}

// 2. Call the delegate when you need to send him a message

// ProductPresenter.swift
class ProductPresenter {

    // MARK: Properties
    weak var view: ProductView?
    var router: ProductWireframe?
    var interactor: ProductUseCase?
    var delegate: ProductScreenDelegate?
}

extension ProductPresenter: ProductPresentation {

    //View tells Presenter that view disappeared
    func onViewDidDisappear() {

        //Presenter tells its delegate that the screen was dismissed
        delegate?.onProductScreenDismissed()
    }
}

// 3. Implement the delegate protocol to do something when you receive the message

// ScannerPresenter.swift
class ScannerPresenter: ProductScreenDelegate {

    //Presenter receives the message from the sender
    func onProductScreenDismissed() {

        //Presenter tells view what to do once product screen was dismissed
        view?.startScanning()
    }
    ...
}

// 4. Link the delegate from the Product presenter in order to proper initialize it

// File ScannerRouter.swift
class ProductRouter {

    static func setupModule(delegate: ProductScreenDelegate?) -> ProductViewController {
        ...
        let presenter = ScannerPresenter()

        presenter.view = view
        presenter.interactor = interactor
        presenter.router = router
        presenter.delegate = delegate // Add this line to link the delegate
        ...
        }
}

于 2017-04-11T02:33:54.247 回答
1

我的理解是,每个屏幕都有一个视图/视图控制器和演示者,然后每个用例有一个交互器,这可能意味着每个屏幕不止一个。从单一职责原则的角度来看,这是一种很好的做法,因此有助于测试。但有时会做出让步,让交互者处理多个用例。

于 2017-02-24T10:45:55.003 回答