Files
yingmeng-ios-switf/yinmeng-ios/Modules/Chat/Model/ChatBaseObject.swift
2024-02-27 23:34:55 +08:00

106 lines
3.5 KiB
Swift

//
// ChatBaseObject.swift
// yinmeng-ios
//
// Created by MaiMang on 2024/2/27.
//
import Foundation
import NIMSDK
public enum SessionType: Int {
case text = 1
case image
case time
}
public protocol ChatSessionProtocol: NSObjectProtocol {
var msg:NIMMessage? {get set}
// cell
var contentSize: CGSize { get set }
///
var height: Float { get set }
///id
var userID: String? { get set }
///
var name: String? { get set }
///
var avatar: String? { get set }
///
var type:SessionType{get set}
init(msg: NIMMessage?)
}
open class ChatBaseObject:NSObject, ChatSessionProtocol {
public var msg: NIMMessage?
public var contentSize: CGSize
public var height: Float
public var userID: String?
public var name: String?
public var avatar: String?
public var type: SessionType = .text
public required init(msg: NIMMessage?) {
self.msg = msg
if let uid = msg?.from {
self.userID = uid
let user = NIMSDK.shared().userManager.userInfo(uid)
self.avatar = user?.userInfo?.avatarUrl
self.name = user?.userInfo?.nickName
}
contentSize = CGSize(width: 32.0, height: ChatUIConfig.layout.bubbleMinHeight)
height = Float(ChatUIConfig.layout.bubbleMinHeight + ChatUIConfig.layout.margin)
}
}
class ChatTextObject: ChatBaseObject {
public var attribute: NSMutableAttributedString?
required init(msg: NIMMessage?) {
super.init(msg: msg)
type = .text
let style = NSMutableParagraphStyle()
style.lineSpacing = 6
let attributeStr = NSMutableAttributedString(string: msg?.text ?? "", attributes: [NSAttributedString.Key.font: ChatUIConfig.ui.messageFont, NSAttributedString.Key.paragraphStyle: style])
attribute = attributeStr
let textSize = ChatAttributeTool.boundingRect(attribute: attributeStr, font: ChatUIConfig.ui.messageFont, maxSize: CGSize(width: ChatUIConfig.layout.contentMaxWidth, height: CGFloat.greatestFiniteMagnitude))
var h = ChatUIConfig.layout.bubbleMinHeight
h = textSize.height + 1 + ChatUIConfig.layout.textInsets.top + ChatUIConfig.layout.textInsets.bottom
if h < 36 {
h = 36
}
contentSize = CGSize(width: textSize.width + ChatUIConfig.layout.textInsets.left + ChatUIConfig.layout.textInsets.right + 1, height: h)
height = Float(contentSize.height + ChatUIConfig.layout.cellContentInsets.bottom + ChatUIConfig.layout.cellContentInsets.top)
}
}
class ChatTimeObject: ChatBaseObject {
var text:String = ""
public var attribute: NSMutableAttributedString?
required init(msg: NIMMessage?) {
super.init(msg: msg)
type = .time
let style = NSMutableParagraphStyle()
style.lineSpacing = 6
let attributeStr = NSMutableAttributedString(string: msg?.text ?? "", attributes: [NSAttributedString.Key.font: ChatUIConfig.ui.messageFont, NSAttributedString.Key.paragraphStyle: style])
attribute = attributeStr
let textSize = ChatAttributeTool.boundingRect(attribute: attributeStr, font: ChatUIConfig.ui.messageFont, maxSize: CGSize(width: ChatUIConfig.layout.contentMaxWidth, height: CGFloat.greatestFiniteMagnitude))
var h = ChatUIConfig.layout.bubbleMinHeight
h = textSize.height + 1 + ChatUIConfig.layout.textInsets.top + ChatUIConfig.layout.textInsets.bottom
if h < 36 {
h = 36
}
contentSize = CGSize(width: textSize.width + ChatUIConfig.layout.textInsets.left + ChatUIConfig.layout.textInsets.right + 1, height: h)
height = Float(contentSize.height + ChatUIConfig.layout.cellContentInsets.bottom + ChatUIConfig.layout.cellContentInsets.top)
}
}