emelagumat
emelagumat

Reputation: 311

SwiftUI. How to keep VIPER protocols adding EnvironmentObject?

I'm trying to use VIPER architecture with SwiftUI, using some protocols I was already using with UIKit.

These are my BasePresenter and Assembler implementations:

class BasePresenter <V, I, R>: NSObject, Presenter {
    typealias View = V
    typealias Interactor = I
    typealias Router = R
    
    var view: View!
    var interactor: Interactor!
    var router: Router!
    
    func viewDidLoad() {
           print("Base presenter: Viewdidload")
       }
}

protocol Assembler {
    associatedtype CustomView: ViewProtocol
    associatedtype CustomInteractor: Interactor
    associatedtype CustomPresenter: Presenter
    associatedtype CustomRouter: Router
    static func assembleView(_ view: CustomView) -> CustomView
    
}


extension Assembler {
    static func assembleView(_ view: CustomView) -> CustomView {
        let presenter = CustomPresenter.init()
        var view = view
        
        presenter.view = (view as! CustomPresenter.View)
        view.presenter = (presenter as! CustomView.Presenter)
        
        let interactor = CustomInteractor.init()
        interactor.presenter = (presenter as! CustomInteractor.Presenter)
        presenter.interactor = (interactor as! CustomPresenter.Interactor)
        
        let router = CustomRouter.init()
        router.presenter = (presenter as! CustomRouter.Presenter)
        presenter.router = (router as! CustomPresenter.Router)
        
        return view
    }
}

It was working fine with SwiftUI views until I wanted to add environment objects.

The problem is that if I add it this way:

LoginAssembler.assembleView(LoginView()).environmentObject(user)

the app crashes when I try to modify it with

A View.environmentObject(_:) for UserModel may be missing as an ancestor of this view

I tried to change the environment object injection to the view's initialization, but then it's no longer of type LoginView (it's ModifiedContent<...>) and the assembler crashes.

let view = view.init().environmentObject(environmentObject)
let presenter = CustomPresenter.init()
print(view is LoginView) // Prints false

Any idea of what could I use to assemble my modules and having the environmentObject option working?

Upvotes: 2

Views: 237

Answers (0)

Related Questions