-
Notifications
You must be signed in to change notification settings - Fork 0
/
MoviesSearchFlowCoordinator.swift
68 lines (55 loc) · 2.84 KB
/
MoviesSearchFlowCoordinator.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//
// MoviesSearchFlowCoordinator.swift
// ExampleMVVM
//
// Created by Oleh Kudinov on 03.03.19.
//
import UIKit
import RxSwift
protocol MoviesSearchFlowCoordinatorDependencies {
func makeMoviesListViewController(closures: MoviesListViewModelClosures) -> MoviesListViewController
func makeMoviesDetailsViewController(movie: Movie) -> UIViewController
func makeMoviesQueriesSuggestionsListViewController(didSelect: @escaping MoviesQueryListViewModelDidSelectClosure) -> UIViewController
}
class MoviesSearchFlowCoordinator {
private let navigationController: UINavigationController
private let dependencies: MoviesSearchFlowCoordinatorDependencies
private weak var moviesListVC: MoviesListViewController?
private weak var moviesQueriesSuggestionsVC: UIViewController?
// 의존성 주입
// ViewController들에 대한 의존성을 가지지 않기 위해 DI컨테이너에서 의존성을 주입합
// 이로 인해 ViewController들로 부터 의존성이 독립됨
// Coordinator가 의존성이 없는 클래스가 됨
init(navigationController: UINavigationController,
dependencies: MoviesSearchFlowCoordinatorDependencies) {
self.navigationController = navigationController
self.dependencies = dependencies
}
// 앱의 첫 화면 전환
func start() {
// Note: here we keep strong reference with closures, this way this flow do not need to be strong referenced
let closures = MoviesListViewModelClosures(showMovieDetails: showMovieDetails,
showMovieQueriesSuggestions: showMovieQueriesSuggestions,
closeMovieQueriesSuggestions: closeMovieQueriesSuggestions)
let vc = dependencies.makeMoviesListViewController(closures: closures)
navigationController.pushViewController(vc, animated: false)
moviesListVC = vc
}
private func showMovieDetails(movie: Movie) {
let vc = dependencies.makeMoviesDetailsViewController(movie: movie)
navigationController.pushViewController(vc, animated: true)
}
private func showMovieQueriesSuggestions(didSelect: @escaping (MovieQuery) -> Void) {
guard let moviesListViewController = moviesListVC, moviesQueriesSuggestionsVC == nil,
let container = moviesListViewController.suggestionsListContainer else { return }
let vc = dependencies.makeMoviesQueriesSuggestionsListViewController(didSelect: didSelect)
moviesListViewController.add(child: vc, container: container)
moviesQueriesSuggestionsVC = vc
container.isHidden = false
}
private func closeMovieQueriesSuggestions() {
moviesQueriesSuggestionsVC?.remove()
moviesQueriesSuggestionsVC = nil
moviesListVC?.suggestionsListContainer.isHidden = true
}
}