Skip to content

Commit

Permalink
[Refactor] 영화 뷰모델 분리 (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
seuriseuljjeok committed May 31, 2024
1 parent 8d155ec commit a29ba14
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,57 @@
// Created by 윤희슬 on 5/31/24.
//

import Foundation
import UIKit

final class MovieViewModel: NSObject {

// MARK: - Properties

private var dailyBoxOfficeData: [DailyBoxOfficeList] = []

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

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

}

extension MovieViewModel {

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

self.didChangeLoadingIndicator.value = true

MainService.shared.getMovieList(date: date) { response in
switch response {
case .success(let data):
guard let data = data as? GetMovieResponseModel else { return }
self.dailyBoxOfficeData = data.boxOfficeResult.dailyBoxOfficeList

self.didChangeLoadingIndicator.value = false
self.didUpdateNetworkResult.value = true

default:
self.didUpdateNetworkResult.value = false
return
}
}
guard let networkResult = self.didUpdateNetworkResult.value else { return false}
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 @@ -21,7 +21,7 @@ class MovieView: UIView {

// MARK: - Properties

private var mainViewModel: MainViewModel = MainViewModel()
private var movieViewModel: MovieViewModel = MovieViewModel()


// MARK: - Life Cycles
Expand All @@ -35,7 +35,7 @@ class MovieView: UIView {
setDelegate()
registerCell()
setViewModel()
mainViewModel.getDailyBoxOffice()
getDailyBoxOffice()
}

required init?(coder: NSCoder) {
Expand Down Expand Up @@ -71,19 +71,28 @@ private extension MovieView {
}

func setViewModel() {
mainViewModel.didUpdateNetworkResult = { [weak self] _ in
DispatchQueue.main.async {
movieViewModel.didUpdateNetworkResult.bind { [weak self] isSuccess in
guard let isSuccess else { return }
if isSuccess {
self?.dailyBoxOfficeCollectionView.reloadData()
}
}

mainViewModel.didChangeLoadingIndicator = { [weak self] isLoading in
movieViewModel.didChangeLoadingIndicator.bind { [weak self] isLoading in
guard let isLoading else { return }
if isLoading {
self?.loadingIndicator.startAnimating()
} else {
self?.loadingIndicator.stopAnimating()
}
}

}

func getDailyBoxOffice() {
if movieViewModel.getDailyBoxOffice() {
self.dailyBoxOfficeCollectionView.reloadData()
}
}

func registerCell() {
Expand All @@ -92,9 +101,9 @@ private extension MovieView {

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

}

extension MovieView: UICollectionViewDelegateFlowLayout {
Expand All @@ -117,23 +126,3 @@ extension MovieView: UICollectionViewDelegateFlowLayout {

}

extension MovieView: UICollectionViewDataSource {

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

return mainViewModel.fetchDailyBoxOfficeData().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: mainViewModel.fetchDailyBoxOfficeData()[indexPath.row])

return cell
}




}

0 comments on commit a29ba14

Please sign in to comment.