Files
SodaLive
Preview Content
Resources
Sources
Agora
App
Common
Content
All
Create
Curation
Detail
Comment
AudioContentCommentItemView.swift
AudioContentCommentListView.swift
AudioContentCommentListViewModel.swift
AudioContentListReplyView.swift
AudioContentListReplyViewModel.swift
ContentDetailCommentView.swift
GetAudioContentCommentListResponse.swift
ModifyCommentRequest.swift
RegisterAudioContentCommentRequest.swift
AudioContentDeleteDialogView.swift
AudioContentReportDialogView.swift
ContentDetailAnotherItemView.swift
ContentDetailCreatorProfileView.swift
ContentDetailInfoView.swift
ContentDetailMenuView.swift
ContentDetailMosaicView.swift
ContentDetailOtherContentView.swift
ContentDetailPlayView.swift
ContentDetailPurchaseButton.swift
ContentDetailView.swift
ContentDetailViewModel.swift
ContentOrderConfirmDialogView.swift
ContentOrderDialogView.swift
GetAudioContentDetailResponse.swift
LiveRoomDonationDialogView.swift
PutAudioContentLikeRequest.swift
Donation
Main
Modify
Order
AddAllPlaybackTrackingRequest.swift
ContentApi.swift
ContentListItemView.swift
ContentListView.swift
ContentListViewModel.swift
ContentPlayManager.swift
ContentRepository.swift
PlaybackTracking.swift
PlaybackTrackingRepository.swift
CustomView
Debug
Dialog
Explorer
Extensions
Follow
Font
FortuneWheel
IAP
ImagePicker
Keyboard
Live
Main
Message
MyPage
NavigationBar
Onboarding
Report
Settings
Shape
Splash
User
Utils
ContentView.swift
SodaLive.entitlements
SodaLive.xcworkspace
generated
.gitignore
Podfile
Podfile.lock
SodaLive-dev.entitlements
model-SodaLive-dev.json
model-SodaLive.json
sodalive-ios/SodaLive/Sources/Content/Detail/Comment/AudioContentListReplyViewModel.swift
2023-09-08 19:33:09 +09:00

198 lines
7.4 KiB
Swift

//
// AudioContentListReplyViewModel.swift
// SodaLive
//
// Created by klaus on 2023/08/13.
//
import Foundation
import Combine
final class AudioContentListReplyViewModel: ObservableObject {
private let repository = ContentRepository()
private var subscription = Set<AnyCancellable>()
@Published var isLoading = false
@Published var errorMessage = ""
@Published var isShowPopup = false
@Published var comment = ""
@Published var totalCommentCount = 0
@Published var commentList = [GetAudioContentCommentListItem]()
var audioContentId = 0
var commentId = 0
var page = 1
var isLast = false
private let pageSize = 10
func getCommentList() {
if (!isLast && !isLoading) {
repository
.getAudioContentCommentReplyList(commentId: commentId, page: page, size: pageSize)
.sink { result in
switch result {
case .finished:
DEBUG_LOG("finish")
case .failure(let error):
ERROR_LOG(error.localizedDescription)
}
} receiveValue: { [unowned self] response in
let responseData = response.data
do {
let jsonDecoder = JSONDecoder()
let decoded = try jsonDecoder.decode(ApiResponse<GetAudioContentCommentListResponse>.self, from: responseData)
if let data = decoded.data, decoded.success {
if page == 1 {
commentList.removeAll()
}
if !data.items.isEmpty {
page += 1
self.totalCommentCount = data.totalCount
self.commentList.append(contentsOf: data.items)
} else {
isLast = true
}
} 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 registerComment() {
if comment.trimmingCharacters(in: .whitespaces).isEmpty {
return
}
isLoading = true
repository.registerComment(audioContentId: audioContentId, comment: comment, parentId: commentId)
.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)
if decoded.success {
self.comment = ""
self.page = 1
self.isLast = false
self.getCommentList()
} 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)
}
func modifyComment(
commentId: Int,
audioContentId: Int,
comment: String? = nil,
isActive: Bool? = nil
) {
if comment == nil && isActive == nil {
errorMessage = "변경사항이 없습니다."
isShowPopup = true
return
}
if let comment = comment, comment.trimmingCharacters(in: .whitespaces).isEmpty {
errorMessage = "내용을 입력하세요."
isShowPopup = true
return
}
isLoading = true
var request = ModifyCommentRequest(commentId: commentId)
if let comment = comment {
request.comment = comment
}
if let isActive = isActive {
request.isActive = isActive
}
repository.modifyComment(request: request)
.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)
if decoded.success {
self.comment = ""
self.page = 1
self.isLast = false
self.commentList.removeAll()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
self.getCommentList()
}
} else {
if let message = decoded.message {
self.errorMessage = message
} else {
self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
}
self.isShowPopup = true
}
} catch {
self.isLoading = false
self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
self.isShowPopup = true
}
}
.store(in: &subscription)
}
}