Skip to content

Commit

Permalink
add: WriteBoard의 복잡한 데이터 흐름을 쪼개기 위한 파일 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
MaraMincho committed Jan 13, 2024
1 parent bdce4ba commit c62e50b
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ public extension Record {
let hours = Int(timeInterval / 3600)
let minutes = Int((timeInterval.truncatingRemainder(dividingBy: 3600)) / 60)
if minutes == 0 {
return hours == 0 ? "" : "(\(hours)시간)"
return hours == 0 ? "" : "\(hours)시간"
}
return hours == 0 ? "(\(minutes))" : "(\(hours)시간\(minutes))"
return hours == 0 ? "\(minutes)" : "\(hours)시간\(minutes)"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ final class WorkoutHistoryDescriptionView: UIStackView {
WorkoutHistoryDescriptionRowView(titleString: Constants.workoutName, description: record.name),
WorkoutHistoryDescriptionRowView(titleString: Constants.date, description: record.dateString),
WorkoutHistoryDescriptionRowView(titleString: Constants.time, description: record.durationTime + " (" + record.startTime + "~" + record.endTime + ")"),
WorkoutHistoryDescriptionRowView(titleString: Constants.distance, description: String(format: "%01f", Double(record.distance) / 1000) + "km"),
WorkoutHistoryDescriptionRowView(titleString: Constants.distance, description: String(format: "%.1f", Double(record.distance) / 1000) + "km"),
])
stackView.axis = .vertical
stackView.spacing = Constants.cellSpacing
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
//
// AttachPictureViewController.swift
// WriteBoardFeature
//
// Created by MaraMincho on 1/13/24.
// Copyright © 2024 kr.codesquad.boostcamp8. All rights reserved.
//

import Combine
import UIKit

// MARK: - AttachPictureViewController

