|
| 1 | +// |
| 2 | +// MypageView.swift |
| 3 | +// Presentation |
| 4 | +// |
| 5 | +// Created by 이동현 on 7/17/25. |
| 6 | +// |
| 7 | +import Combine |
| 8 | +import Shared |
| 9 | +import SnapKit |
| 10 | +import UIKit |
| 11 | + |
| 12 | +final class MypageView: BaseViewController<MypageViewModel> { |
| 13 | + private enum Layout { |
| 14 | + static let titleLabelHeight: CGFloat = 54 |
| 15 | + static let settingButtonSize: CGFloat = 48 |
| 16 | + static let settingButtonTrailingSpacing: CGFloat = 8 |
| 17 | + static let profileImageViewSize: CGFloat = 80 |
| 18 | + static let profileImageViewCornerRadius: CGFloat = profileImageViewSize / 2 |
| 19 | + static let profileImageViewTopSpacing: CGFloat = 32 |
| 20 | + static let nicknameLabelHeight: CGFloat = 24 |
| 21 | + static let nicknameLabelTopSpacing: CGFloat = 12 |
| 22 | + static let divideLineHeight: CGFloat = 6 |
| 23 | + static let divideLineTopSpacing: CGFloat = 28 |
| 24 | + static let tableViewCellHeight: CGFloat = 48 |
| 25 | + } |
| 26 | + |
| 27 | + private let titleLabel = UILabel() |
| 28 | + private let settingButton = UIButton() |
| 29 | + private let profileImageView = UIImageView() |
| 30 | + private let nicknameLabel = UILabel() |
| 31 | + private let dividerView = UIView() |
| 32 | + private let tableView = UITableView() |
| 33 | + private var cancellables: Set<AnyCancellable> |
| 34 | + |
| 35 | + override init(viewModel: MypageViewModel) { |
| 36 | + cancellables = [] |
| 37 | + super.init(viewModel: viewModel) |
| 38 | + } |
| 39 | + |
| 40 | + required init?(coder: NSCoder) { |
| 41 | + fatalError("init(coder:) has not been implemented") |
| 42 | + } |
| 43 | + |
| 44 | + override func viewDidLoad() { |
| 45 | + super.viewDidLoad() |
| 46 | + } |
| 47 | + |
| 48 | + override func configureAttribute() { |
| 49 | + view.backgroundColor = .white |
| 50 | + |
| 51 | + titleLabel.text = "마이페이지" |
| 52 | + titleLabel.font = BitnagilFont(style: .title3, weight: .semiBold).font |
| 53 | + titleLabel.textAlignment = .center |
| 54 | + titleLabel.textColor = .black |
| 55 | + |
| 56 | + settingButton.setImage(BitnagilIcon.settingIcon, for: .normal) |
| 57 | + |
| 58 | + profileImageView.layer.cornerRadius = Layout.profileImageViewCornerRadius |
| 59 | + profileImageView.layer.masksToBounds = true |
| 60 | + profileImageView.backgroundColor = BitnagilColor.gray40 // 임시 |
| 61 | + |
| 62 | + nicknameLabel.font = BitnagilFont(style: .title3, weight: .semiBold).font |
| 63 | + nicknameLabel.textColor = .black |
| 64 | + nicknameLabel.textAlignment = .center |
| 65 | + |
| 66 | + dividerView.backgroundColor = BitnagilColor.gray99 |
| 67 | + |
| 68 | + tableView.register(MypageTableViewCell.self, forCellReuseIdentifier: MypageTableViewCell.className) |
| 69 | + tableView.dataSource = self |
| 70 | + tableView.delegate = self |
| 71 | + tableView.separatorStyle = .none |
| 72 | + } |
| 73 | + |
| 74 | + override func configureLayout() { |
| 75 | + let safeArea = view.safeAreaLayoutGuide |
| 76 | + view.addSubview(titleLabel) |
| 77 | + view.addSubview(settingButton) |
| 78 | + view.addSubview(profileImageView) |
| 79 | + view.addSubview(nicknameLabel) |
| 80 | + view.addSubview(dividerView) |
| 81 | + view.addSubview(tableView) |
| 82 | + |
| 83 | + titleLabel.snp.makeConstraints { make in |
| 84 | + make.horizontalEdges.top.equalTo(safeArea) |
| 85 | + make.height.equalTo(Layout.titleLabelHeight) |
| 86 | + } |
| 87 | + |
| 88 | + settingButton.snp.makeConstraints { make in |
| 89 | + make.centerY.equalTo(titleLabel) |
| 90 | + make.trailing.equalToSuperview().inset(Layout.settingButtonTrailingSpacing) |
| 91 | + make.size.equalTo(Layout.settingButtonSize) |
| 92 | + } |
| 93 | + |
| 94 | + profileImageView.snp.makeConstraints { make in |
| 95 | + make.top.equalTo(titleLabel.snp.bottom).offset(Layout.profileImageViewTopSpacing) |
| 96 | + make.centerX.equalToSuperview() |
| 97 | + make.size.equalTo(Layout.profileImageViewSize) |
| 98 | + } |
| 99 | + |
| 100 | + nicknameLabel.snp.makeConstraints { make in |
| 101 | + make.top.equalTo(profileImageView.snp.bottom).offset(Layout.nicknameLabelTopSpacing) |
| 102 | + make.centerX.equalToSuperview() |
| 103 | + make.height.equalTo(Layout.nicknameLabelHeight) |
| 104 | + } |
| 105 | + |
| 106 | + dividerView.snp.makeConstraints { make in |
| 107 | + make.top.equalTo(nicknameLabel.snp.bottom).offset(Layout.divideLineTopSpacing) |
| 108 | + make.horizontalEdges.equalToSuperview() |
| 109 | + make.height.equalTo(Layout.divideLineHeight) |
| 110 | + } |
| 111 | + |
| 112 | + tableView.snp.makeConstraints { make in |
| 113 | + make.top.equalTo(dividerView.snp.bottom) |
| 114 | + make.horizontalEdges.bottom.equalTo(safeArea) |
| 115 | + } |
| 116 | + |
| 117 | + } |
| 118 | + |
| 119 | + override func bind() { |
| 120 | + viewModel.output.nickNamePublisher |
| 121 | + .sink { [weak self] nickname in |
| 122 | + self?.nicknameLabel.text = nickname |
| 123 | + } |
| 124 | + .store(in: &cancellables) |
| 125 | + |
| 126 | + viewModel.output.externalURLPublisher |
| 127 | + .sink { url in |
| 128 | + UIApplication.shared.open(url) |
| 129 | + } |
| 130 | + .store(in: &cancellables) |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +extension MypageView: UITableViewDelegate { |
| 135 | + |
| 136 | +} |
| 137 | + |
| 138 | +extension MypageView: UITableViewDataSource { |
| 139 | + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { |
| 140 | + return MypageViewModel.MypageMenu.allCases.count |
| 141 | + } |
| 142 | + |
| 143 | + func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { |
| 144 | + return Layout.tableViewCellHeight |
| 145 | + } |
| 146 | + |
| 147 | + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { |
| 148 | + guard |
| 149 | + let cell = tableView.dequeueReusableCell(withIdentifier: MypageTableViewCell.className) as? MypageTableViewCell |
| 150 | + else { return .init() } |
| 151 | + |
| 152 | + let title = MypageViewModel.MypageMenu |
| 153 | + .allCases[indexPath.row] |
| 154 | + .rawValue |
| 155 | + cell.configure(title: title) |
| 156 | + |
| 157 | + return cell |
| 158 | + } |
| 159 | + |
| 160 | + func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { |
| 161 | + defer { tableView.deselectRow(at: indexPath, animated: true) } |
| 162 | + |
| 163 | + let selectedMenu = MypageViewModel.MypageMenu.allCases[indexPath.row] |
| 164 | + |
| 165 | + guard selectedMenu == .resetGoal else { |
| 166 | + viewModel.action(input: .didSelectMenu(menu: selectedMenu)) |
| 167 | + return |
| 168 | + } |
| 169 | + |
| 170 | + guard let onboardingViewModel = DIContainer.shared.resolve(type: OnboardingViewModel.self) else { |
| 171 | + fatalError("onboardingViewModel 의존성이 등록되지 않았습니다.") |
| 172 | + } |
| 173 | + |
| 174 | + let onboardingView = OnboardingView(viewModel: onboardingViewModel, onboarding: .time) |
| 175 | + navigationController?.pushViewController(onboardingView, animated: true) |
| 176 | + } |
| 177 | +} |
0 commit comments