From 6ef8eec966deea903a6268530448d06166b4ef14 Mon Sep 17 00:00:00 2001 From: noeyiz Date: Thu, 29 Feb 2024 21:31:06 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=ED=94=84=EB=A6=AC=EB=A1=9C?= =?UTF-8?q?=EB=93=9C=20on/off=20=EC=85=80=20=EC=B6=94=EA=B0=80=20(UserDefa?= =?UTF-8?q?ults=20=EC=A0=80=EC=9E=A5=EC=9D=80=20=EC=95=84=EC=A7=81)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- iBox/Sources/Model/MyPageItem.swift | 25 +++++- .../Presenter/MyPage/MyPageItemCell.swift | 46 ++++++++--- .../Sources/Presenter/MyPage/MyPageView.swift | 77 +++++++++++-------- .../MyPage/MyPageViewController.swift | 32 ++++---- iBox/Sources/Utils/UserDefaultsManager.swift | 3 + .../ViewModel/MyPageCellViewModel.swift | 14 ++-- .../ViewModel/MyPageSectionViewModel.swift | 7 +- iBox/Sources/ViewModel/MyPageViewModel.swift | 32 ++++---- 8 files changed, 146 insertions(+), 90 deletions(-) diff --git a/iBox/Sources/Model/MyPageItem.swift b/iBox/Sources/Model/MyPageItem.swift index 9c695c6..790c1c1 100644 --- a/iBox/Sources/Model/MyPageItem.swift +++ b/iBox/Sources/Model/MyPageItem.swift @@ -7,12 +7,29 @@ import Foundation -struct MyPageSection { - var title: String - var items: [MyPageItem] +enum MyPageType { + case theme + case homeTab + case preload + case guide + case feedback + case developer + + func toString() -> String { + switch self { + case .theme: "테마" + case .homeTab: "홈화면" + case .preload: "페이지 미리 로드" + case .guide: "이용 가이드" + case .feedback: "앱 피드백" + case .developer: "개발자 정보" + } + } + } struct MyPageItem { - var title: String + var type: MyPageType var description: String? + var flag: Bool? } diff --git a/iBox/Sources/Presenter/MyPage/MyPageItemCell.swift b/iBox/Sources/Presenter/MyPage/MyPageItemCell.swift index c3a3c72..9b71644 100644 --- a/iBox/Sources/Presenter/MyPage/MyPageItemCell.swift +++ b/iBox/Sources/Presenter/MyPage/MyPageItemCell.swift @@ -11,17 +11,28 @@ import SnapKit class MyPageItemCell: UITableViewCell, BaseViewProtocol { + static let reuseIdentifier = "MyPageItemCell" + private var viewModel: MyPageCellViewModel? + // MARK: - UI let titleLabel = UILabel().then { $0.font = .systemFont(ofSize: 16) } + let stackView = UIStackView().then { + $0.axis = .horizontal + } + let descriptionLabel = UILabel().then { $0.font = .systemFont(ofSize: 13, weight: .regular) $0.textColor = .gray } + let controlSwitch = UISwitch().then { + $0.tintColor = .box3 + } + let chevronButton = UIButton().then { $0.configuration = .plain() $0.configuration?.image = UIImage(systemName: "chevron.right") @@ -34,7 +45,7 @@ class MyPageItemCell: UITableViewCell, BaseViewProtocol { override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) configureUI() - selectionStyle = .none // 셀 선택했을 때 회색으로 변하는 것 비활성화 + selectionStyle = .none } required init?(coder: NSCoder) { @@ -46,23 +57,40 @@ class MyPageItemCell: UITableViewCell, BaseViewProtocol { func configureUI() { backgroundColor = .clear addSubview(titleLabel) - addSubview(descriptionLabel) - addSubview(chevronButton) + addSubview(stackView) + stackView.addArrangedSubview(controlSwitch) + stackView.addArrangedSubview(descriptionLabel) + stackView.addArrangedSubview(chevronButton) titleLabel.snp.makeConstraints { $0.left.equalToSuperview().inset(20) $0.centerY.equalToSuperview() } - chevronButton.snp.makeConstraints { - $0.right.equalToSuperview().inset(20) + stackView.snp.makeConstraints { + $0.right.equalToSuperview().inset(30) $0.centerY.equalToSuperview() - $0.width.height.equalTo(20) } + } + + // MARK: - Bind ViewModel + + func bindViewModel(_ viewModel: MyPageCellViewModel) { + self.viewModel = viewModel + titleLabel.text = viewModel.title - descriptionLabel.snp.makeConstraints { - $0.right.equalTo(chevronButton.snp.left).offset(-10) - $0.centerY.equalToSuperview() + descriptionLabel.isHidden = true + controlSwitch.isHidden = true + chevronButton.isHidden = true + + if let description = viewModel.description { + descriptionLabel.text = viewModel.description + descriptionLabel.isHidden = false + } else if let flag = viewModel.flag { + controlSwitch.isOn = flag + controlSwitch.isHidden = false + } else { + chevronButton.isHidden = false } } diff --git a/iBox/Sources/Presenter/MyPage/MyPageView.swift b/iBox/Sources/Presenter/MyPage/MyPageView.swift index c0f57a7..7803d97 100644 --- a/iBox/Sources/Presenter/MyPage/MyPageView.swift +++ b/iBox/Sources/Presenter/MyPage/MyPageView.swift @@ -40,7 +40,7 @@ class MyPageView: BaseView { } let tableView = UITableView().then { - $0.register(MyPageItemCell.self, forCellReuseIdentifier: "MyPageItemCell") + $0.register(MyPageItemCell.self, forCellReuseIdentifier: MyPageItemCell.reuseIdentifier) $0.separatorStyle = .none $0.sectionHeaderTopPadding = 0 $0.backgroundColor = .clear @@ -50,7 +50,7 @@ class MyPageView: BaseView { override init(frame: CGRect) { super.init(frame: frame) - setupProperties() + setupProperty() } required init?(coder: NSCoder) { @@ -59,7 +59,7 @@ class MyPageView: BaseView { // MARK: - BaseViewProtocol - private func setupProperties() { + private func setupProperty() { tableView.delegate = self tableView.dataSource = self @@ -91,7 +91,7 @@ class MyPageView: BaseView { } chevronButton.snp.makeConstraints { - $0.right.equalToSuperview().inset(20) + $0.right.equalToSuperview().inset(30) $0.centerY.equalToSuperview() $0.width.height.equalTo(20) } @@ -124,53 +124,64 @@ class MyPageView: BaseView { } -extension MyPageView: UITableViewDelegate, UITableViewDataSource { +extension MyPageView: UITableViewDelegate { + + // MARK: - section - // 테이블 뷰의 섹션 개수 설정 func numberOfSections(in tableView: UITableView) -> Int { guard let viewModel = viewModel else { return 0 } return viewModel.myPageSectionViewModels.count } - // 테이블 뷰의 행 개수 설정 - func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { - guard let viewModel = viewModel else { return 0 } - return viewModel.myPageSectionViewModels[section].model.items.count - } - - // 테이블 뷰 셀 구성 - func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { - guard let viewModel = viewModel, - let cell = tableView.dequeueReusableCell(withIdentifier: "MyPageItemCell") - as? MyPageItemCell else { return UITableViewCell() } - let item = viewModel.myPageSectionViewModels[indexPath.section].model.items[indexPath.row] - cell.titleLabel.text = item.title - cell.descriptionLabel.text = item.description - return cell - } - - // 셀의 높이 설정 - func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { - return 65 - } - - // 섹션 헤더의 View 설정 func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { let headerView = UIView() headerView.backgroundColor = .backgroundColor return headerView } - // 섹션 헤더의 높이 설정 func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return 10 } - // 테이블 뷰 셀이 선택되었을 때 실행되는 메서드 + // MARK: - cell + + func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { + return 55 + } + func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { guard let viewModel = viewModel else { return } - let item = viewModel.myPageSectionViewModels[indexPath.section].model.items[indexPath.row] - delegate?.pushViewController(indexPath) + let myPageItem = viewModel.myPageSectionViewModels[indexPath.section].cellViewModels[indexPath.row].myPageItem + if (myPageItem.type != MyPageType.preload) { + delegate?.pushViewController(myPageItem.type) + } + } + +} + +extension MyPageView: UITableViewDataSource { + + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + guard let viewModel = viewModel else { return 0 } + return viewModel.myPageSectionViewModels[section].cellViewModels.count + } + + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + guard let viewModel = viewModel, + let cell = tableView.dequeueReusableCell(withIdentifier: MyPageItemCell.reuseIdentifier) + as? MyPageItemCell else { return UITableViewCell() } + let cellViewModel = viewModel.myPageSectionViewModels[indexPath.section].cellViewModels[indexPath.row] + cell.bindViewModel(cellViewModel) + if cellViewModel.flag != nil { + cell.controlSwitch.addTarget(self, action: #selector(handleControlSwitchTap), for: .touchUpInside) + } + return cell + } + + @objc private func handleControlSwitchTap(_ controlSwitch: UISwitch) { + guard let viewModel = viewModel else { return } + viewModel.input.send(.setPreload(controlSwitch.isOn)) + print(controlSwitch.isOn) } } diff --git a/iBox/Sources/Presenter/MyPage/MyPageViewController.swift b/iBox/Sources/Presenter/MyPage/MyPageViewController.swift index 69349b1..388a2a1 100644 --- a/iBox/Sources/Presenter/MyPage/MyPageViewController.swift +++ b/iBox/Sources/Presenter/MyPage/MyPageViewController.swift @@ -8,7 +8,7 @@ import UIKit protocol MyPageViewDelegate { - func pushViewController(_ indexPath: IndexPath) + func pushViewController(_ type: MyPageType) func pushViewController(_ viewController: UIViewController) } @@ -36,27 +36,27 @@ class MyPageViewController: BaseNavigationBarViewController { // MARK: - BaseNavigationBarViewControllerProtocol override func setupNavigationBar() { - setNavigationBarTitleLabelText("My Page") + setNavigationBarTitleLabelText("마이 페이지") } } extension MyPageViewController: MyPageViewDelegate { - func pushViewController(_ indexPath: IndexPath) { - if indexPath.section == 0 { - switch indexPath.row { - case 0: navigationController?.pushViewController(ThemeViewController(), animated: true) - case 1: navigationController?.pushViewController(HomeTabSelectorViewController(), animated: true) - default: break - } - } else { - switch indexPath.row { - case 0: print("이용 가이드 탭 !") - case 1: print("앱 피드백 탭 !") - case 2: print("개발자 정보 탭 !") - default: break - } + func pushViewController(_ type: MyPageType) { + switch type { + case .theme: + navigationController?.pushViewController(ThemeViewController(), animated: true) + case .homeTab: + navigationController?.pushViewController(HomeTabSelectorViewController(), animated: true) + case .preload: + print("미리 로드 탭 !") + case .guide: + print("이용 가이드 탭 !") + case .feedback: + print("앱 피드백 탭 !") + case .developer: + print("개발자 정보 탭 !") } } diff --git a/iBox/Sources/Utils/UserDefaultsManager.swift b/iBox/Sources/Utils/UserDefaultsManager.swift index 5d3e226..48e785d 100644 --- a/iBox/Sources/Utils/UserDefaultsManager.swift +++ b/iBox/Sources/Utils/UserDefaultsManager.swift @@ -20,6 +20,9 @@ final class UserDefaultsManager { @UserDefaultsData(key: "isDefaultDataInserted", defaultValue: false) static var isDefaultDataInserted: Bool + + @UserDefaultsData(key: "isPreload", defaultValue: false) + static var isPreload: Bool } diff --git a/iBox/Sources/ViewModel/MyPageCellViewModel.swift b/iBox/Sources/ViewModel/MyPageCellViewModel.swift index f5c4c81..77a77c4 100644 --- a/iBox/Sources/ViewModel/MyPageCellViewModel.swift +++ b/iBox/Sources/ViewModel/MyPageCellViewModel.swift @@ -9,18 +9,22 @@ import Foundation class MyPageCellViewModel { - let model: MyPageItem + let myPageItem: MyPageItem - init(_ model: MyPageItem) { - self.model = model + init(_ myPageItem: MyPageItem) { + self.myPageItem = myPageItem } var title: String { - model.title + myPageItem.type.toString() + } + + var flag: Bool? { + myPageItem.flag } var description: String? { - model.description + myPageItem.description } } diff --git a/iBox/Sources/ViewModel/MyPageSectionViewModel.swift b/iBox/Sources/ViewModel/MyPageSectionViewModel.swift index 550d976..53b2a81 100644 --- a/iBox/Sources/ViewModel/MyPageSectionViewModel.swift +++ b/iBox/Sources/ViewModel/MyPageSectionViewModel.swift @@ -9,11 +9,10 @@ import Foundation class MyPageSectionViewModel { + let cellViewModels: [MyPageCellViewModel] - let model: MyPageSection - - init(_ model: MyPageSection) { - self.model = model + init(cellViewModels: [MyPageCellViewModel]) { + self.cellViewModels = cellViewModels } } diff --git a/iBox/Sources/ViewModel/MyPageViewModel.swift b/iBox/Sources/ViewModel/MyPageViewModel.swift index 6a414a0..cbaa62f 100644 --- a/iBox/Sources/ViewModel/MyPageViewModel.swift +++ b/iBox/Sources/ViewModel/MyPageViewModel.swift @@ -12,6 +12,7 @@ class MyPageViewModel { enum Input { case viewWillAppear + case setPreload(_ isOn: Bool) } enum Output { @@ -32,31 +33,24 @@ class MyPageViewModel { self?.myPageSectionViewModels.removeAll() self?.updateMyPageSectionViewModels() self?.output.send(.updateMyPageSectionViewModels) + case let .setPreload(isOn): + UserDefaultsManager.isPreload = isOn } }.store(in: &cancellables) return output.eraseToAnyPublisher() } private func updateMyPageSectionViewModels() { - myPageSectionViewModels.append(MyPageSectionViewModel( - MyPageSection( - title: "settings", - items: [ - MyPageItem(title: "테마", description: UserDefaultsManager.theme.toString()), - MyPageItem(title: "홈화면", description: HomeTabType.allCases[UserDefaultsManager.homeTabIndex].toString()) - ] - ) - )) - myPageSectionViewModels.append(MyPageSectionViewModel( - MyPageSection( - title: "help", - items: [ - MyPageItem(title: "이용 가이드"), - MyPageItem(title: "앱 피드백"), - MyPageItem(title: "개발자 정보") - ] - )) - ) + myPageSectionViewModels.append(MyPageSectionViewModel(cellViewModels: [ + MyPageCellViewModel(MyPageItem(type: .theme, description: UserDefaultsManager.theme.toString())), + MyPageCellViewModel(MyPageItem(type: .homeTab, description: HomeTabType.allCases[UserDefaultsManager.homeTabIndex].toString())), + MyPageCellViewModel(MyPageItem(type: .preload, flag: UserDefaultsManager.isPreload)) + ])) + myPageSectionViewModels.append(MyPageSectionViewModel(cellViewModels: [ + MyPageCellViewModel(MyPageItem(type: .guide)), + MyPageCellViewModel(MyPageItem(type: .feedback)), + MyPageCellViewModel(MyPageItem(type: .developer)) + ])) } } From d9f761d4dafec28ddffbd7fe032364097cf98029 Mon Sep 17 00:00:00 2001 From: noeyiz Date: Sat, 2 Mar 2024 16:19:24 +0900 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20=ED=94=84=EB=A6=AC=EB=A1=9C?= =?UTF-8?q?=EB=93=9C=20UserDefaults=20=EC=A0=80=EC=9E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Presenter/MyPage/MyPageItemCell.swift | 60 +++++++++++-------- .../Sources/Presenter/MyPage/MyPageView.swift | 58 +++++++++--------- .../MyPage/MyPageViewController.swift | 10 +--- iBox/Sources/ViewModel/MyPageViewModel.swift | 16 ++--- 4 files changed, 77 insertions(+), 67 deletions(-) diff --git a/iBox/Sources/Presenter/MyPage/MyPageItemCell.swift b/iBox/Sources/Presenter/MyPage/MyPageItemCell.swift index 9b71644..a72402e 100644 --- a/iBox/Sources/Presenter/MyPage/MyPageItemCell.swift +++ b/iBox/Sources/Presenter/MyPage/MyPageItemCell.swift @@ -9,28 +9,24 @@ import UIKit import SnapKit -class MyPageItemCell: UITableViewCell, BaseViewProtocol { +class MyPageItemCell: UITableViewCell { static let reuseIdentifier = "MyPageItemCell" private var viewModel: MyPageCellViewModel? - // MARK: - UI + // MARK: - UI Components let titleLabel = UILabel().then { $0.font = .systemFont(ofSize: 16) } - let stackView = UIStackView().then { - $0.axis = .horizontal - } - let descriptionLabel = UILabel().then { $0.font = .systemFont(ofSize: 13, weight: .regular) $0.textColor = .gray } - let controlSwitch = UISwitch().then { - $0.tintColor = .box3 + let switchControl = UISwitch().then { + $0.onTintColor = .box2 } let chevronButton = UIButton().then { @@ -40,37 +36,53 @@ class MyPageItemCell: UITableViewCell, BaseViewProtocol { $0.tintColor = .systemGray3 } - // MARK: - initializer + // MARK: - Initializer override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) - configureUI() - selectionStyle = .none + setupProperty() + setupHierarchy() + setupLayout() } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } - // MARK: - BaseViewProtocol + // MARK: - Setup Methods - func configureUI() { + private func setupProperty() { backgroundColor = .clear - addSubview(titleLabel) - addSubview(stackView) - stackView.addArrangedSubview(controlSwitch) - stackView.addArrangedSubview(descriptionLabel) - stackView.addArrangedSubview(chevronButton) - + selectionStyle = .none + } + + private func setupHierarchy() { + contentView.addSubview(titleLabel) + contentView.addSubview(switchControl) + contentView.addSubview(descriptionLabel) + contentView.addSubview(chevronButton) + } + + private func setupLayout() { titleLabel.snp.makeConstraints { $0.left.equalToSuperview().inset(20) $0.centerY.equalToSuperview() } - stackView.snp.makeConstraints { + switchControl.snp.makeConstraints { + $0.right.equalToSuperview().inset(30) + $0.centerY.equalToSuperview() + } + + descriptionLabel.snp.makeConstraints { $0.right.equalToSuperview().inset(30) $0.centerY.equalToSuperview() } + + chevronButton.snp.makeConstraints { + $0.right.equalToSuperview().inset(20) + $0.centerY.equalToSuperview() + } } // MARK: - Bind ViewModel @@ -80,15 +92,15 @@ class MyPageItemCell: UITableViewCell, BaseViewProtocol { titleLabel.text = viewModel.title descriptionLabel.isHidden = true - controlSwitch.isHidden = true + switchControl.isHidden = true chevronButton.isHidden = true if let description = viewModel.description { - descriptionLabel.text = viewModel.description + descriptionLabel.text = description descriptionLabel.isHidden = false } else if let flag = viewModel.flag { - controlSwitch.isOn = flag - controlSwitch.isHidden = false + switchControl.isOn = flag + switchControl.isHidden = false } else { chevronButton.isHidden = false } diff --git a/iBox/Sources/Presenter/MyPage/MyPageView.swift b/iBox/Sources/Presenter/MyPage/MyPageView.swift index 7803d97..00ae02b 100644 --- a/iBox/Sources/Presenter/MyPage/MyPageView.swift +++ b/iBox/Sources/Presenter/MyPage/MyPageView.swift @@ -8,15 +8,13 @@ import Combine import UIKit -class MyPageView: BaseView { - - // MARK: - Properties +final class MyPageView: BaseView { var delegate: MyPageViewDelegate? private var viewModel: MyPageViewModel? private var cancellables = Set() - // MARK: - UI + // MARK: - UI Components let profileView = UIView().then { $0.isUserInteractionEnabled = true @@ -51,29 +49,37 @@ class MyPageView: BaseView { override init(frame: CGRect) { super.init(frame: frame) setupProperty() + setupHierarchy() + setupLayout() } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } - // MARK: - BaseViewProtocol + // MARK: - Setup Methods private func setupProperty() { tableView.delegate = self tableView.dataSource = self - let tapGesture = UITapGestureRecognizer(target: self, action: #selector(profileViewTapped)) - profileView.addGestureRecognizer(tapGesture) + profileView.addGestureRecognizer( + UITapGestureRecognizer( + target: self, + action: #selector(profileViewTapped) + ) + ) } - override func configureUI() { + private func setupHierarchy() { addSubview(profileView) profileView.addSubview(profileImageView) profileView.addSubview(profileLabel) profileView.addSubview(chevronButton) addSubview(tableView) - + } + + private func setupLayout() { profileView.snp.makeConstraints { $0.left.top.right.equalToSuperview() $0.height.equalTo(90) @@ -110,27 +116,30 @@ class MyPageView: BaseView { .receive(on: RunLoop.main) .sink { [weak self] event in switch event { - case .updateMyPageSectionViewModels: + case .updateSectionViewModels: self?.tableView.reloadData() } }.store(in: &cancellables) } - // MARK: - functions + // MARK: - Action Functions - @objc func profileViewTapped() { + @objc private func profileViewTapped() { delegate?.pushViewController(ProfileViewController()) } + @objc private func handleSwitchControlTap(_ controlSwitch: UISwitch) { + guard let viewModel = viewModel else { return } + viewModel.input.send(.setPreload(controlSwitch.isOn)) + } + } extension MyPageView: UITableViewDelegate { - // MARK: - section - func numberOfSections(in tableView: UITableView) -> Int { guard let viewModel = viewModel else { return 0 } - return viewModel.myPageSectionViewModels.count + return viewModel.sectionViewModels.count } func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { @@ -140,18 +149,16 @@ extension MyPageView: UITableViewDelegate { } func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { - return 10 + return 20 } - // MARK: - cell - func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 55 } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { guard let viewModel = viewModel else { return } - let myPageItem = viewModel.myPageSectionViewModels[indexPath.section].cellViewModels[indexPath.row].myPageItem + let myPageItem = viewModel.sectionViewModels[indexPath.section].cellViewModels[indexPath.row].myPageItem if (myPageItem.type != MyPageType.preload) { delegate?.pushViewController(myPageItem.type) } @@ -163,25 +170,20 @@ extension MyPageView: UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { guard let viewModel = viewModel else { return 0 } - return viewModel.myPageSectionViewModels[section].cellViewModels.count + return viewModel.sectionViewModels[section].cellViewModels.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { guard let viewModel = viewModel, let cell = tableView.dequeueReusableCell(withIdentifier: MyPageItemCell.reuseIdentifier) as? MyPageItemCell else { return UITableViewCell() } - let cellViewModel = viewModel.myPageSectionViewModels[indexPath.section].cellViewModels[indexPath.row] + let cellViewModel = viewModel.sectionViewModels[indexPath.section].cellViewModels[indexPath.row] cell.bindViewModel(cellViewModel) if cellViewModel.flag != nil { - cell.controlSwitch.addTarget(self, action: #selector(handleControlSwitchTap), for: .touchUpInside) + cell.switchControl.removeTarget(nil, action: nil, for: .valueChanged) + cell.switchControl.addTarget(self, action: #selector(handleSwitchControlTap), for: .valueChanged) } return cell } - @objc private func handleControlSwitchTap(_ controlSwitch: UISwitch) { - guard let viewModel = viewModel else { return } - viewModel.input.send(.setPreload(controlSwitch.isOn)) - print(controlSwitch.isOn) - } - } diff --git a/iBox/Sources/Presenter/MyPage/MyPageViewController.swift b/iBox/Sources/Presenter/MyPage/MyPageViewController.swift index 388a2a1..1c6a0f1 100644 --- a/iBox/Sources/Presenter/MyPage/MyPageViewController.swift +++ b/iBox/Sources/Presenter/MyPage/MyPageViewController.swift @@ -12,20 +12,17 @@ protocol MyPageViewDelegate { func pushViewController(_ viewController: UIViewController) } -class MyPageViewController: BaseNavigationBarViewController { - - // MARK: - Properties +final class MyPageViewController: BaseNavigationBarViewController { private let viewModel = MyPageViewModel() - // MARK: - life cycle + // MARK: - Life Cycle override func viewDidLoad() { super.viewDidLoad() guard let contentView = contentView as? MyPageView else { return } contentView.delegate = self contentView.bindViewModel(viewModel) - viewModel.input.send(.viewWillAppear) } override func viewWillAppear(_ animated: Bool) { @@ -49,14 +46,13 @@ extension MyPageViewController: MyPageViewDelegate { navigationController?.pushViewController(ThemeViewController(), animated: true) case .homeTab: navigationController?.pushViewController(HomeTabSelectorViewController(), animated: true) - case .preload: - print("미리 로드 탭 !") case .guide: print("이용 가이드 탭 !") case .feedback: print("앱 피드백 탭 !") case .developer: print("개발자 정보 탭 !") + default: break } } diff --git a/iBox/Sources/ViewModel/MyPageViewModel.swift b/iBox/Sources/ViewModel/MyPageViewModel.swift index cbaa62f..f388ac2 100644 --- a/iBox/Sources/ViewModel/MyPageViewModel.swift +++ b/iBox/Sources/ViewModel/MyPageViewModel.swift @@ -16,7 +16,7 @@ class MyPageViewModel { } enum Output { - case updateMyPageSectionViewModels + case updateSectionViewModels } // MARK: - Properties @@ -24,15 +24,15 @@ class MyPageViewModel { let input = PassthroughSubject() private let output = PassthroughSubject() private var cancellables = Set() - var myPageSectionViewModels = [MyPageSectionViewModel]() + var sectionViewModels = [MyPageSectionViewModel]() func transform(input: AnyPublisher) -> AnyPublisher { input.sink { [weak self] event in switch event { case .viewWillAppear: - self?.myPageSectionViewModels.removeAll() - self?.updateMyPageSectionViewModels() - self?.output.send(.updateMyPageSectionViewModels) + self?.sectionViewModels.removeAll() + self?.updateSectionViewModels() + self?.output.send(.updateSectionViewModels) case let .setPreload(isOn): UserDefaultsManager.isPreload = isOn } @@ -40,13 +40,13 @@ class MyPageViewModel { return output.eraseToAnyPublisher() } - private func updateMyPageSectionViewModels() { - myPageSectionViewModels.append(MyPageSectionViewModel(cellViewModels: [ + private func updateSectionViewModels() { + sectionViewModels.append(MyPageSectionViewModel(cellViewModels: [ MyPageCellViewModel(MyPageItem(type: .theme, description: UserDefaultsManager.theme.toString())), MyPageCellViewModel(MyPageItem(type: .homeTab, description: HomeTabType.allCases[UserDefaultsManager.homeTabIndex].toString())), MyPageCellViewModel(MyPageItem(type: .preload, flag: UserDefaultsManager.isPreload)) ])) - myPageSectionViewModels.append(MyPageSectionViewModel(cellViewModels: [ + sectionViewModels.append(MyPageSectionViewModel(cellViewModels: [ MyPageCellViewModel(MyPageItem(type: .guide)), MyPageCellViewModel(MyPageItem(type: .feedback)), MyPageCellViewModel(MyPageItem(type: .developer))