Skip to content

Commit 1d07f1d

Browse files
committed
[#58]Refactor: userCardDimView 커스텀
- TFBaseCollectionViewCell를 상속받은UserCardViewCollectionViewCell 구현 - DimView Manager 클래스를 따로 빼려고 했지만, 셀이 생성됐을 때의 x, y좌표를 알아야 해서 cell을 커스텀하는 방식으로 구현함. - 또한, 셀 각각의 view model의 output을 구독하다보니 다른 셀에 대한 트리거를 처리하기 애매해서 미리 셀에 dimView를 추가해놓고 - 셀에서 시작 시간을 output으로 구독해서 타이머가 시작됐을 때 dim view를 hidden하는 방식으로 구현. - 특정 셀이 다른 모든 셀의 트리거를 알게 할 수는 있지만, 사이드 이펙트 및 비효율적이어서 이렇게 하지 않음.
1 parent 142c69a commit 1d07f1d

File tree

3 files changed

+66
-25
lines changed

3 files changed

+66
-25
lines changed

Projects/Features/Falling/Src/Subviews/Cell/FallingUserCollectionViewCell.swift

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ struct FallingUserCollectionViewCellObserver {
1616
var timerActiveTrigger: Observable<Bool>
1717
}
1818

19-
final class FallingUserCollectionViewCell: TFBaseCollectionViewCell {
20-
19+
final class FallingUserCollectionViewCell: UserCardViewCollectionViewCell {
2120
lazy var profileCarouselView = ProfileCarouselView()
2221

2322
lazy var cardTimeView = CardTimeView()
@@ -36,6 +35,8 @@ final class FallingUserCollectionViewCell: TFBaseCollectionViewCell {
3635
$0.top.leading.trailing.equalToSuperview().inset(12)
3736
$0.height.equalTo(32)
3837
}
38+
39+
self.showUserCardDimView()
3940
}
4041

4142
override func prepareForReuse() {
@@ -52,31 +53,27 @@ final class FallingUserCollectionViewCell: TFBaseCollectionViewCell {
5253

5354
let output = viewModel
5455
.transform(input: input)
56+
57+
output.user
58+
.drive(with: self, onNext: { owner, user in
59+
owner.profileCarouselView.bind(user)
60+
})
61+
.disposed(by: disposeBag)
5562

5663
output.timeState
5764
.drive(self.rx.timeState)
5865
.disposed(by: self.disposeBag)
5966

60-
output.isDimViewHidden
61-
.drive(with: self, onNext: { owner, isHidden in
62-
if isHidden {
63-
owner.profileCarouselView.hiddenDimView()
64-
} else {
65-
owner.profileCarouselView.showDimView()
66-
}
67+
output.timeStart
68+
.drive(with: self, onNext: { owner, _ in
69+
owner.hiddenUserCardDimView()
6770
})
6871
.disposed(by: disposeBag)
6972

7073
output.timeZero
7174
.drive(scrollToNextObserver)
7275
.disposed(by: disposeBag)
7376

74-
output.user
75-
.drive(with: self, onNext: { owner, user in
76-
owner.profileCarouselView.bind(user)
77-
})
78-
.disposed(by: disposeBag)
79-
8077
profileCarouselView.infoButton.rx.tap.asDriver()
8178
.scan(true) { lastValue, _ in
8279
return !lastValue

Projects/Features/Falling/Src/Subviews/Cell/FallinguserCollectionViewCellModel.swift

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,10 @@ final class FallinguserCollectionViewCellModel: ViewModelType {
128128
}
129129

130130
struct Output {
131+
let user: Driver<FallingUser>
131132
let timeState: Driver<TimeState>
133+
let timeStart: Driver<Void>
132134
let timeZero: Driver<Void>
133-
let user: Driver<FallingUser>
134-
let isDimViewHidden: Driver<Bool>
135135
}
136136

137137
func transform(input: Input) -> Output {
@@ -162,18 +162,14 @@ final class FallinguserCollectionViewCellModel: ViewModelType {
162162
}.asDriver(onErrorJustReturn: 8.0)
163163

164164
let timeState = timer.map { TimeState(rawValue: $0) }
165+
let timeStart = timer.filter { $0 == 8.0 }.map { _ in }
165166
let timeZero = timer.filter { $0 == 0 }.map { _ in }
166167

167-
let isDimViewHidden = Driver.merge(
168-
timerActiveTrigger.take(1).asDriverOnErrorJustEmpty(), // take(1)을 해주지 않으면 일시정지 때마다 dimmed 됨
169-
timeZero.map { _ in false }
170-
)
171-
172168
return Output(
173-
timeState: timeState,
174-
timeZero: timeZero,
175169
user: user,
176-
isDimViewHidden: isDimViewHidden
170+
timeState: timeState,
171+
timeStart: timeStart,
172+
timeZero: timeZero
177173
)
178174
}
179175
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//
2+
// UserCardViewCollectionViewCell.swift
3+
// DSKit
4+
//
5+
// Created by SeungMin on 1/31/24.
6+
//
7+
8+
import UIKit
9+
10+
open class UserCardViewCollectionViewCell: TFBaseCollectionViewCell {
11+
private let userCardDimView: UIView = {
12+
let userCardWidth = (UIWindow.keyWindow?.bounds.width ?? .zero) - 32
13+
let view = UIView(
14+
frame: CGRect(
15+
x: 0,
16+
y: 0,
17+
width: userCardWidth,
18+
height: userCardWidth * 1.64
19+
)
20+
)
21+
view.layer.cornerRadius = 20
22+
return view
23+
}()
24+
25+
public func showUserCardDimView() {
26+
DispatchQueue.main.async { [weak self] in
27+
guard let self = self else { return }
28+
self.addSubviews(userCardDimView)
29+
print("DimView added!")
30+
UIView.animate(withDuration: 0.0) {
31+
self.userCardDimView.backgroundColor = DSKitAsset.Color.DimColor.default.color
32+
}
33+
}
34+
}
35+
36+
public func hiddenUserCardDimView() {
37+
DispatchQueue.main.async { [weak self] in
38+
guard let self = self else { return }
39+
UIView.animate(withDuration: 0.0) {
40+
self.userCardDimView.backgroundColor = DSKitAsset.Color.clear.color
41+
} completion: { [weak self] _ in
42+
guard let self = self else { return }
43+
print("Removed DimView!")
44+
self.userCardDimView.removeFromSuperview()
45+
}
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)