-
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] yetti, maxhyunm #111
base: ic_9_yetti
Are you sure you want to change the base?
Changes from all commits
a5970fc
569af41
b017a9f
4d95f8c
f9c52ea
3e22b97
bfa29d9
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 |
---|---|---|
|
@@ -12,13 +12,14 @@ final class BoxOfficeViewController: UIViewController, URLSessionDelegate { | |
private var refreshControl = UIRefreshControl() | ||
private var dataSource: UICollectionViewDiffableDataSource<NetworkConfiguration, BoxOfficeEntity.BoxOfficeResult.DailyBoxOffice>? | ||
private var date: Date = Calendar.current.date(byAdding: .day, value: -1, to: Date()) ?? Date() | ||
|
||
private let collectionView: UICollectionView = { | ||
let configuration = UICollectionLayoutListConfiguration(appearance: .plain) | ||
let layout = UICollectionViewCompositionalLayout.list(using: configuration) | ||
let view = UICollectionView(frame: .zero, collectionViewLayout: layout) | ||
view.translatesAutoresizingMaskIntoConstraints = false | ||
view.register(BoxOfficeRankingCell.self, forCellWithReuseIdentifier: BoxOfficeRankingCell.cellIdentifier) | ||
view.register(BoxOfficeRankingListCell.self, forCellWithReuseIdentifier: BoxOfficeRankingListCell.cellIdentifier) | ||
view.register(BoxOfficeRankingIconCell.self, forCellWithReuseIdentifier: BoxOfficeRankingIconCell.cellIdentifier) | ||
|
||
return view | ||
}() | ||
|
@@ -42,6 +43,14 @@ final class BoxOfficeViewController: UIViewController, URLSessionDelegate { | |
} | ||
} | ||
} | ||
|
||
private var isListMode = true { | ||
didSet { | ||
setUpCollectionViewLayout() | ||
setUpDataSource() | ||
passFetchedData() | ||
} | ||
} | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
|
@@ -58,11 +67,15 @@ extension BoxOfficeViewController { | |
private func setUpUI() { | ||
let safeArea = view.safeAreaLayoutGuide | ||
let dateSelectionButton = UIBarButtonItem(title: "날짜선택", style: .plain, target: self, action: #selector(showCalendar)) | ||
let modeChangeButton = UIBarButtonItem(title: "화면 모드 변경", style: .plain, target: self, action: #selector(hitChangeModeButton)) | ||
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil) | ||
|
||
view.backgroundColor = .systemBackground | ||
view.addSubview(collectionView) | ||
view.addSubview(indicatorView) | ||
|
||
self.navigationController?.isToolbarHidden = false | ||
self.toolbarItems = [flexibleSpace, modeChangeButton, flexibleSpace] | ||
self.title = getDateString(format: Namespace.dateWithHyphen) | ||
self.navigationItem.rightBarButtonItem = dateSelectionButton | ||
|
||
|
@@ -77,13 +90,45 @@ extension BoxOfficeViewController { | |
]) | ||
} | ||
|
||
private func setUpCollectionViewLayout() { | ||
if isListMode { | ||
let configuration = UICollectionLayoutListConfiguration(appearance: .plain) | ||
let layout = UICollectionViewCompositionalLayout.list(using: configuration) | ||
|
||
collectionView.collectionViewLayout = layout | ||
} else { | ||
let layout = UICollectionViewFlowLayout() | ||
let width = (view.frame.width - 45) / 2.0 | ||
|
||
layout.sectionInset = UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15) | ||
layout.minimumLineSpacing = 10 | ||
layout.minimumInteritemSpacing = 15 | ||
layout.itemSize = CGSize(width: width, height: width) | ||
|
||
collectionView.collectionViewLayout = layout | ||
} | ||
} | ||
|
||
@objc func showCalendar(_ sender: UIButton) { | ||
let viewController = CalendarViewController(date) | ||
viewController.delegate = self | ||
viewController.modalPresentationStyle = UIModalPresentationStyle.automatic | ||
|
||
self.present(viewController, animated: true) | ||
} | ||
|
||
@objc func hitChangeModeButton() { | ||
let mode: String = isListMode == true ? "아이콘" : "리스트" | ||
let alert = UIAlertController(title: "화면 모드 변경", message: nil, preferredStyle: .actionSheet) | ||
let modeChangeAction = UIAlertAction(title: mode, style: .default) { _ in | ||
self.isListMode.toggle() | ||
} | ||
let cancelAction = UIAlertAction(title: "취소", style: .cancel) | ||
|
||
alert.addAction(modeChangeAction) | ||
alert.addAction(cancelAction) | ||
present(alert, animated: true) | ||
} | ||
} | ||
|
||
extension BoxOfficeViewController { | ||
|
@@ -104,14 +149,26 @@ extension BoxOfficeViewController { | |
} | ||
|
||
private func setUpDataSource() { | ||
dataSource = UICollectionViewDiffableDataSource<NetworkConfiguration, BoxOfficeEntity.BoxOfficeResult.DailyBoxOffice>(collectionView: self.collectionView) { (collectionView, indexPath, data) -> UICollectionViewCell? in | ||
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: BoxOfficeRankingCell.cellIdentifier, for: indexPath) as? BoxOfficeRankingCell else { | ||
return UICollectionViewCell() | ||
if isListMode { | ||
dataSource = UICollectionViewDiffableDataSource<NetworkConfiguration, BoxOfficeEntity.BoxOfficeResult.DailyBoxOffice>(collectionView: self.collectionView) { (collectionView, indexPath, data) -> UICollectionViewCell? in | ||
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: BoxOfficeRankingListCell.cellIdentifier, for: indexPath) as? BoxOfficeRankingListCell else { | ||
return BoxOfficeRankingListCell() | ||
} | ||
|
||
cell.setUpLabelText(data) | ||
|
||
return cell | ||
} | ||
} else { | ||
dataSource = UICollectionViewDiffableDataSource<NetworkConfiguration, BoxOfficeEntity.BoxOfficeResult.DailyBoxOffice>(collectionView: self.collectionView) { (collectionView, indexPath, data) -> UICollectionViewCell? in | ||
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: BoxOfficeRankingIconCell.cellIdentifier, for: indexPath) as? BoxOfficeRankingIconCell else { | ||
return BoxOfficeRankingIconCell() | ||
} | ||
|
||
cell.setUpLabelText(data) | ||
|
||
return cell | ||
} | ||
Comment on lines
+152
to
171
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. isListMode 분기를 데이터소스 클로져 안에서 진행하는 편이 코드 가독성 등 더 깔끔할 것 같습니다 |
||
|
||
cell.setUpLabelText(data) | ||
|
||
return cell | ||
} | ||
} | ||
|
||
|
@@ -186,6 +243,9 @@ extension BoxOfficeViewController: BoxOfficeDelegate { | |
func setUpDate(_ date: Date) { | ||
self.date = date | ||
self.title = getDateString(format: Namespace.dateWithHyphen) | ||
|
||
setUpCollectionViewLayout() | ||
setUpDataSource() | ||
Comment on lines
243
to
+248
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. 질문 남겨주신 사항 중 여기서 두 메서드 다시 호출하는 방식보다 아마 말씀해주신 셀의 |
||
passFetchedData() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,20 +15,24 @@ struct NetworkingManager { | |
} | ||
|
||
func load(_ networkType: NetworkConfiguration, completion: @escaping (Result<Data, NetworkingError>) -> Void) { | ||
var urlComponents = URLComponents(string: networkType.url) | ||
guard let urlString = networkType.url, let header = networkType.header else { | ||
completion(.failure(NetworkingError.invalidAPIKey)) | ||
return | ||
} | ||
Comment on lines
+18
to
+21
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. 👍🏼 |
||
var urlComponents = URLComponents(string: urlString) | ||
|
||
networkType.query.forEach { | ||
urlComponents?.queryItems = [URLQueryItem(name: $0.name, value: $0.value)] | ||
} | ||
|
||
guard let url = urlComponents?.url else { | ||
completion(.failure(NetworkingError.invalidURL)) | ||
return | ||
return | ||
} | ||
|
||
var request = URLRequest(url: url) | ||
networkType.header.forEach { | ||
|
||
header.forEach { | ||
request.setValue($0.value, forHTTPHeaderField: $0.forHTTPHeaderField) | ||
} | ||
|
||
|
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.
지금은 리스트, 아이콘 형태의 UI 뿐인데, 좀 다르게 생긴 UI등이 더 추가된다면 어떨까요?
isListMode
라는 불 타입의 변수로 관리하는 것 말고 다른 좋은 방법이 있을 것 같아요.