Building Netflix App in Swift 5 and UIKit - (Xcode 13, 2021) - Episode 6 - Networking/Extensions
private func addSubcription() {
APICaller
.shared
.getTrendingContents(with: .movie)
.sink { completion in
switch completion {
case.finished: break
case .failure(let error): print(error.localizedDescription)
}
} receiveValue: { [weak self] movieModels in
self?.trendingMovieModels.send(movieModels)
}
.store(in: &cancellabels)
APICaller
.shared
.getTrendingContents(with: .tv)
.sink { completion in
switch completion {
case.finished: break
case .failure(let error): print(error.localizedDescription)
}
} receiveValue: { [weak self] tvModels in
self?.trendingTVModels.send(tvModels)
}
.store(in: &cancellabels)
APICaller
.shared
.getPopularContents(with: .movie)
.sink { completion in
switch completion {
case.finished: break
case .failure(let error): print(error.localizedDescription)
}
} receiveValue: { [weak self] movieModels in
self?.popularMovieModels.send(movieModels)
}
.store(in: &cancellabels)
APICaller
.shared
.getUpcomingContents(with: .movie)
.sink { completion in
switch completion {
case.finished: break
case .failure(let error): print(error.localizedDescription)
}
} receiveValue: { [weak self] movieModels in
self?.upcomingMovieModels.send(movieModels)
}
.store(in: &cancellabels)
APICaller
.shared
.getTopRatedContents(with: .movie)
.sink { completion in
switch completion {
case.finished: break
case .failure(let error): print(error.localizedDescription)
}
} receiveValue: { [weak self] movieModels in
self?.topRatedMovieModels.send(movieModels)
}
.store(in: &cancellabels)
}
private func bind() {
viewModel
.trendingMovieModels
.receive(on: DispatchQueue.main)
.sink { [weak self] movieModels in
self?.setHeaderView()
self?.tableView.reloadData()
}
.store(in: &cancellables)
viewModel
.trendingTVModels
.receive(on: DispatchQueue.main)
.sink { [weak self] tvModels in
self?.tableView.reloadData()
}
.store(in: &cancellables)
viewModel
.popularMovieModels
.receive(on: DispatchQueue.main)
.sink { [weak self] movieModels in
self?.tableView.reloadData()
}
.store(in: &cancellables)
viewModel
.upcomingMovieModels
.receive(on: DispatchQueue.main)
.sink { [weak self] movieModels in
self?.tableView.reloadData()
}
.store(in: &cancellables)
viewModel
.topRatedMovieModels
.receive(on: DispatchQueue.main)
.sink { [weak self] movieModels in
self?.tableView.reloadData()
}
.store(in: &cancellables)
}
receive
및 바인딩func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: HomeTableViewCell.identifier, for: indexPath) as? HomeTableViewCell else {
return UITableViewCell()
}
switch indexPath.section {
case 0:
let movieModel = viewModel.trendingMovieModels.value
cell.configure(with: movieModel)
case 1:
let tvModel = viewModel.trendingTVModels.value
cell.configure(with: tvModel)
case 2:
let movieModel = viewModel.popularMovieModels.value
cell.configure(with: movieModel)
case 3:
let movieModel = viewModel.upcomingMovieModels.value
cell.configure(with: movieModel)
case 4:
let movieModel = viewModel.topRatedMovieModels.value
cell.configure(with: movieModel)
default: break
}
return cell
}
indexPath
의 section
값을 통해 어떤 종류의 테이블 셀을 그릴지 구체적으로 표현 가능섹션 자체를 이차원 배열로 선언할 수 있을 듯하다. 이 경우 병렬 네트워킹을 통한 다운로드가 필요한데, 이는 이후 차차 생각해보자.