106 lines
3.5 KiB
Swift
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)
|
|
}
|
|
}
|