@@ -11,31 +11,63 @@ import Core
1111import Domain
1212
1313final 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}
0 commit comments