Home > Software design >  viper module without link on view
viper module without link on view

Time:10-21

I downloaded template for viper module and it has no link on view, how do I use navigation here?

Scene Delegate

let startModule = CardsCollectionRouter.createModule()
        let navigationController = UINavigationController(rootViewController: startModule)
        window?.rootViewController = navigationController
        window?.makeKeyAndVisible()

Router

class CardsCollectionRouter: PresenterToRouterCardsCollectionProtocol {
    
    // MARK: Static methods
    static func createModule() -> UIViewController {
        
        let viewController = CardsCollectionViewController()
        let presenter: ViewToPresenterCardsCollectionProtocol & InteractorToPresenterCardsCollectionProtocol = CardsCollectionPresenter()
        let networkService = NetworkService()
        
        viewController.presenter = presenter
        viewController.presenter?.router = CardsCollectionRouter()
        viewController.presenter?.view = viewController
        viewController.presenter?.interactor = CardsCollectionInteractor(networkService: networkService)
        viewController.presenter?.interactor?.presenter = presenter
        
        return viewController
    }
    
    func showDescription(description: CharacterModel?) {
        let descriptionVC = DescripModuleRouter.createModule(description: description)
        //How to go on next screen??
        let viewController = CardsCollectionViewController()
        viewController.navigationController?.pushViewController(descriptionVC, animated: true)
    }
    
    
}

may be I have an easy way to get a link to the view?

like view.goToAnotherScreen from router and I will have this method on my ViewController

CodePudding user response:

You shouldn't have to create navigation methods outside your router when using this pattern, as navigation is the router's responsibility. You can create router.goToAnotherScreen and call it from your view controller. When creating the router, you should pass the view controller to the router and inside your router you should call viewController.navigationController?.push...

class CardsCollectionRouter: PresenterToRouterCardsCollectionProtocol {
    
    // MARK: Dependencies
    private weak var viewController: UIViewController!

    // MARK: Initialization
    init(viewController: UIViewController) {
        self.viewController = viewController
    }

    // MARK: Static methods
    static func createModule() -> UIViewController {
        let viewController = CardsCollectionViewController()
        let presenter = CardsCollectionPresenter()
        let networkService = NetworkService()
        
        viewController.presenter = presenter
        viewController.presenter?.router = CardsCollectionRouter(viewController: viewController)
        viewController.presenter?.view = viewController
        viewController.presenter?.interactor = CardsCollectionInteractor(networkService: networkService)
        viewController.presenter?.interactor?.presenter = presenter
        
        return viewController
    }
    
    func showDescription(description: CharacterModel?) {
        let descriptionVC = DescripModuleRouter.createModule(description: description)
        //How to go on next screen: Use your VC instance, which is the one your VIPER scene is controlling
        self.viewController.navigationController?.pushViewController(descriptionVC, animated: true)
    }
    
    
}

  • Related