Skip to content

Commit

Permalink
[Refactor] ViewModel 분리 (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
seuriseuljjeok committed May 27, 2024
1 parent dd60da8 commit 0789a1c
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 1 deletion.
14 changes: 13 additions & 1 deletion Tving_CloneProject/Tving_CloneProject.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
CA3953202BC6F6BE00B15E91 /* LoginView.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA39531F2BC6F6BE00B15E91 /* LoginView.swift */; };
CA3953222BC6FA9F00B15E91 /* Constant.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA3953212BC6FA9F00B15E91 /* Constant.swift */; };
CA3953252BC6FB1100B15E91 /* ScreenUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA3953242BC6FB1100B15E91 /* ScreenUtils.swift */; };
CA3BA70F2BFE7E9800F77616 /* MainViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA3BA70E2BFE7E9800F77616 /* MainViewModel.swift */; };
CA8496E22BE7E36D0064E0CC /* Moya in Frameworks */ = {isa = PBXBuildFile; productRef = CA8496E12BE7E36D0064E0CC /* Moya */; };
CA8496E52BE7E54D0064E0CC /* Config.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA8496E42BE7E54D0064E0CC /* Config.swift */; };
CA8496E72BE7E8E60064E0CC /* GetMovieResponseModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA8496E62BE7E8E60064E0CC /* GetMovieResponseModel.swift */; };
Expand Down Expand Up @@ -91,6 +92,7 @@
CA39531F2BC6F6BE00B15E91 /* LoginView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginView.swift; sourceTree = "<group>"; };
CA3953212BC6FA9F00B15E91 /* Constant.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Constant.swift; sourceTree = "<group>"; };
CA3953242BC6FB1100B15E91 /* ScreenUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScreenUtils.swift; sourceTree = "<group>"; };
CA3BA70E2BFE7E9800F77616 /* MainViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainViewModel.swift; sourceTree = "<group>"; };
CA8496E42BE7E54D0064E0CC /* Config.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Config.swift; sourceTree = "<group>"; };
CA8496E62BE7E8E60064E0CC /* GetMovieResponseModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GetMovieResponseModel.swift; sourceTree = "<group>"; };
CA8496E92BE89A840064E0CC /* BaseTargetType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BaseTargetType.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -133,7 +135,7 @@
CA1F68ED2BD568F300439E33 /* Main */ = {
isa = PBXGroup;
children = (
CA1F68F92BD5884200439E33 /* Cells */,
CA3BA70D2BFE7E8700F77616 /* ViewModel */,
CA1F68F62BD57D2900439E33 /* Models */,
CA1F68F12BD5697500439E33 /* Views */,
CA1F68EE2BD5690000439E33 /* ViewControllers */,
Expand All @@ -152,6 +154,7 @@
CA1F68F12BD5697500439E33 /* Views */ = {
isa = PBXGroup;
children = (
CA1F68F92BD5884200439E33 /* Cells */,
CA1F68F22BD5698C00439E33 /* MainPosterView.swift */,
CA1F68F42BD578B500439E33 /* NavigationBarView.swift */,
CA902EBF2BD6FE1400560D26 /* BasicHeaderView.swift */,
Expand Down Expand Up @@ -344,6 +347,14 @@
path = ViewControllers;
sourceTree = "<group>";
};
CA3BA70D2BFE7E8700F77616 /* ViewModel */ = {
isa = PBXGroup;
children = (
CA3BA70E2BFE7E9800F77616 /* MainViewModel.swift */,
);
path = ViewModel;
sourceTree = "<group>";
};
CA8496DA2BE7D6550064E0CC /* Network */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -556,6 +567,7 @@
CA8496F02BE8A5880064E0CC /* MoyaPlugin.swift in Sources */,
CA8496EA2BE89A850064E0CC /* BaseTargetType.swift in Sources */,
CA39531E2BC5AFE300B15E91 /* CreateNicknameViewController.swift in Sources */,
CA3BA70F2BFE7E9800F77616 /* MainViewModel.swift in Sources */,
CA1F68F52BD578B500439E33 /* NavigationBarView.swift in Sources */,
CA8496FA2BE9482C0064E0CC /* MovieView.swift in Sources */,
CA902EC42BD9542000560D26 /* ImageOnlyCell.swift in Sources */,
Expand Down
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
}
}
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 ""
}
}
}

0 comments on commit 0789a1c

Please sign in to comment.