CNLiveShortVideoHomeModel.swift 8.35 KB
//
//  CNLiveShortVideoHomeModel.swift
//  metaCode
//
//  Created by open on 2024/1/8.
//

import Foundation
import YYText
import HandyJSON
import CNLiveBusinessTools
//!!!: 评论相关 model
class ShortVideoHomeCommentModel: BaseModel {
    var total_hd: Int? = 0 //总数
    var hasNext: Bool? = false
    var comments = [ShortVideoHomeCommentListModel]()
}

class ShortVideoHomeCommentListModel: BaseModel {
    var comment = ShortVideoHomeCommentFListModel()
    var replyData = ShortVideoHomeCommentRListModel()
    
    var cell_h: CGFloat! = 0.0
    var tableView_h: CGFloat! = 0.0
    override func didFinishMapping() {
        super.didFinishMapping()
        cell_h += comment.cellH
        cell_h += 10
        
        for replyModel in replyData.replys {
            tableView_h += replyModel.cellH
        }
        if replyData.total_hd ?? 0 > 2 {
            cell_h += 30
        }
        cell_h += tableView_h
    }
    
}
//评论数据
class ShortVideoHomeCommentFListModel: BaseModel {
    var commentId: String? = ""
    var pid: String? = ""
    var uid: String? = ""
    var nick: String? = ""
    var facepath: String? = ""
    var content: String? = ""
    var created_at: String? = ""
    var praise: Int? = 0
    var plat: String? = ""
    var isOffical: Bool? = false
    var model: String? = ""
    var spId: String? = ""
    var seqId: Int? = 0
    var topicId: String? = ""
    var praiseCount: Int? = 0
    var isPraised: Bool? = false
    var icon: String? = ""
    
    var iconSize: CGSize = CGSize(width: 50, height: 50)
    var textLayout: YYTextLayout!
    var cellH: CGFloat! = 0
    
    override func didFinishMapping() {
        super.didFinishMapping()

        let modifier = CNLiveShortVideoMateCodeLinePositionModifier(font: medium_adapt_font(pointSize: 16), paddingTop: 0, paddingBottom: 0, lineHeightMultiple: 1.5)
        let comment = NSMutableAttributedString.init(string: self.content ?? "")
//        let comment = CNLiveBusinessTools.replaceQQemoji(withCustomText: self.content ?? "")
        let temp_string = CNLiveShortVideoMateCodeLinePositionModifier.replaceEmotions(textAttributedString: comment, mainString: self.content ?? "")
        let attributes = NSMutableAttributedString(attributedString: temp_string)
        attributes.addAttribute(NSAttributedString.Key.foregroundColor, value: HexColor("#333333")!, range: NSRange(location: 0, length: temp_string.length))
        attributes.addAttribute(NSAttributedString.Key.font, value: medium_adapt_font(pointSize: 16), range: NSRange(location: 0, length: temp_string.length))
        let textContainer = YYTextContainer(size: CGSize(width: KSreenWidth-80, height: CGFloat(HUGE)))
        textContainer.linePositionModifier = modifier;
        let textLayout = YYTextLayout(container: textContainer, text: attributes)
        self.textLayout = textLayout
        self.cellH = 70 + (textLayout?.textBoundingRect.size.height ?? 0)
    }
}
//回复数据
class ShortVideoHomeCommentRListModel: BaseModel {
    var total_hd: Int? = 0 //总数
    var replys = [ShortVideoHomeCommentRListItemModel]()
    
}

class ShortVideoHomeCommentRListItemModel: BaseModel {
    var praiseCount: Int? = 0
    var isPraised: Bool? = false
    var commentId: String? = ""
    var replyId: String? = ""
    var topicId: String? = ""
    var created_at: String? = ""
    var plat: String? = ""
    var fromFace: String? = ""
    var fromSid: String? = ""
    var fromNick: String? = ""
    var toSid: String? = ""
    var toNick: String? = ""
    var toFace: String? = ""
    var seqId: Int? = 0
    var content: String? = ""
    var level: Int? = 0
    var icon: String? = ""
    
    var textLayout: YYTextLayout!
    var cellH: CGFloat! = 0
    
