Skip to content

Commit

Permalink
[Refactor] UICollectionViewDataSource 뷰/뷰컨에서 처리하도록 수정 (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
seuriseuljjeok committed Jun 2, 2024
1 parent 5184982 commit 0237df0
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ private extension MainViewController {

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

func registerCell() {
Expand Down Expand Up @@ -386,21 +386,6 @@ 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 @@ -422,3 +407,91 @@ 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 mainViewModel.dataSource[section] {
case .mainPoster:
return 1
case .recommendedContents:
return mainViewModel.fetchData(type: .recommendedContents).count
case .popularLiveChannel:
return mainViewModel.fetchData(type: .popularLiveChannel).count
case .paramounts:
return mainViewModel.fetchData(type: .paramounts).count
case .categories:
return mainViewModel.fetchData(type: .categories).count
}
}

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

switch 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.fetchData(type: .mainPoster))
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.fetchData(type: .recommendedContents)
: mainViewModel.fetchData(type: .paramounts)
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.fetchData(type: .popularLiveChannel)[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.fetchData(type: .categories)[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 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.fetchData(type: .mainPoster).count
footer.delegate = self
return footer
} else {
return UICollectionReusableView()
}

}

}

Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,47 @@
// Created by 윤희슬 on 5/23/24.
//

import UIKit
import Foundation

final class MainViewModel: NSObject {
final class MainViewModel {

// MARK: - Properties

var didUpdateNetworkResult: ObservablePattern<Bool> = ObservablePattern(false)

var didChangeLoadingIndicator: ObservablePattern<Bool> = ObservablePattern(true)

private var mainData: [Contents] = []
private var mainData: ObservablePattern<[Contents]> = ObservablePattern.init([])

private var recommendedData: [Contents] = []
private var recommendedData: ObservablePattern<[Contents]> = ObservablePattern.init([])

private var popularData: [Contents] = []
private var popularData: ObservablePattern<[Contents]> = ObservablePattern.init([])

private var paramountsData: [Contents] = []
private var paramountsData: ObservablePattern<[Contents]> = ObservablePattern.init([])

private var categoryData: [Contents] = []
private var categoryData: ObservablePattern<[Contents]> = ObservablePattern.init([])

let dataSource: [MainSection] = MainSection.dataSource

}

extension MainViewModel {

func fetchData(type: MainSection) -> [Contents] {
switch type {
case .mainPoster:
return self.mainData.value ?? []
case .recommendedContents:
return self.recommendedData.value ?? []
case .popularLiveChannel:
return self.popularData.value ?? []
case .paramounts:
return self.paramountsData.value ?? []
case .categories:
return self.categoryData.value ?? []
}
}

func getMovieInfo() -> Bool {
let currentDate = String.calculateDate()

Expand All @@ -42,11 +57,11 @@ extension MainViewModel {
guard let data = data as? GetMovieResponseModel else { return }
var count = 0
for i in data.boxOfficeResult.dailyBoxOfficeList {
self.mainData.append(Contents(image: Contents.posterImages[count]))
self.recommendedData.append(Contents(image: Contents.posterImages[count], title: i.movieNm))
self.paramountsData.append(Contents(image: Contents.posterImages[count], title: i.movieNm))
self.categoryData.append(Contents(image: Contents.categoryImages[count]))
self.popularData.append(Contents(image: Contents.posterImages[count],
self.mainData.value?.append(Contents(image: Contents.posterImages[count]))
self.recommendedData.value?.append(Contents(image: Contents.posterImages[count], title: i.movieNm))
self.paramountsData.value?.append(Contents(image: Contents.posterImages[count], title: i.movieNm))
self.categoryData.value?.append(Contents(image: Contents.categoryImages[count]))
self.popularData.value?.append(Contents(image: Contents.posterImages[count],
title: i.movieNm,
ranking: "\(count + 1)",
channelName: "tvn",
Expand All @@ -66,89 +81,3 @@ 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()
}

}

}

Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
// Created by 윤희슬 on 5/31/24.
//

import UIKit
import Foundation

final class MovieViewModel: NSObject {
final class MovieViewModel {

// MARK: - Properties

private var dailyBoxOfficeData: [DailyBoxOfficeList] = []
private var dailyBoxOfficeData: ObservablePattern<[DailyBoxOfficeList]> = ObservablePattern([])

var didUpdateNetworkResult: ObservablePattern<Bool> = ObservablePattern(false)

Expand All @@ -21,6 +21,10 @@ final class MovieViewModel: NSObject {

extension MovieViewModel {

func fetchData() -> [DailyBoxOfficeList] {
return self.dailyBoxOfficeData.value ?? []
}

func getDailyBoxOffice() -> Bool {
let date = String.calculateDate()

Expand All @@ -30,7 +34,7 @@ extension MovieViewModel {
switch response {
case .success(let data):
guard let data = data as? GetMovieResponseModel else { return }
self.dailyBoxOfficeData = data.boxOfficeResult.dailyBoxOfficeList
self.dailyBoxOfficeData.value = data.boxOfficeResult.dailyBoxOfficeList

self.didChangeLoadingIndicator.value = false
self.didUpdateNetworkResult.value = true
Expand All @@ -44,18 +48,3 @@ extension MovieViewModel {
return networkResult
}
}

extension MovieViewModel: UICollectionViewDataSource {

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return dailyBoxOfficeData.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: DailyBoxOfficeCell.identifier, for: indexPath) as? DailyBoxOfficeCell else { return UICollectionViewCell() }
cell.setCell(contents: dailyBoxOfficeData[indexPath.row])

return cell
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private extension MovieView {

func setDelegate() {
dailyBoxOfficeCollectionView.delegate = self
dailyBoxOfficeCollectionView.dataSource = movieViewModel
dailyBoxOfficeCollectionView.dataSource = self
}

}
Expand All @@ -126,3 +126,17 @@ extension MovieView: UICollectionViewDelegateFlowLayout {

}

extension MovieView: UICollectionViewDataSource {

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return movieViewModel.fetchData().count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: DailyBoxOfficeCell.identifier, for: indexPath) as? DailyBoxOfficeCell else { return UICollectionViewCell() }
cell.setCell(contents: movieViewModel.fetchData()[indexPath.row])

return cell
}

}

0 comments on commit 0237df0

Please sign in to comment.