-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
박스오피스II [STEP 2] Erick, kyungmin #105
base: ic_9_kyungmin
Are you sure you want to change the base?
Changes from 18 commits
e0a51ed
9414154
b757f78
77215c4
12918c9
bbd3ff2
57a4c8d
7fd2e4d
b91361c
5d124a0
e555577
97b8d2b
c6b8118
37524cd
1ba9a90
9243927
30daee2
869dd61
c852eb4
7869c80
749c7d6
c06bc43
5997921
553f24b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ import UIKit | |
final class BoxOfficeViewController: UIViewController { | ||
private let boxOfficeManager: BoxOfficeManager | ||
private var collectionView: UICollectionView! | ||
private var collectionViewMode: CollectionViewMode = .list | ||
private var dailyBoxOfficeDataSource: UICollectionViewDiffableDataSource<Section, DailyBoxOffice>! | ||
private let activityIndicator: UIActivityIndicatorView = { | ||
let activityIndicator = UIActivityIndicatorView(style: .large) | ||
|
@@ -37,13 +38,20 @@ final class BoxOfficeViewController: UIViewController { | |
configureUI() | ||
setupConstraint() | ||
} | ||
|
||
override func viewWillAppear(_ animated: Bool) { | ||
super.viewWillAppear(animated) | ||
|
||
setupNavigation() | ||
} | ||
} | ||
|
||
// MARK: setup Components | ||
extension BoxOfficeViewController { | ||
private func setupComponents() { | ||
setupView() | ||
setupNavigation() | ||
setupToolbar() | ||
setupCollectionView() | ||
setupRefreshControl() | ||
} | ||
|
@@ -57,15 +65,33 @@ extension BoxOfficeViewController { | |
|
||
navigationItem.title = DateFormatter().dateString(from: boxOfficeManager.targetDate, with: DateFormatter.FormatCase.hyphen) | ||
navigationItem.rightBarButtonItem = selectDateButton | ||
navigationController?.setToolbarHidden(false, animated: false) | ||
} | ||
|
||
private func setupToolbar() { | ||
let changeViewModeButton = UIBarButtonItem(title: NameSpace.changeMode, style: .plain, target: self, action: #selector(didTapChangeViewModeButton)) | ||
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil) | ||
|
||
toolbarItems = [flexibleSpace, changeViewModeButton, flexibleSpace] | ||
} | ||
|
||
private func setupCollectionView() { | ||
collectionView = UICollectionView(frame: .zero, collectionViewLayout: verticalLayout()) | ||
collectionView = UICollectionView(frame: .zero, collectionViewLayout: listLayout()) | ||
collectionView.translatesAutoresizingMaskIntoConstraints = false | ||
collectionView.register(BoxOfficeCollectionViewCell.self, forCellWithReuseIdentifier: BoxOfficeCollectionViewCell.identifier) | ||
collectionView.register(BoxOfficeCollectionViewListCell.self, forCellWithReuseIdentifier: BoxOfficeCollectionViewListCell.identifier) | ||
collectionView.register(BoxOfficeCollectionViewGridCell.self, forCellWithReuseIdentifier: BoxOfficeCollectionViewGridCell.identifier) | ||
collectionView.delegate = self | ||
} | ||
|
||
private func setupCollectionViewLayout() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 인자로 안받고 이렇게 하신 이유가 있을까요 ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
switch collectionViewMode { | ||
case .list: | ||
collectionView.setCollectionViewLayout(listLayout(), animated: true) | ||
case .grid: | ||
collectionView.setCollectionViewLayout(gridLayout(), animated: true) | ||
} | ||
} | ||
|
||
private func setupRefreshControl() { | ||
let refreshControl = UIRefreshControl() | ||
refreshControl.addTarget(self, action: #selector(fetchBoxOfficeData), for: .valueChanged) | ||
|
@@ -85,7 +111,8 @@ extension BoxOfficeViewController { | |
boxOfficeManager.fetchBoxOffice { result in | ||
if result == false { | ||
DispatchQueue.main.async { | ||
let alert = UIAlertController.errorAlert(NameSpace.fail, NameSpace.loadDataFail, actionTitle: NameSpace.check, actionType: .default) | ||
let alertAction = UIAlertAction(title: NameSpace.check, style: .default) | ||
let alert = UIAlertController.customAlert(alertTile: NameSpace.fail, alertMessage: NameSpace.loadDataFail, preferredStyle: .alert, alertActions: [alertAction]) | ||
|
||
self.collectionView.refreshControl?.endRefreshing() | ||
self.activityIndicator.stopAnimating() | ||
|
@@ -117,32 +144,87 @@ extension BoxOfficeViewController { | |
return | ||
} | ||
} | ||
|
||
@objc private func didTapChangeViewModeButton() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LGTM :) |
||
let action: UIAlertAction = { | ||
switch collectionViewMode { | ||
case .list: | ||
return UIAlertAction(title: NameSpace.icon, style: .default) { [weak self] _ in | ||
guard let self else { | ||
return | ||
} | ||
|
||
self.collectionViewMode = .grid | ||
self.setupCollectionViewLayout() | ||
self.applyReloadSnapshot() | ||
} | ||
case .grid: | ||
return UIAlertAction(title: NameSpace.list, style: .default) { [weak self] _ in | ||
guard let self else { | ||
return | ||
} | ||
|
||
self.collectionViewMode = .list | ||
self.setupCollectionViewLayout() | ||
self.applyReloadSnapshot() | ||
} | ||
} | ||
}() | ||
|
||
let cancelAction = UIAlertAction(title: NameSpace.cancel, style: .cancel) | ||
let changeModeAlert = UIAlertController.customAlert(alertTile: NameSpace.selectMode, alertMessage: nil, preferredStyle: .actionSheet, alertActions: [action, cancelAction]) | ||
|
||
present(changeModeAlert, animated: true) | ||
} | ||
} | ||
|
||
// MARK: CollectionView Layout | ||
extension BoxOfficeViewController { | ||
private func verticalLayout() -> UICollectionViewCompositionalLayout { | ||
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .fractionalHeight(0.1)) | ||
private func listLayout() -> UICollectionViewCompositionalLayout { | ||
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .estimated(80)) | ||
let item = NSCollectionLayoutItem(layoutSize: itemSize) | ||
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .fractionalHeight(1.2)) | ||
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .estimated(80)) | ||
let group = NSCollectionLayoutGroup.vertical(layoutSize: groupSize, subitems: [item]) | ||
let section = NSCollectionLayoutSection(group: group) | ||
|
||
return UICollectionViewCompositionalLayout(section: section) | ||
} | ||
|
||
private func gridLayout() -> UICollectionViewCompositionalLayout { | ||
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.5), heightDimension: .fractionalHeight(1.0)) | ||
let item = NSCollectionLayoutItem(layoutSize: itemSize) | ||
item.contentInsets = NSDirectionalEdgeInsets(top: 8, leading: 8, bottom: 8, trailing: 8) | ||
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .fractionalWidth(0.5)) | ||
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item]) | ||
group.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 8, bottom: 0, trailing: 8) | ||
let section = NSCollectionLayoutSection(group: group) | ||
|
||
return UICollectionViewCompositionalLayout(section: section) | ||
} | ||
} | ||
|
||
// MARK: CollectionView DataSource | ||
extension BoxOfficeViewController { | ||
private func setupDataSource() { | ||
dailyBoxOfficeDataSource = UICollectionViewDiffableDataSource<Section, DailyBoxOffice>(collectionView: collectionView) { collectionView, indexPath, dailyBoxOffice in | ||
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: BoxOfficeCollectionViewCell.identifier, for: indexPath) as? BoxOfficeCollectionViewCell else { | ||
return UICollectionViewCell() | ||
switch self.collectionViewMode { | ||
case .list: | ||
guard let listCell = collectionView.dequeueReusableCell(withReuseIdentifier: BoxOfficeCollectionViewListCell.identifier, for: indexPath) as? BoxOfficeCollectionViewListCell else { | ||
return UICollectionViewCell() | ||
} | ||
|
||
listCell.setupLabels(dailyBoxOffice) | ||
|
||
return listCell | ||
case .grid: | ||
guard let gridCell = collectionView.dequeueReusableCell(withReuseIdentifier: BoxOfficeCollectionViewGridCell.identifier, for: indexPath) as? BoxOfficeCollectionViewGridCell else { | ||
return UICollectionViewCell() | ||
} | ||
|
||
gridCell.setupLabels(dailyBoxOffice) | ||
|
||
return gridCell | ||
} | ||
|
||
cell.setupLabels(dailyBoxOffice) | ||
|
||
return cell | ||
} | ||
} | ||
} | ||
|
@@ -167,6 +249,15 @@ extension BoxOfficeViewController { | |
|
||
dailyBoxOfficeDataSource.apply(snapshot, animatingDifferences: true) | ||
} | ||
|
||
private func applyReloadSnapshot() { | ||
var snapshot = NSDiffableDataSourceSnapshot<Section, DailyBoxOffice>() | ||
snapshot.appendSections([.main]) | ||
snapshot.appendItems(boxOfficeManager.dailyBoxOffices, toSection: .main) | ||
snapshot.reloadSections([.main]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. snapshot.reloadSections([.main]) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
dailyBoxOfficeDataSource.apply(snapshot, animatingDifferences: true) | ||
} | ||
} | ||
|
||
// MARK: CalendarViewControllerDelegate | ||
|
@@ -227,6 +318,11 @@ extension BoxOfficeViewController { | |
static let fail = "실패" | ||
static let loadDataFail = "데이터 로드에 실패했습니다." | ||
static let check = "확인" | ||
static let changeMode = "화면 모드 변경" | ||
static let icon = "아이콘" | ||
static let list = "리스트" | ||
static let cancel = "취소" | ||
static let selectMode = "화면모드선택" | ||
} | ||
} | ||
|
||
|
@@ -236,3 +332,11 @@ extension BoxOfficeViewController { | |
case main | ||
} | ||
} | ||
|
||
// MARK: CollectionView Mode | ||
extension BoxOfficeViewController { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 주입을 받을게 아니라면, private이여도 될 것 같네요. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 주입을 받는 방식으로 바꿔줬기 때문에 |
||
private enum CollectionViewMode { | ||
case list | ||
case grid | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
주입을 받아보는건 어떨까요 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
주입시켜줬습니다:)