KMAnnotationModel.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // KMAnnotationModel.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2024/4/3.
  6. //
  7. import Cocoa
  8. class KMAnnotationModel: NSObject {
  9. weak var anno: CPDFAnnotation?
  10. }
  11. class KMBotaAnnotationBaseModel: KMAnnotationModel {
  12. var isExpand = false
  13. var animated = false
  14. var isSelected = false
  15. }
  16. class KMBotaAnnotationModel: KMBotaAnnotationBaseModel {
  17. var showPage = true
  18. var showTime = true
  19. var showAuthor = true
  20. var foldType: KMFoldType = .none
  21. var foldH: CGFloat = 30
  22. weak var sectionModel: KMBotaAnnotationSectionModel?
  23. weak var footerModel: KMBotaAnnotationFooterModel?
  24. var replyAnnos: [KMBotaAnnotationReplyModel] = []
  25. func isFold() -> Bool {
  26. if self.foldType == .unfold {
  27. return false
  28. }
  29. return true
  30. }
  31. func canFold() -> Bool {
  32. guard let data = self.anno else {
  33. return false
  34. }
  35. if data is CPDFMarkupAnnotation {
  36. return true
  37. }
  38. if data is CPDFTextAnnotation {
  39. return true
  40. }
  41. return false
  42. }
  43. }
  44. class KMBotaAnnotationFooterModel: KMBotaAnnotationBaseModel {
  45. weak var annoModel: KMBotaAnnotationModel?
  46. weak var replyModel: KMBotaAnnotationReplyModel?
  47. var isFirstResp = false
  48. }
  49. class KMBotaAnnotationReplyModel: KMBotaAnnotationBaseModel {
  50. // weak
  51. var replyAnno: CPDFAnnotation?
  52. weak var annoModel: KMBotaAnnotationModel?
  53. }
  54. class KMBotaAnnotationSectionModel: NSObject {
  55. var items: [KMBotaAnnotationBaseModel] = []
  56. var itemCount: Int {
  57. get {
  58. return self.items.count
  59. }
  60. }
  61. var isExpand = true
  62. }
  63. class KMAnnotationListModel: NSObject {
  64. var datas: [KMBotaAnnotationSectionModel] = []
  65. // 默认全部折叠
  66. var foldType: KMFoldType {
  67. set {
  68. for model in self.canFoldModel() {
  69. if newValue != .none {
  70. model.foldType = newValue
  71. }
  72. }
  73. }
  74. get {
  75. let canFolds = self.canFoldModel()
  76. if canFolds.isEmpty {
  77. return .none
  78. }
  79. // 是否全部折叠
  80. var isAll = true
  81. for model in canFolds {
  82. if model.isFold() == false {
  83. isAll = false
  84. break
  85. }
  86. }
  87. if isAll {
  88. return .fold
  89. }
  90. // 是否全部展开
  91. for model in canFolds {
  92. if model.isFold() {
  93. isAll = false
  94. break
  95. }
  96. }
  97. return isAll ? .unfold : .none
  98. }
  99. }
  100. func canFoldModel() -> [KMBotaAnnotationModel] {
  101. var models: [KMBotaAnnotationModel] = []
  102. for model in self.datas {
  103. for itemM in model.items {
  104. if let data = itemM as? KMBotaAnnotationModel {
  105. if data.canFold() {
  106. models.append(data)
  107. }
  108. }
  109. }
  110. }
  111. return models
  112. }
  113. func isAllFold() -> Bool {
  114. for model in self.canFoldModel() {
  115. if model.isFold() == false {
  116. return false
  117. }
  118. }
  119. return true
  120. }
  121. }