재생목록 만들기 페이지 추가
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
//
|
||||
// ContentPlaylistCreateViewModel.swift
|
||||
// SodaLive
|
||||
//
|
||||
// Created by klaus on 12/7/24.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Combine
|
||||
|
||||
final class ContentPlaylistCreateViewModel: ObservableObject {
|
||||
private let repository = ContentPlaylistListRepository()
|
||||
private var subscription = Set<AnyCancellable>()
|
||||
|
||||
@Published var isLoading = false
|
||||
@Published var errorMessage = ""
|
||||
@Published var isShowPopup = false
|
||||
|
||||
@Published var title: String = ""
|
||||
@Published var desc: String = ""
|
||||
var contentList = [AudioContentPlaylistContent]()
|
||||
|
||||
func savePlaylist(onSuccess: @escaping () -> Void) {
|
||||
if (validate()) {
|
||||
isLoading = true
|
||||
|
||||
let contentIdAndOrderList = contentList.mapIndexed { index, item in
|
||||
PlaylistContentIdAndOrder(contentId: item.id, order: index + 1)
|
||||
}
|
||||
|
||||
repository.createPlaylist(
|
||||
request: CreatePlaylistRequest(
|
||||
title: title,
|
||||
desc: desc,
|
||||
contentIdAndOrderList: contentIdAndOrderList
|
||||
)
|
||||
)
|
||||
.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(ApiResponseWithoutData.self, from: responseData)
|
||||
self.isLoading = false
|
||||
|
||||
if decoded.success {
|
||||
onSuccess()
|
||||
} 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)
|
||||
}
|
||||
}
|
||||
|
||||
private func validate() -> Bool {
|
||||
if (title.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty || title.count < 3) {
|
||||
errorMessage = "제목을 3자 이상 입력하세요"
|
||||
isShowPopup = true
|
||||
return false
|
||||
}
|
||||
|
||||
if (contentList.isEmpty) {
|
||||
errorMessage = "콘텐츠를 1개 이상 추가하세요"
|
||||
isShowPopup = true
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user