feat(character-detail) 대화하기 버튼 액션 추가

- 채팅방 생성 API 호출
This commit is contained in:
Yu Sung
2025-09-04 01:31:36 +09:00
parent 154ca01a74
commit 96cabbc6a7
6 changed files with 55 additions and 3 deletions

View File

@@ -340,7 +340,12 @@ extension CharacterDetailView {
.cornerRadius(16)
.padding(.horizontal, 24)
.onTapGesture {
// TODO:
viewModel.createChatRoom {
AppState.shared
.setAppStep(
step: .chatRoom(id: $0)
)
}
}
}
}

View File

@@ -67,4 +67,44 @@ final class CharacterDetailViewModel: ObservableObject {
}
.store(in: &subscription)
}
func createChatRoom(onSuccess: @escaping (Int) -> Void) {
isLoading = true
repository.createChatRoom(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<CreateChatRoomResponse>.self, from: responseData)
if let data = decoded.data, decoded.success {
onSuccess(data.chatRoomId)
} 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)
}
}

View File

@@ -15,6 +15,8 @@ struct ChatRoomView: View {
@AppStorage("can") private var can: Int = UserDefaults.int(forKey: .can)
let roomId: Int
var body: some View {
BaseView(isLoading: $viewModel.isLoading) {
ChatRoomBgView()
@@ -207,5 +209,5 @@ struct ChatRoomBgView: View {
}
#Preview {
ChatRoomView()
ChatRoomView(roomId: 0)
}

View File

@@ -6,5 +6,5 @@
//
struct CreateChatRoomResponse: Decodable {
let chatRoomId: Int64
let chatRoomId: Int
}