hmlongco / Resolver

Swift Ultralight Dependency Injection / Service Locator framework
MIT License
2.15k stars 190 forks source link

Resolver and Texture when doing Viper modular #49

Closed tychota closed 3 years ago

tychota commented 4 years ago

I have two questions: 1) am I using resolver correctly to build VIPER modules 2) ~~why is it working when using UIKit (UIviewController) and not when using AsyncDisplayKit/Texture (ASViewController<ASDisplayNode>)~~


1) Am I using resolver correctly

I have multiple module (here Root and Signin). Each module are in there own *.xcodeproj.

Here are my files, with UIKit (working ✅):

(Public) Module Root ```swift import UIKit import Resolver public class RootModule { @LazyInjected var view: RootView public init() {} public func getViewController() -> UIViewController { view } } ```
(Internal) RootView ```swift import Resolver import UIKit final class RootView: UIViewController { @Injected var presenter: RootPresenter override func viewDidLoad() { super.viewDidLoad() } override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) presenter.viewDidAppear() } } ```
(Internal) RootInteractor ```swift final class RootInteractor { weak var presenter: RootPresenter! } ```
(Internal) RootPresenter ```swift import Resolver final class RootPresenter { @Injected var router: RootRouter @Injected var interactor: RootInteractor weak var view: RootView! func viewDidAppear() { router.showSignIn() } } ```

(Internal) RootRouter ```swift import Resolver import SignIn final class RootRouter { @Injected var signInModule: SignInModule weak var presenter: RootPresenter! func showSignIn() { let siginInView = signInModule.getPresentation() siginInView.modalPresentationStyle = .fullScreen let rootView = presenter.view rootView?.present(siginInView, animated: true, completion: nil) } } ```

I'm setuping DI like this:

import Resolver

extension Resolver {
    public static func registerRoot() {
        register { RootModule() }.scope(application)
        register { RootView() }.resolveProperties { _, view in
            view.presenter.view = view
        }
        register { RootInteractor() }
        register { RootRouter() }
        register { RootPresenter() }.resolveProperties { _, presenter in
            presenter.router.presenter = presenter
            presenter.interactor.presenter = presenter
        }
    }
}

Few questions: 1) Is the registration correct? There shouldn't be any dependency cycle nor retain cycle? 2) Who should hold the view? Is it correct if the RootModule is holding a lazy view? 3) Does the order in the registerRoot() change something?


# 2) why is it working when using UIKit and not AsyncDisplayKit

Before EDIT Doing the following changes (and only this changes): ```diff import Resolver -import UIKit +import AsyncDisplayKit -final class RootView: UIViewController { +final class RootView: ASViewController { @Injected var presenter: RootPresenter override func viewDidLoad() { super.viewDidLoad() } override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) presenter.viewDidAppear() } } ``` makes: ![image](https://user-images.githubusercontent.com/13785185/86127939-f8554580-bae0-11ea-955b-3e5b5d5b5654.png) when looking at the callstack I can see: ![image](https://user-images.githubusercontent.com/13785185/86127970-00ad8080-bae1-11ea-8516-dcef817d39e0.png) What does that means ? any idea why there is a difference between `ASViewController` and `UIViewController`?

EDIT: This solves it

import Resolver
import AsyncDisplayKit

final class RootView: ASViewController<ASDisplayNode> {
    @Injected var presenter: RootPresenter

+   init() {
+       super.init(node: ASDisplayNode())
+   }

+   required init?(coder _: NSCoder) {
+       fatalError("init(coder:) has not been implemented")
+   }

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        presenter.viewDidAppear()
    }
}
hmlongco commented 3 years ago

Looks like this was figured out.