diff --git a/SodaLive/Sources/App/AppStep.swift b/SodaLive/Sources/App/AppStep.swift index 7e0eed7..6197f98 100644 --- a/SodaLive/Sources/App/AppStep.swift +++ b/SodaLive/Sources/App/AppStep.swift @@ -163,4 +163,6 @@ enum AppStep { case audition case characterDetail(characterId: Int) + + case chatRoom(id: Int) } diff --git a/SodaLive/Sources/Chat/Character/Detail/CharacterDetailView.swift b/SodaLive/Sources/Chat/Character/Detail/CharacterDetailView.swift index df121f6..338ec0a 100644 --- a/SodaLive/Sources/Chat/Character/Detail/CharacterDetailView.swift +++ b/SodaLive/Sources/Chat/Character/Detail/CharacterDetailView.swift @@ -340,7 +340,12 @@ extension CharacterDetailView { .cornerRadius(16) .padding(.horizontal, 24) .onTapGesture { - // TODO: 대화하기 액션 + viewModel.createChatRoom { + AppState.shared + .setAppStep( + step: .chatRoom(id: $0) + ) + } } } } diff --git a/SodaLive/Sources/Chat/Character/Detail/CharacterDetailViewModel.swift b/SodaLive/Sources/Chat/Character/Detail/CharacterDetailViewModel.swift index 0396007..fb4321c 100644 --- a/SodaLive/Sources/Chat/Character/Detail/CharacterDetailViewModel.swift +++ b/SodaLive/Sources/Chat/Character/Detail/CharacterDetailViewModel.swift @@ -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.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) + } } diff --git a/SodaLive/Sources/Chat/Talk/Room/ChatRoomView.swift b/SodaLive/Sources/Chat/Talk/Room/ChatRoomView.swift index 9345486..443d2c7 100644 --- a/SodaLive/Sources/Chat/Talk/Room/ChatRoomView.swift +++ b/SodaLive/Sources/Chat/Talk/Room/ChatRoomView.swift @@ -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) } diff --git a/SodaLive/Sources/Chat/Talk/Room/Create/CreateChatRoomResponse.swift b/SodaLive/Sources/Chat/Talk/Room/Create/CreateChatRoomResponse.swift index 7365de0..d20f54b 100644 --- a/SodaLive/Sources/Chat/Talk/Room/Create/CreateChatRoomResponse.swift +++ b/SodaLive/Sources/Chat/Talk/Room/Create/CreateChatRoomResponse.swift @@ -6,5 +6,5 @@ // struct CreateChatRoomResponse: Decodable { - let chatRoomId: Int64 + let chatRoomId: Int } diff --git a/SodaLive/Sources/ContentView.swift b/SodaLive/Sources/ContentView.swift index e417dc5..ab3a22a 100644 --- a/SodaLive/Sources/ContentView.swift +++ b/SodaLive/Sources/ContentView.swift @@ -251,6 +251,9 @@ struct ContentView: View { case .characterDetail(let characterId): CharacterDetailView(characterId: characterId) + case .chatRoom(let id): + ChatRoomView(roomId: id) + default: EmptyView() .frame(width: 0, height: 0, alignment: .topLeading)