How to make interactor in swift

Viewed 49

I'm kinda stuck right now with this scenario I want my 2 class to interact with each other. but on my case only 1 class can interact to the other class.

sample :

request >> << factory/middleware >> << controller

but on my case this is what happening:

request << factory/middleware << controller

the problem here is that only controller can ping middle/factory and send to the request class but how can I do it request also can ping middle/factory and give the result to the controller?

Controller

class Controller {
 let displayScreen: UIViewController!
 let onRequest: ((Bool) -> Void)?
 init(displayScreen: UIViewController, onRequest: ((Bool) -> Void)?) {
  self.displayScreen = displayScreen
  self.onRequest = onRequest
  
  self.onRequest = { [weak self] status in
   dlog("result \(status)", level: .assert) <- nothing happend sad story.
  }
 }
}

Factory/MiddleWare

protocol FactoryRequirement {
 func makeController(displayScreen: UIViewController, onRequest: ((Bool) -> Void)?)
}

class Factory: FactoryRequirement {
 func makeController(displayScreen: UIViewController, onRequest: ((Bool) -> Void)?) -> Controller {
  return Controller(displayScreen: displayScreen, onRequest)
 }
}

Request

class Request: BaseArchetictureLayer {

 private var onResult: ((Bool) -> Void)? <-- this is I want to observe and send to the factory and controller will receive it.

 private var actionPerform = false {
  didSet {
   onResult?(actionPerform) <-- update once it changes
  }
 }
 
 let controllerFactory: Factory!

 init() {
  self.controllerFactory = Factory() 
 }

 override func start() {
  let controller = controllerFactory.makeController(
   displayScreen: uiFromControllerToDisplay() <- this is actually work!
   onRequest: onResult <- Actually I didn't receive anything from this guy.
  )
  self.interactorListener = { [weak self] interact in
   actionPerform = interact <- I can actually get the result of interactor but I was not able to observe in the controller.
  }
  self.perform(controller, animated: true)
 }
}
0 Answers
Related