uptechteam / Coordinator-MVVM-Rx-Example

Example of MVVM-C architecture implemented with RxSwift
564 stars 100 forks source link

How to pass a parameter to next viewmodel #2

Closed sandart closed 6 years ago

sandart commented 6 years ago

Hi, guys!

Thanks for you shared it .

I have a question for you.

If i want to pass an argument to next ViewModel. What should i do? Can i break the start() function of BaseCoordinator?

arthur-here commented 6 years ago

Hi @sandart. Thanks for the feedback!

You can pass those arguments in the coordinator init:

class ParentCoordinator: BaseCoordinator<Void> { 
  override func start() -> Observable<Void> { 
    let parentViewModel = ParentViewModel()
    parentViewModel.showChild
      .flatMap { self.showChild(argument: argument) }
      .subscribe()
      .disposed(by: disposeBag)
    ...
  }

  func showChild(argument: Int) -> Observable<Void> { 
    let childCoordinator = ChildCoordinator(argument: argument)
    return coordinate(to: childCoordinator)
  }
}

class ChildCoordinator: BaseCoordinator<Void> {
  private let argument: Int

  init(argument: Int) {
    self.argument = argument
  }

  override func start() -> Observable<Void> {
        // Here we pass those argument to the view model
        let childViewModel = ChildViewModel(argument: self.argument)
    ...
  }
}
sandart commented 6 years ago

@arthur-here Thanks for your feedback very much. I appreciate your help. I got it. 🙂🙂🙂🙂🙂

karta88821 commented 6 years ago

@arthur-here I also faced the same problem I want to present an Int to the next viewModel

class FirstCoordinator: BaseCoordinator<Void> {

    private let navigationController: UINavigationController

    init(navigationController: UINavigationController) {
        self.navigationController = navigationController
    }

    override func start() -> Observable<Void> {

        let viewModel = FirstViewModel()
        let viewController = FirstViewController.initFromStoryboard(name: "Main")

        viewController.viewModel = viewModel

        viewModel.showCompositions
            .flatMap{ id in self.passComposition(by: id)}
            .subscribe(onNext: { [weak self] in self?.showComposition(in: (self?.navigationController)!) })
            .disposed(by: disposeBag)

        navigationController.pushViewController(viewController, animated: false)

        return Observable.never()
    }

    private func passComposition(by id: Int) -> Observable<Void> {
        let compositionCoordinator = CompositionCoordinator(caseId: id)
        return coordinate(to: compositionCoordinator)
    }

// I'm not sure this method is correct or not
    private func showComposition(in navigationController: UINavigationController) {
        let compositionViewController = CompositionViewController.initFromStoryboard(name: "Main")
        navigationController.pushViewController(compositionViewController, animated: true)
    }

}

And in my SecondCoordinator,it has an caseId(Do I need to add viewController instance in secondCoordinator?)

class SecondCoordinator: BaseCoordinator<Void> {

    private let caseId: Int

    init(caseId: Int) {
        self.caseId = caseId
    }

    override func start() -> Observable<Void> {

        let viewController = SecondViewController.initFromStoryboard(name: "Main")
        let viewModel = SecondViewModel(caseId: self.caseId) 

        viewController.viewModel = viewModel

        return Observable.never()

    }
}

But after I tap the cell and didn't push out a new viewController, would like to ask this question is out where?Thanks