콘텐츠 메인, 콘텐츠 업로드 페이지 추가
This commit is contained in:
124
SodaLive/Sources/Content/Main/ContentMainViewModel.swift
Normal file
124
SodaLive/Sources/Content/Main/ContentMainViewModel.swift
Normal file
@@ -0,0 +1,124 @@
|
||||
//
|
||||
// ContentMainViewModel.swift
|
||||
// SodaLive
|
||||
//
|
||||
// Created by klaus on 2023/08/11.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
import Combine
|
||||
|
||||
final class ContentMainViewModel: ObservableObject {
|
||||
|
||||
private let repository = ContentRepository()
|
||||
private var subscription = Set<AnyCancellable>()
|
||||
|
||||
@Published var errorMessage = ""
|
||||
@Published var isShowPopup = false
|
||||
@Published var isLoading = false
|
||||
|
||||
@Published var newContentUploadCreatorList = [GetNewContentUploadCreator]()
|
||||
@Published var newContentList = [GetAudioContentMainItem]()
|
||||
@Published var bannerList = [GetAudioContentBannerResponse]()
|
||||
@Published var orderList = [GetAudioContentMainItem]()
|
||||
@Published var themeList = [String]()
|
||||
@Published var curationList = [GetAudioContentCurationResponse]()
|
||||
|
||||
@Published var selectedTheme = "전체" {
|
||||
didSet {
|
||||
getNewContentOfTheme()
|
||||
}
|
||||
}
|
||||
|
||||
func getMain() {
|
||||
isLoading = true
|
||||
|
||||
repository.getMain()
|
||||
.sink { result in
|
||||
switch result {
|
||||
case .finished:
|
||||
DEBUG_LOG("finish")
|
||||
case .failure(let error):
|
||||
ERROR_LOG(error.localizedDescription)
|
||||
}
|
||||
} receiveValue: { [unowned self] response in
|
||||
self.isLoading = false
|
||||
let responseData = response.data
|
||||
|
||||
do {
|
||||
let jsonDecoder = JSONDecoder()
|
||||
let decoded = try jsonDecoder.decode(ApiResponse<GetAudioContentMainResponse>.self, from: responseData)
|
||||
self.isLoading = false
|
||||
|
||||
if let data = decoded.data, decoded.success {
|
||||
self.newContentUploadCreatorList.removeAll()
|
||||
self.newContentList.removeAll()
|
||||
self.bannerList.removeAll()
|
||||
self.orderList.removeAll()
|
||||
self.curationList.removeAll()
|
||||
self.themeList.removeAll()
|
||||
|
||||
self.newContentUploadCreatorList.append(contentsOf: data.newContentUploadCreatorList)
|
||||
self.newContentList.append(contentsOf: data.newContentList)
|
||||
self.bannerList.append(contentsOf: data.bannerList)
|
||||
self.orderList.append(contentsOf: data.orderList)
|
||||
self.curationList.append(contentsOf: data.curationList)
|
||||
|
||||
self.themeList.append("전체")
|
||||
self.themeList.append(contentsOf: data.themeList)
|
||||
} else {
|
||||
if let message = decoded.message {
|
||||
self.errorMessage = message
|
||||
} else {
|
||||
self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
|
||||
}
|
||||
|
||||
self.isShowPopup = true
|
||||
}
|
||||
} catch {
|
||||
self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
|
||||
self.isShowPopup = true
|
||||
self.isLoading = false
|
||||
}
|
||||
}
|
||||
.store(in: &subscription)
|
||||
}
|
||||
|
||||
func getNewContentOfTheme() {
|
||||
repository.getNewContentOfTheme(theme: selectedTheme == "전체" ? "" : selectedTheme)
|
||||
.sink { result in
|
||||
switch result {
|
||||
case .finished:
|
||||
DEBUG_LOG("finish")
|
||||
case .failure(let error):
|
||||
ERROR_LOG(error.localizedDescription)
|
||||
}
|
||||
} receiveValue: { [unowned self] response in
|
||||
self.isLoading = false
|
||||
let responseData = response.data
|
||||
|
||||
do {
|
||||
let jsonDecoder = JSONDecoder()
|
||||
let decoded = try jsonDecoder.decode(ApiResponse<[GetAudioContentMainItem]>.self, from: responseData)
|
||||
|
||||
if let data = decoded.data, decoded.success {
|
||||
self.newContentList.removeAll()
|
||||
self.newContentList.append(contentsOf: data)
|
||||
} else {
|
||||
if let message = decoded.message {
|
||||
self.errorMessage = message
|
||||
} else {
|
||||
self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
|
||||
}
|
||||
|
||||
self.isShowPopup = true
|
||||
}
|
||||
} catch {
|
||||
self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
|
||||
self.isShowPopup = true
|
||||
}
|
||||
}
|
||||
.store(in: &subscription)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user