Skip to content

Commit

Permalink
[Refactor/#84] 클린 아키텍쳐 방식으로 리펙토링
Browse files Browse the repository at this point in the history
  • Loading branch information
HELLOHIDI committed Sep 3, 2024
1 parent 56048c9 commit b1201db
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class UserManager: ObservableObject {
appState = AppState(rawValue: appStateString) ?? .login
}

func clearLogout() {
public func clearLogout() {
accessToken = ""
refreshToken = ""
socialToken = ""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// MyPageUseCase.swift
// MyPageFeature
//
// Created by 류희재 on 9/3/24.
// Copyright © 2024 HMH-iOS. All rights reserved.
//

import Foundation
import Combine
import Core
import Domain

protocol MyPageUseCaseType {
func getUserDate() -> AnyPublisher<GetUserDataResponseDTO, Error>
func logout()
func revokeUser()
}

final class MyPageUseCase: MyPageUseCaseType {
private var container: DIContainer
private var cancelBag = CancelBag()

init(container: DIContainer) {
self.container = container
}

func getUserDate() -> AnyPublisher<GetUserDataResponseDTO, Error> {
container.services.userService.getUserData()
.map { $0.data! }
.eraseToAnyPublisher()
}

func logout() {
container.services.authService.logoutUser()
.sink { _ in
} receiveValue: { _ in
UserManager.shared.clearLogout()
}.store(in: cancelBag)
}

func revokeUser() {
container.services.authService.revokeUser()
.sink { _ in
} receiveValue: { _ in
UserManager.shared.revokeData()
}.store(in: cancelBag)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import DSKit

class MyPageViewModel_Refactor: ObservableObject {

private var container: DIContainer
private var useCase: MyPageUseCaseType
private var cancelBag = CancelBag()

init(container: DIContainer) {
self.container = container
init(useCase: MyPageUseCaseType) {
self.useCase = useCase
}

@Published private(set) var state = State(
Expand All @@ -29,9 +29,10 @@ class MyPageViewModel_Refactor: ObservableObject {
//MARK: Action

enum Action {
case getUserData
case revokeUser
case logout
case onAppearEvent
case logoutButtonDidTap
case withdrawButtonDidTap
case confirmButtonDidTap
}

struct State {
Expand All @@ -42,27 +43,20 @@ class MyPageViewModel_Refactor: ObservableObject {

func send(action: Action) {
switch action {
case .getUserData:
container.services.userService.getUserData()
case .onAppearEvent:
useCase.getUserDate()
.sink { _ in
} receiveValue: { [weak self] data in
self?.state.name = data.data?.name ?? ""
self?.state.point = data.data?.point ?? 0
self?.state.name = data.name
self?.state.point = data.point
}.store(in: cancelBag)

case .revokeUser:
container.services.authService.revokeUser()
.sink { _ in
} receiveValue: { _ in
UserManager.shared.revokeData()
}.store(in: cancelBag)

case .logout:
container.services.authService.logoutUser()
.sink { _ in
} receiveValue: { _ in
UserManager.shared.revokeData()
}.store(in: cancelBag)
case .logoutButtonDidTap:
state.alertType = .logout
case .withdrawButtonDidTap:
state.alertType = .withdraw
case .confirmButtonDidTap:
state.alertType == .logout ? useCase.logout() : useCase.revokeUser()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,36 +28,29 @@ public struct MyPageView_Refactor: View {
Spacer()
AccountControlView()
}
.onAppear { viewModel.send(action: .onAppearEvent)}
.padding(20)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(DSKitAsset.blackground.swiftUIColor)
.customAlert(
isPresented: $isPresented,
customAlert: {
CustomAlertView(
alertType: viewModel.alertType,
alertType: viewModel.state.alertType,
confirmBtn: CustomAlertButtonView(
buttonType: .Confirm,
alertType: viewModel.alertType,
alertType: viewModel.state.alertType,
isPresented: $isPresented,
action: {
UserManager.shared.appStateString = "login"

if viewModel.alertType == .logout {
viewModel.send(action: .logout)
} else {
viewModel.send(action: .revokeUser)
UserManager.shared.revokeData()
}
viewModel.send(action: .confirmButtonDidTap)
}
),
cancelBtn: CustomAlertButtonView(
buttonType: .Cancel,
alertType: viewModel.alertType,
alertType: viewModel.state.alertType,
isPresented: $isPresented,
action: {
isPresented = false
}
action: { isPresented = false }
), currentPoint: 0, usagePoint: 0
)
}
Expand All @@ -71,14 +64,14 @@ extension MyPageView_Refactor {
Image(uiImage: DSKitAsset.profile.image)
.frame(width: 54, height: 54)
.padding(10)
Text(viewModel.name)
Text(viewModel.state.name)
.font(.title4_semibold_20)
Spacer()
.frame(height: 16)
HStack {
Text(StringLiteral.MyPageAccountControl.point)
.font(.text6_medium_14)
Text("\(viewModel.point)")
Text("\(viewModel.state.point)")
.font(.text6_medium_14)
}
.frame(maxWidth: .infinity)
Expand All @@ -89,13 +82,15 @@ extension MyPageView_Refactor {
.foregroundColor(DSKitAsset.whiteText.swiftUIColor)
.frame(width: 133, height: 150)
}

private func MyInfoView() -> some View {
VStack(spacing: 0) {
MyPageButton_Refactor(buttonType: .travel)
MyPageButton_Refactor(buttonType: .market)
}
.background(DSKitAsset.gray7.swiftUIColor)
}

private func HMHInfoView() -> some View {
VStack(alignment: .leading, spacing: 0) {
Text("정보")
Expand All @@ -106,21 +101,22 @@ extension MyPageView_Refactor {
MyPageButton_Refactor(buttonType: .term)
}
}

private func AccountControlView() -> some View {
HStack {
Text(StringLiteral.MyPageAccountControl.logout)
.font(.text6_medium_14)
.onTapGesture {
isPresented = true
viewModel.alertType = .logout
viewModel.send(action: .logoutButtonDidTap)
}
Rectangle()
.frame(width: 1, height: 16)
Text(StringLiteral.MyPageAccountControl.revoke)
.font(.text6_medium_14)
.onTapGesture {
isPresented = true
viewModel.alertType = .withdraw
viewModel.send(action: .withdrawButtonDidTap)
}
}
.foregroundColor(DSKitAsset.gray3.swiftUIColor)
Expand Down

0 comments on commit b1201db

Please sign in to comment.