    override func didFinishMapping() {
        super.didFinishMapping()
        
        let modifier = CNLiveShortVideoMateCodeLinePositionModifier(font: semibold_adapt_font(pointSize: 14), paddingTop: 0, paddingBottom: 0, lineHeightMultiple: 1.5)
        let comment = NSMutableAttributedString.init(string: self.content ?? "")
//        let comment = CNLiveBusinessTools.replaceQQemoji(withCustomText: self.content ?? "")
        let temp_string = CNLiveShortVideoMateCodeLinePositionModifier.replaceEmotions(textAttributedString: comment, mainString: self.content ?? "")
        let attributes = NSMutableAttributedString(attributedString: temp_string)
        attributes.addAttribute(NSAttributedString.Key.foregroundColor, value: HexColor("#333333")!, range: NSRange(location: 0, length: temp_string.length))
        attributes.addAttribute(NSAttributedString.Key.font, value: medium_adapt_font(pointSize: 16), range: NSRange(location: 0, length: temp_string.length))
        let textContainer = YYTextContainer(size: CGSize(width: KSreenWidth-80-40, height: CGFloat(HUGE)))
        textContainer.linePositionModifier = modifier;
        let textLayout = YYTextLayout(container: textContainer, text: attributes)
        self.textLayout = textLayout
        self.cellH = 70 + (textLayout?.textBoundingRect.size.height ?? 0)
    }
}

class CNLiveShortVideoMateCodeLinePositionModifier: NSObject, YYTextLinePositionModifier {
    
    var font: UIFont // 基准字体 (例如 Heiti SC/PingFang SC)
    var paddingTop: CGFloat // 文本顶部留白
    var paddingBottom: CGFloat // 文本底部留白
    var lineHeightMultiple: CGFloat // 行距倍数

    init(font: UIFont, paddingTop: CGFloat, paddingBottom: CGFloat, lineHeightMultiple: CGFloat) {
        self.font = font
        self.paddingTop = paddingTop
        self.paddingBottom = paddingBottom
        self.lineHeightMultiple = lineHeightMultiple
        super.init()
        
        if #available(iOS 9.0, *) {
            self.lineHeightMultiple = 1.34 // for PingFang SC
        } else {
            self.lineHeightMultiple = 1.3125 // for Heiti SC
        }
    }
    
    func modifyLines(_ lines: [YYTextLine], fromText text: NSAttributedString, in container: YYTextContainer) {
        let ascent = font.pointSize * 0.86
        let lineHeight = font.pointSize * lineHeightMultiple

        for item in lines {
            let line = item as YYTextLine
            var position = line.position
            position.y = paddingTop + ascent + CGFloat(line.row) * lineHeight
            line.position = position
        }
    }

    func copy(with zone: NSZone? = nil) -> Any {
        let one = CNLiveShortVideoMateCodeLinePositionModifier(
            font: font,
            paddingTop: paddingTop,
            paddingBottom: paddingBottom,
            lineHeightMultiple: lineHeightMultiple
        )
        return one
    }

    func heightForLineCount(_ lineCount: UInt) -> CGFloat {
        if lineCount == 0 {
            return 0
        }
        let ascent = font.pointSize * 0.86
        let descent = font.pointSize * 0.14
        let lineHeight = font.pointSize * lineHeightMultiple
        return paddingTop + paddingBottom + ascent + descent + CGFloat(lineCount - 1) * lineHeight
    }
    
    static func replaceEmotions(textAttributedString: NSMutableAttributedString, mainString: String) -> NSMutableAttributedString {
        do {
            let regex = try NSRegularExpression(pattern: "\\[(.*?)\\]", options: .caseInsensitive)
            
            let matches = regex.matches(in: textAttributedString.string, options: [], range: NSRange(location: 0, length: textAttributedString.string.count))
            if !matches.isEmpty {
                let emojiArr = CNLiveQQEmotionManager.emotionsForQQ()
                for i in (0..<matches.count).reversed() {
                    let result = matches[i]
                    
                    for j in 0..<emojiArr.count {
                        if mainString.substring(with: Range(result.range, in: mainString)!) == emojiArr[j].displayName {
                            let attachText = NSAttributedString.yy_attachmentString(withContent: emojiArr[j].image, contentMode: .scaleAspectFit, attachmentSize: CGSize(width: adapt_length(length: 18), height: adapt_length(length: 18)), alignTo: semibold_adapt_font(pointSize: 16), alignment: .center)
                            
                            textAttributedString.replaceCharacters(in: result.range, with: attachText)
                        }
                    }
                }
            }
            return textAttributedString
        } catch {
            print("Error creating NSRegularExpression: \(error.localizedDescription)")
            return textAttributedString
        }
    }
}