Skip to content

[#439] MainView의 destination ViewModel 생성 및 생명주기를 개선한다#446

Merged
opficdev merged 4 commits intodevelopfrom
refactor/#439-MainView-lifecycle
May 10, 2026
Merged

[#439] MainView의 destination ViewModel 생성 및 생명주기를 개선한다#446
opficdev merged 4 commits intodevelopfrom
refactor/#439-MainView-lifecycle

Conversation

@opficdev
Copy link
Copy Markdown
Owner

@opficdev opficdev commented May 10, 2026

🔗 연관된 이슈

🎯 의도

  • 메인 셸인 MainView가 탭 구성, SplitView 분기, 하위 화면 진입을 함께 담당하는 구조
  • 기존 MainView에 하위 화면 ViewModel 생성 로직까지 남아 있어 화면 구성 책임과 객체 생명주기 관리 책임이 섞이던 문제 발생
  • MainView는 UI 조합에 집중하고, 하위 화면 ViewModel 생성 및 보관 책임은 MainViewCoordinator로 이동하기 위한 변경
  • compact/regular 전환처럼 동일 화면이 다시 구성되는 상황에서 ViewModel 인스턴스가 불필요하게 새로 생성되지 않도록 생명주기 정리
  • 현재 하위 화면 기준으로 단일 ViewModel을 유지하여 화면 재구성 시 기존 인스턴스를 재사용하기 위한 구조
  • TodoListViewModel의 category처럼 생성 시점에 고정되는 값은 화면 상태가 아닌 ViewModel 식별 입력으로 관리하기 위한 정리

📝 작업 내용

📌 요약

  • MainViewCoordinator의 하위 화면 ViewModel 생성 역할 추가
  • TodoListViewModel / TodoDetailViewModel 단일 인스턴스 보관 방식 적용
  • TodoListViewModel의 category를 State에서 ViewModel 생성 기준값으로 분리
  • PR 템플릿에 작업 의도 섹션 추가

🔍 상세

  • MainView의 make~ViewModel() 생성 로직을 MainViewCoordinator로 이동
  • TodoListViewModel은 현재 category와 일치할 때 기존 인스턴스 재사용
  • TodoDetailViewModel은 현재 todoId와 showEditButton 조건이 일치할 때 기존 인스턴스 재사용
  • category별 dictionary 및 todoId별 dictionary 대신 현재 하위 화면 기준 단일 ViewModel 보관
  • ViewModel이 가진 생성 기준 값을 재사용 조건으로 활용하여 코디네이터의 별도 key 상태 제거
  • TodoDetailViewModel의 todoId를 ViewModel 재사용 판단에 활용하도록 접근 범위 조정
  • TodoListViewModel.State에서 category 제거 및 TodoListViewModel의 불변 프로퍼티로 이동
  • TodoListView에서 category 참조를 viewModel.state.category에서 viewModel.category로 변경

opficdev added 4 commits May 10, 2026 21:00
MainViewCoordinator가 생성하는 하위 화면 ViewModel을 단일 인스턴스 기준으로 보관하도록 변경

- TodoListViewModel은 현재 category와 일치할 때 기존 인스턴스 재사용
- TodoDetailViewModel은 현재 todoId와 showEditButton 조건이 일치할 때 기존 인스턴스 재사용
- category별 dictionary와 todoId별 dictionary 제거
- ViewModel이 가진 생성 기준 값을 재사용 조건으로 활용하여 코디네이터의 별도 key 상태 제거
- compact/regular 전환으로 같은 하위 화면이 재생성될 때 기존 ViewModel 인스턴스를 유지하는 구조
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors ViewModel instantiation by moving factory methods from MainView to MainViewCoordinator and implementing a basic caching mechanism for TodoListViewModel and TodoDetailViewModel. Additionally, the category property in TodoListViewModel was moved from the State struct directly into the ViewModel class. Feedback was provided regarding the single-instance caching strategy in the coordinator, noting that it may cause state loss when navigating through multiple instances of the same view type in a NavigationStack. An ID-based caching strategy was suggested as a more robust alternative to preserve state during navigation.

Comment thread DevLog/UI/Main/MainViewCoordinator.swift
@opficdev opficdev merged commit ed8cd34 into develop May 10, 2026
1 check passed
@opficdev opficdev deleted the refactor/#439-MainView-lifecycle branch May 10, 2026 15:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MainView의 destination ViewModel 생성 및 생명주기를 개선한다

1 participant