Skip to content

Commit

Permalink
feat: CollectionView -> TableView로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
MaraMincho committed Jan 10, 2024
1 parent 542cdf3 commit 0cacd99
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import UIKit

// MARK: - WorkoutHistoryCell

final class WorkoutHistoryCell: UICollectionViewCell {
final class WorkoutHistoryCell: UITableViewCell {
static let identifier = "WorkoutHistoryCell"

override init(frame: CGRect) {
super.init(frame: frame)
override init(style _: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
setup()
}

Expand All @@ -29,14 +29,16 @@ final class WorkoutHistoryCell: UICollectionViewCell {
label.textColor = DesignSystemColor.primaryText
label.font = .preferredFont(forTextStyle: .title2)
label.text = "사이클"
label.textAlignment = .right
label.setContentHuggingPriority(.required, for: .horizontal)

label.translatesAutoresizingMaskIntoConstraints = false
return label
}()

private let workoutDistanceLabel: UILabel = {
let label = UILabel()
label.textColor = DesignSystemColor.gray03
label.textColor = DesignSystemColor.primaryText
label.font = .preferredFont(forTextStyle: .headline)
label.text = "1.5 km"

Expand All @@ -58,53 +60,62 @@ final class WorkoutHistoryCell: UICollectionViewCell {
label.textColor = DesignSystemColor.primaryText
label.font = .preferredFont(forTextStyle: .body)
label.text = "1월 10일"
label.textAlignment = .right

label.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()

private let timeLabel: UILabel = {
let label = UILabel()
label.textColor = DesignSystemColor.gray03
label.textColor = DesignSystemColor.primaryText
label.font = .preferredFont(forTextStyle: .body)
label.text = "06:00 ~ 06:30 (30분)"
label.setContentHuggingPriority(.defaultHigh, for: .horizontal)

label.translatesAutoresizingMaskIntoConstraints = false
return label
}()

private lazy var footer: UIStackView = {
let stackView = UIStackView(arrangedSubviews: [dateLabel, timeLabel])
stackView.axis = .horizontal
stackView.spacing = Metrics.footerSpacing
stackView.distribution = .fillProportionally

stackView.translatesAutoresizingMaskIntoConstraints = false
return stackView
}()

private lazy var descriptionContentStackView: UIStackView = {
let stackView = UIStackView(arrangedSubviews: [
header, footer
header, footer,
])
stackView.spacing = Metrics.headerAndFooterSpacing
stackView.axis = .vertical

stackView.translatesAutoresizingMaskIntoConstraints = false
return stackView
}()

private let workoutImageView: UIImageView = {
let configure: UIImage.SymbolConfiguration = .init(font: .boldSystemFont(ofSize: 35))
let image = UIImage(systemName: "figure.run", withConfiguration: configure)
var image = UIImage(systemName: "figure.run", withConfiguration: configure)

let imageView = UIImageView(image: image)
imageView.contentMode = .scaleAspectFit
imageView.setContentHuggingPriority(.required, for: .horizontal)
imageView.tintColor = DesignSystemColor.primaryText

imageView.translatesAutoresizingMaskIntoConstraints = false
return imageView
}()

private lazy var itemStackView: UIStackView = {
let stackView = UIStackView(arrangedSubviews: [ workoutImageView, descriptionContentStackView])
let stackView = UIStackView(arrangedSubviews: [workoutImageView, descriptionContentStackView])
stackView.spacing = Metrics.imageAndContentSpacing

stackView.translatesAutoresizingMaskIntoConstraints = false
return stackView
}()
Expand All @@ -114,27 +125,37 @@ final class WorkoutHistoryCell: UICollectionViewCell {
static let footerSpacing: CGFloat = 12
static let headerAndFooterSpacing: CGFloat = 9
static let imageAndContentSpacing: CGFloat = 15

static let contentViewAndItemSpacing: CGFloat = 12
}
}

private extension WorkoutHistoryCell {
func setup() {
makeShadowAndRounded()
setupStyle()
setupViewHierarchyAndConstraints()
}

func setupStyle() {
contentView.backgroundColor = DesignSystemColor.secondaryBackground
}

func setupViewHierarchyAndConstraints() {
addSubview(itemStackView)
contentView.addSubview(itemStackView)
itemStackView.topAnchor
.constraint(equalTo: contentView.topAnchor, constant: Metrics.contentViewAndItemSpacing).isActive = true
itemStackView.leadingAnchor
.constraint(equalTo: contentView.leadingAnchor, constant: Metrics.contentViewAndItemSpacing).isActive = true
.constraint(equalTo: contentView.leadingAnchor, constant: 23).isActive = true
itemStackView.trailingAnchor
.constraint(equalTo: contentView.trailingAnchor, constant: -Metrics.contentViewAndItemSpacing).isActive = true
.constraint(equalTo: contentView.trailingAnchor, constant: -23).isActive = true
itemStackView.bottomAnchor
.constraint(equalTo: contentView.bottomAnchor, constant: -Metrics.contentViewAndItemSpacing).isActive = true

workoutTitleLabel.widthAnchor.constraint(equalToConstant: 82).isActive = true
dateLabel.widthAnchor.constraint(equalToConstant: 82).isActive = true

workoutImageView.widthAnchor.constraint(equalToConstant: 53).isActive = true
workoutImageView.heightAnchor.constraint(equalToConstant: 53).isActive = true
}

func makeShadowAndRounded() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@ final class WorkoutHistorySelectViewController: UIViewController {

private var subscriptions: Set<AnyCancellable> = []

private var dataSource: UITableViewDiffableDataSource<Int, UUID>? = nil

// MARK: UI Components

private lazy var workoutHistoryCollectionView: UICollectionView = {
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: makeCompositionalLayout())
collectionView.register(WorkoutHistoryCell.self, forCellWithReuseIdentifier: WorkoutHistoryCell.identifier)
collectionView.delegate = self
collectionView.translatesAutoresizingMaskIntoConstraints = false
return collectionView
private lazy var workoutHistoryTableView: UITableView = {
let tableView = UITableView(frame: .zero, style: .plain)
tableView.register(WorkoutHistoryCell.self, forCellReuseIdentifier: WorkoutHistoryCell.identifier)
tableView.estimatedRowHeight = UITableView.automaticDimension
tableView.delegate = self

tableView.translatesAutoresizingMaskIntoConstraints = false
return tableView
}()

// MARK: Initializations
Expand All @@ -52,18 +56,56 @@ final class WorkoutHistorySelectViewController: UIViewController {
super.viewDidLoad()
setup()
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
}

private extension WorkoutHistorySelectViewController {
func setup() {
setDataSource()
setupStyles()
setupNavigationItem()
setupHierarchyAndConstraints()
bind()
setFakeData()
}

func setupHierarchyAndConstraints() {
let safeArea = view.safeAreaLayoutGuide

view.addSubview(workoutHistoryTableView)
workoutHistoryTableView.topAnchor.constraint(equalTo: safeArea.topAnchor).isActive = true
workoutHistoryTableView.leadingAnchor
.constraint(equalTo: safeArea.leadingAnchor).isActive = true
workoutHistoryTableView.trailingAnchor
.constraint(equalTo: safeArea.trailingAnchor).isActive = true
workoutHistoryTableView.bottomAnchor.constraint(equalTo: safeArea.bottomAnchor).isActive = true
}

func setDataSource() {
dataSource = .init(tableView: workoutHistoryTableView) { tableView, _, _ in
guard let cell = tableView.dequeueReusableCell(withIdentifier: WorkoutHistoryCell.identifier) as? WorkoutHistoryCell else {
return UITableViewCell()
}

return cell
}
guard let dataSource else { return }

var snapshot = dataSource.snapshot()
snapshot.appendSections([0])
dataSource.apply(snapshot)
}

func setFakeData() {
guard let dataSource else {
return
}
var snapshot = dataSource.snapshot()
snapshot.appendItems([.init(), .init(), .init()])
dataSource.apply(snapshot)
}

func setupStyles() {
Expand All @@ -88,28 +130,10 @@ private extension WorkoutHistorySelectViewController {
enum Metrics {}
}

// MARK: UICollectionViewDelegate
// MARK: UITableViewDelegate

extension WorkoutHistorySelectViewController: UICollectionViewDelegate {}

private extension WorkoutHistorySelectViewController {
func makeCompositionalLayout() -> UICollectionViewCompositionalLayout {
let item = NSCollectionLayoutItem(
layoutSize: .init(
widthDimension: .fractionalWidth(1),
heightDimension: .fractionalHeight(1)
)
)

let group = NSCollectionLayoutGroup(
layoutSize: .init(
widthDimension: .fractionalHeight(1),
heightDimension: .fractionalHeight(1 / 9)
)
)

let section = NSCollectionLayoutSection(group: group)

return .init(section: section)
extension WorkoutHistorySelectViewController: UITableViewDelegate {
func tableView(_: UITableView, estimatedHeightForRowAt _: IndexPath) -> CGFloat {
return 80
}
}

0 comments on commit 0cacd99

Please sign in to comment.