diff --git a/Makgulli/Application/Coordinator/AppCoordinator.swift b/Makgulli/Application/Coordinator/AppCoordinator.swift index 9164423..451c852 100644 --- a/Makgulli/Application/Coordinator/AppCoordinator.swift +++ b/Makgulli/Application/Coordinator/AppCoordinator.swift @@ -21,8 +21,14 @@ final class AppCoordinator: Coordinator { let flow = PassthroughSubject() private var cancellable = Set() - init(navigationController: UINavigationController) { + private let dependency: injector + + init( + navigationController: UINavigationController, + dependency: injector + ) { self.navigationController = navigationController + self.dependency = dependency bindState() } @@ -52,7 +58,7 @@ extension AppCoordinator { } private func startTabBar() { - let tabBarCoordinator = TabBarCoordinator(navigationController: navigationController) + let tabBarCoordinator = TabBarCoordinator(navigationController: navigationController, dependency: dependency) tabBarCoordinator.parentCoordinator = self tabBarCoordinator.start() diff --git a/Makgulli/Application/DIContainer/AppDIContainer.swift b/Makgulli/Application/DIContainer/AppDIContainer.swift index d490e0b..af79b07 100644 --- a/Makgulli/Application/DIContainer/AppDIContainer.swift +++ b/Makgulli/Application/DIContainer/AppDIContainer.swift @@ -7,24 +7,32 @@ import Foundation +protocol injector: AnyObject { + func makeLocationDIContainer() -> LocationDIContainer + func makeEpisodeDIContainer() -> EpisodeDIContainer + func makeFavoriteDIContainer() -> FavoriteDIContainer + func makeAppInfoDIContainer() -> AppInfoDIContainer +} -final class AppDIContainer { - - static let shared = AppDIContainer() - - private init() {} - - lazy var networkManager = NetworkManager() +final class AppDIContainer: injector { + private lazy var networkManager = NetworkManager() + private lazy var imageStorage = DefaultImageStorage(fileManager: FileManager.default) func makeLocationDIContainer() -> LocationDIContainer { let dependencies = LocationDIContainer.Dependencies( - networkManager: networkManager + networkManager: networkManager, + imageStorage: imageStorage ) + return LocationDIContainer(dependencies: dependencies) } func makeEpisodeDIContainer() -> EpisodeDIContainer { - return EpisodeDIContainer() + let dependencies = EpisodeDIContainer.Dependencies( + imageStorage: imageStorage + ) + + return EpisodeDIContainer(dependencies: dependencies) } func makeFavoriteDIContainer() -> FavoriteDIContainer { diff --git a/Makgulli/Application/DIContainer/EpisodeDIContainer.swift b/Makgulli/Application/DIContainer/EpisodeDIContainer.swift index 8914ae6..7fa1a8d 100644 --- a/Makgulli/Application/DIContainer/EpisodeDIContainer.swift +++ b/Makgulli/Application/DIContainer/EpisodeDIContainer.swift @@ -9,10 +9,20 @@ import Foundation final class EpisodeDIContainer { + struct Dependencies { + let imageStorage: ImageStorage + } + + private let dependencies: Dependencies + + init(dependencies: Dependencies) { + self.dependencies = dependencies + } + // MARK: - Repository private func makeWriteEpisodeRepository() -> WriteEpisodeRepository { DefaultWriteEpisodeRepository( - imageStorage: DefaultImageStorage(fileManager: FileManager()) + imageStorage: dependencies.imageStorage ) } @@ -30,7 +40,7 @@ final class EpisodeDIContainer { private func makeEpisodeDetailRepository() -> EpisodeDetailRepository { DefaultEpisodeDetailRepository( - imageStorage: DefaultImageStorage(fileManager: FileManager()) + imageStorage: dependencies.imageStorage ) } diff --git a/Makgulli/Application/DIContainer/LocationDIContainer.swift b/Makgulli/Application/DIContainer/LocationDIContainer.swift index 2145b0d..2fd5b77 100644 --- a/Makgulli/Application/DIContainer/LocationDIContainer.swift +++ b/Makgulli/Application/DIContainer/LocationDIContainer.swift @@ -11,6 +11,7 @@ final class LocationDIContainer { struct Dependencies { let networkManager: NetworkManager + let imageStorage: ImageStorage } private let dependencies: Dependencies @@ -38,7 +39,7 @@ final class LocationDIContainer { private func makeLocationDetailRepository() -> LocationDetailRepository { DefaultLocationDetailRepository( - imageStorage: DefaultImageStorage(fileManager: FileManager()) + imageStorage: dependencies.imageStorage ) } diff --git a/Makgulli/Application/SceneDelegate.swift b/Makgulli/Application/SceneDelegate.swift index 975b622..3aeadcd 100644 --- a/Makgulli/Application/SceneDelegate.swift +++ b/Makgulli/Application/SceneDelegate.swift @@ -20,7 +20,7 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate { window = UIWindow(windowScene: windowScene) let navigationController = UINavigationController() - coordinator = AppCoordinator(navigationController: navigationController) + coordinator = AppCoordinator(navigationController: navigationController, dependency: AppDIContainer()) window?.rootViewController = navigationController window?.makeKeyAndVisible() diff --git a/Makgulli/Presentation/AppInfo/Coordinator/AppInfoCoordinator.swift b/Makgulli/Presentation/AppInfo/Coordinator/AppInfoCoordinator.swift index f6d098f..eff4572 100644 --- a/Makgulli/Presentation/AppInfo/Coordinator/AppInfoCoordinator.swift +++ b/Makgulli/Presentation/AppInfo/Coordinator/AppInfoCoordinator.swift @@ -9,12 +9,18 @@ import UIKit import SafariServices final class AppInfoCoordinator: Coordinator { - var parentCoordinator: (any Coordinator)? + weak var parentCoordinator: (any Coordinator)? var childCoordinators: [any Coordinator] = [] var navigationController: UINavigationController - init(navigationController: UINavigationController) { + private let dependency: injector + + init( + navigationController: UINavigationController, + dependency: injector + ) { self.navigationController = navigationController + self.dependency = dependency } deinit { @@ -22,11 +28,11 @@ final class AppInfoCoordinator: Coordinator { } func start() { - let viewModel = AppDIContainer.shared + let viewModel = dependency .makeAppInfoDIContainer() .makeAppInfoViewModel() viewModel.coordinator = self - + let viewController = AppInfoViewController(viewModel: viewModel) push(viewController: viewController, navibarHidden: true, swipe: false) } diff --git a/Makgulli/Presentation/Common/Tabbar/TabBarCoordinator.swift b/Makgulli/Presentation/Common/Tabbar/TabBarCoordinator.swift index e01a00a..a4b96ee 100644 --- a/Makgulli/Presentation/Common/Tabbar/TabBarCoordinator.swift +++ b/Makgulli/Presentation/Common/Tabbar/TabBarCoordinator.swift @@ -13,8 +13,14 @@ final class TabBarCoordinator: Coordinator { var navigationController: UINavigationController private var tabBarController = UITabBarController() - init(navigationController: UINavigationController) { + private let dependency: injector + + init( + navigationController: UINavigationController, + dependency: injector + ) { self.navigationController = navigationController + self.dependency = dependency } func start() { @@ -54,7 +60,7 @@ extension TabBarCoordinator { private func setTabBarFlow(of tabBar: TabBar, to tabNavigationController: UINavigationController) { switch tabBar { case .makgulli: - let locationCoordinator = LocationCoordinator(navigationController: tabNavigationController) + let locationCoordinator = LocationCoordinator(navigationController: tabNavigationController, dependency: dependency) locationCoordinator.parentCoordinator = self locationCoordinator.start() @@ -62,7 +68,7 @@ extension TabBarCoordinator { case .favorite: - let favoriteCoordinator = FavoriteCoordinator(navigationController: tabNavigationController) + let favoriteCoordinator = FavoriteCoordinator(navigationController: tabNavigationController, dependency: dependency) favoriteCoordinator.parentCoordinator = self favoriteCoordinator.start() diff --git a/Makgulli/Presentation/EpisodeDetail/Coordinator/EpisodeDetailCoordinator.swift b/Makgulli/Presentation/EpisodeDetail/Coordinator/EpisodeDetailCoordinator.swift index 4712fd5..dffc2d2 100644 --- a/Makgulli/Presentation/EpisodeDetail/Coordinator/EpisodeDetailCoordinator.swift +++ b/Makgulli/Presentation/EpisodeDetail/Coordinator/EpisodeDetailCoordinator.swift @@ -8,14 +8,20 @@ import UIKit final class EpisodeDetailCoordinator: Coordinator { - var parentCoordinator: (any Coordinator)? + weak var parentCoordinator: (any Coordinator)? var childCoordinators: [any Coordinator] = [] var navigationController: UINavigationController var episode: Episode? var storeId: String? - init(navigationController: UINavigationController) { + private let dependency: injector + + init( + navigationController: UINavigationController, + dependency: injector + ) { self.navigationController = navigationController + self.dependency = dependency } deinit { @@ -25,7 +31,9 @@ final class EpisodeDetailCoordinator: Coordinator { func start() { guard let episode, let storeId else { return } - let viewModel = AppDIContainer.shared.makeEpisodeDIContainer().makeEpisodeDetailViewModel(episode: episode, storeId: storeId) + let viewModel = dependency + .makeEpisodeDIContainer() + .makeEpisodeDetailViewModel(episode: episode, storeId: storeId) viewModel.coordinator = self let viewController = EpisodeDetailViewController(viewModel: viewModel) diff --git a/Makgulli/Presentation/Favorite/Coordinator/FavoriteCoordinator.swift b/Makgulli/Presentation/Favorite/Coordinator/FavoriteCoordinator.swift index c46811a..9065515 100644 --- a/Makgulli/Presentation/Favorite/Coordinator/FavoriteCoordinator.swift +++ b/Makgulli/Presentation/Favorite/Coordinator/FavoriteCoordinator.swift @@ -11,13 +11,19 @@ final class FavoriteCoordinator: Coordinator { weak var parentCoordinator: (any Coordinator)? var childCoordinators: [any Coordinator] = [] var navigationController: UINavigationController - - init(navigationController: UINavigationController) { + + private let dependency: injector + + init( + navigationController: UINavigationController, + dependency: injector + ) { self.navigationController = navigationController + self.dependency = dependency } func start() { - let viewModel = AppDIContainer.shared + let viewModel = dependency .makeFavoriteDIContainer() .makeFavoriteViewModel() viewModel.coordinator = self @@ -30,7 +36,7 @@ final class FavoriteCoordinator: Coordinator { extension FavoriteCoordinator { func startLocationDetail(_ store: StoreVO) { - let locationDetailCoordinator = LocationDetailCoordinator(navigationController: navigationController) + let locationDetailCoordinator = LocationDetailCoordinator(navigationController: navigationController, dependency: dependency) locationDetailCoordinator.parentCoordinator = self locationDetailCoordinator.store = store locationDetailCoordinator.start() @@ -39,7 +45,7 @@ extension FavoriteCoordinator { } func startAppInfo() { - let appInfoCoordinator = AppInfoCoordinator(navigationController: navigationController) + let appInfoCoordinator = AppInfoCoordinator(navigationController: navigationController, dependency: dependency) appInfoCoordinator.parentCoordinator = self appInfoCoordinator.start() diff --git a/Makgulli/Presentation/Location/Coordinator/LocationCoordinator.swift b/Makgulli/Presentation/Location/Coordinator/LocationCoordinator.swift index 142ee2a..5ea34b0 100644 --- a/Makgulli/Presentation/Location/Coordinator/LocationCoordinator.swift +++ b/Makgulli/Presentation/Location/Coordinator/LocationCoordinator.swift @@ -12,12 +12,18 @@ final class LocationCoordinator: Coordinator { var childCoordinators: [any Coordinator] = [] var navigationController: UINavigationController - init(navigationController: UINavigationController) { + private let dependency: injector + + init( + navigationController: UINavigationController, + dependency: injector + ) { self.navigationController = navigationController + self.dependency = dependency } func start() { - let viewModel = AppDIContainer.shared + let viewModel = dependency .makeLocationDIContainer() .makeLocationViewModel() viewModel.coordinator = self @@ -30,7 +36,7 @@ final class LocationCoordinator: Coordinator { extension LocationCoordinator { func startLocationDetail(_ store: StoreVO) { - let locationDetailCoordinator = LocationDetailCoordinator(navigationController: navigationController) + let locationDetailCoordinator = LocationDetailCoordinator(navigationController: navigationController, dependency: dependency) locationDetailCoordinator.parentCoordinator = self locationDetailCoordinator.store = store locationDetailCoordinator.start() diff --git a/Makgulli/Presentation/LocationDetail/Coordinator/LocationDetailCoordinator.swift b/Makgulli/Presentation/LocationDetail/Coordinator/LocationDetailCoordinator.swift index c0e452a..61eb923 100644 --- a/Makgulli/Presentation/LocationDetail/Coordinator/LocationDetailCoordinator.swift +++ b/Makgulli/Presentation/LocationDetail/Coordinator/LocationDetailCoordinator.swift @@ -13,8 +13,14 @@ final class LocationDetailCoordinator: Coordinator { var navigationController: UINavigationController var store: StoreVO? - init(navigationController: UINavigationController) { + private let dependency: injector + + init( + navigationController: UINavigationController, + dependency: injector + ) { self.navigationController = navigationController + self.dependency = dependency } deinit { @@ -24,7 +30,7 @@ final class LocationDetailCoordinator: Coordinator { func start() { guard let store else { return } - let viewModel = AppDIContainer.shared + let viewModel = dependency .makeLocationDIContainer() .makeLocationDetailViewModel(store: store) viewModel.coordinator = self @@ -43,7 +49,7 @@ extension LocationDetailCoordinator { } func startWriteEpisode(store: StoreVO) { - let writeEpisodeCoordinator = WriteEpisodeCoordinator(navigationController: navigationController) + let writeEpisodeCoordinator = WriteEpisodeCoordinator(navigationController: navigationController, dependency: dependency) writeEpisodeCoordinator.parentCoordinator = self writeEpisodeCoordinator.store = store writeEpisodeCoordinator.start() @@ -52,7 +58,7 @@ extension LocationDetailCoordinator { } func startEpisodeDetail(episode: Episode, storeId: String) { - let episodeDetailCoordinator = EpisodeDetailCoordinator(navigationController: navigationController) + let episodeDetailCoordinator = EpisodeDetailCoordinator(navigationController: navigationController, dependency: dependency) episodeDetailCoordinator.parentCoordinator = self episodeDetailCoordinator.episode = episode episodeDetailCoordinator.storeId = storeId diff --git a/Makgulli/Presentation/WriteEpisode/Coordinator/WriteEpisodeCoordinator.swift b/Makgulli/Presentation/WriteEpisode/Coordinator/WriteEpisodeCoordinator.swift index df28b3b..f26efb6 100644 --- a/Makgulli/Presentation/WriteEpisode/Coordinator/WriteEpisodeCoordinator.swift +++ b/Makgulli/Presentation/WriteEpisode/Coordinator/WriteEpisodeCoordinator.swift @@ -8,13 +8,19 @@ import UIKit final class WriteEpisodeCoordinator: Coordinator { - var parentCoordinator: (any Coordinator)? + weak var parentCoordinator: (any Coordinator)? var childCoordinators: [any Coordinator] = [] var navigationController: UINavigationController var store: StoreVO? - init(navigationController: UINavigationController) { + private let dependency: injector + + init( + navigationController: UINavigationController, + dependency: injector + ) { self.navigationController = navigationController + self.dependency = dependency } deinit { @@ -24,7 +30,9 @@ final class WriteEpisodeCoordinator: Coordinator { func start() { guard let store else { return } - let viewModel = AppDIContainer.shared.makeEpisodeDIContainer().makeWriteEpisodeViewModel(store: store) + let viewModel = dependency + .makeEpisodeDIContainer() + .makeWriteEpisodeViewModel(store: store) viewModel.coordinator = self let viewController = WriteEpisodeViewController(viewModel: viewModel)