Skip to content

Commit 66d3d52

Browse files
committed
[Chore/#97] PointViewModel Refactor
1 parent 623df83 commit 66d3d52

2 files changed

Lines changed: 69 additions & 30 deletions

File tree

HMH_Tuist_iOS/Projects/Features/ChallengeFeature/Sources/ViewModels/PointViewModel.swift

Lines changed: 64 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,63 @@ import Core
1111
import Domain
1212

1313
final class PointViewModel: ObservableObject {
14-
@Published var period = 0
15-
@Published var pointStatues: [PointStatuse] = []
16-
@Published var isPresented = false
17-
@Published var earnPoint = 0
18-
@Published var totalPoint = 0
1914

20-
private var cancelBag = CancelBag()
15+
// MARK: - State
16+
public struct State {
17+
var period: Int = 0
18+
var pointStatues: [PointStatuse] = []
19+
var isPresented: Bool = false
20+
var earnPoint: Int = 0
21+
var totalPoint: Int = 0
22+
}
23+
24+
// MARK: - Action
25+
public enum Action {
26+
case setEarnPoint(Int)
27+
case setTotalPoint(Int)
28+
case setPointStatues([PointStatuse])
29+
case setPeriod(Int)
30+
case setToastPresented(Bool)
31+
}
2132

22-
// MARK: Usecase 주입
33+
@Published var state = State()
34+
35+
36+
private var cancelBag = CancelBag()
2337
private let pointUseCase: PointUseCaseType
2438

2539
init(pointUseCase: PointUseCaseType) {
2640
self.pointUseCase = pointUseCase
41+
loadInitialData()
2742
}
2843

29-
func getEarnPoint() {
30-
pointUseCase.getEarnPoint()
31-
.sink { _ in } receiveValue: { [weak self] point in
32-
self?.earnPoint = point
33-
}
34-
.store(in: cancelBag)
44+
private func loadInitialData() {
45+
getEarnPoint()
46+
getPointList()
47+
getCurrentPoint()
48+
}
49+
50+
// MARK: - Actions
51+
func send(_ action: Action) {
52+
switch action {
53+
case .setEarnPoint(let point):
54+
state.earnPoint = point
55+
case .setTotalPoint(let totalPoint):
56+
state.totalPoint = totalPoint
57+
UserDefaults.standard.set(totalPoint, forKey: "totalPoint")
58+
case .setPointStatues(let statues):
59+
state.pointStatues = statues
60+
case .setPeriod(let period):
61+
state.period = period
62+
case .setToastPresented(let isPresented):
63+
state.isPresented = isPresented
64+
}
3565
}
3666

37-
func patchEarnPoint(index: Int) {
38-
let point = pointStatues[index]
67+
// MARK: - UseCase Call
68+
69+
func patchEarnPoint(index: Int) {
70+
let point = state.pointStatues[index]
3971

4072
pointUseCase.earnPoint(point: point)
4173
.sink(receiveCompletion: { _ in }) { point in
@@ -44,26 +76,34 @@ final class PointViewModel: ObservableObject {
4476
.store(in: cancelBag)
4577
}
4678

47-
func getPointList() {
79+
func pointStatus(index: Int) -> PointStatusEnum {
80+
return state.pointStatues[index].getStatus()
81+
}
82+
83+
private func getEarnPoint() {
84+
pointUseCase.getEarnPoint()
85+
.sink { _ in } receiveValue: { [weak self] point in
86+
self?.send(.setEarnPoint(point))
87+
}
88+
.store(in: cancelBag)
89+
}
90+
91+
private func getPointList() {
4892
pointUseCase.getPointStatues()
4993
.receive(on: DispatchQueue.main)
5094
.sink(receiveCompletion: { _ in }) { [weak self] statues in
51-
self?.pointStatues = statues
52-
self?.period = statues.count
95+
self?.send(.setPointStatues(statues))
96+
self?.send(.setPeriod(statues.count))
5397
}
5498
.store(in: cancelBag)
5599
}
56100

57-
func getCurrentPoint() {
101+
private func getCurrentPoint() {
58102
pointUseCase.getUsagePoint()
59103
.sink(receiveCompletion: {_ in }) { [weak self] totalPoint in
60-
self?.totalPoint = totalPoint
61-
UserDefaults.standard.set(totalPoint, forKey: "totalPoint")
104+
self?.send(.setTotalPoint(totalPoint))
62105
}
63106
.store(in: cancelBag)
64107
}
65108

66-
func pointStatus(index: Int) -> PointStatusEnum {
67-
return pointStatues[index].getStatus()
68-
}
69109
}

HMH_Tuist_iOS/Projects/Features/ChallengeFeature/Sources/Views/PointView.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ struct PointView: View {
2121
.padding(.vertical, 16)
2222
.padding(.horizontal, 20)
2323
}
24-
.showToast(toastType: .earnPoint, isPresented: $viewModel.isPresented)
24+
.showToast(toastType: .earnPoint, isPresented: $viewModel.state.isPresented)
2525
.customNavigationBar(
2626
title: StringLiteral.NavigationBar.point,
2727
showBackButton: true,
2828
showPointButton: true,
2929
isPointView: true,
30-
point: viewModel.totalPoint
30+
point: viewModel.state.totalPoint
3131
)
3232
.background(DSKitAsset.blackground.swiftUIColor)
3333
.navigationBarHidden(true)
@@ -36,14 +36,14 @@ struct PointView: View {
3636

3737
extension PointView {
3838
private var listView: some View {
39-
ForEach(viewModel.pointStatues.indices, id: \.self) { index in
39+
ForEach(viewModel.state.pointStatues.indices, id: \.self) { index in
4040
HStack {
4141
VStack(alignment: .leading) {
4242
Text("\(index + 1)" + StringLiteral.Challenge.pointTitle)
4343
.font(.text4_semibold_16)
4444
.foregroundColor(DSKitAsset.whiteText.swiftUIColor)
4545
.padding(.bottom, 2)
46-
Text("\(viewModel.period)" + StringLiteral.Challenge.pointSubTitle)
46+
Text("\(viewModel.state.period)" + StringLiteral.Challenge.pointSubTitle)
4747
.font(.detail4_medium_12)
4848
.foregroundColor(DSKitAsset.gray2.swiftUIColor)
4949
}
@@ -57,7 +57,6 @@ extension PointView {
5757
.frame(height: 80)
5858
}
5959
}
60-
6160
}
6261

6362

@@ -70,7 +69,7 @@ struct EarnPointButton: View {
7069
Button(action: {
7170
viewModel.patchEarnPoint(index: day)
7271
}, label: {
73-
Text(StringLiteral.Challenge.pointButton + " \(viewModel.earnPoint)P")
72+
Text(StringLiteral.Challenge.pointButton + " \(viewModel.state.earnPoint)P")
7473
.font(.text4_semibold_16)
7574
.foregroundColor(status.titleColor) // 컬러값 설정
7675
.frame(width: 73, height: 40)

0 commit comments

Comments
 (0)