Skip to content

Commit a7ee80e

Browse files
authored
[Refactor] DI Container 사용법 수정 (#36)
* feat(delete): DI 패키지 삭제 * feat(chore): DI 패키지 -> Common, Intro, Login 패키지로 DI 이동 * feat(refactor): 늘어나는 의존성 dependency로 사용 이후에 라우팅 프로토콜도 분리하여 의존성 분리 예정 * Update iOS Simulator destination OS version
1 parent d6957cb commit a7ee80e

29 files changed

Lines changed: 547 additions & 394 deletions

Alarm/Package.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ let package = Package(
3737
)
3838
],
3939
dependencies: [
40-
.package(name: "DI", path: "../DI"),
4140
.package(name: "Common", path: "../Common"),
4241
.package(name: "Infrastructure", path: "../Infrastructure"),
4342
],
@@ -48,8 +47,7 @@ let package = Package(
4847
.target(config: .domain),
4948
.target(config: .data),
5049
.target(config: .presentation),
51-
.product(name: "DI", package: "DI"),
52-
.product(name: "AppDI", package: "DI"),
50+
.product(name: "DIKit", package: "Common"),
5351
.product(name: "NetworkInterface", package: "Infrastructure"),
5452
],
5553
),

Alarm/Sources/DI/AlarmDIContainer.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import Foundation
99
import DIKit
10-
import AppDI
1110
import NetworkInterface
1211
import AlarmDomain
1312
import AlarmData
File renamed without changes.

AppCore/Package.swift

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// swift-tools-version: 5.9
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
enum Config: String, CaseIterable {
7+
static let name: String = "AppCore"
8+
9+
case di = "DI"
10+
11+
var name: String {
12+
return "\(Config.name)\(rawValue)"
13+
}
14+
15+
var path: String {
16+
"Sources/\(rawValue)"
17+
}
18+
}
19+
20+
let package = Package(
21+
name: Config.name,
22+
platforms: [.iOS(.v17)],
23+
products: [
24+
.library(
25+
name: "AppCore",
26+
targets: Config.allCases.map(\.name)
27+
),
28+
],
29+
dependencies: [
30+
.package(name: "Intro", path: "../Intro"),
31+
.package(name: "Login", path: "../Login"),
32+
.package(name: "Home", path: "../Home"),
33+
.package(name: "Community", path: "../Community"),
34+
.package(name: "MyPage", path: "../MyPage"),
35+
.package(name: "Alarm", path: "../Alarm"),
36+
37+
.package(name: "3rdParty", path: "../3rdParty"),
38+
.package(name: "Common", path: "../Common"),
39+
.package(name: "Infrastructure", path: "../Infrastructure"),
40+
],
41+
targets: [
42+
.target(
43+
config: .di,
44+
dependencies: [
45+
.product(name: "DataSources", package: "Common"),
46+
.product(name: "Managers", package: "Common"),
47+
.product(name: "NetworkInterface", package: "Infrastructure"),
48+
.product(name: "NetworkImpl", package: "Infrastructure"),
49+
.product(name: "FCMService", package: "3rdParty"),
50+
51+
.product(name: "IntroDI", package: "Intro"),
52+
.product(name: "LoginDI", package: "Login"),
53+
.product(name: "HomeDI", package: "Home"),
54+
.product(name: "CommunityDI", package: "Community"),
55+
.product(name: "MyPageDI", package: "MyPage"),
56+
.product(name: "AlarmDI", package: "Alarm"),
57+
]
58+
)
59+
]
60+
)
61+
62+
extension Target {
63+
static func target(
64+
config: Config,
65+
dependencies: [Dependency] = [],
66+
exclude: [String] = [],
67+
sources: [String]? = nil,
68+
resources: [Resource]? = nil,
69+
publicHeadersPath: String? = nil,
70+
packageAccess: Bool = false,
71+
cSettings: [CSetting]? = nil,
72+
cxxSettings: [CXXSetting]? = nil,
73+
swiftSettings: [SwiftSetting]? = nil,
74+
linkerSettings: [LinkerSetting]? = nil,
75+
plugins: [PluginUsage]? = nil,
76+
) -> Target {
77+
return .target(
78+
name: config.name,
79+
dependencies: dependencies,
80+
path: config.path,
81+
exclude: exclude,
82+
sources: sources,
83+
resources: resources,
84+
publicHeadersPath: publicHeadersPath,
85+
packageAccess: packageAccess,
86+
cSettings: cSettings,
87+
cxxSettings: cxxSettings,
88+
swiftSettings: swiftSettings,
89+
linkerSettings: linkerSettings,
90+
plugins: plugins)
91+
}
92+
}
93+
94+
extension Target.Dependency {
95+
static func target(config: Config) -> Self {
96+
return .target(name: config.name)
97+
}
98+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ import NetworkInterface
1515
import NetworkImpl
1616
import FCMService
1717

18+
import IntroDI
19+
import LoginDI
20+
import HomeDI
21+
import CommunityDI
22+
import MyPageDI
23+
import AlarmDI
24+
1825
// MARK: - App Assembly
1926
struct AppAssembly: Assembly {
2027
func assemble(container: GenericDIContainer) {
@@ -71,6 +78,37 @@ struct AppAssembly: Assembly {
7178
udManager: resolver.resolve(UserDefaultsManager.self)
7279
)
7380
}
81+
82+
// MARK: - IntroDIContainer
83+
container.register(IntroDIContainer.self, scope: .singleton) { r in
84+
IntroDIContainer(appContainer: container)
85+
}
86+
87+
// MARK: - LoginDIContainer
88+
container.register(LoginDIContainer.self, scope: .singleton) { r in
89+
LoginDIContainer(appContainer: container)
90+
}
91+
92+
// MARK: - HomeDIContainer
93+
container.register(HomeDIContainer.self, scope: .singleton) { r in
94+
HomeDIContainer(appContainer: container)
95+
}
96+
97+
// MARK: - CommunityDIContainer
98+
container.register(CommunityDIContainer.self, scope: .singleton) { r in
99+
CommunityDIContainer(appContainer: container)
100+
}
101+
102+
// MARK: - MyPageDIContainer
103+
container.register(MyPageDIContainer.self, scope: .singleton) { r in
104+
MyPageDIContainer(appContainer: container)
105+
}
106+
107+
// MARK: - AlarmDIContainer
108+
container.register(AlarmDIContainer.self, scope: .singleton) { r in
109+
AlarmDIContainer(appContainer: container)
110+
}
111+
74112
}
75113
}
76114

