feat(chat-room): 유료 메시지 구매 기능 추가

This commit is contained in:
Yu Sung
2025-09-04 06:34:00 +09:00
parent 6ce85a485a
commit 20801bdcfb
6 changed files with 165 additions and 5 deletions

View File

@@ -36,6 +36,20 @@ class ChatRoomRepository {
return talkApi.requestPublisher(.sendMessage(roomId: roomId, request: SendChatMessageRequest(message: message))) return talkApi.requestPublisher(.sendMessage(roomId: roomId, request: SendChatMessageRequest(message: message)))
} }
/**
*
* -
*/
func purchaseMessage(roomId: Int, messageId: Int64) -> AnyPublisher<Response, MoyaError> {
return talkApi.requestPublisher(
.purchaseMessage(
roomId: roomId,
messageId: messageId,
request: ChatMessagePurchaseRequest()
)
)
}
/** */ /** */
func getChatQuotaStatus() -> AnyPublisher<Response, MoyaError> { func getChatQuotaStatus() -> AnyPublisher<Response, MoyaError> {
return talkApi.requestPublisher(.getChatQuotaStatus) return talkApi.requestPublisher(.getChatQuotaStatus)

View File

@@ -122,7 +122,10 @@ struct ChatRoomView: View {
AiMessageItemView( AiMessageItemView(
message: message, message: message,
characterName: viewModel.characterName characterName: viewModel.characterName
) ) {
viewModel.selectedMessage = message
viewModel.selectedMessageIndex = index
}
.id(index) .id(index)
} }
} }
@@ -210,6 +213,21 @@ struct ChatRoomView: View {
.frame(width: screenSize().width) .frame(width: screenSize().width)
} }
} }
if let message = viewModel.selectedMessage, viewModel.selectedMessageIndex >= 0 {
SodaDialog(
title: "잠금된 메시지",
desc: "이 메시지를 \(message.price ?? 5)캔으로 잠금해제 하시겠습니까?",
confirmButtonTitle: "잠금해제",
confirmButtonAction: {
viewModel.purchaseChatMessage()
},
cancelButtonTitle: "취소"
) {
viewModel.selectedMessage = nil
viewModel.selectedMessageIndex = -1
}
}
} }
.onAppear { .onAppear {
viewModel.enterRoom(roomId: roomId) viewModel.enterRoom(roomId: roomId)
@@ -217,6 +235,23 @@ struct ChatRoomView: View {
.onDisappear { .onDisappear {
viewModel.stopTimer() viewModel.stopTimer()
} }
.popup(isPresented: $viewModel.isShowPopup, type: .toast, position: .top, autohideIn: 2) {
GeometryReader { geo in
HStack {
Spacer()
Text(viewModel.errorMessage)
.padding(.vertical, 13.3)
.frame(width: geo.size.width - 66.7, alignment: .center)
.font(.custom(Font.medium.rawValue, size: 12))
.background(Color.button)
.foregroundColor(Color.white)
.multilineTextAlignment(.center)
.cornerRadius(20)
.padding(.top, 66.7)
Spacer()
}
}
}
} }
} }

View File

