CNLiveShortVideoHomeModel.swift 10.9 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_top: CGFloat! = 0.0
    var tableView_h: CGFloat! = 0.0
    override func didFinishMapping() {
        super.didFinishMapping()
        cell_h += comment.cellH
        
        for replyModel in replyData.replys {
            tableView_h += replyModel.cellH
        }
        if replyData.replyCount ?? 0 > 2 {
            tableView_top += adapt_length(length: 30)
            cell_h += adapt_length(length: 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 nickLayout: YYTextLayout!
    var cellH: CGFloat! = adapt_length(length: 50)
    
    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-adapt_length(length: 80), height: CGFloat(HUGE)))
        textContainer.linePositionModifier = modifier;
        let textLayout = YYTextLayout(container: textContainer, text: attributes)
        self.textLayout = textLayout
        self.cellH += (textLayout?.textBoundingRect.size.height ?? 0)

        let nick_modifier = CNLiveShortVideoMateCodeLinePositionModifier(font: medium_adapt_font(pointSize: 12), paddingTop: 0, paddingBottom: 0, lineHeightMultiple: 1.5)
        let textAttributedString = NSMutableAttributedString.init(string: self.nick ?? "")
        textAttributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: HexColor("#AFAFAF")!, range: NSRange(location: 0, length: textAttributedString.length))
        textAttributedString.addAttribute(NSAttributedString.Key.font, value: medium_adapt_font(pointSize: 12), range: NSRange(location: 0, length: textAttributedString.length))
        let nickContainer = YYTextContainer(size: CGSize(width: KSreenWidth-adapt_length(length: 80), height: CGFloat(HUGE)))
        nickContainer.linePositionModifier = nick_modifier;
        self.nickLayout = YYTextLayout(container: nickContainer, text: textAttributedString)
        self.cellH += (self.nickLayout?.textBoundingRect.size.height ?? 0)
    }
}
//回复数据
class ShortVideoHomeCommentRListModel: BaseModel {
    var replyCount: 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 nickLayout: YYTextLayout!
    var cellH: CGFloat! = adapt_length(length: 50)
    
    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-adapt_length(length: 120), height: CGFloat(HUGE)))
        textContainer.linePositionModifier = modifier;
        self.textLayout = YYTextLayout(container: textContainer, text: attributes)
        self.cellH += (self.textLayout?.textBoundingRect.size.height ?? 0)
        
        let nick_modifier = CNLiveShortVideoMateCodeLinePositionModifier(font: medium_adapt_font(pointSize: 12), paddingTop: 0, paddingBottom: 0, lineHeightMultiple: 1.5)
        let textAttributedString = NSMutableAttributedString.init(string: "\(self.fromNick ?? "")  ??  \(self.toNick ?? "")")
        textAttributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: HexColor("#AFAFAF")!, range: NSRange(location: 0, length: textAttributedString.length))
        textAttributedString.addAttribute(NSAttributedString.Key.font, value: medium_adapt_font(pointSize: 12), range: NSRange(location: 0, length: textAttributedString.length))
        let attachText = NSAttributedString.yy_attachmentString(withContent: UIImage(named: "short_video_reply_name"), contentMode: .scaleAspectFill, attachmentSize: CGSize(width: adapt_length(length: 8), height: adapt_length(length: 8)), alignTo: medium_adapt_font(pointSize: 12), alignment: .center)
        textAttributedString.replaceCharacters(in: NSRange(location: self.fromNick!.count + 2, length: 2), with: attachText)
        let nickContainer = YYTextContainer(size: CGSize(width: KSreenWidth-adapt_length(length: 120), height: adapt_length(length: 40)))
        nickContainer.linePositionModifier = nick_modifier;
        self.nickLayout = YYTextLayout(container: nickContainer, text: textAttributedString)
        self.cellH += (self.nickLayout?.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: 25), height: adapt_length(length: 25)), 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
        }
    }
}