feat(chat-talk): 톡 목록 조회 API 연동 및 목록 UI 구성
This commit is contained in:
63
SodaLive/Sources/Chat/Talk/TalkViewModel.swift
Normal file
63
SodaLive/Sources/Chat/Talk/TalkViewModel.swift
Normal file
@@ -0,0 +1,63 @@
|
||||
//
|
||||
// TalkViewModel.swift
|
||||
// SodaLive
|
||||
//
|
||||
// Created by klaus on 8/29/25.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Combine
|
||||
import Moya
|
||||
|
||||
final class TalkViewModel: ObservableObject {
|
||||
// MARK: - Published State
|
||||
@Published private(set) var talkRooms = [TalkRoom]()
|
||||
|
||||
@Published var isLoading: Bool = false
|
||||
@Published var errorMessage: String = ""
|
||||
@Published var isShowPopup = false
|
||||
|
||||
// MARK: - Private
|
||||
private let repository = TalkRepository()
|
||||
private var subscription = Set<AnyCancellable>()
|
||||
|
||||
// MARK: - API
|
||||
func getTalkRooms() {
|
||||
isLoading = true
|
||||
|
||||
repository.getTalkRooms()
|
||||
.sink { result in
|
||||
switch result {
|
||||
case .finished:
|
||||
DEBUG_LOG("finish")
|
||||
case .failure(let error):
|
||||
ERROR_LOG(error.localizedDescription)
|
||||
}
|
||||
} receiveValue: { response in
|
||||
let responseData = response.data
|
||||
self.isLoading = false
|
||||
|
||||
do {
|
||||
let jsonDecoder = JSONDecoder()
|
||||
let decoded = try jsonDecoder.decode(ApiResponse<[TalkRoom]>.self, from: responseData)
|
||||
|
||||
if let data = decoded.data, decoded.success {
|
||||
self.talkRooms = 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