[Refactor] UINavigationController 기반 코디네이터 적용 #91
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PULL REQUEST
📄 작업 내용
💻 주요 코드 설명
코디네이터 사용하기
1. 코어 개념
Coordinator 프로토콜
UINavigationController를 보유하고buildViewController(for:)에서 Route별 화면을 생성push(to:),pop(),popToRoot(),present(to:presentationStyle:transitionStyle:),dismiss()Route 프로토콜
EnvironmentKey & EnvironmentValues
@Observable없이 SwiftUI 환경에 주입하기 위한 메커니즘2. How?
1) Route 정의
화면 전환 단위를 enum case로 설계 (필요 데이터는 연관값으로 전달)
2) EnvironmentKey 정의
각 Feature의 Coordinator를 환경에 주입하기 위한 키 정의
3) Coordinator 구현
buildViewController(for:)는 모든 Route 케이스에 대해 처리UIHostingController로 감싸고.environment(\.fooCoordinator, self)로 주입4) Navigation Host 연결 (Tab 또는 부모 컨테이너에서 시작)
start()호출UINavigationControllerDelegate로 스택 변화 감시5) 뷰에서 Coordinator 접근
@Environment(\.fooCoordinator)로 접근coordinator?체이닝 사용6) 화면 전환 호출 예시
coordinator?.push(to: .detail(id: 123))coordinator?.present(to: .web(url), presentationStyle: .fullScreen, transitionStyle: .crossDissolve)coordinator?.pop(),coordinator?.popToRoot()coordinator?.dismiss()3. 핵심 개념 상세
UIViewControllerRepresentable
UIViewController를 생성, 관리하기 위한 브릿지 역할makeUIViewController(context:)는 컨트롤러 생성을,updateUIViewController(_:context:)는 SwiftUI 상태 변경 시 기존 컨트롤러에 필요한 변경을 적용updateUIViewController(_:context:)에서 아무 동작을 수행하지 않음EnvironmentKey를 사용하는 이유
1. Observable 오버헤드 제거
@Observable은 상태 관찰 시스템을 활성화하므로 불필요한 경우 제거2. 타입 안전한 주입
EnvironmentKey방식은 일반 클래스도 환경에 주입 가능homeCoordinator,loginCoordinator,searchCoordinator)로 명확한 분리4. 실제 프로젝트 예시
Home Module
HomeRoute(home,interest)\.homeCoordinatorHomeView,InterestTempViewLogin Module
LoginRoute(login,terms,nickname,loginForbidden,signupFailed,safari)\.loginCoordinatorLoginView,TermsView,NicknameSettingViewSearch Module
SearchRoute(explore,search)\.searchCoordinatorExploreView,SearchView📚 참고자료
내 두뇌🔗 연결된 이슈