@@ -8,7 +8,10 @@ import DesignSystem
88import Domain
99
1010public final class InterestFieldViewController : BaseViewController < InterestFieldViewModel > {
11- private let viewAppearRelay = PublishRelay < Void > ( )
11+ private let selectedIndexesRelay = BehaviorRelay < Set < IndexPath > > ( value: [ ] )
12+ private let interestsRelay = BehaviorRelay < [ CodeEntity ] > ( value: [ ] )
13+ private let selectedInterestsRelay = BehaviorRelay < [ CodeEntity ] > ( value: [ ] )
14+
1215 private let interestFieldTitleLabel = UILabel ( ) . then {
1316 $0. setJobisText (
1417 " 님의 \n 관심사를 선택해주세요 " ,
@@ -26,7 +29,25 @@ public final class InterestFieldViewController: BaseViewController<InterestField
2629 )
2730 }
2831
29- private let majorCollectionView = InterestFieldCollectionView ( )
32+ private let layout = AlignedCollectionViewFlowLayout ( ) . then {
33+ $0. minimumLineSpacing = 8
34+ $0. minimumInteritemSpacing = 8
35+ $0. scrollDirection = . vertical
36+ $0. estimatedItemSize = CGSize ( width: 100 , height: 31 )
37+ $0. sectionInset = . zero
38+ }
39+
40+ private lazy var majorCollectionView = UICollectionView (
41+ frame: . zero,
42+ collectionViewLayout: layout
43+ ) . then {
44+ $0. backgroundColor = . clear
45+ $0. isScrollEnabled = false
46+ $0. register (
47+ MajorCollectionViewCell . self,
48+ forCellWithReuseIdentifier: MajorCollectionViewCell . identifier
49+ )
50+ }
3051
3152 private let selectButton = JobisButton ( style: . main) . then {
3253 $0. setText ( " 관심 분야를 선택해 주세요! " )
@@ -57,45 +78,56 @@ public final class InterestFieldViewController: BaseViewController<InterestField
5778 $0. bottom. equalTo ( selectButton. snp. top) . offset ( - 24 )
5879 }
5980 selectButton. snp. makeConstraints {
60- $0. bottom. equalTo ( self . view. safeAreaLayoutGuide. snp. bottom) . inset ( 12 )
81+ $0. bottom. equalTo ( view. safeAreaLayoutGuide. snp. bottom) . inset ( 12 )
6182 $0. leading. trailing. equalToSuperview ( ) . inset ( 24 )
6283 }
6384 }
6485
65- public override func configureNavigation( ) {
66- setSmallTitle ( title: " 관심사 설정 " )
67- self . navigationItem. largeTitleDisplayMode = . never
68- self . hideTabbar ( )
69- }
70-
7186 public override func bind( ) {
7287 let input = InterestFieldViewModel . Input (
73- viewAppear: viewAppearRelay ,
88+ viewAppear: viewWillAppearPublisher ,
7489 selectButtonDidTap: selectButton. rx. tap. asSignal ( ) ,
75- selectedInterests: majorCollectionView . selectedInterests
90+ selectedInterests: selectedInterestsRelay . asObservable ( )
7691 )
7792
7893 let output = viewModel. transform ( input)
7994
80- rx. methodInvoked ( #selector( UIViewController . viewWillAppear ( _: ) ) )
81- . map { _ in }
82- . bind ( to: viewAppearRelay)
95+ output. availableInterests
96+ . bind ( to: interestsRelay)
8397 . disposed ( by: disposeBag)
8498
85- output. availableInterests
86- . subscribe ( onNext: { [ weak self] list in
87- self ? . majorCollectionView. updateInterests ( list)
88- } )
99+ interestsRelay
100+ . bind ( to: majorCollectionView. rx. items (
101+ cellIdentifier: MajorCollectionViewCell . identifier,
102+ cellType: MajorCollectionViewCell . self
103+ ) ) { [ weak self] index, codeEntity, cell in
104+ let indexPath = IndexPath ( item: index, section: 0 )
105+ let isSelected = self ? . selectedIndexesRelay. value. contains ( indexPath) ?? false
106+
107+ cell. adapt ( model: codeEntity)
108+ cell. isCheck = isSelected
109+ }
89110 . disposed ( by: disposeBag)
90111
91- Observable . combineLatest (
92- output. availableInterests,
93- output. userSavedInterests
94- )
95- . subscribe ( onNext: { [ weak self] ( availableInterests, userInterests) in
96- self ? . majorCollectionView. preSelectInterests ( userInterests)
97- } )
98- . disposed ( by: disposeBag)
112+ majorCollectionView. rx. itemSelected
113+ . withUnretained ( self )
114+ . bind { owner, indexPath in
115+ var currentSelected = owner. selectedIndexesRelay. value
116+
117+ if currentSelected. contains ( indexPath) {
118+ currentSelected. remove ( indexPath)
119+ } else {
120+ currentSelected. insert ( indexPath)
121+ }
122+
123+ owner. selectedIndexesRelay. accept ( currentSelected)
124+ owner. updateSelectedInterests ( )
125+
126+ DispatchQueue . main. async {
127+ owner. majorCollectionView. reloadItems ( at: [ indexPath] )
128+ }
129+ }
130+ . disposed ( by: disposeBag)
99131
100132 output. selectedInterests
101133 . map { $0. count }
@@ -110,5 +142,28 @@ public final class InterestFieldViewController: BaseViewController<InterestField
110142 }
111143 } )
112144 . disposed ( by: disposeBag)
145+
146+ output. studentName
147+ . drive ( onNext: { [ weak self] name in
148+ self ? . interestFieldTitleLabel. setJobisText (
149+ " \( name) 님의 \n 관심사를 선택해주세요 " ,
150+ font: . smallBody,
151+ color: . GrayScale. gray90
152+ )
153+ } )
154+ . disposed ( by: disposeBag)
155+ }
156+
157+ public override func configureNavigation( ) {
158+ setSmallTitle ( title: " 관심 분야 선택 " )
159+ navigationItem. largeTitleDisplayMode = . never
160+ hideTabbar ( )
161+ }
162+
163+ private func updateSelectedInterests( ) {
164+ let currentInterests = interestsRelay. value
165+ let currentSelectedIndexes = selectedIndexesRelay. value
166+ let selectedInterests = currentSelectedIndexes. map { currentInterests [ $0. item] }
167+ selectedInterestsRelay. accept ( selectedInterests)
113168 }
114169}
0 commit comments