-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dd60da8
commit 0789a1c
Showing
10 changed files
with
155 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
Tving_CloneProject/Tving_CloneProject/Scene/Main/ObservablePattern.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
// Observable.swift | ||
// Tving_CloneProject | ||
// | ||
// Created by 윤희슬 on 5/27/24. | ||
// | ||
|
||
import Foundation | ||
|
||
class ObservablePattern<T> { | ||
|
||
var value: T? { | ||
didSet { | ||
self.listener?(value) | ||
} | ||
} | ||
|
||
init(_ value: T?) { | ||
self.value = value | ||
} | ||
|
||
private var listener: ((T?) -> Void)? // --- c | ||
|
||
func bind(_ listener: @escaping (T?) -> Void) { | ||
listener(value) | ||
self.listener = listener | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
Tving_CloneProject/Tving_CloneProject/Scene/Main/ViewModel/MainViewModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// | ||
// MainViewModel.swift | ||
// Tving_CloneProject | ||
// | ||
// Created by 윤희슬 on 5/23/24. | ||
// | ||
|
||
import UIKit | ||
|
||
final class MainViewModel { | ||
|
||
// 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 mainData: [Contents] = [] | ||
|
||
private var recommendedData: [Contents] = [] | ||
|
||
private var popularData: [Contents] = [] | ||
|
||
private var paramountsData: [Contents] = [] | ||
|
||
private var categoryData: [Contents] = [] | ||
|
||
let dataSource: [MainSection] = MainSection.dataSource | ||
|
||
} | ||
|
||
extension MainViewModel { | ||
|
||
func fetchMainData() -> [Contents] { | ||
return self.mainData | ||
} | ||
|
||
func fetchRecommendedData() -> [Contents] { | ||
return self.recommendedData | ||
} | ||
|
||
func fetchParamountsData() -> [Contents] { | ||
return self.paramountsData | ||
} | ||
|
||
func fetchCategoryData() -> [Contents] { | ||
return self.categoryData | ||
} | ||
|
||
func fetchPopularData() -> [Contents] { | ||
return self.popularData | ||
} | ||
|
||
func getMovieInfo() { | ||
let currentDate = calculateDate() | ||
|
||
self.isLoading = true | ||
|
||
MainService.shared.getMovieList(date: currentDate) { response in | ||
switch response { | ||
case .success(let data): | ||
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], | ||
title: i.movieNm, | ||
ranking: "\(count + 1)", | ||
channelName: "tvn", | ||
rating: i.salesShare)) | ||
count+=1 | ||
} | ||
self.isLoading = false | ||
self.isSuccess = true | ||
|
||
default: | ||
return | ||
} | ||
} | ||
} | ||
|
||
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 "" | ||
} | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.