Class
ViewableRouter
A router that owns a view controller.
@MainActor class ViewableRouter<InteractorType, ViewControllerType>
Mentioned In
Overview
@MainActor-isolated. Subclass ViewableRouter for napkins that have a view of their own; for headless napkins, subclass Router directly.
Overview
ViewableRouter keeps two references to the same view controller instance:
viewController is concretely typed (ViewControllerType) so subclasses can call feature-specific Presentable methods on it directly.
viewControllable is the type-erased ViewControllable view used by parent routers to embed or present the underlying UIViewController / NSViewController.
Both are populated in the initializer; the ViewControllable conformance is enforced at runtime via a fatalError if missing.
Usage
UIKit:
final class HomeViewController: UIViewController, HomeViewControllable, HomePresentable {
// Conforms to ViewControllable automatically via the UIKit extension.
}
@MainActor
final class HomeRouter: ViewableRouter<HomeInteractor, HomeViewController>, HomeRouting {
init(
interactor: HomeInteractor,
viewController: HomeViewController,
profileBuilder: ProfileBuildable
) {
self.profileBuilder = profileBuilder
super.init(interactor: interactor, viewController: viewController)
}
func routeToProfile() async {
let r = await profileBuilder.build(withListener: interactor)
await attachChild(r)
viewController.present(r.viewControllable.uiviewController, animated: true)
}
private let profileBuilder: ProfileBuildable
}
SwiftUI (via UIHostingController):
final class HomeHostingController: UIHostingController<HomeView>, HomeViewControllable {
init(presenter: HomePresenter, listener: HomeViewListener) {
super.init(rootView: HomeView(presenter: presenter, listener: listener))
}
required init?(coder: NSCoder) { fatalError() }
}
@MainActor
final class HomeRouter: ViewableRouter<HomeInteractor, HomeHostingController>, HomeRouting {
// Same shape as the UIKit example.
}
See Also
ViewableRouting
See Also
Router
See Also
ViewControllable
Topics
Creating a ViewableRouter
Accessing the View
Relationships
Conforms To
Inherited By
Inherits From