@@ -96,6 +134,26 @@ public final class AppDIContainer: @unchecked Sendable {
96134
return container
97135
}
98136

137+
public var introDIContainer: IntroDIContainer {
138+
return container.resolve(IntroDIContainer.self)
139+
}
140+
141+
public var loginDIContainer: LoginDIContainer {
142+
return container.resolve(LoginDIContainer.self)
143+
}
144+
145+
public var homeDIContainer: HomeDIContainer {
146+
return container.resolve(HomeDIContainer.self)
147+
}
148+
149+
public var communityDIContainer: CommunityDIContainer {
150+
return container.resolve(CommunityDIContainer.self)
151+
}
152+
153+
public var mypageDIContainer: MyPageDIContainer {
154+
return container.resolve(MyPageDIContainer.self)
155+
}
156+
99157
/// Direct access to AppStateManager for app initialization
100158
public func makeAppStateManager() -> AppStateManager {
101159
return container.resolve(AppStateManager.self)

Common/Package.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ let package = Package(
1919
name: "DataSources",
2020
targets: ["DataSources"]
2121
),
22+
.library(
23+
name: "DIKit",
24+
targets: ["DIKit"]
25+
),
2226
.library(
2327
name: "LocalizedString",
2428
targets: ["LocalizedString"]
@@ -48,6 +52,7 @@ let package = Package(
4852
]
4953
),
5054
.target(name: "DataSources"),
55+
.target(name: "DIKit"),
5156
.target(name: "LocalizedString"),
5257
.target(name: "Util"),
5358
.target(
File renamed without changes.

Common/Sources/Managers/AppStateManager.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ public final class AppStateManager {
4949
}
5050

5151
public func completeSplash() {
52-
if isOnboardingCompleted {
53-
state = isLoginCompleted ? .main : .login
52+
if isLoginCompleted {
53+
state = .main
54+
} else if isOnboardingCompleted {
55+
state = .main
5456
} else {
5557
state = .onboarding
5658
}
@@ -59,7 +61,7 @@ public final class AppStateManager {
5961
// 온보딩 완료
6062
public func completeOnboarding() {
6163
udManager.isOnboardingCompleted = true
62-
state = isLoginCompleted ? .main : .login
64+
state = .login
6365
}
6466

6567
// 로그인 완료

Community/Package.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,19 @@ let package = Package(
2929
targets: Config.allCases.map(\.name)
3030
),
3131
.library(
32-
name: "CommunityDomain",
33-
targets: ["CommunityDomain"]
32+
name: Config.di.name,
33+
targets: [Config.di.name]
34+
),
35+
.library(
36+
name: Config.domain.name,
37+
targets: [Config.domain.name]
38+
),
39+
.library(
40+
name: Config.presentation.name,
41+
targets: [Config.presentation.name]
3442
),
3543
],
3644
dependencies: [
37-
.package(name: "DI", path: "../DI"),
3845
.package(name: "Common", path: "../Common"),
3946
.package(name: "Infrastructure", path: "../Infrastructure"),
4047
.package(name: "Alarm", path: "../Alarm"),
@@ -46,8 +53,7 @@ let package = Package(
4653
.target(config: .domain),
4754
.target(config: .data),
4855
.target(config: .presentation),
49-
.product(name: "DI", package: "DI"),
50-
.product(name: "AppDI", package: "DI"),
56+
.product(name: "DIKit", package: "Common"),
5157
.product(name: "NetworkInterface", package: "Infrastructure"),
5258
.product(name: "NetworkImpl", package: "Infrastructure"),
5359
],

Community/Sources/DI/CommunityDIContainer.swift

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@
77

88
import Foundation
99
import DIKit
10-
import AppDI
1110
import NetworkInterface
1211
import NetworkImpl
1312
import CommunityDomain
1413
import CommunityData
1514
import CommunityPresentation
1615
import Managers
1716

17+
import AlarmDI
18+
import AlarmPresentation
19+
1820
struct CommunityWriteAssembly: Assembly {
1921
func assemble(container: GenericDIContainer) {
2022
container.register(CreateBoardUseCase.self) { resolver in
@@ -100,42 +102,45 @@ public final class CommunityDIContainer {
100102
private let container: GenericDIContainer
101103

102104
// MARK: - Initialization
103-
public init(appContainer: AppDIContainer = .shared) {
104-
self.container = GenericDIContainer(parent: appContainer.baseContainer)
105+
public init(appContainer: GenericDIContainer) {
106+
self.container = appContainer
105107
CommunityAssembly().assemble(container: container)
106108
CommunityWriteAssembly().assemble(container: container)
107109
}
110+
}
108111

112+
113+
extension CommunityDIContainer: CommunityDependency {
114+
public var alarmListComponent: any AlarmPresentation.AlarmListDependecy {
115+
container.resolve(AlarmDIContainer.self)
116+
}
117+
118+
public var component: any CommunityPresentation.CommunityDetailDependency {
119+
self
120+
}
121+
109122
// MARK: - Factory Methods
110-
@MainActor
111123
public func makeCommunityViewModel() -> CommunityViewModel {
112124
return container.resolve(CommunityViewModel.self)
113125
}
114-
}
115-
116-
extension CommunityDIContainer: CommunityWriteFactory {
117-
public func makeWriteViewModel() -> any CommunityWriteViewModelProtocol {
126+
127+
public func makeWriteViewModel() -> any CommunityPresentation.CommunityWriteViewModelProtocol {
118128
container.resolve(CommunityWriteViewModel.self)
119129
}
120130
}
121131

122-
extension CommunityDIContainer: CommunityDetailFactory {
123-
@MainActor
124-
public func makeDetailViewModel() -> CommunityDetailViewModel {
125-
return container.resolve(CommunityDetailViewModel.self)
132+
extension CommunityDIContainer: CommunityDetailDependency {
133+
public func makeDetailViewModel() -> CommunityPresentation.CommunityDetailViewModel {
134+
container.resolve(CommunityDetailViewModel.self)
126135
}
127-
}
128-
129-
extension CommunityDIContainer: UpdateBoardFactory {
130-
public func makeViewModel(boardId: Int) -> CommunityWriteViewModelProtocol {
131-
return UpdateBoardViewModel(
136+
137+
public func makeViewModel(boardId: Int) -> any CommunityPresentation.CommunityWriteViewModelProtocol {
138+
UpdateBoardViewModel(
132139
boardId: boardId,
133140
updateBoardUseCase: container.resolve(UpdateBoardUseCase.self)
134141
)
135142
}
136-
}
137-
138-
extension CommunityDIContainer: ReportBoardFactory {
143+
139144
public func makeViewModel(req: ReportRequest) -> CommunityReportViewModel {
140145
return CommunityReportViewModel(
141146
usecase: container.resolve(ReportContentUseCase.self),

0 commit comments

Comments
 (0)