feat(chat-original): 원작 상세 화면 및 캐릭터 무한 스크롤 로딩 구현

This commit is contained in:
Yu Sung
2025-09-16 15:10:51 +09:00
parent 6a7a407a09
commit 16dcc9f0fe
8 changed files with 407 additions and 0 deletions

View File

@@ -0,0 +1,132 @@
//
// OriginalWorkDetailViewModel.swift
// SodaLive
//
// Created by klaus on 9/15/25.
//
import Foundation
import Combine
import Moya
final class OriginalWorkDetailViewModel: ObservableObject {
@Published var isLoading = false
@Published var errorMessage = ""
@Published var isShowPopup = false
@Published var isLoadingMore: Bool = false
@Published private(set) var characters: [Character] = []
@Published private(set) var totalCount: Int = 0
@Published private(set) var response: OriginalWorkDetailResponse? = nil
private let repository = OriginalWorkRepository()
private var subscription = Set<AnyCancellable>()
private var currentPage: Int = 0
private var hasMorePages: Bool = true
var originalId: Int = 0 {
didSet {
fetchDetail()
}
}
// MARK: - API
func loadMoreIfNeeded(currentIndex: Int) {
guard hasMorePages,
!isLoading,
!isLoadingMore,
currentIndex >= characters.count - 3 else { return }
fetchCharacters()
}
private func fetchDetail() {
isLoading = true
repository.getOriginalDetail(id: originalId)
.receive(on: DispatchQueue.main)
.sink { [weak self] completion in
switch completion {
case .finished:
DEBUG_LOG("finish")
case .failure(let error):
ERROR_LOG(error.localizedDescription)
self?.isLoading = false
self?.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
self?.isShowPopup = true
}
} receiveValue: { [weak self] response in
guard let self = self else { return }
do {
let jsonDecoder = JSONDecoder()
let decoded = try jsonDecoder.decode(ApiResponse<OriginalWorkDetailResponse>.self, from: response.data)
if let data = decoded.data, decoded.success {
self.response = data
self.characters = data.characters
self.isLoading = false
} else {
if let message = decoded.message {
self.errorMessage = message
} else {
self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
}
self.isShowPopup = true
self.isLoading = false
}
} catch {
self.isLoading = false
self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
self.isShowPopup = true
}
}
.store(in: &subscription)
}
private func fetchCharacters() {
currentPage += 1
isLoadingMore = true
repository.getOriginalWorkCharacters(id: originalId, page: currentPage)
.receive(on: DispatchQueue.main)
.sink { [weak self] completion in
switch completion {
case .finished:
DEBUG_LOG("finish")
case .failure(let error):
ERROR_LOG(error.localizedDescription)
self?.isLoadingMore = false
self?.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
self?.isShowPopup = true
}
} receiveValue: { [weak self] response in
guard let self = self else { return }
do {
let jsonDecoder = JSONDecoder()
let decoded = try jsonDecoder.decode(ApiResponse<OriginalWorkCharactersPageResponse>.self, from: response.data)
if let data = decoded.data, decoded.success {
self.totalCount = data.totalCount
if !data.content.isEmpty {
self.characters.append(contentsOf: data.content)
} else {
hasMorePages = false
}
self.isLoadingMore = false
} else {
if let message = decoded.message {
self.errorMessage = message
} else {
self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
}
self.isShowPopup = true
self.isLoadingMore = false
}
} catch {
self.isLoading = false
self.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
self.isShowPopup = true
}
}
.store(in: &subscription)
}
}