feat(chat-room): 채팅 쿼터 구매 기능 추가
This commit is contained in:
@@ -54,4 +54,8 @@ class ChatRoomRepository {
|
||||
func getChatQuotaStatus() -> AnyPublisher<Response, MoyaError> {
|
||||
return talkApi.requestPublisher(.getChatQuotaStatus)
|
||||
}
|
||||
|
||||
func purchaseChatQuota() -> AnyPublisher<Response, MoyaError> {
|
||||
return talkApi.requestPublisher(.purchaseChatQuota(request: ChatQuotaPurchaseRequest()))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -132,7 +132,7 @@ struct ChatRoomView: View {
|
||||
|
||||
if viewModel.showQuotaNoticeView {
|
||||
ChatQuotaNoticeItemView(remainingTime: viewModel.countdownText) {
|
||||
|
||||
viewModel.purchaseChatQuota()
|
||||
}
|
||||
.id(viewModel.messages.count)
|
||||
.padding(.bottom, 12)
|
||||
@@ -230,6 +230,7 @@ struct ChatRoomView: View {
|
||||
}
|
||||
}
|
||||
.onAppear {
|
||||
viewModel.getMemberInfo()
|
||||
viewModel.enterRoom(roomId: roomId)
|
||||
}
|
||||
.onDisappear {
|
||||
|
||||
@@ -163,6 +163,33 @@ final class ChatRoomViewModel: ObservableObject {
|
||||
.store(in: &subscription)
|
||||
}
|
||||
|
||||
func getMemberInfo() {
|
||||
userRepository.getMemberInfo()
|
||||
.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
|
||||
|
||||
do {
|
||||
let jsonDecoder = JSONDecoder()
|
||||
let decoded = try jsonDecoder.decode(ApiResponse<GetMemberInfoResponse>.self, from: responseData)
|
||||
|
||||
if let data = decoded.data, decoded.success {
|
||||
UserDefaults.set(data.can, forKey: .can)
|
||||
UserDefaults.set(data.point, forKey: .point)
|
||||
}
|
||||
} catch {
|
||||
print(error)
|
||||
}
|
||||
}
|
||||
.store(in: &subscription)
|
||||
}
|
||||
|
||||
func purchaseChatMessage() {
|
||||
guard let selectedMessage = selectedMessage else {
|
||||
return
|
||||
@@ -212,7 +239,46 @@ final class ChatRoomViewModel: ObservableObject {
|
||||
}
|
||||
|
||||
func purchaseChatQuota() {
|
||||
isLoading = true
|
||||
|
||||
repository.purchaseChatQuota()
|
||||
.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<ChatQuotaStatusResponse>.self, from: responseData)
|
||||
|
||||
if let data = decoded.data, decoded.success {
|
||||
self?.updateQuota(totalRemaining: data.totalRemaining, nextRechargeAtEpoch: data.nextRechargeAtEpoch)
|
||||
|
||||
let can = UserDefaults.int(forKey: .can)
|
||||
UserDefaults.set(can - 30, forKey: .can)
|
||||
} 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)
|
||||
}
|
||||
|
||||
private func checkQuotaStatus() {
|
||||
|
||||
@@ -16,6 +16,7 @@ enum TalkApi {
|
||||
case getChatRoomMessages(roomId: Int, cursor: Int?, limit: Int)
|
||||
|
||||
case getChatQuotaStatus
|
||||
case purchaseChatQuota(request: ChatQuotaPurchaseRequest)
|
||||
|
||||
case purchaseMessage(roomId: Int, messageId: Int64, request: ChatMessagePurchaseRequest)
|
||||
}
|
||||
@@ -43,6 +44,9 @@ extension TalkApi: TargetType {
|
||||
case .getChatQuotaStatus:
|
||||
return "/api/chat/quota/me"
|
||||
|
||||
case .purchaseChatQuota:
|
||||
return "/api/chat/quota/purchase"
|
||||
|
||||
case .purchaseMessage(let roomId, let messageId, _):
|
||||
return "/api/chat/room/\(roomId)/messages/\(messageId)/purchase"
|
||||
}
|
||||
@@ -68,6 +72,9 @@ extension TalkApi: TargetType {
|
||||
case .getChatQuotaStatus:
|
||||
return .get
|
||||
|
||||
case .purchaseChatQuota:
|
||||
return .post
|
||||
|
||||
case .purchaseMessage:
|
||||
return .post
|
||||
}
|
||||
@@ -111,6 +118,9 @@ extension TalkApi: TargetType {
|
||||
case .getChatQuotaStatus:
|
||||
return .requestPlain
|
||||
|
||||
case .purchaseChatQuota(let request):
|
||||
return .requestJSONEncodable(request)
|
||||
|
||||
case .purchaseMessage(_, _, let request):
|
||||
return .requestJSONEncodable(request)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user