@@ -30,6 +30,9 @@ final class ChatRoomViewModel: ObservableObject {
@Published var messageText: String = "" @Published var messageText: String = ""
@Published private(set) var messages: [ServerChatMessage] = [] @Published private(set) var messages: [ServerChatMessage] = []
@Published var selectedMessage: ServerChatMessage? = nil
@Published var selectedMessageIndex: Int = -1
// MARK: - Private // MARK: - Private
private let userRepository = UserRepository() private let userRepository = UserRepository()
private let repository = ChatRoomRepository() private let repository = ChatRoomRepository()
@@ -160,6 +163,54 @@ final class ChatRoomViewModel: ObservableObject {
.store(in: &subscription) .store(in: &subscription)
} }
func purchaseChatMessage() {
guard let selectedMessage = selectedMessage else {
return
}
isLoading = true
repository.purchaseMessage(roomId: roomId, messageId: selectedMessage.messageId)
.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
do {
let jsonDecoder = JSONDecoder()
let decoded = try jsonDecoder.decode(ApiResponse<ServerChatMessage>.self, from: responseData)
if let data = decoded.data, decoded.success {
self?.messages.insert(data, at: self?.selectedMessageIndex ?? 0)
self?.messages.remove(at: (self?.selectedMessageIndex ?? 0) + 1)
self?.selectedMessage = nil
self?.selectedMessageIndex = -1
} 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)
}
func purchaseChatQuota() { func purchaseChatQuota() {
} }
@@ -178,8 +229,6 @@ final class ChatRoomViewModel: ObservableObject {
} receiveValue: { [weak self] response in } receiveValue: { [weak self] response in
let responseData = response.data let responseData = response.data
DEBUG_LOG(String(data: responseData, encoding: .utf8) ?? "")
do { do {
let jsonDecoder = JSONDecoder() let jsonDecoder = JSONDecoder()
let decoded = try jsonDecoder.decode(ApiResponse<ChatQuotaStatusResponse>.self, from: responseData) let decoded = try jsonDecoder.decode(ApiResponse<ChatQuotaStatusResponse>.self, from: responseData)

View File

@@ -64,6 +64,8 @@ struct AiMessageItemView: View {
let message: ServerChatMessage let message: ServerChatMessage
let characterName: String let characterName: String
let purchaseMessage: () -> Void
var body: some View { var body: some View {
HStack(alignment: .bottom, spacing: 4) { HStack(alignment: .bottom, spacing: 4) {
// //
@@ -88,7 +90,10 @@ struct AiMessageItemView: View {
} }
// ( ) // ( )
if message.messageType.lowercased() == "image", let imageUrl = message.imageUrl, !imageUrl.isEmpty { if message.messageType.lowercased() == "image",
let imageUrl = message.imageUrl,
!imageUrl.isEmpty
{
// //
let maxWidth = (UIScreen.main.bounds.width - 48) * 0.7 let maxWidth = (UIScreen.main.bounds.width - 48) * 0.7
let imageHeight = maxWidth * 5 / 4 // 4:5 let imageHeight = maxWidth * 5 / 4 // 4:5
@@ -97,6 +102,41 @@ struct AiMessageItemView: View {
KFImage(URL(string: imageUrl)) KFImage(URL(string: imageUrl))
.resizable() .resizable()
.scaledToFill() // .scaledToFill() //
Color.black.opacity(0.2)
.frame(width: maxWidth, height: imageHeight)
.cornerRadius(30)
if let price = message.price, price > 0, !message.hasAccess {
VStack(spacing: 18) {
HStack(spacing: 4) {
Image("ic_can")
.resizable()
.frame(width: 24, height: 24)
Text("\(message.price ?? 5)")
.font(.custom(Font.preBold.rawValue, size: 16))
.foregroundColor(Color(hex: "263238"))
}
.padding(.horizontal, 10)
.padding(.vertical, 3)
.background(Color(hex: "B5E7FA"))
.cornerRadius(30)
.overlay {
RoundedRectangle(cornerRadius: 30)
.stroke(lineWidth: 1)
.foregroundColor(.button)
}
Text("눌러서 잠금해제")
.font(.custom(Font.preBold.rawValue, size: 18))
.foregroundColor(.white)
}
.frame(width: maxWidth, height: imageHeight)
.onTapGesture {
purchaseMessage()
}
}
} }
.frame(width: maxWidth, height: imageHeight) .frame(width: maxWidth, height: imageHeight)
.clipShape(RoundedRectangle(cornerRadius: 10, style: .continuous)) .clipShape(RoundedRectangle(cornerRadius: 10, style: .continuous))
@@ -189,7 +229,8 @@ struct AiMessageItemView: View {
price: nil, price: nil,
hasAccess: true hasAccess: true
), ),
characterName: "보라" characterName: "보라",
purchaseMessage: {}
) )
.padding() .padding()
.background(Color.black) .background(Color.black)

View File

@@ -0,0 +1,10 @@
//
// ChatMessagePurchaseRequest.swift
// SodaLive
//
// Created by klaus on 9/4/25.
//
struct ChatMessagePurchaseRequest: Encodable {
let container: String = "ios"
}

View File

@@ -16,6 +16,8 @@ enum TalkApi {
case getChatRoomMessages(roomId: Int, cursor: Int?, limit: Int) case getChatRoomMessages(roomId: Int, cursor: Int?, limit: Int)
case getChatQuotaStatus case getChatQuotaStatus
case purchaseMessage(roomId: Int, messageId: Int64, request: ChatMessagePurchaseRequest)
} }
extension TalkApi: TargetType { extension TalkApi: TargetType {
@@ -40,6 +42,9 @@ extension TalkApi: TargetType {
case .getChatQuotaStatus: case .getChatQuotaStatus:
return "/api/chat/quota/me" return "/api/chat/quota/me"
case .purchaseMessage(let roomId, let messageId, _):
return "/api/chat/room/\(roomId)/messages/\(messageId)/purchase"
} }
} }
@@ -62,6 +67,9 @@ extension TalkApi: TargetType {
case .getChatQuotaStatus: case .getChatQuotaStatus:
return .get return .get
case .purchaseMessage:
return .post
} }
} }
@@ -102,6 +110,9 @@ extension TalkApi: TargetType {
case .getChatQuotaStatus: case .getChatQuotaStatus:
return .requestPlain return .requestPlain
case .purchaseMessage(_, _, let request):
return .requestJSONEncodable(request)
} }
} }