final class AttachPictureViewController: UICollectionViewController {
// MARK: - Property

private var subscriptions: Set<AnyCancellable> = []

private var attachPictureCollectionViewDataSource: UICollectionViewDiffableDataSource<Int, UIImage>? = nil

// MARK: - View Life Cycle

override func viewDidLoad() {
super.viewDidLoad()
}

// MARK: - Init Function

init() {
super.init(nibName: nil, bundle: nil)
setup()
}

@available(*, unavailable)
required init?(coder _: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

private extension AttachPictureViewController {
func setup() {
collectionView = UICollectionView(frame: .zero, collectionViewLayout: .makeAttachPictureCollectionViewLayout())
collectionView.register(AttachPictureCollectionViewCell.self, forCellWithReuseIdentifier: AttachPictureCollectionViewCell.identifier)
setupStyle()
setAttachPictureCollectionViewDataSource()
}

func setupStyle() {
collectionView.backgroundColor = .clear
}

func setAttachPictureCollectionViewDataSource() {
attachPictureCollectionViewDataSource = .init(collectionView: collectionView) { collectionView, indexPath, itemIdentifier in
guard
let cell = collectionView.dequeueReusableCell(
withReuseIdentifier: AttachPictureCollectionViewCell.identifier,
for: indexPath
) as? AttachPictureCollectionViewCell
else {
return UICollectionViewCell()
}

cell.configure(image: itemIdentifier)
return cell
}
guard
var snapshot = attachPictureCollectionViewDataSource?.snapshot(),
let addPicImage = Constants.addPictureImage,
let testImage = UIImage(systemName: "figure.run")
else {
return
}
snapshot.appendSections([0])
snapshot.appendItems([addPicImage, testImage])
attachPictureCollectionViewDataSource?.apply(snapshot)
}

private enum Constants {
static let addPictureImage: UIImage? = .init(systemName: "plus")
}
}

private extension UICollectionViewLayout {
static func makeAttachPictureCollectionViewLayout() -> UICollectionViewLayout {
let itemSize = NSCollectionLayoutSize(
widthDimension: .absolute(attachPictureCollectionViewCellConstants.width),
heightDimension: .fractionalHeight(1)
)
let item = NSCollectionLayoutItem(layoutSize: itemSize)
item.contentInsets = .init(
top: 0,
leading: attachPictureCollectionViewCellConstants.itemLeadingAndTrailingInset,
bottom: 0,
trailing: attachPictureCollectionViewCellConstants.itemLeadingAndTrailingInset
)

let groupSize = NSCollectionLayoutSize(
widthDimension: .fractionalWidth(1),
heightDimension: .absolute(attachPictureCollectionViewCellConstants.height)
)
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])
group.contentInsets = .init(
top: 0,
leading: attachPictureCollectionViewCellConstants.groupLeadingAndTrailingInset,
bottom: 0,
trailing: attachPictureCollectionViewCellConstants.groupLeadingAndTrailingInset
)

let section = NSCollectionLayoutSection(group: group)

return UICollectionViewCompositionalLayout(section: section)
}

private enum attachPictureCollectionViewCellConstants {
static let height: CGFloat = 94
static let width: CGFloat = 94

static let itemLeadingAndTrailingInset: CGFloat = 6

static let groupLeadingAndTrailingInset: CGFloat = 29
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ final class WriteBoardViewController: UIViewController {

private let viewModel: WriteBoardViewModelRepresentable

private var attachPictureCollectionViewDataSource: UICollectionViewDiffableDataSource<Int, UIImage>? = nil

private let completeButtonDidTapPublisher: PassthroughSubject<Void, Never> = .init()

private var subscriptions: Set<AnyCancellable> = []
Expand All @@ -46,15 +44,7 @@ final class WriteBoardViewController: UIViewController {
return label
}()

private lazy var attachPictureCollectionView: UICollectionView = {
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: .makeAttachPictureCollectionViewLayout())
collectionView.register(AttachPictureCollectionViewCell.self, forCellWithReuseIdentifier: AttachPictureCollectionViewCell.identifier)
collectionView.delegate = self
collectionView.backgroundColor = .clear

collectionView.translatesAutoresizingMaskIntoConstraints = false
return collectionView
}()
private let attachPictureViewController = AttachPictureViewController()

private let boardDetailTitleLabel: UILabel = {
let label = UILabel()
Expand Down Expand Up @@ -111,39 +101,12 @@ final class WriteBoardViewController: UIViewController {
private extension WriteBoardViewController {
func setup() {
workoutHistoryDescriptionView = .init(record: viewModel.record())
setAttachPictureCollectionViewDataSource()
setupStyles()
setupHierarchyAndConstraints()
bind()
setupNavigationItem()
}

func setAttachPictureCollectionViewDataSource() {
attachPictureCollectionViewDataSource = .init(collectionView: attachPictureCollectionView) { collectionView, indexPath, itemIdentifier in
guard
let cell = collectionView.dequeueReusableCell(
withReuseIdentifier: AttachPictureCollectionViewCell.identifier,
for: indexPath
) as? AttachPictureCollectionViewCell
else {
return UICollectionViewCell()
}

cell.configure(image: itemIdentifier)
return cell
}
guard
var snapshot = attachPictureCollectionViewDataSource?.snapshot(),
let addPicImage = Constants.addPictureImage,
let testImage = UIImage(systemName: "figure.run")
else {
return
}
snapshot.appendSections([0])
snapshot.appendItems([addPicImage, testImage])
attachPictureCollectionViewDataSource?.apply(snapshot)
}

func setupNavigationItem() {
navigationItem.backButtonTitle = "뒤로"
navigationItem.title = "글쓰기"
Expand Down Expand Up @@ -177,7 +140,10 @@ private extension WriteBoardViewController {
attachPictureTitleLabel.leadingAnchor
.constraint(equalTo: workoutHistoryDescriptionView.leadingAnchor).isActive = true

// TODO: View에 관한 layout이 어떻게 이루어지는지 그리고 View에 lifeCycle에 대해서 자세하게 공부하기
guard let attachPictureCollectionView = attachPictureViewController.collectionView else { return }
contentScrollView.addSubview(attachPictureCollectionView)
attachPictureCollectionView.translatesAutoresizingMaskIntoConstraints = false
attachPictureCollectionView.topAnchor
.constraint(equalTo: attachPictureTitleLabel.bottomAnchor, constant: Metrics.attachPictureBottomSpacing).isActive = true
attachPictureCollectionView.leadingAnchor
Expand Down Expand Up @@ -236,57 +202,10 @@ private extension WriteBoardViewController {

enum Constants {
static let pictureTitleLabelText = "사진"

static let addPictureImage: UIImage? = .init(systemName: "plus")
}
}

// MARK: UICollectionViewDelegate

extension WriteBoardViewController: UICollectionViewDelegate {}

extension UICollectionViewLayout {
static func makeAttachPictureCollectionViewLayout() -> UICollectionViewLayout {
let itemSize = NSCollectionLayoutSize(
widthDimension: .absolute(attachPictureCollectionViewCellConstants.width),
heightDimension: .fractionalHeight(1)
)
let item = NSCollectionLayoutItem(layoutSize: itemSize)
item.contentInsets = .init(
top: 0,
leading: attachPictureCollectionViewCellConstants.itemLeadingAndTrailingInset,
bottom: 0,
trailing: attachPictureCollectionViewCellConstants.itemLeadingAndTrailingInset
)

let groupSize = NSCollectionLayoutSize(
widthDimension: .fractionalWidth(1),
heightDimension: .absolute(attachPictureCollectionViewCellConstants.height)
)
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])
group.contentInsets = .init(
top: 0,
leading: attachPictureCollectionViewCellConstants.groupLeadingAndTrailingInset,
bottom: 0,
trailing: attachPictureCollectionViewCellConstants.groupLeadingAndTrailingInset
)

let section = NSCollectionLayoutSection(group: group)

return UICollectionViewCompositionalLayout(section: section)
}

private enum attachPictureCollectionViewCellConstants {
static let height: CGFloat = 94
static let width: CGFloat = 94

static let itemLeadingAndTrailingInset: CGFloat = 6

static let groupLeadingAndTrailingInset: CGFloat = 29
}
}

// MARK: - WriteBoardViewController + UIScrollViewDelegate
// MARK: UIScrollViewDelegate

extension WriteBoardViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
Expand Down

0 comments on commit c62e50b

Please sign in to comment.