Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REFACTOR] 의존성 주입 수정 #33

Merged
merged 3 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions Makgulli/Application/Coordinator/AppCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@ final class AppCoordinator: Coordinator {
let flow = PassthroughSubject<AppFlow, Never>()
private var cancellable = Set<AnyCancellable>()

init(navigationController: UINavigationController) {
private let dependency: injector

init(
navigationController: UINavigationController,
dependency: injector
) {
self.navigationController = navigationController
self.dependency = dependency
bindState()
}

Expand Down Expand Up @@ -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()

Expand Down
26 changes: 17 additions & 9 deletions Makgulli/Application/DIContainer/AppDIContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<LocationAPI>()
final class AppDIContainer: injector {
private lazy var networkManager = NetworkManager<LocationAPI>()
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 {
Expand Down
14 changes: 12 additions & 2 deletions Makgulli/Application/DIContainer/EpisodeDIContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}

Expand All @@ -30,7 +40,7 @@ final class EpisodeDIContainer {

private func makeEpisodeDetailRepository() -> EpisodeDetailRepository {
DefaultEpisodeDetailRepository(
imageStorage: DefaultImageStorage(fileManager: FileManager())
imageStorage: dependencies.imageStorage
)
}

Expand Down
3 changes: 2 additions & 1 deletion Makgulli/Application/DIContainer/LocationDIContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ final class LocationDIContainer {

struct Dependencies {
let networkManager: NetworkManager<LocationAPI>
let imageStorage: ImageStorage
}

private let dependencies: Dependencies
Expand Down Expand Up @@ -38,7 +39,7 @@ final class LocationDIContainer {

private func makeLocationDetailRepository() -> LocationDetailRepository {
DefaultLocationDetailRepository(
imageStorage: DefaultImageStorage(fileManager: FileManager())
imageStorage: dependencies.imageStorage
)
}

Expand Down
2 changes: 1 addition & 1 deletion Makgulli/Application/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
14 changes: 10 additions & 4 deletions Makgulli/Presentation/AppInfo/Coordinator/AppInfoCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,30 @@ 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 {
debugPrint("deinit Coordinator: \(self)")
}

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)
}
Expand Down
12 changes: 9 additions & 3 deletions Makgulli/Presentation/Common/Tabbar/TabBarCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -54,15 +60,15 @@ 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()

addDependency(locationCoordinator)


case .favorite:
let favoriteCoordinator = FavoriteCoordinator(navigationController: tabNavigationController)
let favoriteCoordinator = FavoriteCoordinator(navigationController: tabNavigationController, dependency: dependency)
favoriteCoordinator.parentCoordinator = self
favoriteCoordinator.start()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
Loading