Skip to content

Commit f614fc4

Browse files
authored
Merge pull request #176 from DSM-PICK/feature/#122-classroom_move_scroll
feat :: [#122] 교실이동 신청 스크롤 기능 추가
2 parents 3709c05 + 640780d commit f614fc4

File tree

3 files changed

+164
-40
lines changed

3 files changed

+164
-40
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import UIKit
2+
3+
import SnapKit
4+
import Then
5+
6+
import RxSwift
7+
import RxCocoa
8+
9+
import Core
10+
import DesignSystem
11+
12+
class ClassroomBackgroundCell: BaseCollectionViewCell<Any> {
13+
static let identifier = "ClassroomBackgroundCell"
14+
15+
public var didSelectClassroom: ((String) -> Void)?
16+
17+
private let classroomData = ClassroomData.shared
18+
19+
private let classroomMoveApplyRelay = PublishRelay<Void>()
20+
private var classroomText = BehaviorRelay<String>(value: "")
21+
private var startPeriod = BehaviorRelay<Int>(value: 1)
22+
private var endPeriod = BehaviorRelay<Int>(value: 1)
23+
private lazy var selectedSegemetedControlIndex = BehaviorRelay<Int>(value: 1)
24+
private lazy var currentFloorClassroomArray = BehaviorRelay<[String]>(value: classroomData.firstFloor)
25+
26+
private lazy var collectionViewFlowLayout = LeftAlignedCollectionViewFlowLayout().then {
27+
$0.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
28+
$0.minimumLineSpacing = 15
29+
$0.minimumInteritemSpacing = 16
30+
}
31+
private lazy var classroomCollectionView = UICollectionView(
32+
frame: .zero,
33+
collectionViewLayout: collectionViewFlowLayout
34+
).then {
35+
$0.backgroundColor = .background
36+
$0.register(
37+
ClassroomCollectionViewCell.self,
38+
forCellWithReuseIdentifier: ClassroomCollectionViewCell.identifier
39+
)
40+
$0.contentInsetAdjustmentBehavior = .always
41+
$0.bounces = false
42+
}
43+
44+
public override init(frame: CGRect) {
45+
super.init(frame: frame)
46+
}
47+
required init?(coder: NSCoder) {
48+
fatalError("init(coder:) has not been implemented")
49+
}
50+
51+
public func setup(data: [String]) {
52+
self.currentFloorClassroomArray.accept(data)
53+
}
54+
public override func attribute() {
55+
super.attribute()
56+
57+
self.backgroundColor = .background
58+
}
59+
override func bind() {
60+
currentFloorClassroomArray
61+
.bind(to: classroomCollectionView.rx.items(
62+
cellIdentifier: ClassroomCollectionViewCell.identifier,
63+
cellType: ClassroomCollectionViewCell.self
64+
)) { _, item, cell in
65+
cell.setup(classroom: item)
66+
}.disposed(by: disposeBag)
67+
68+
classroomCollectionView.rx.itemSelected
69+
.withUnretained(self)
70+
.bind { owner, index in
71+
owner.didSelectClassroom?(owner.currentFloorClassroomArray.value[index.row])
72+
}.disposed(by: disposeBag)
73+
}
74+
public override func layout() {
75+
self.addSubview(classroomCollectionView)
76+
77+
classroomCollectionView.snp.makeConstraints {
78+
$0.edges.equalToSuperview()
79+
}
80+
}
81+
82+
}

Projects/Presentation/Sources/Scene/Apply/Classroom/ClassroomMoveApplyViewController.swift

Lines changed: 66 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,34 @@ public class ClassroomMoveApplyViewController: BaseViewController<ClassroomMoveA
2929
textColor: .gray600,
3030
font: .pickFont(.body2)
3131
)
32-
private let floorSegmentedControl = ClassroomSegmentedControl(items: [
33-
"1층", "2층", "3층", "4층", "5층"
34-
]).then {
32+
private let floorSegmentedControl = ClassroomSegmentedControl(items:
33+
ClassroomData.shared.allFloors.enumerated().map { index, _ in
34+
"\(index + 1)"
35+
}
36+
).then {
3537
$0.selectedSegmentIndex = 0
3638
}
37-
private lazy var collectionViewFlowLayout = LeftAlignedCollectionViewFlowLayout().then {
38-
$0.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
39-
$0.minimumLineSpacing = 15
40-
$0.minimumInteritemSpacing = 16
39+
private lazy var backgroundcollectionViewFlowLayout = UICollectionViewFlowLayout().then {
40+
$0.scrollDirection = .horizontal
41+
$0.itemSize = .init(
42+
width: self.view.frame.width - 48,
43+
height: self.view.frame.height * 0.7
44+
)
45+
$0.minimumLineSpacing = 0
46+
$0.minimumInteritemSpacing = 0
4147
}
42-
private lazy var classroomCollectionView = UICollectionView(
48+
private lazy var backgroundCollectionView = UICollectionView(
4349
frame: .zero,
44-
collectionViewLayout: collectionViewFlowLayout
50+
collectionViewLayout: backgroundcollectionViewFlowLayout
4551
).then {
4652
$0.backgroundColor = .background
4753
$0.register(
48-
ClassroomCollectionViewCell.self,
49-
forCellWithReuseIdentifier: ClassroomCollectionViewCell.identifier
54+
ClassroomBackgroundCell.self,
55+
forCellWithReuseIdentifier: ClassroomBackgroundCell.identifier
5056
)
51-
$0.contentInsetAdjustmentBehavior = .always
57+
$0.showsHorizontalScrollIndicator = false
58+
$0.showsVerticalScrollIndicator = false
59+
$0.isPagingEnabled = true
5260
$0.bounces = false
5361
}
5462
private let nextButton = PiCKButton(buttonText: "다음")
@@ -77,42 +85,66 @@ public class ClassroomMoveApplyViewController: BaseViewController<ClassroomMoveA
7785
}.disposed(by: disposeBag)
7886

7987
floorSegmentedControl.rx.selectedSegmentIndex
80-
.map { [weak self] index -> [String] in
81-
self?.selectedSegemetedControlIndex.accept(index + 1)
82-
self?.classroomText.accept("")
88+
.withUnretained(self)
89+
.map { owner, index -> [String] in
90+
owner.selectedSegemetedControlIndex.accept(index + 1)
91+
owner.classroomText.accept("")
92+
let itemCount = owner.backgroundCollectionView.numberOfItems(inSection: 0)
93+
94+
guard itemCount > 0, index < itemCount else {
95+
return owner.classroomData.firstFloor
96+
}
97+
98+
owner.backgroundCollectionView.scrollToItem(
99+
at: IndexPath(item: index, section: 0),
100+
at: .centeredHorizontally,
101+
animated: false
102+
)
103+
83104
switch index {
84105
case 0:
85-
return self?.classroomData.firstFloor ?? []
106+
return owner.classroomData.firstFloor
86107
case 1:
87-
return self?.classroomData.secondFloor ?? []
108+
return owner.classroomData.secondFloor
88109
case 2:
89-
return self?.classroomData.thirdFloor ?? []
110+
return owner.classroomData.thirdFloor
90111
case 3:
91-
return self?.classroomData.fourthFloor ?? []
112+
return owner.classroomData.fourthFloor
92113
case 4:
93-
return self?.classroomData.fifthFloor ?? []
114+
return owner.classroomData.fifthFloor
94115
default:
95-
return self?.classroomData.firstFloor ?? []
116+
return owner.classroomData.firstFloor
96117
}
97118
}
98119
.bind(to: currentFloorClassroomArray)
99120
.disposed(by: disposeBag)
100121

101-
currentFloorClassroomArray
102-
.bind(to: classroomCollectionView.rx.items(
103-
cellIdentifier: ClassroomCollectionViewCell.identifier,
104-
cellType: ClassroomCollectionViewCell.self
122+
Observable.just(classroomData.allFloors)
123+
.bind(to: backgroundCollectionView.rx.items(
124+
cellIdentifier: ClassroomBackgroundCell.identifier,
125+
cellType: ClassroomBackgroundCell.self
105126
)) { _, item, cell in
106-
cell.setup(classroom: item)
127+
cell.setup(data: item)
128+
cell.didSelectClassroom = { [weak self] classroom in
129+
self?.classroomText.accept(classroom)
130+
}
107131
}.disposed(by: disposeBag)
108132

109-
classroomCollectionView.rx.itemSelected
133+
backgroundCollectionView.rx.didScroll
110134
.withUnretained(self)
111-
.bind { owner, index in
112-
owner.classroomText.accept(
113-
owner.currentFloorClassroomArray.value[index.row]
114-
)
115-
}.disposed(by: disposeBag)
135+
.bind { owner, _ in
136+
guard let layout = owner.backgroundCollectionView.collectionViewLayout as? UICollectionViewFlowLayout else { return }
137+
138+
let cellWidth = layout.itemSize.width
139+
let spacing = layout.minimumLineSpacing
140+
let totalCellWidth = cellWidth + spacing
141+
142+
let offset = owner.backgroundCollectionView.contentOffset.x + owner.backgroundCollectionView.contentInset.left
143+
let index = Int(round(offset / totalCellWidth))
144+
145+
owner.floorSegmentedControl.selectedSegmentIndex = index
146+
}
147+
.disposed(by: disposeBag)
116148

117149
nextButton.buttonTap
118150
.bind { [weak self] in
@@ -135,7 +167,7 @@ public class ClassroomMoveApplyViewController: BaseViewController<ClassroomMoveA
135167
titleLabel,
136168
explainLabel,
137169
floorSegmentedControl,
138-
classroomCollectionView,
170+
backgroundCollectionView,
139171
nextButton
140172
].forEach { view.addSubview($0) }
141173
}
@@ -153,7 +185,7 @@ public class ClassroomMoveApplyViewController: BaseViewController<ClassroomMoveA
153185
$0.leading.trailing.equalToSuperview()
154186
$0.height.equalTo(25)
155187
}
156-
classroomCollectionView.snp.makeConstraints {
188+
backgroundCollectionView.snp.makeConstraints {
157189
$0.top.equalTo(floorSegmentedControl.snp.bottom).offset(24)
158190
$0.leading.trailing.equalToSuperview().inset(24)
159191
$0.bottom.equalToSuperview()

Projects/Presentation/Sources/Scene/Apply/Classroom/Data/ClassroomData.swift

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,40 @@ public class ClassroomData {
66

77
let firstFloor = [
88
"산학협력부", "새롬홀", "무한 상상실",
9-
"청죽관", "탁구실", "운동장", "밴드부실"
9+
"청죽관", "AI 연구실", "탁구실", "운동장", "밴드부실"
1010
]
1111

1212
let secondFloor = [
1313
"3-1", "3-2", "3-3", "3-4", "세미나실 2-1",
14-
"세미나실 2-2", "세미나실 2-3", "SW 1실", "SW 2실",
15-
"SW 3실", "본부 교무실", "제 3 교무실", "카페테리아",
14+
"세미나실 2-2", "세미나실 2-3", "세미나실 2-4", "SW 1실", "SW 2실",
15+
"SW 3실", "본부 교무실", "제3 교무실", "카페테리아",
1616
"창조실", "방송실", "진로 상담실", "여자 헬스장"
1717
]
1818

1919
let thirdFloor = [
2020
"2-1", "2-2", "2-3", "2-4", "세미나실 3-1",
2121
"세미나실 3-2", "세미나실 3-3", "보안 1실",
22-
"보안 2실", "제 2 교무실", "그린존", "남자 헬스장"
22+
"보안 2실", "제2 교무실", "그린존", "남자 헬스장"
2323
]
2424

2525
let fourthFloor = [
2626
"1-1", "1-2", "1-3", "1-4", "세미나실 4-1",
27-
"세미나실 4-2", "세미나실 4-3",
28-
"임베 1실", "임베 2실", "제 1 교무실"
27+
"세미나실 4-2", "세미나실 4-3", "세미나실 4-4",
28+
"임베 1실", "임베 2실", "제1 교무실"
2929
]
3030

3131
let fifthFloor = [
3232
"음악실", "음악 준비실", "상담실",
3333
"수학실", "과학실"
3434
]
35+
36+
public var allFloors: [[String]] {
37+
return [
38+
firstFloor,
39+
secondFloor,
40+
thirdFloor,
41+
fourthFloor,
42+
fifthFloor
43+
]
44+
}
3545
}

0 commit comments

Comments
 (0)