feat(character-detail): 상세 화면 도입 및 네비게이션/API 연동
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
//
|
||||
// CharacterDetailViewModel.swift
|
||||
// SodaLive
|
||||
//
|
||||
// Created by klaus on 9/1/25.
|
||||
//
|
||||
import Foundation
|
||||
import Combine
|
||||
import Moya
|
||||
|
||||
final class CharacterDetailViewModel: ObservableObject {
|
||||
// MARK: - Published State
|
||||
@Published private(set) var characterDetail: CharacterDetailResponse?
|
||||
|
||||
@Published var isLoading: Bool = false
|
||||
@Published var errorMessage: String = ""
|
||||
@Published var isShowPopup = false
|
||||
@Published var characterId: Int = 0 {
|
||||
didSet {
|
||||
if characterId > 0 {
|
||||
getCharacterDetail(characterId: characterId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Private
|
||||
private let repository = CharacterDetailRepository()
|
||||
private var subscription = Set<AnyCancellable>()
|
||||
|
||||
// MARK: - Public Methods
|
||||
func getCharacterDetail(characterId: Int) {
|
||||
isLoading = true
|
||||
|
||||
repository.getCharacterDetail(characterId: characterId)
|
||||
.receive(on: DispatchQueue.main)
|
||||
.sink { result in
|
||||
switch result {
|
||||
case .finished:
|
||||
DEBUG_LOG("finish")
|
||||
case .failure(let error):
|
||||
ERROR_LOG(error.localizedDescription)
|
||||
}
|
||||
} receiveValue: { [weak self] response in
|
||||
let responseData = response.data
|
||||
self?.isLoading = false
|
||||
|
||||
do {
|
||||
let jsonDecoder = JSONDecoder()
|
||||
let decoded = try jsonDecoder.decode(ApiResponse<CharacterDetailResponse>.self, from: responseData)
|
||||
|
||||
if let data = decoded.data, decoded.success {
|
||||
self?.characterDetail = data
|
||||
} else {
|
||||
if let message = decoded.message {
|
||||
self?.errorMessage = message
|
||||
} else {
|
||||
self?.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
|
||||
}
|
||||
|
||||
self?.isShowPopup = true
|
||||
}
|
||||
} catch {
|
||||
ERROR_LOG(String(describing: error))
|
||||
self?.errorMessage = "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
|
||||
self?.isShowPopup = true
|
||||
}
|
||||
}
|
||||
.store(in: &subscription)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user