Skip to content

Commit

Permalink
[Feat] UICollectionViewDataSource 뷰모델로 분리 (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
seuriseuljjeok committed May 31, 2024
1 parent ae81c15 commit a902919
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class LoginViewController: UIViewController {

var isActivate: Bool = false

var nickname: String = ""
var nickname: String? = nil

private let loginViewModel: LoginViewModel = LoginViewModel()

Expand Down Expand Up @@ -81,7 +81,6 @@ private extension LoginViewController {
findIdLabel.snp.makeConstraints {
$0.top.equalTo(loginView.snp.bottom).offset(30)
$0.leading.equalToSuperview().inset(85)
// $0.trailing.equalToSuperview().inset(225)
}

divider.snp.makeConstraints {
Expand All @@ -94,7 +93,6 @@ private extension LoginViewController {
findPwLabel.snp.makeConstraints {
$0.top.equalTo(loginView.snp.bottom).offset(30)
$0.leading.equalTo(divider.snp.trailing).offset(35)
// $0.width.equalTo(ScreenUtils.getWidth(75))
}

messageLabel.snp.makeConstraints {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ private extension MainViewController {

func setDelegate() {
mainCollectionView.delegate = self
mainCollectionView.dataSource = self
mainCollectionView.dataSource = mainViewModel
}

func registerCell() {
Expand Down Expand Up @@ -380,6 +380,21 @@ extension MainViewController: MainPosterDelegate {

extension MainViewController: UICollectionViewDelegate {

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
switch cell {
case let mainPosterCell as MainPosterCell:
mainPosterCell.delegate = self
default:
break
}
}

func collectionView(_ collectionView: UICollectionView, willDisplaySupplementaryView view: UICollectionReusableView, forElementKind elementKind: String, at indexPath: IndexPath) {
if let footer = view as? PageControlButtonView {
footer.delegate = self
}
}

func scrollViewDidScroll(_ scrollView: UIScrollView) {
// contentOffset.y: 손가락을 위로 올리면 + 값, 손가락을 아래로 내리면 - 값
let topPadding = scrollView.safeAreaInsets.top
Expand All @@ -401,90 +416,3 @@ extension MainViewController: UICollectionViewDelegate {
}
}
}

extension MainViewController: UICollectionViewDataSource {

func numberOfSections(in collectionView: UICollectionView) -> Int {
return mainViewModel.dataSource.count
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
switch self.mainViewModel.dataSource[section] {
case .mainPoster:
return 1
case .recommendedContents:
return mainViewModel.fetchRecommendedData().count
case .popularLiveChannel:
return mainViewModel.fetchPopularData().count
case .paramounts:
return mainViewModel.fetchParamountsData().count
case .categories:
return mainViewModel.fetchCategoryData().count
}
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

switch self.mainViewModel.dataSource[indexPath.section] {
case .mainPoster:
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MainPosterCell.identifier, for: indexPath)
as? MainPosterCell else { return UICollectionViewCell() }
cell.setPageVC(imageData: mainViewModel.fetchMainData())
cell.delegate = self
return cell

case .recommendedContents, .paramounts:
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ImageWithTitleCell.identifier, for: indexPath)
as? ImageWithTitleCell else { return UICollectionViewCell() }
let data = mainViewModel.dataSource[indexPath.section] == .recommendedContents
? mainViewModel.fetchRecommendedData()
: mainViewModel.fetchParamountsData()
cell.setCell(contents: data[indexPath.row])
return cell

case .popularLiveChannel:
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PopularLiveCell.identifier, for: indexPath)
as? PopularLiveCell else { return UICollectionViewCell() }
cell.setCell(contents: mainViewModel.fetchPopularData()[indexPath.row])
return cell

case .categories:
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ImageOnlyCell.identifier, for: indexPath)
as? ImageOnlyCell else { return UICollectionViewCell() }
cell.setCell(contents: mainViewModel.fetchCategoryData()[indexPath.row])
return cell
}

}

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

if kind == BasicHeaderView.elementKinds {
guard let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: BasicHeaderView.identifier, for: indexPath)
as? BasicHeaderView else { return UICollectionReusableView() }

switch self.mainViewModel.dataSource[indexPath.section] {
case .mainPoster, .categories:
return header
case .recommendedContents:
header.bindTitle(headerTitle: "티빙에서 꼭 봐야하는 콘텐츠")
case .popularLiveChannel:
header.bindTitle(headerTitle: "인기 LIVE 채널")
case .paramounts:
header.bindTitle(headerTitle: "1화 무료! 파라마운트+ 인기 시리즈")
}
return header
} else if kind == PageControlButtonView.elementKinds {
guard let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: PageControlButtonView.identifier, for: indexPath)
as? PageControlButtonView else { return UICollectionReusableView() }

footer.buttonCount = mainViewModel.fetchMainData().count
footer.delegate = self
return footer
} else {
return UICollectionReusableView()
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import UIKit

final class MainViewModel {
final class MainViewModel: NSObject {

// MARK: - Properties

Expand Down Expand Up @@ -45,30 +45,6 @@ final class MainViewModel {

extension MainViewModel {

func fetchMainData() -> [Contents] {
return self.mainData
}

func fetchRecommendedData() -> [Contents] {
return self.recommendedData
}

func fetchParamountsData() -> [Contents] {
return self.paramountsData
}

func fetchCategoryData() -> [Contents] {
return self.categoryData
}

func fetchPopularData() -> [Contents] {
return self.popularData
}

func fetchDailyBoxOfficeData() -> [DailyBoxOfficeList] {
return self.dailyBoxOfficeData
}

func getMovieInfo() {
let currentDate = calculateDate()

Expand Down Expand Up @@ -139,3 +115,89 @@ extension MainViewModel {
}
}
}

extension MainViewModel: UICollectionViewDataSource {

func numberOfSections(in collectionView: UICollectionView) -> Int {
return dataSource.count
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
switch self.dataSource[section] {
case .mainPoster:
return 1
case .recommendedContents:
return recommendedData.count
case .popularLiveChannel:
return popularData.count
case .paramounts:
return paramountsData.count
case .categories:
return categoryData.count
}
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

switch self.dataSource[indexPath.section] {
case .mainPoster:
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MainPosterCell.identifier, for: indexPath)
as? MainPosterCell else { return UICollectionViewCell() }
cell.setPageVC(imageData: mainData)
return cell

case .recommendedContents, .paramounts:
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ImageWithTitleCell.identifier, for: indexPath)
as? ImageWithTitleCell else { return UICollectionViewCell() }
let data = dataSource[indexPath.section] == .recommendedContents
? recommendedData
: paramountsData
cell.setCell(contents: data[indexPath.row])
return cell

case .popularLiveChannel:
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PopularLiveCell.identifier, for: indexPath)
as? PopularLiveCell else { return UICollectionViewCell() }
cell.setCell(contents: popularData[indexPath.row])
return cell

case .categories:
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ImageOnlyCell.identifier, for: indexPath)
as? ImageOnlyCell else { return UICollectionViewCell() }
cell.setCell(contents: categoryData[indexPath.row])
return cell
}

}

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

if kind == BasicHeaderView.elementKinds {
guard let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: BasicHeaderView.identifier, for: indexPath)
as? BasicHeaderView else { return UICollectionReusableView() }

switch self.dataSource[indexPath.section] {
case .mainPoster, .categories:
return header
case .recommendedContents:
header.bindTitle(headerTitle: "티빙에서 꼭 봐야하는 콘텐츠")
case .popularLiveChannel:
header.bindTitle(headerTitle: "인기 LIVE 채널")
case .paramounts:
header.bindTitle(headerTitle: "1화 무료! 파라마운트+ 인기 시리즈")
}
return header
} else if kind == PageControlButtonView.elementKinds {
guard let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: PageControlButtonView.identifier, for: indexPath)
as? PageControlButtonView else { return UICollectionReusableView() }

footer.buttonCount = mainData.count
return footer
} else {
return UICollectionReusableView()
}

}

}

0 comments on commit a902919

Please sign in to comment.