|
| 1 | +// |
| 2 | +// TraineeMyPageView.swift |
| 3 | +// Presentation |
| 4 | +// |
| 5 | +// Created by 박민서 on 2/3/25. |
| 6 | +// Copyright © 2025 yapp25thTeamTnT. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import SwiftUI |
| 10 | +import ComposableArchitecture |
| 11 | + |
| 12 | +import Domain |
| 13 | +import DesignSystem |
| 14 | + |
| 15 | +@ViewAction(for: TraineeMyPageFeature.self) |
| 16 | +public struct TraineeMyPageView: View { |
| 17 | + |
| 18 | + @Bindable public var store: StoreOf<TraineeMyPageFeature> |
| 19 | + |
| 20 | + public init(store: StoreOf<TraineeMyPageFeature>) { |
| 21 | + self.store = store |
| 22 | + } |
| 23 | + |
| 24 | + public var body: some View { |
| 25 | + ScrollView { |
| 26 | + VStack(spacing: 0) { |
| 27 | + ProfileSection() |
| 28 | + |
| 29 | + VStack(spacing: 12) { |
| 30 | + TopItemSection() |
| 31 | + InfoItemSection() |
| 32 | + BottomItemSection() |
| 33 | + } |
| 34 | + .padding(20) |
| 35 | + } |
| 36 | + } |
| 37 | + .background(Color.neutral50) |
| 38 | + .navigationBarBackButtonHidden() |
| 39 | + } |
| 40 | + |
| 41 | + // MARK: - Sections |
| 42 | + @ViewBuilder |
| 43 | + private func ProfileSection() -> some View { |
| 44 | + VStack(spacing: 0) { |
| 45 | + ProfileImageView(imageURL: store.userImageUrl, name: store.userName) |
| 46 | + .padding(.vertical, 12) |
| 47 | + |
| 48 | + Text(store.userName) |
| 49 | + .typographyStyle(.heading2, with: .neutral950) |
| 50 | + .padding(.bottom, store.isTrainerConnected ? 8 : 16) |
| 51 | + |
| 52 | + if store.isTrainerConnected { |
| 53 | + TButton( |
| 54 | + title: "개인정보 수정", |
| 55 | + config: .small, |
| 56 | + state: .default(.gray(isEnabled: true)), |
| 57 | + action: { send(.tapEditProfileButton) } |
| 58 | + ) |
| 59 | + .frame(width: 90, height: 34) |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @ViewBuilder |
| 65 | + private func TopItemSection() -> some View { |
| 66 | + VStack(spacing: 12) { |
| 67 | + if !store.isTrainerConnected { |
| 68 | + ProfileItemView(title: "트레이너 연결하기", tapAction: { send(.tapConnectTrainerButton) }) |
| 69 | + .padding(.vertical, 4) |
| 70 | + .background(Color.common0) |
| 71 | + .clipShape(.rect(cornerRadius: 12)) |
| 72 | + } |
| 73 | + |
| 74 | + ProfileItemView(title: "앱 푸시 알림", rightView: { |
| 75 | + Toggle("appPushNotification", isOn: $store.appPushNotificationAllowed) |
| 76 | + .applyTToggleStyle() |
| 77 | + }) |
| 78 | + .padding(.vertical, 4) |
| 79 | + .background(Color.common0) |
| 80 | + .clipShape(.rect(cornerRadius: 12)) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + @ViewBuilder |
| 85 | + private func InfoItemSection() -> some View { |
| 86 | + VStack(spacing: 12) { |
| 87 | + ProfileItemView(title: "서비스 이용약관", tapAction: { send(.tapTOSButton) }) |
| 88 | + ProfileItemView(title: "개인정보 처리방침", tapAction: { send(.tapPrivacyPolicyButton) }) |
| 89 | + ProfileItemView(title: "버전 정보", rightView: { |
| 90 | + Text(store.versionInfo) |
| 91 | + .typographyStyle(.body2Medium, with: .neutral400) |
| 92 | + }) |
| 93 | + ProfileItemView(title: "오픈소스 라이선스", tapAction: { send(.tapOpenSourceLicenseButton) }) |
| 94 | + } |
| 95 | + .padding(.vertical, 12) |
| 96 | + .background(Color.common0) |
| 97 | + .clipShape(.rect(cornerRadius: 12)) |
| 98 | + } |
| 99 | + |
| 100 | + @ViewBuilder |
| 101 | + private func BottomItemSection() -> some View { |
| 102 | + VStack(spacing: 12) { |
| 103 | + if store.isTrainerConnected { |
| 104 | + ProfileItemView(title: "트레이너와 연결끊기", tapAction: { send(.tapDisconnectTrainerButton) }) |
| 105 | + .padding(.vertical, 4) |
| 106 | + .background(Color.common0) |
| 107 | + .clipShape(.rect(cornerRadius: 12)) |
| 108 | + } |
| 109 | + |
| 110 | + VStack(spacing: 12) { |
| 111 | + ProfileItemView(title: "로그아웃", tapAction: { send(.tapLogoutButton) }) |
| 112 | + ProfileItemView(title: "계정 탈퇴", tapAction: { send(.tapWithdrawButton) }) |
| 113 | + } |
| 114 | + .padding(.vertical, 12) |
| 115 | + .background(Color.common0) |
| 116 | + .clipShape(.rect(cornerRadius: 12)) |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +private extension TraineeMyPageView { |
| 122 | + struct ProfileImageView: View { |
| 123 | + let imageURL: String? |
| 124 | + let name: String |
| 125 | + |
| 126 | + var body: some View { |
| 127 | + if let urlString = imageURL, let url = URL(string: urlString) { |
| 128 | + AsyncImage(url: url) { phase in |
| 129 | + switch phase { |
| 130 | + case .empty: |
| 131 | + ProgressView() |
| 132 | + .tint(.red500) |
| 133 | + .frame(width: 132, height: 132) |
| 134 | + |
| 135 | + case .success(let image): |
| 136 | + image |
| 137 | + .resizable() |
| 138 | + .aspectRatio(contentMode: .fill) |
| 139 | + .frame(width: 132, height: 132) |
| 140 | + .clipShape(Circle()) |
| 141 | + |
| 142 | + case .failure: |
| 143 | + Image(.imgDefaultTraineeImage) |
| 144 | + .resizable() |
| 145 | + .aspectRatio(contentMode: .fill) |
| 146 | + .frame(width: 132, height: 132) |
| 147 | + .clipShape(Circle()) |
| 148 | + |
| 149 | + @unknown default: |
| 150 | + EmptyView() |
| 151 | + } |
| 152 | + } |
| 153 | + } else { |
| 154 | + Image(.imgDefaultTraineeImage) |
| 155 | + .resizable() |
| 156 | + .scaledToFill() |
| 157 | + .frame(width: 132, height: 132) |
| 158 | + .clipShape(Circle()) |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + struct ProfileItemView<RightView: View>: View { |
| 164 | + let title: String |
| 165 | + let rightView: () -> RightView |
| 166 | + let tapAction: (() -> Void)? |
| 167 | + |
| 168 | + init( |
| 169 | + title: String, |
| 170 | + rightView: @escaping () -> RightView = { EmptyView() }, |
| 171 | + tapAction: (() -> Void)? = nil |
| 172 | + ) { |
| 173 | + self.title = title |
| 174 | + self.rightView = rightView |
| 175 | + self.tapAction = tapAction |
| 176 | + } |
| 177 | + |
| 178 | + var body: some View { |
| 179 | + HStack { |
| 180 | + Text(title) |
| 181 | + .typographyStyle(.body2Medium, with: .neutral700) |
| 182 | + Spacer() |
| 183 | + rightView() |
| 184 | + } |
| 185 | + .onTapGesture { |
| 186 | + tapAction?() |
| 187 | + } |
| 188 | + .padding(.horizontal, 20) |
| 189 | + .padding(.vertical, 8) |
| 190 | + } |
| 191 | + } |
| 192 | +} |
0 commit comments