Skip to content

Commit

Permalink
[Feat/#42] 옵션 선택
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoe0929 committed Sep 22, 2024
1 parent 6f3454e commit 7875704
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ final class OptionSelectViewController: UIViewController {
bottomButton.button.rx.tap
.bind {
self.dismiss(animated: true) {
self.delegate?.optionViewControllerDidFinish(self)
let unwrappedOptions = self.selectedOptions.compactMap { $0 }
self.delegate?.optionViewControllerDidFinish(self, optionList: unwrappedOptions)
Amplitude.instance().logEvent("click_option_next")
}
}
Expand Down Expand Up @@ -154,26 +155,30 @@ final class OptionSelectViewController: UIViewController {

optionCollectionView.rx.itemSelected
.bind(with: self) { owner, indexPath in
if let cell = owner.optionCollectionView.cellForItem(at: indexPath) as? OptionCollectionViewCell {
cell.isSelectedRelay.accept(true)
owner.selectedOptions[indexPath.section] = self.option[indexPath.section].optionDetailList[indexPath.row].optionDetailID
owner.updateButtonState()
// 섹션 내 이전 선택 해제
if let selectedIndex = owner.selectedOptions[indexPath.section] {
let deselectedIndexPath = IndexPath(row: selectedIndex, section: indexPath.section)
if let cell = owner.optionCollectionView.cellForItem(at: deselectedIndexPath) as? OptionCollectionViewCell {
cell.isSelectedRelay.accept(false) // 이전 선택 해제
}
}
}
.disposed(by: disposeBag)

optionCollectionView.rx.itemDeselected
.bind(with: self) { owner, indexPath in

// 선택한 옵션을 섹션별로 저장
let selectedOptionId = owner.option[indexPath.section].optionDetailList[indexPath.row].optionDetailID
owner.selectedOptions[indexPath.section] = selectedOptionId // optionDetailId 저장

if let cell = owner.optionCollectionView.cellForItem(at: indexPath) as? OptionCollectionViewCell {
cell.isSelectedRelay.accept(false)
owner.selectedOptions[indexPath.section] = nil
owner.updateButtonState()
cell.isSelectedRelay.accept(true) // 새로운 선택 적용
}

owner.updateButtonState() // 버튼 상태 업데이트
}
.disposed(by: disposeBag)

}

private func updateButtonState() {
print(selectedOptions)
let allSectionsSelected = selectedOptions.allSatisfy { $0 != nil }
bottomButton.button.isEnabled = allSectionsSelected
bottomButton.button.backgroundColor = allSectionsSelected ? .black : .gray2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import Amplitude

// MARK: - OptionDelegate
protocol OptionViewControllerDelegate: AnyObject {
func optionViewControllerDidFinish(_ viewController: OptionSelectViewController)
func optionViewControllerDidFinish(_ viewController: OptionSelectViewController, optionList: [Int])
}

final class ProductDetailViewController: UIViewController {
Expand Down Expand Up @@ -324,9 +324,9 @@ final class ProductDetailViewController: UIViewController {
}

extension ProductDetailViewController: OptionViewControllerDelegate {
func optionViewControllerDidFinish(_ viewController: OptionSelectViewController) {
func optionViewControllerDidFinish(_ viewController: OptionSelectViewController, optionList: [Int]) {
let purchaseVC = PurchaseViewController()
purchaseVC.orderModel = .init(productId: productId, optionList: optionList.map({ $0.optionID }))
purchaseVC.orderModel = .init(productId: productId, optionList: optionList)
self.navigationController?.pushViewController(purchaseVC, animated: true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ final class TermsTableViewCell: UITableViewCell {
private func setConstraints() {
containerView.snp.makeConstraints {
$0.height.equalTo(38)
$0.horizontalEdges.equalToSuperview().inset(20)
$0.horizontalEdges.equalToSuperview()
$0.verticalEdges.equalToSuperview().inset(5)
}

Expand Down

0 comments on commit 7875704

Please sign in to comment.