Swift-轉(zhuǎn)模型Mappable

項(xiàng)目中的 PaintModel
//MARK: 實(shí)現(xiàn)Mappable協(xié)議的基類(lèi)
/// 實(shí)現(xiàn)Mappable協(xié)議的基類(lèi)
open class SJBaseMapModel: Mappable {
    var index: Int = -1
    var totalHeight: CGFloat = 44.0
    var cellIdentifier: String = "SJBaseMapModel"
    public required init?(map: Map) {
    }
    open func mapping(map: Map) {
    }
}

/// 名畫(huà)模型
public class PaintModel: SJBaseMapModel {
    /// 畫(huà)作名稱
    var opusName: String = ""
    /// 畫(huà)作作者
    var opusAuthor: String = ""
    /// 畫(huà)作時(shí)期
    var opusTime: String = ""
    // 以下接口返回?cái)?shù)據(jù)
    open var avatarUrl: String = "" {
        didSet{
            self.peopleInfo.imgLocation = URL(string: avatarUrl)
        }
    }
    /// 作者ID =用戶ID
    open var authorId: Int = -1 {
        didSet{
            isSelf = (SDPublicService.isLogin()?.userId ?? "") == "\(authorId)"
        }
    }
    /// 帖子ID
    public var taskId: Int = -1
    /// 創(chuàng)建時(shí)間 毫秒
    var createTime: CLongLong = 0
    var isNeedTimerCalulator: Bool = false
    /// 瀏覽量
    var viewCount: Int = 0 {
        didSet{
            if self.viewCount > 999 {
                self.viewCountStr = afterDecimals(value: self.viewCount, formatInt: 1000, formatStr: "K")
            }else{
                self.viewCountStr = "\(viewCount)"
            }
        }
    }
    var viewCountStr: String = "0"
    /// 評(píng)論量
    var commentCount: Int = 0 {
        didSet{
            if self.commentCount > 999 {
                self.commentCountStr = afterDecimals(value: self.commentCount, formatInt: 1000, formatStr: "K")
            }else{
                self.commentCountStr = "\(commentCount)"
            }
        }
    }
    var commentCountStr: String = "0"
    /// 點(diǎn)贊量
    var likeCount: Int = 0 {
        didSet{
            if self.likeCount > 999 {
                self.likeCountStr = afterDecimals(value: self.likeCount, formatInt: 1000, formatStr: "K")
            }else{
                self.likeCountStr = "\(likeCount)"
            }
        }
    }
    var likeCountStr: String = "0"
    /// true-已點(diǎn)贊,false-未點(diǎn)贊
    var isLike: Bool = false
    /// 畫(huà)作簡(jiǎn)介
    var opusSynopsis: String = ""
    var opusSynopsisHeight: CGFloat = 0.0
    var opusSynopsisAttr: NSAttributedString? = nil
    /// 頭圖地址
    var headImageString: String = ""
    var headImageUrl: URL? = nil
    /// 畫(huà)作id
    var opusId: Int = 0
    /// 帖子id
    var taskId: Int = 0
    /// 詳情-尺寸
    var opusSize: String = ""
    /// 詳情-類(lèi)別
    var opusCategory: String = ""
    /// 詳情-內(nèi)容標(biāo)簽
    var opusContent: String = ""
    /// 詳情-內(nèi)容
    var opusDetails: String = ""
    var opusDetailsHeight: CGFloat = 0.0
    var opusDetailsAttr: NSAttributedString? = nil
    /// 詳情-內(nèi)容材質(zhì)
    var opusQuality: String = ""
    /// 臨摹圖標(biāo)識(shí),true-有,false-無(wú)
    var isCopy: Bool = false
    public override func mapping(map: Map) {
        opusName <- map["opusName"]
        opusTime <- map["opusTime"]
        opusAuthor <- map["opusAuthor"]
        viewCount <- map["viewCount"]
        commentCount <- map["commentCount"]
        likeCount <- map["likeCount"]
        isLike <- map["isLike"]
        opusSynopsis <- map["opusSynopsis"]
        headImageString <- map["headImageUrl"]
        opusId <- map["opusId"]
        taskId <- map["taskId"]
        opusSize <- map["opusSize"]
        opusCategory <- map["opusCategory"]
        opusContent <- map["opusContent"]
        opusDetails <- map["opusDetails"]
        opusQuality <- map["opusQuality"]
        isCopy <- map["isCopy"]
        // 頭圖
        if headImageString.count > 0 {
            if headImageString.contains("#"){
                headImageString = headImageString.extNewURLEncodedString()
            }
            if isIncludeChineseIn(string: headImageString) {
                headImageString = (headImageString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? "")
            }
            self.headImageUrl = URL(string: headImageString)
        }else{
            self.headImageUrl = nil
        }
        calculateHeight()
    }
    
    func calculateHeight(){
        // 計(jì)算文本高度
        if opusSynopsis.count > 0 {
            let attr = NSMutableAttributedString(string: opusSynopsis, attributes: [
                                                    NSAttributedString.Key.foregroundColor : UIColor.extColorWithHex(SDColor.Community.FontColor6.rawValue),
                                                    NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16)])
            attr.yy_lineSpacing = 2.0
            attr.yy_lineBreakMode = .byCharWrapping
            self.opusSynopsisAttr = attr
            let container = YYTextContainer(size: CGSize(width: SDJG_ScreenWidth - 30, height: CGFloat(MAXFLOAT)))
            container.truncationType = .charWrapping
            if var contentLayout = YYTextLayout(container: container, text: attr) , let c = self.opusSynopsisAttr {
                var rowCount = contentLayout.rowCount
                if rowCount > 3 {
                    rowCount = 3
                }else{
                    rowCount = rowCount - 1
                    rowCount = rowCount < 1 ? 1 : rowCount
                }
                container.maximumNumberOfRows = rowCount
                container.truncationType = .charWrapping
                contentLayout = YYTextLayout(container: container, text: c)!
                self.opusSynopsisHeight = contentLayout.textBoundingSize.height
            }
        }
    }
    
    func handlePraiseState(){
        self.isLike = !self.isLike
        if self.isLike {
            self.likeCount = self.likeCount + 1
        }else{
            self.likeCount = self.likeCount - 1
            if likeCount < 0 {
                likeCount = 0
            }
        }
    }
}


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容