美文网首页
Swift-转模型Mappable

Swift-转模型Mappable

作者: 物非0人非 | 来源:发表于2024-06-17 09:18 被阅读0次
    项目中的 PaintModel
    //MARK: 实现Mappable协议的基类
    /// 实现Mappable协议的基类
    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) {
        }
    }
    
    
    /// 名画模型
    public class PaintModel: SJBaseMapModel {
        /// 画作名称
        var opusName: String = ""
        /// 画作作者
        var opusAuthor: String = ""
        /// 画作时期
        var opusTime: String = ""
        // 以下接口返回数据
        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
        /// 创建时间 毫秒
        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"
        /// 评论量
        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"
        /// 点赞量
        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-已点赞,false-未点赞
        var isLike: Bool = false
        /// 画作简介
        var opusSynopsis: String = ""
        var opusSynopsisHeight: CGFloat = 0.0
        var opusSynopsisAttr: NSAttributedString? = nil
        /// 头图地址
        var headImageString: String = ""
        var headImageUrl: URL? = nil
        /// 画作id
        var opusId: Int = 0
        /// 帖子id
        var taskId: Int = 0
        /// 详情-尺寸
        var opusSize: String = ""
        /// 详情-类别
        var opusCategory: String = ""
        /// 详情-内容标签
        var opusContent: String = ""
        /// 详情-内容
        var opusDetails: String = ""
        var opusDetailsHeight: CGFloat = 0.0
        var opusDetailsAttr: NSAttributedString? = nil
        /// 详情-内容材质
        var opusQuality: String = ""
        /// 临摹图标识,true-有,false-无
        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(){
            // 计算文本高度
            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
                }
            }
        }
    }
    
    
    

    相关文章

      网友评论

          本文标题:Swift-转模型Mappable

          本文链接:https://www.haomeiwen.com/subject/uqxtcjtx.html