From 035c574ccda2daee773f2a8c83aecffda80f2ab8 Mon Sep 17 00:00:00 2001 From: HELLOHIDI Date: Thu, 28 Nov 2024 08:26:04 +0900 Subject: [PATCH] =?UTF-8?q?[Refactor/#122]=20mypageViewModel=20=EB=A6=AC?= =?UTF-8?q?=ED=8E=99=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Sources/ViewModels/MyPageViewModel.swift | 41 +++++++++++++------ .../Sources/Views/MyPageView.swift | 9 ++-- 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/HMH_Tuist_iOS/Projects/Features/MyPageFeature/Sources/ViewModels/MyPageViewModel.swift b/HMH_Tuist_iOS/Projects/Features/MyPageFeature/Sources/ViewModels/MyPageViewModel.swift index 7453170..9684c03 100644 --- a/HMH_Tuist_iOS/Projects/Features/MyPageFeature/Sources/ViewModels/MyPageViewModel.swift +++ b/HMH_Tuist_iOS/Projects/Features/MyPageFeature/Sources/ViewModels/MyPageViewModel.swift @@ -26,8 +26,7 @@ class MyPageViewModel: ObservableObject { @Published private(set) var state = State( alertType: .logout, - name: "", - point: 0, + user: User(name: "유저정보를 불러오는 중입니다...", point: 0), showToast: "" ) @@ -44,8 +43,7 @@ class MyPageViewModel: ObservableObject { struct State { var alertType: CustomAlertType - var name: String - var point: Int + var user: User var showToast: String } @@ -53,11 +51,10 @@ class MyPageViewModel: ObservableObject { 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 @@ -66,12 +63,14 @@ 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) @@ -79,3 +78,21 @@ class MyPageViewModel: ObservableObject { } } +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) + } +} + diff --git a/HMH_Tuist_iOS/Projects/Features/MyPageFeature/Sources/Views/MyPageView.swift b/HMH_Tuist_iOS/Projects/Features/MyPageFeature/Sources/Views/MyPageView.swift index 4738e58..cd8b619 100644 --- a/HMH_Tuist_iOS/Projects/Features/MyPageFeature/Sources/Views/MyPageView.swift +++ b/HMH_Tuist_iOS/Projects/Features/MyPageFeature/Sources/Views/MyPageView.swift @@ -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, @@ -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)