|
| 1 | +// |
| 2 | +// OverallCollectionViewCell.swift |
| 3 | +// FeatureLayer |
| 4 | +// |
| 5 | +// Created by 최재혁 on 8/12/25. |
| 6 | +// |
| 7 | + |
| 8 | +import UIKit |
| 9 | +import DesignSystem |
| 10 | +import Base |
| 11 | + |
| 12 | +struct OverallCollectionViewCellModel { |
| 13 | + let title: String |
| 14 | + let modal : [OverallModalCollectionViewCellModel] |
| 15 | +} |
| 16 | + |
| 17 | +final class OverallCollectionViewCell: UICollectionViewCell { |
| 18 | + |
| 19 | + private var modalModels: [OverallModalCollectionViewCellModel] = [] |
| 20 | + |
| 21 | + private lazy var contentStackView = UIStackView().then { |
| 22 | + $0.axis = .vertical |
| 23 | + $0.spacing = 12 |
| 24 | + $0.alignment = .center |
| 25 | + } |
| 26 | + |
| 27 | + private lazy var titleStackView = UIStackView().then { |
| 28 | + $0.axis = .horizontal |
| 29 | + $0.spacing = 4 |
| 30 | + $0.isLayoutMarginsRelativeArrangement = true |
| 31 | + $0.layoutMargins = UIEdgeInsets(top: 0, left: 24, bottom: 0, right: 0) |
| 32 | + } |
| 33 | + |
| 34 | + private lazy var titleLabel = UILabel().then { |
| 35 | + $0.style = Typography.Body_18_B |
| 36 | + $0.textColor = STColors.gray2.color |
| 37 | + } |
| 38 | + |
| 39 | + private lazy var modalCollectionView = UICollectionView( |
| 40 | + frame: .zero, collectionViewLayout: createLayout() |
| 41 | + ).then { |
| 42 | + $0.backgroundColor = .clear |
| 43 | + $0.isPagingEnabled = false |
| 44 | + $0.showsHorizontalScrollIndicator = false |
| 45 | + $0.delegate = self |
| 46 | + $0.dataSource = self |
| 47 | + $0.register( |
| 48 | + OverallModalCollectionViewCell.self, |
| 49 | + forCellWithReuseIdentifier: OverallModalCollectionViewCell.typeName) |
| 50 | + } |
| 51 | + |
| 52 | + override init(frame: CGRect) { |
| 53 | + super.init(frame: frame) |
| 54 | + setupUI() |
| 55 | + } |
| 56 | + |
| 57 | + required init?(coder: NSCoder) { |
| 58 | + fatalError("init(coder:) has not been implemented") |
| 59 | + } |
| 60 | + |
| 61 | + private func setupUI() { |
| 62 | + contentView.addSubview(contentStackView) |
| 63 | + contentStackView.addArrangedSubview(titleStackView) |
| 64 | + contentStackView.addArrangedSubview(modalCollectionView) |
| 65 | + titleStackView.addArrangedSubview(titleLabel) |
| 66 | + |
| 67 | + contentStackView.snp.makeConstraints { make in |
| 68 | + make.edges.equalToSuperview() |
| 69 | + } |
| 70 | + |
| 71 | + modalCollectionView.snp.makeConstraints { make in |
| 72 | + make.leading.trailing.equalToSuperview() |
| 73 | + make.height.equalTo(129) |
| 74 | + } |
| 75 | + |
| 76 | + titleStackView.snp.makeConstraints { make in |
| 77 | + make.leading.equalToSuperview() |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + func update(with cellModel: OverallCollectionViewCellModel) { |
| 82 | + titleLabel.styledText = cellModel.title |
| 83 | + modalModels = cellModel.modal |
| 84 | + modalCollectionView.reloadData() |
| 85 | + } |
| 86 | + |
| 87 | + private func createLayout() -> UICollectionViewCompositionalLayout { |
| 88 | + let layout = UICollectionViewCompositionalLayout { sectionIndex, environment -> NSCollectionLayoutSection? in |
| 89 | + |
| 90 | + let itemSize = NSCollectionLayoutSize( |
| 91 | + widthDimension: .absolute(134), |
| 92 | + heightDimension: .estimated(200) |
| 93 | + ) |
| 94 | + let item = NSCollectionLayoutItem(layoutSize: itemSize) |
| 95 | + |
| 96 | + let groupSize = NSCollectionLayoutSize( |
| 97 | + widthDimension: .absolute(134), |
| 98 | + heightDimension: .estimated(200) |
| 99 | + ) |
| 100 | + |
| 101 | + let group = NSCollectionLayoutGroup.horizontal( |
| 102 | + layoutSize: groupSize, |
| 103 | + subitems: [item] |
| 104 | + ) |
| 105 | + |
| 106 | + let section = NSCollectionLayoutSection(group: group) |
| 107 | + section.orthogonalScrollingBehavior = .continuous |
| 108 | + |
| 109 | + section.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 20, bottom: 0, trailing: 0) |
| 110 | + |
| 111 | + section.interGroupSpacing = 10 |
| 112 | + |
| 113 | + return section |
| 114 | + } |
| 115 | + |
| 116 | + return layout |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +extension OverallCollectionViewCell : UICollectionViewDataSource, UICollectionViewDelegate { |
| 121 | + func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { |
| 122 | + return modalModels.count |
| 123 | + } |
| 124 | + |
| 125 | + func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { |
| 126 | + guard let cell = collectionView.dequeueReusableCell( |
| 127 | + withReuseIdentifier: OverallModalCollectionViewCell.typeName, |
| 128 | + for: indexPath) as? OverallModalCollectionViewCell else { |
| 129 | + return UICollectionViewCell() |
| 130 | + } |
| 131 | + |
| 132 | + let model = modalModels[indexPath.item] |
| 133 | + cell.update(with: model) |
| 134 | + return cell |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +@available(iOS 17.0, *) |
| 139 | +#Preview { |
| 140 | + let containerView = UIView().then { |
| 141 | + $0.backgroundColor = .systemBackground |
| 142 | + } |
| 143 | + |
| 144 | + let cell = OverallCollectionViewCell(frame: .zero) |
| 145 | + |
| 146 | + let modalModels: [OverallModalCollectionViewCellModel] = [ |
| 147 | + OverallModalCollectionViewCellModel( |
| 148 | + title: "첫 번째 모달", |
| 149 | + description: "이것은 첫 번째 모달", |
| 150 | + image: UIImage(systemName: "star.fill") ?? UIImage() |
| 151 | + ), |
| 152 | + OverallModalCollectionViewCellModel( |
| 153 | + title: "두 번째 모달", |
| 154 | + description: "이것은 두 번째 모달", |
| 155 | + image: UIImage(systemName: "heart.fill") ?? UIImage() |
| 156 | + ), |
| 157 | + OverallModalCollectionViewCellModel( |
| 158 | + title: "세 번째 모달", |
| 159 | + description: "이것은 세 번째 모달입니다.", |
| 160 | + image: UIImage(systemName: "circle.fill") ?? UIImage() |
| 161 | + ) |
| 162 | + ] |
| 163 | + let cellModel = OverallCollectionViewCellModel(title: "종합 운세", modal: modalModels) |
| 164 | + |
| 165 | + cell.update(with: cellModel) |
| 166 | + containerView.backgroundColor = STColors.primary7.color |
| 167 | + |
| 168 | + containerView.addSubview(cell) |
| 169 | + cell.snp.makeConstraints { make in |
| 170 | + make.top.leading.trailing.bottom.equalToSuperview() |
| 171 | + make.height.equalTo(195) |
| 172 | + } |
| 173 | + |
| 174 | + return containerView |
| 175 | +} |
0 commit comments