KMMergeFileModel.swift 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //
  2. // KMMergeFileModel.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2022/11/24.
  6. //
  7. import Cocoa
  8. import PDFKit
  9. enum KMMergeFilePageRange: String {
  10. case all = "All Page"
  11. case oddPages = "Odd pages Only"
  12. case evenPages = "Even pages Only"
  13. case custom = ""
  14. }
  15. class KMMergeFileModel: NSObject {
  16. var documentURL: URL!
  17. var document: CPDFDocument!
  18. var open : Bool = false
  19. var page: CPDFPage!
  20. var password: String!
  21. var myDocument: CPDFDocument!
  22. var selected: Bool = false
  23. var pageRange: KMMergeFilePageRange = .all
  24. var pageRangeString: String = ""
  25. var pages: [KMMergePageModel] = []
  26. func canExpand() -> Bool {
  27. return document.pageCount > 1
  28. }
  29. func updateChildPageRange(pageRange: KMMergeFilePageRange) {
  30. if pageRange == .all {
  31. for pageModel in pages {
  32. pageModel.selected = true
  33. }
  34. } else if pageRange == .oddPages {
  35. var i = 0
  36. for pageModel in pages {
  37. if i % 2 == 0 {
  38. pageModel.selected = true
  39. } else {
  40. pageModel.selected = false
  41. }
  42. i += 1
  43. }
  44. } else if pageRange == .evenPages {
  45. var i = 0
  46. for pageModel in pages {
  47. if i % 2 == 0 {
  48. pageModel.selected = false
  49. } else {
  50. pageModel.selected = true
  51. }
  52. i += 1
  53. }
  54. } else if pageRange == .custom {
  55. }
  56. }
  57. func updatePagePange() {
  58. var allPage = true
  59. var oddPage = true
  60. var evenPage = true
  61. var selectedPages: [Int] = []
  62. for model in pages {
  63. if model.selected {
  64. selectedPages.append(model.pageID+1)
  65. if model.pageID % 2 == 1 { /// 偶数
  66. oddPage = false
  67. } else { /// 奇数
  68. evenPage = false
  69. }
  70. } else {
  71. allPage = false
  72. if model.pageID % 2 == 0 { /// 奇数
  73. oddPage = false
  74. } else { /// 偶数
  75. evenPage = false
  76. }
  77. }
  78. }
  79. if allPage {
  80. pageRange = .all
  81. } else if oddPage {
  82. pageRange = .oddPages
  83. } else if evenPage {
  84. pageRange = .evenPages
  85. } else {
  86. pageRange = .custom
  87. var string: String = ""
  88. var index = 0
  89. var prePage = 0
  90. var count = 0
  91. var cubeStart = NSNotFound
  92. var cubeEnd = NSNotFound
  93. for i in selectedPages {
  94. if selectedPages.count == 1 {
  95. string.append(String(i))
  96. } else if selectedPages.count == 2 {
  97. string.append(String(i))
  98. if index != selectedPages.count-1 {
  99. string.append(",")
  100. }
  101. } else {
  102. if index == 0 {
  103. string.append(String(i))
  104. } else {
  105. if i - prePage == 1 {
  106. if count == 0 {
  107. cubeStart = prePage
  108. }
  109. count += 1
  110. } else {
  111. count = 0
  112. }
  113. if count == 0 || index == selectedPages.count-1 { /// 块结束
  114. if cubeStart != NSNotFound && cubeEnd != NSNotFound { /// 有效块
  115. if cubeStart == selectedPages.first {
  116. string.removeFirst()
  117. } else {
  118. string.append(",")
  119. }
  120. if index == selectedPages.count-1 {
  121. if count == 0 {
  122. string.append(String(cubeStart))
  123. string.append("-")
  124. string.append(String(cubeEnd))
  125. string.append(",")
  126. string.append(String(i))
  127. } else {
  128. string.append(String(cubeStart))
  129. string.append("-")
  130. string.append(String(i))
  131. }
  132. } else {
  133. string.append(String(cubeStart))
  134. string.append("-")
  135. string.append(String(cubeEnd))
  136. }
  137. } else {
  138. if prePage == selectedPages.first {
  139. // string.append(",")
  140. } else {
  141. string.append(",")
  142. string.append(String(prePage))
  143. }
  144. cubeStart = NSNotFound
  145. cubeEnd = NSNotFound
  146. }
  147. }
  148. // else if count == 1 {
  149. // if index == selectedPages.count-1 {
  150. // if prePage == selectedPages.first {
  151. //// string.append(",")
  152. // } else {
  153. // string.append(",")
  154. // string.append(String(prePage))
  155. // }
  156. // }
  157. // }
  158. else if count >= 2 { /// 可成块
  159. cubeEnd = i
  160. }
  161. }
  162. }
  163. prePage = i
  164. index += 1
  165. }
  166. pageRangeString = string
  167. }
  168. }
  169. }