Skip to content

Commit

Permalink
[Refactor] Observable Pattern 적용 (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
seuriseuljjeok committed May 31, 2024
1 parent a29ba14 commit 6adcb5a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ final class MainViewController: UIViewController {
setDelegate()
registerCell()
setViewModel()
mainViewModel.getMovieInfo()
getMovieInfo()
setSegmentDidChange()
}

Expand Down Expand Up @@ -166,23 +166,29 @@ private extension MainViewController {
}

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

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

func getMovieInfo() {
if mainViewModel.getMovieInfo() {
self.mainCollectionView.reloadData()
}
}

func setDelegate() {
mainCollectionView.delegate = self
mainCollectionView.dataSource = mainViewModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ final class MainViewModel: NSObject {

// MARK: - Properties

private var isSuccess: Bool = false {
didSet {
self.didUpdateNetworkResult?(isSuccess)
}
}

private var isLoading: Bool = true {
didSet {
self.didChangeLoadingIndicator?(isLoading)
}
}

var didUpdateNetworkResult: ((Bool) -> Void)?

var didChangeLoadingIndicator: ((Bool) -> Void)?
// private var isSuccess: Bool = false {
// didSet {
// self.didUpdateNetworkResult?(isSuccess)
// }
// }
//
// private var isLoading: Bool = true {
// didSet {
// self.didChangeLoadingIndicator?(isLoading)
// }
// }

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

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

private var mainData: [Contents] = []

Expand All @@ -36,19 +36,17 @@ final class MainViewModel: NSObject {
private var paramountsData: [Contents] = []

private var categoryData: [Contents] = []

private var dailyBoxOfficeData: [DailyBoxOfficeList] = []


let dataSource: [MainSection] = MainSection.dataSource

}

extension MainViewModel {

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

self.isLoading = true
self.didChangeLoadingIndicator.value = true

MainService.shared.getMovieList(date: currentDate) { response in
switch response {
Expand All @@ -67,53 +65,18 @@ extension MainViewModel {
rating: i.salesShare))
count+=1
}
self.isLoading = false
self.isSuccess = true

default:
return
}
}
}

func getDailyBoxOffice() {

let date = calculateDate()

self.isLoading = 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.isLoading = false
self.isSuccess = true
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
}

func calculateDate() -> String {

let today = Date()
let calendar = Calendar.current
var dateComponents = DateComponents()
dateComponents.day = -1

if let oneDayAgo = calendar.date(byAdding: dateComponents, to: today) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyyMMdd"

let oneDayAgoString = dateFormatter.string(from: oneDayAgo)
return oneDayAgoString
} else {
return ""
}
}
}

extension MainViewModel: UICollectionViewDataSource {
Expand Down

0 comments on commit 6adcb5a

Please sign in to comment.