라이브 방
- 하트 후원 API 연결 - 하트 후원 성공시 하트 애니메이션 호출 - 하트 후원 성공시 채팅으로 알림
This commit is contained in:
@@ -118,6 +118,7 @@ final class LiveRoomViewModel: NSObject, ObservableObject {
|
||||
}
|
||||
|
||||
@Published var totalDonationCan = 0
|
||||
@Published var totalHeartCount = 0
|
||||
@Published var donationMessageList = [LiveRoomDonationMessage]()
|
||||
@Published var donationMessageCount = 0
|
||||
|
||||
@@ -361,6 +362,7 @@ final class LiveRoomViewModel: NSObject, ObservableObject {
|
||||
)
|
||||
|
||||
getTotalDonationCan()
|
||||
getTotalHeartCount()
|
||||
|
||||
if data.isAdult && !UserDefaults.bool(forKey: .auth) {
|
||||
changeIsAdult = true
|
||||
@@ -1133,6 +1135,32 @@ final class LiveRoomViewModel: NSObject, ObservableObject {
|
||||
}
|
||||
}
|
||||
|
||||
private func getTotalHeartCount() {
|
||||
repository.getTotalHeartCount(roomId: AppState.shared.roomId)
|
||||
.sink { result in
|
||||
switch result {
|
||||
case .finished:
|
||||
DEBUG_LOG("finish")
|
||||
case .failure(let error):
|
||||
ERROR_LOG(error.localizedDescription)
|
||||
}
|
||||
} receiveValue: { [unowned self] response in
|
||||
self.isLoading = false
|
||||
let responseData = response.data
|
||||
|
||||
do {
|
||||
let jsonDecoder = JSONDecoder()
|
||||
let decoded = try jsonDecoder.decode(ApiResponse<GetLiveRoomHeartTotalResponse>.self, from: responseData)
|
||||
|
||||
if let data = decoded.data, decoded.success {
|
||||
self.totalHeartCount = data.totalHeartCount
|
||||
}
|
||||
} catch {
|
||||
}
|
||||
}
|
||||
.store(in: &subscription)
|
||||
}
|
||||
|
||||
func getTotalDonationCan() {
|
||||
repository.getTotalDoantionCan(roomId: AppState.shared.roomId)
|
||||
.sink { result in
|
||||
@@ -1821,11 +1849,61 @@ final class LiveRoomViewModel: NSObject, ObservableObject {
|
||||
if isAvailableLikeHeart {
|
||||
if !isLoadingLikeHeart {
|
||||
isLoadingLikeHeart = true
|
||||
addHeart()
|
||||
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) { [unowned self] in
|
||||
self.isLoadingLikeHeart = false
|
||||
}
|
||||
repository.likeHeart(roomId: AppState.shared.roomId)
|
||||
.sink { result in
|
||||
switch result {
|
||||
case .finished:
|
||||
DEBUG_LOG("finish")
|
||||
case .failure(let error):
|
||||
ERROR_LOG(error.localizedDescription)
|
||||
}
|
||||
} receiveValue: { [unowned self] response in
|
||||
self.isLoadingLikeHeart = false
|
||||
let responseData = response.data
|
||||
|
||||
do {
|
||||
let jsonDecoder = JSONDecoder()
|
||||
let decoded = try jsonDecoder.decode(ApiResponseWithoutData.self, from: responseData)
|
||||
|
||||
if decoded.success {
|
||||
UserDefaults.set(UserDefaults.int(forKey: .can) - 1, forKey: .can)
|
||||
|
||||
let donationRawMessage = LiveRoomChatRawMessage(
|
||||
type: .HEART_DONATION,
|
||||
message: "",
|
||||
can: 1,
|
||||
donationMessage: nil
|
||||
)
|
||||
|
||||
agora.sendRawMessageToGroup(
|
||||
rawMessage: donationRawMessage,
|
||||
completion: { [unowned self] errorCode in
|
||||
if errorCode == .errorOk {
|
||||
let (nickname, _) = self.getUserNicknameAndProfileUrl(accountId: UserDefaults.int(forKey: .userId))
|
||||
self.messages.append(LiveRoomHeartDonationChat(nickname: nickname))
|
||||
|
||||
totalHeartCount += 1
|
||||
addHeart()
|
||||
|
||||
self.messageChangeFlag.toggle()
|
||||
if self.messages.count > 100 {
|
||||
self.messages.remove(at: 0)
|
||||
}
|
||||
} else {
|
||||
refundDonation()
|
||||
}
|
||||
},
|
||||
fail: { [unowned self] in
|
||||
refundDonation()
|
||||
}
|
||||
)
|
||||
}
|
||||
} catch {
|
||||
refundDonation()
|
||||
}
|
||||
}
|
||||
.store(in: &subscription)
|
||||
}
|
||||
} else {
|
||||
isShowNoticeLikeHeart = true
|
||||
@@ -2114,6 +2192,10 @@ extension LiveRoomViewModel: AgoraRtmChannelDelegate {
|
||||
self.isActiveRoulette = decoded.isActiveRoulette!
|
||||
} else if decoded.type == .EDIT_ROOM_INFO || decoded.type == .SET_MANAGER {
|
||||
self.getRoomInfo()
|
||||
} else if decoded.type == .HEART_DONATION {
|
||||
self.messages.append(LiveRoomHeartDonationChat(nickname: nickname))
|
||||
self.totalHeartCount += decoded.can
|
||||
self.addHeart()
|
||||
}
|
||||
} catch {
|
||||
}
|
||||
|
Reference in New Issue
Block a user