@@ -8,50 +8,122 @@ import UIKit
88
99final class EmotionEditViewController : BaseViewController < EmotionEditView > {
1010 override var bkNavigationTitle : String { " " }
11-
11+
1212 override var bkNavigationBarStyle : UINavigationController . BKNavigationBarStyle {
1313 . standard( viewController: self )
1414 }
15-
15+
1616 weak var coordinator : NoteEditCoordinator ?
1717 private var cancellables = Set < AnyCancellable > ( )
18-
19- private let initialEmotion : Emotion ?
20- @Published private var selectedEmotion : Emotion ?
21- private let completion : ( Emotion ) -> Void
22-
23- init ( currentEmotion: Emotion ? , completion: @escaping ( Emotion ) -> Void ) {
24- self . initialEmotion = currentEmotion
18+
19+ private let initialPrimaryEmotion : PrimaryEmotion ?
20+ private let initialDetailEmotions : [ DetailEmotion ]
21+ @Published private var selectedPrimaryEmotion : PrimaryEmotion ?
22+ private let completion : ( PrimaryEmotion , [ DetailEmotion ] ) -> Void
23+
24+ @Autowired private var fetchDetailEmotionsUseCase : FetchDetailEmotionsUseCase
25+
26+ // 바텀시트 표시 대기용
27+ private var pendingEmotionForSheet : PrimaryEmotion ?
28+ private var isLoadingEmotions : Bool = false
29+
30+ init (
31+ currentEmotion: PrimaryEmotion ? ,
32+ currentDetailEmotions: [ DetailEmotion ] = [ ] ,
33+ completion: @escaping ( PrimaryEmotion , [ DetailEmotion ] ) -> Void
34+ ) {
35+ self . initialPrimaryEmotion = currentEmotion
36+ self . initialDetailEmotions = currentDetailEmotions
2537 self . completion = completion
26- self . _selectedEmotion = . init( initialValue: currentEmotion)
38+ self . _selectedPrimaryEmotion = . init( initialValue: currentEmotion)
2739 super. init ( )
2840 }
29-
41+
3042 override func viewWillAppear( _ animated: Bool ) {
3143 super. viewWillAppear ( animated)
32-
33- if let emotion = initialEmotion {
34- contentView. setSelectedEmotion ( emotion)
44+
45+ if let emotion = initialPrimaryEmotion {
46+ contentView. setSelectedPrimaryEmotion ( emotion)
47+ contentView. setDetailEmotions ( initialDetailEmotions)
3548 }
3649 contentView. setEditButtonEnabled ( false )
3750 }
38-
51+
3952 override func bindAction( ) {
4053 contentView. eventPublisher
4154 . sink { [ weak self] event in
4255 guard let self = self else { return }
4356 switch event {
4457 case . emotionDidChange( let newEmotion) :
45- self . selectedEmotion = newEmotion
46- let isDiff = ( newEmotion != self . initialEmotion)
47- self . contentView. setEditButtonEnabled ( isDiff)
58+ self . selectedPrimaryEmotion = newEmotion
59+ self . updateEditButtonState ( )
60+
61+ case . emotionSelected( let emotion) :
62+ // other가 아닌 경우 세부감정 바텀시트 표시
63+ if emotion != . other {
64+ self . fetchAndPresentDetailEmotionSheet ( for: emotion)
65+ }
66+
4867 case . editButtonTapped:
49- if let emotion = self . selectedEmotion {
50- self . completion ( emotion)
68+ if let emotion = self . selectedPrimaryEmotion {
69+ let detailEmotions = self . contentView. getSelectedDetailEmotions ( )
70+ self . completion ( emotion, detailEmotions)
5171 self . navigationController? . popViewController ( animated: true )
5272 }
5373 }
5474 }
5575 . store ( in: & cancellables)
5676 }
77+
78+ private func updateEditButtonState( ) {
79+ let emotionChanged = selectedPrimaryEmotion != initialPrimaryEmotion
80+ let detailEmotionsChanged = contentView. getSelectedDetailEmotions ( ) != initialDetailEmotions
81+ let isDiff = emotionChanged || detailEmotionsChanged
82+ contentView. setEditButtonEnabled ( isDiff)
83+ }
84+
85+ private func fetchAndPresentDetailEmotionSheet( for emotion: PrimaryEmotion ) {
86+ guard !isLoadingEmotions else { return }
87+
88+ isLoadingEmotions = true
89+ contentView. setLoadingEmotions ( true )
90+
91+ fetchDetailEmotionsUseCase. execute ( for: emotion)
92+ . receive ( on: DispatchQueue . main)
93+ . sink (
94+ receiveCompletion: { [ weak self] completion in
95+ self ? . isLoadingEmotions = false
96+ self ? . contentView. setLoadingEmotions ( false )
97+ if case . failure = completion {
98+ // 에러 처리 - 필요시 coordinator에서 처리
99+ }
100+ } ,
101+ receiveValue: { [ weak self] detailEmotions in
102+ self ? . presentDetailEmotionSheet ( for: emotion, detailEmotions: detailEmotions)
103+ }
104+ )
105+ . store ( in: & cancellables)
106+ }
107+
108+ private func presentDetailEmotionSheet( for primaryEmotion: PrimaryEmotion , detailEmotions: [ DetailEmotion ] ) {
109+ let currentDetailEmotions = contentView. getSelectedDetailEmotions ( )
110+ let sheet = BKBottomSheetViewController . makeDetailEmotionSheet (
111+ primaryEmotion: primaryEmotion,
112+ detailEmotions: detailEmotions,
113+ initialSelectedDetailEmotions: currentDetailEmotions,
114+ skipAction: { [ weak self] in
115+ // 건너뛰기: 세부감정 초기화
116+ self ? . contentView. setDetailEmotions ( [ ] )
117+ self ? . updateEditButtonState ( )
118+ self ? . dismiss ( animated: true )
119+ } ,
120+ confirmAction: { [ weak self] selectedDetailEmotions in
121+ self ? . contentView. setDetailEmotions ( selectedDetailEmotions)
122+ self ? . updateEditButtonState ( )
123+ self ? . dismiss ( animated: true )
124+ }
125+ )
126+
127+ sheet. show ( from: self , animated: true )
128+ }
57129}
0 commit comments