diff --git a/SodaLive/Sources/Live/LiveApi.swift b/SodaLive/Sources/Live/LiveApi.swift index 8ebaf7e..a933c3f 100644 --- a/SodaLive/Sources/Live/LiveApi.swift +++ b/SodaLive/Sources/Live/LiveApi.swift @@ -95,7 +95,7 @@ extension LiveApi: TargetType { return "/live/room/info/\(roomId)" case .donation: - return "/live/room/donation" + return "/live/room/donation/v2" case .refundDonation(let roomId): return "/live/room/donation/refund/\(roomId)" diff --git a/SodaLive/Sources/Live/Room/Chat/LiveRoomChatRawMessage.swift b/SodaLive/Sources/Live/Room/Chat/LiveRoomChatRawMessage.swift index 8232361..49b7c22 100644 --- a/SodaLive/Sources/Live/Room/Chat/LiveRoomChatRawMessage.swift +++ b/SodaLive/Sources/Live/Room/Chat/LiveRoomChatRawMessage.swift @@ -15,6 +15,7 @@ struct LiveRoomChatRawMessage: Codable { let type: LiveRoomChatRawMessageType let message: String let can: Int + var signature: LiveRoomDonationResponse? = nil var signatureImageUrl: String? = nil let donationMessage: String? var isActiveRoulette: Bool? = nil diff --git a/SodaLive/Sources/Live/Room/LiveRoomDonationResponse.swift b/SodaLive/Sources/Live/Room/LiveRoomDonationResponse.swift new file mode 100644 index 0000000..4c8fe46 --- /dev/null +++ b/SodaLive/Sources/Live/Room/LiveRoomDonationResponse.swift @@ -0,0 +1,13 @@ +// +// LiveRoomDonationResponse.swift +// SodaLive +// +// Created by klaus on 5/1/24. +// + +import Foundation + +struct LiveRoomDonationResponse: Codable { + let imageUrl: String + let time: Int +} diff --git a/SodaLive/Sources/Live/Room/LiveRoomViewModel.swift b/SodaLive/Sources/Live/Room/LiveRoomViewModel.swift index e758b9f..95f864d 100644 --- a/SodaLive/Sources/Live/Room/LiveRoomViewModel.swift +++ b/SodaLive/Sources/Live/Room/LiveRoomViewModel.swift @@ -159,17 +159,13 @@ final class LiveRoomViewModel: NSObject, ObservableObject { @Published var signatureImageUrl = "" { didSet { - if signatureImageUrl.trimmingCharacters(in: .whitespacesAndNewlines).count > 0 { - DispatchQueue.main.asyncAfter(deadline: .now() + 7) { - if let imageUrl = self.signatureImageUrls.first { - self.signatureImageUrl = imageUrl - self.signatureImageUrls.removeFirst() - } else { - self.signatureImageUrl = "" - self.isShowSignatureImage = false - } - } - } + showSignatureImage() + } + } + + @Published var signature: LiveRoomDonationResponse? = nil { + didSet { + showSignatureImage() } } @@ -187,6 +183,7 @@ final class LiveRoomViewModel: NSObject, ObservableObject { @Published var selectedMenu: SelectedMenu? = nil var signatureImageUrls = [String]() + var signatureList = [LiveRoomDonationResponse]() var isShowSignatureImage = false var timer: DispatchSourceTimer? @@ -399,7 +396,7 @@ final class LiveRoomViewModel: NSObject, ObservableObject { do { let jsonDecoder = JSONDecoder() - let decoded = try jsonDecoder.decode(ApiResponse.self, from: responseData) + let decoded = try jsonDecoder.decode(ApiResponse.self, from: responseData) self.isLoading = false @@ -409,7 +406,8 @@ final class LiveRoomViewModel: NSObject, ObservableObject { type: .DONATION, message: rawMessage, can: can, - signatureImageUrl: decoded.data, + signature: decoded.data, + signatureImageUrl: decoded.data?.imageUrl, donationMessage: message ) @@ -431,7 +429,7 @@ final class LiveRoomViewModel: NSObject, ObservableObject { ) totalDonationCan += can - showSignatureImage(imageUrl: decoded.data ?? "") + addSignature(signature: decoded.data) self.messageChangeFlag.toggle() if self.messages.count > 100 { @@ -1689,7 +1687,7 @@ final class LiveRoomViewModel: NSObject, ObservableObject { .store(in: &subscription) } - private func showSignatureImage(imageUrl: String) { + private func addSignatureImage(imageUrl: String) { if imageUrl.trimmingCharacters(in: .whitespacesAndNewlines).count > 0 { if !isShowSignatureImage { isShowSignatureImage = true @@ -1699,6 +1697,41 @@ final class LiveRoomViewModel: NSObject, ObservableObject { } } } + + private func addSignature(signature: LiveRoomDonationResponse?) { + if let signature = signature { + if !isShowSignatureImage { + self.signature = signature + isShowSignatureImage = true + } else { + self.signatureList.append(signature) + } + } + } + + private func showSignatureImage() { + if let signature = signature { + DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(signature.time)) { + if let nextSignature = self.signatureList.first { + self.signature = nextSignature + self.signatureList.removeFirst() + } else { + self.signature = nil + self.isShowSignatureImage = false + } + } + } else if signatureImageUrl.trimmingCharacters(in: .whitespacesAndNewlines).count > 0 { + DispatchQueue.main.asyncAfter(deadline: .now() + 7) { + if let imageUrl = self.signatureImageUrls.first { + self.signatureImageUrl = imageUrl + self.signatureImageUrls.removeFirst() + } else { + self.signatureImageUrl = "" + self.isShowSignatureImage = false + } + } + } + } } extension LiveRoomViewModel: AgoraRtcEngineDelegate { @@ -1863,7 +1896,12 @@ extension LiveRoomViewModel: AgoraRtmChannelDelegate { ) self.totalDonationCan += decoded.can - self.showSignatureImage(imageUrl: decoded.signatureImageUrl ?? "") + + if let signature = decoded.signature { + self.addSignature(signature: signature) + } else if let imageUrl = decoded.signatureImageUrl { + self.addSignatureImage(imageUrl: imageUrl) + } } else if decoded.type == .ROULETTE_DONATION { self.messages.append( LiveRoomRouletteDonationChat( diff --git a/SodaLive/Sources/Live/Room/V2/LiveRoomViewV2.swift b/SodaLive/Sources/Live/Room/V2/LiveRoomViewV2.swift index cfc76fe..8066a3a 100644 --- a/SodaLive/Sources/Live/Room/V2/LiveRoomViewV2.swift +++ b/SodaLive/Sources/Live/Room/V2/LiveRoomViewV2.swift @@ -272,6 +272,21 @@ struct LiveRoomViewV2: View { Spacer() } } + + if let signature = viewModel.signature { + VStack { + Spacer() + + AnimatedImage(url: URL(string: signature.imageUrl)) + .resizable() + .aspectRatio(contentMode: .fit) + .frame(width: screenSize().width - 64) + + Spacer() + Spacer() + Spacer() + } + } } }