[UIKit] NetflixClone: Networking & Extensions

Junyoung Park·2022년 10월 30일
0

UIKit

목록 보기
69/142
post-thumbnail

Building Netflix App in Swift 5 and UIKit - (Xcode 13, 2021) - Episode 6 - Networking/Extensions

NetflixClone: Networking & Extensions

구현 목표

  • 섹션 별 API 호출을 통한 UI 패치

구현 태스크

  • API 호출 모듈화
  • 테이블 뷰 섹션 별 스위치 문을 통한 셀 바인딩 모듈화

핵심 코드

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)
    }
  • 뷰 모델에서 API 데이터 서비스 클래스의 함수를 통해 컨텐츠 데이터를 백그라운드 스레드를 통해 다운로드받은 부분
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
    }
  • indexPathsection 값을 통해 어떤 종류의 테이블 셀을 그릴지 구체적으로 표현 가능

구현 화면

섹션 자체를 이차원 배열로 선언할 수 있을 듯하다. 이 경우 병렬 네트워킹을 통한 다운로드가 필요한데, 이는 이후 차차 생각해보자.

profile
JUST DO IT

0개의 댓글