|
| 1 | +// |
| 2 | +// AccountFlow.swift |
| 3 | +// TVToday |
| 4 | +// |
| 5 | +// Created by Jeans Ruiz on 6/19/20. |
| 6 | +// Copyright © 2020 Jeans. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | +import RxFlow |
| 11 | + |
| 12 | +public class AccountFlow: Flow { |
| 13 | + |
| 14 | + public struct Dependencies { |
| 15 | + let apiDataTransferService: DataTransferService |
| 16 | + let appConfigurations: AppConfigurations |
| 17 | + } |
| 18 | + |
| 19 | + private let dependencies: Dependencies |
| 20 | + |
| 21 | + public var root: Presentable { |
| 22 | + return self.rootViewController |
| 23 | + } |
| 24 | + |
| 25 | + private lazy var rootViewController: UINavigationController = { |
| 26 | + let navigationController = UINavigationController() |
| 27 | + return navigationController |
| 28 | + }() |
| 29 | + |
| 30 | + // MARK: - Repositories |
| 31 | + |
| 32 | + private lazy var showsRepository: TVShowsRepository = { |
| 33 | + return DefaultTVShowsRepository( |
| 34 | + dataTransferService: dependencies.apiDataTransferService, |
| 35 | + basePath: dependencies.appConfigurations.imagesBaseURL) |
| 36 | + }() |
| 37 | + |
| 38 | + private lazy var authRepository: AuthRepository = { |
| 39 | + return DefaultAuthRepository(dataTransferService: dependencies.apiDataTransferService) |
| 40 | + }() |
| 41 | + |
| 42 | + private lazy var accountRepository: AccountRepository = { |
| 43 | + return DefaultAccountRepository(dataTransferService: dependencies.apiDataTransferService) |
| 44 | + }() |
| 45 | + |
| 46 | + private lazy var keychainRepository: KeychainRepository = { |
| 47 | + return DefaultKeychainRepository() |
| 48 | + }() |
| 49 | + |
| 50 | + // MARK: - Life Cycle |
| 51 | + |
| 52 | + public init(dependencies: Dependencies) { |
| 53 | + self.dependencies = dependencies |
| 54 | + } |
| 55 | + |
| 56 | + // MARK: - Navigation |
| 57 | + |
| 58 | + public func navigate(to step: Step) -> FlowContributors { |
| 59 | + switch step { |
| 60 | + case AccountStep.accountFeatureInit: |
| 61 | + return navigateToAccountFeature() |
| 62 | + |
| 63 | + case AccountStep.signInIsPicked(let url, let delegate): |
| 64 | + return navigateToAuthPermission(url: url, delegate: delegate) |
| 65 | + |
| 66 | + case AccountStep.authorizationIsComplete: |
| 67 | + self.rootViewController.presentedViewController?.dismiss(animated: true) |
| 68 | + return .none |
| 69 | + |
| 70 | + case AccountStep.favoritesIsPicked: |
| 71 | + return navigateToFavorites() |
| 72 | + |
| 73 | + case AccountStep.watchListIsPicked: |
| 74 | + return navigateToWatchList() |
| 75 | + |
| 76 | + case SearchStep.showIsPicked(let id): |
| 77 | + return navigateToShowDetailScreen(with: id) |
| 78 | + |
| 79 | + default: |
| 80 | + return .none |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + fileprivate func navigateToAccountFeature() -> FlowContributors { |
| 85 | + |
| 86 | + let signViewModel = SignInViewModel() |
| 87 | + let signInViewController = SignInViewController.create(with: signViewModel) |
| 88 | + |
| 89 | + let profileViewModel = ProfileViewModel() |
| 90 | + let profileViewController = ProfileViewController.create(with: profileViewModel) |
| 91 | + |
| 92 | + let accountViewModel = AccountViewModel(requestToken: makeCreateTokenUseCase(), |
| 93 | + createNewSession: makeCreateSessionUseCase(), |
| 94 | + fetchAccountDetails: makeFetchAccountDetailsUseCase(), |
| 95 | + fetchLoggedUser: makeFetchLoggedUserUseCase(), |
| 96 | + deleteLoguedUser: makeDeleteLoguedUserUseCase(), |
| 97 | + signInViewModel: signViewModel, |
| 98 | + profileViewMoel: profileViewModel) |
| 99 | + signViewModel.delegate = accountViewModel |
| 100 | + profileViewModel.delegate = accountViewModel |
| 101 | + let accountViewController = AccountViewController.create(with: accountViewModel, |
| 102 | + signInViewController: signInViewController, |
| 103 | + profileViewController: profileViewController) |
| 104 | + |
| 105 | + rootViewController.pushViewController(accountViewController, animated: true) |
| 106 | + |
| 107 | + return .one(flowContributor: .contribute( |
| 108 | + withNextPresentable: accountViewController, withNextStepper: accountViewModel)) |
| 109 | + } |
| 110 | + |
| 111 | + fileprivate func navigateToAuthPermission(url: URL, delegate: AuthPermissionViewModelDelegate?) -> FlowContributors { |
| 112 | + let authViewModel = AuthPermissionViewModel(url: url) |
| 113 | + authViewModel.delegate = delegate |
| 114 | + let authViewController = AuthPermissionViewController.create(with: authViewModel) |
| 115 | + |
| 116 | + let navController = UINavigationController(rootViewController: authViewController) |
| 117 | + |
| 118 | + rootViewController.present(navController, animated: true) |
| 119 | + |
| 120 | + return .none |
| 121 | + } |
| 122 | + |
| 123 | + // MARK: - Navigate to Favorites User |
| 124 | + |
| 125 | + fileprivate func navigateToFavorites() -> FlowContributors { |
| 126 | + let viewModel = TVShowListViewModel(filter: .favorites(userId: 0, sessionId: ""), |
| 127 | + fetchTVShowsUseCase: makeShowListUseCase()) |
| 128 | + let showList = TVShowListViewController.create(with: viewModel) |
| 129 | + |
| 130 | + rootViewController.pushViewController(showList, animated: true) |
| 131 | + |
| 132 | + return .one(flowContributor: .contribute( |
| 133 | + withNextPresentable: showList, withNextStepper: viewModel)) |
| 134 | + } |
| 135 | + |
| 136 | + // MARK: - Navigate to WatchList User |
| 137 | + |
| 138 | + fileprivate func navigateToWatchList() -> FlowContributors { |
| 139 | + let viewModel = TVShowListViewModel(filter: .watchList(userId: 0, sessionId: ""), |
| 140 | + fetchTVShowsUseCase: makeShowListUseCase()) |
| 141 | + let showList = TVShowListViewController.create(with: viewModel) |
| 142 | + |
| 143 | + rootViewController.pushViewController(showList, animated: true) |
| 144 | + |
| 145 | + return .one(flowContributor: .contribute( |
| 146 | + withNextPresentable: showList, withNextStepper: viewModel)) |
| 147 | + } |
| 148 | + |
| 149 | + // MARK: - Navigate to Detail TVShow |
| 150 | + |
| 151 | + fileprivate func navigateToShowDetailScreen(with id: Int) -> FlowContributors { |
| 152 | + let detailShowFlow = TVShowDetailFlow(rootViewController: rootViewController, |
| 153 | + dependencies: TVShowDetailFlow.Dependencies( |
| 154 | + apiDataTransferService: dependencies.apiDataTransferService, |
| 155 | + appConfigurations: dependencies.appConfigurations)) |
| 156 | + |
| 157 | + return .one(flowContributor: .contribute( |
| 158 | + withNextPresentable: detailShowFlow, |
| 159 | + withNextStepper: |
| 160 | + OneStepper(withSingleStep: |
| 161 | + ShowDetailsStep.showDetailsIsRequired(withId: id)))) |
| 162 | + } |
| 163 | + |
| 164 | + // MARK: - Uses Cases |
| 165 | + |
| 166 | + private func makeCreateTokenUseCase() -> CreateTokenUseCase { |
| 167 | + return DefaultCreateTokenUseCase(authRepository: authRepository, keyChainRepository: keychainRepository) |
| 168 | + } |
| 169 | + |
| 170 | + private func makeCreateSessionUseCase() -> CreateSessionUseCase { |
| 171 | + return DefaultCreateSessionUseCase(authRepository: authRepository, keyChainRepository: keychainRepository) |
| 172 | + } |
| 173 | + |
| 174 | + private func makeFetchAccountDetailsUseCase() -> FetchAccountDetailsUseCase { |
| 175 | + return DefaultFetchAccountDetailsUseCase(accountRepository: accountRepository, keychainRepository: keychainRepository) |
| 176 | + } |
| 177 | + |
| 178 | + private func makeFetchLoggedUserUseCase() -> FetchLoggedUser { |
| 179 | + return DefaultFetchLoggedUser(keychainRepository: keychainRepository) |
| 180 | + } |
| 181 | + |
| 182 | + private func makeDeleteLoguedUserUseCase() -> DeleteLoguedUserUseCase { |
| 183 | + return DefaultDeleteLoguedUserUseCase(keychainRepository: keychainRepository) |
| 184 | + } |
| 185 | + |
| 186 | + fileprivate func makeShowListUseCase() -> FetchTVShowsUseCase { |
| 187 | + return DefaultUserFetchShowsUserUseCase(tvShowsRepository: showsRepository, |
| 188 | + keychainRepository: keychainRepository) |
| 189 | + } |
| 190 | + |
| 191 | +} |
| 192 | + |
| 193 | +// MARK: - Steps |
| 194 | + |
| 195 | +enum AccountStep: Step { |
| 196 | + |
| 197 | + case |
| 198 | + |
| 199 | + accountFeatureInit, |
| 200 | + |
| 201 | + signIsShow, |
| 202 | + |
| 203 | + profileIsShow, |
| 204 | + |
| 205 | + signInIsPicked(url: URL, delegate: AuthPermissionViewModelDelegate?), |
| 206 | + |
| 207 | + authorizationIsComplete, |
| 208 | + |
| 209 | + favoritesIsPicked, |
| 210 | + |
| 211 | + watchListIsPicked |
| 212 | +} |
0 commit comments