Skip to content

Commit

Permalink
[Refactor/#122] mypageViewModel 리펙토링
Browse files Browse the repository at this point in the history
  • Loading branch information
HELLOHIDI committed Nov 27, 2024
1 parent 1d704fb commit 035c574
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ class MyPageViewModel: ObservableObject {

@Published private(set) var state = State(
alertType: .logout,
name: "",
point: 0,
user: User(name: "유저정보를 불러오는 중입니다...", point: 0),
showToast: ""
)

Expand All @@ -44,20 +43,18 @@ class MyPageViewModel: ObservableObject {

struct State {
var alertType: CustomAlertType
var name: String
var point: Int
var user: User
var showToast: String
}

func send(action: Action) {
switch action {
case .onAppearEvent:
useCase.getUserData()
.sink { _ in
} receiveValue: { [weak self] data in
self?.state.name = data.name
self?.state.point = data.point
}.store(in: cancelBag)
.catch { _ in Empty() }
.receive(on: RunLoop.main)
.assign(to: \.state.userData, on: self)
.store(in: cancelBag)

case .logoutButtonDidTap:
state.alertType = .logout
Expand All @@ -66,16 +63,36 @@ class MyPageViewModel: ObservableObject {
state.alertType = .withdraw

case .confirmButtonDidTap:
state.alertType == .logout ? useCase.logout() : useCase.revokeUser()
state.alertType == .logout
? handleLogout()
: handleRevokeUser()
}
}

func bindState() {
useCase.loginFailed
private func bindState() {
useCase.logoutFailed
.merge(with: useCase.revokeUserFailed)
.receive(on: RunLoop.main)
.assign(to: \.state.showToast, on: self)
.store(in: cancelBag)
}
}

extension MyPageViewModel {
func handleLogout() {
useCase.logout()
.sink(receiveValue: {
//여기서 화면전환
UserManager.shared.appStateString = "login"
}).store(in: cancelBag)
}

func handleRevokeUser() {
useCase.revokeUser()
.sink(receiveValue: {
//여기서 화면전환
UserManager.shared.appStateString = "login"
}).store(in: cancelBag)
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ public struct MyPageView: View {
buttonType: .Confirm,
alertType: viewModel.state.alertType,
isPresented: $isPresented,
action: {
UserManager.shared.appStateString = "login"
viewModel.send(action: .confirmButtonDidTap)
}
action: { viewModel.send(action: .confirmButtonDidTap) }
),
cancelBtn: CustomAlertButtonView(
buttonType: .Cancel,
Expand All @@ -64,14 +61,14 @@ extension MyPageView {
Image(uiImage: DSKitAsset.profile.image)
.frame(width: 54, height: 54)
.padding(10)
Text(viewModel.state.name)
Text(viewModel.state.user.name)
.font(.title4_semibold_20)
Spacer()
.frame(height: 16)
HStack {
Text(StringLiteral.MyPageAccountControl.point)
.font(.text6_medium_14)
Text("\(viewModel.state.point)")
Text("\(viewModel.state.user.point)")
.font(.text6_medium_14)
}
.frame(maxWidth: .infinity)
Expand Down

0 comments on commit 035c574

Please sign in to comment.