美文网首页
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-转模型HandyJSON

    一 简介二 特性三 安装使用以及封装四 使用示例 一 简介 HandyJSON是一个用于Swift语言中的JSON...

  • Swift-转模型Codable

    前言:Codable是苹果自带的基于KVC编码的数据转换模型的协议 一:首先创建一个xyModel.swift文件...

  • Swift 中关于操作符的那些事儿

    知道 ObjectMapper 的人大概都见过在使用 Mappable 定义的模型中 func mapping(m...

  • Swift-自动归档(HandyJSON转模型)

    本文是依赖HandyJSON转模型来实现归档不需要依赖第三方可以看Swift-自动归档[https://www.j...

  • ObjectMapper 源码阅读笔记

    协议Mappable 通常使用Mappable的时候都是自己定义的Model遵守Mappable协议,然后在fun...

  • ObjectMapper 基本使用

    基本使用 使用时,类或结构体必须遵循Mappable协议 classUser:Mappable{ varusern...

  • ObjectMapper使用

    基本使用 使用时,类或结构体必须遵循Mappable协议 对JSON进行互换 支持类型 Mappable Prot...

  • 14-Swift中字典转模型

    字典转模型(初始化时传入字典) 字典转模型(利用KVC转化) 一、 普通的字典转模型: 二、利用KVC字典转模型:

  • swift-类属性

    了解属性之前,需要先了解前面的swift-类结构内容 - swift-类结构源码探寻[https://www.ji...

  • Swift 字典转模型

    这里探讨字典转模型中模型的类型单一模型模型嵌套 (模型中包含模型 || 模型中包含模型数组) Swfit的字典转模...

网友评论

      本文标题:Swift-转模型Mappable

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