TSCollectionViewVM.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // TSCollectionViewVM.swift
  3. // AIRingtone
  4. //
  5. // Created by 100Years on 2025/2/28.
  6. //
  7. import ObjectMapper
  8. //MARK: TSColVVMStyple
  9. enum TSColVVMStyple : Int {
  10. case themeGuide //主题引导
  11. case themeContent //主题内容
  12. case posterHistory //生成海报的历史记录
  13. case photoHistory //生成头像的历史记录
  14. case ringCategories //铃声分类
  15. var config:TSColVVMSizeConfig {
  16. switch self {
  17. case .themeGuide:
  18. return themeGuideConfig
  19. case .themeContent:
  20. return themeContentConfig
  21. case .posterHistory:
  22. return posterHistoryConfig
  23. case .photoHistory:
  24. return photoHistoryConfig
  25. case .ringCategories:
  26. return ringCategoriesConfig
  27. }
  28. }
  29. var sectionInset: UIEdgeInsets {
  30. config.sectionInset
  31. }
  32. var lineSpacing: CGFloat {
  33. config.lineSpacing
  34. }
  35. var itemSpacing: CGFloat {
  36. config.itemSpacing
  37. }
  38. var cellSize:CGSize {
  39. config.cellSize
  40. }
  41. var cellClass: UICollectionViewCell.Type {
  42. config.cellClass
  43. }
  44. var headerViewSize:CGSize {
  45. config.headerViewSize
  46. }
  47. var headerView:UICollectionReusableView.Type {
  48. config.headerView
  49. }
  50. }
  51. //MARK: TSColVVMSectionModel
  52. class TSColVVMSectionModel: TSBaseModel {
  53. var style:TSColVVMStyple = .themeContent {
  54. didSet{
  55. for itemModel in items {
  56. itemModel.style = style
  57. }
  58. }
  59. }
  60. var name:String = "History"
  61. var items:[TSColVVMItemModel] = [TSColVVMItemModel]()
  62. override func mapping(map: ObjectMapper.Map) {
  63. style <- map["style"]
  64. name <- map["name"]
  65. items <- map["items"]
  66. }
  67. }
  68. extension TSColVVMSectionModel : TSCollectionViewSectionComponent{
  69. var sectionInset: UIEdgeInsets {
  70. return style.sectionInset
  71. }
  72. var lineSpacing: CGFloat {
  73. return style.lineSpacing
  74. }
  75. var itemSpacing: CGFloat {
  76. return style.itemSpacing
  77. }
  78. var headerComponent: TSCollectionViewReuseViewComponent? {
  79. return TSColVVMComponentReuseViewModel(sectionModel: self)
  80. }
  81. var cells: [TSCollectionViewCellComponent] {
  82. return items
  83. }
  84. }
  85. //MARK: TSColVVMItemModel
  86. class TSColVVMItemModel: TSBaseModel {
  87. var style:TSColVVMStyple = .themeContent
  88. var dataModel:TSBaseModel = TSBaseModel()
  89. override func mapping(map: ObjectMapper.Map) {
  90. dataModel <- map["dataModel"]
  91. style <- map["style"]
  92. }
  93. }
  94. extension TSColVVMItemModel: TSCollectionViewCellComponent{
  95. var cellClass: UICollectionViewCell.Type {
  96. style.cellClass
  97. }
  98. func cellSize(with attrubites: [String : Any]?) -> CGSize {
  99. style.cellSize
  100. }
  101. }
  102. //MARK: 分区头
  103. class TSColVVMComponentReuseViewModel : TSBaseModel ,TSCollectionViewReuseViewComponent {
  104. var sectionModel:TSColVVMSectionModel
  105. var style:TSColVVMStyple = .themeContent
  106. var dataModel:TSBaseModel = TSBaseModel()
  107. init(sectionModel:TSColVVMSectionModel) {
  108. self.sectionModel = sectionModel
  109. }
  110. required convenience init?(json: [String : Any]) {
  111. fatalError("init(json:) has not been implemented")
  112. }
  113. required convenience init?(map: ObjectMapper.Map) {
  114. fatalError("init(map:) has not been implemented")
  115. }
  116. override func mapping(map: ObjectMapper.Map) {
  117. sectionModel <- map["sectionModel"]
  118. }
  119. var viewClass: UICollectionReusableView.Type {
  120. return style.headerView
  121. }
  122. var reuseIdentifier: String{
  123. return viewClass.description()
  124. }
  125. var viewSize: CGSize {
  126. return sectionModel.style.headerViewSize
  127. }
  128. var viewType: TSCollectionViewReuseViewType {
  129. return .header
  130. }
  131. }