KMAnnotationModel.swift 3.2 KB

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