Visual_Novel_iOS/crush/Crush/Src/Modules/Chat/Model/IMMessageModels.swift

130 lines
3.5 KiB
Swift
Raw Normal View History

2025-10-09 10:29:35 +00:00
//
// IMMessageModels.swift
// Crush
//
// Created by Leon on 2025/8/22.
//
import Foundation
enum CLCustomAttchType: String, Codable {
case IMAGE
case IM_SEND_GIFT
case HEARTBEAT_LEVEL_UP
case HEARTBEAT_LEVEL_DOWN
case CALL
// raw:{"type":"INSUFFICIENT_BALANCE","content":""}
case INSUFFICIENT_BALANCE
/// 使
case VOICE_CHAT_CONTENT
///
var needInstantScrollDisplay: Bool{
switch self {
case .IMAGE, .IM_SEND_GIFT:
return true
default:
return false
}
}
}
struct IMCustomAttachment: Codable{
var type: CLCustomAttchType?
/// url
var url: String? //
var width: FlexibleCGFloat?
var height: FlexibleCGFloat?
var albumId: Int? // id
var unlockPrice: Int? // AI
// -
var giftId: Int?
var giftName: String?
var giftNum: Int?
var giftIcon: String?
/**
// HEARTBEAT_LEVEL_UP HEARTBEAT_LEVEL_DOWNtitle
imContent
*/
var title : String?
var icon: String?
// - Call
var callType: CallType?
var duration: Int?
// -
var heartbeatLevel: HeartbeatLevel?
var heartbeatLevelNum: Int?
var heartbeatVal: CGFloat?
var heartbeatLevelName: String?
func getDisplayString() -> String{
var displayString = ""
// guard let theType = type else{
// return displayString
// }
switch type {
case .IMAGE:
displayString = "[Image]"
case .IM_SEND_GIFT:
displayString = "[Gift]"
case .CALL:
displayString = dealCallAttachment()
default:
displayString = " "
#if DEBUG
// coin
displayString = "[测试环境显示]Check 类型⚠️ is \(String(describing: type))"
#endif
}
return displayString
}
}
// MARK: - Helper
extension IMCustomAttachment{
//
static func getCustomInfo(model: SessionBaseModel) -> IMCustomAttachment? {
guard let raw = model.v2msg?.attachment?.raw else{
dlog("☁️❌ getCustom error")
return nil
}
return CodableHelper.decode(IMCustomAttachment.self, from: raw)
}
private func dealCallAttachment() -> String {
guard let theCallType = callType else { return "" }
if theCallType == .CALL_CANCEL {
return "Call Canceled"
} else {
let durationInMinistamp = duration ?? 0
return formatDuration(durationInMinistamp)
}
}
fileprivate func formatDuration(_ duration: Int) -> String {
//
let totalSeconds = Int(ceil(Double(duration) / 1000.0))
let hours = totalSeconds / 3600
let minutes = (totalSeconds / 60) % 60
let seconds = totalSeconds % 60
if hours > 0 {
return String(format: "%@ %02d:%02d:%02d", "Call Duration", hours, minutes, seconds)
} else {
return String(format: "%@ %02d:%02d", "Call Duration", minutes, seconds)
}
}
}
enum CallType: String, Codable{
case CALL_CANCEL
case CALL_END
}