KMConvertSettingView.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //
  2. // KMConvertSettingView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/5/9.
  6. //
  7. import KMComponentLibrary
  8. let kKMConvertLanugageSelectedIndex: String = "KMConvertLanugageSelectedIndex"
  9. typealias KMConvertSettingViewPageRangeDidChangeCallback = (_ pageRange: KMPageRange) -> Void
  10. typealias KMConvertSettingViewPageRangeDidInputFinishCallback = (_ pageRangeString: String) -> ()
  11. typealias KMConvertWordSettingViewNeedOCRAction = (_ needOCR: Bool) -> Void
  12. // 转档 设置视图 基类
  13. class KMConvertSettingView: KMConvertBaseView {
  14. private var _currentLanguage: String?
  15. var type: KMBatchConvertPDFType = .word
  16. var isBatch = false
  17. var isDisable: Bool = false {
  18. didSet {
  19. self.reloadData()
  20. }
  21. }
  22. //OCR
  23. var needOCRAction: KMConvertWordSettingViewNeedOCRAction?
  24. var currentLanguage: String? {
  25. get {
  26. return self._currentLanguage
  27. }
  28. set {
  29. self._currentLanguage = newValue
  30. if let indexString = UserDefaults.standard.string(forKey: kKMConvertLanugageSelectedIndex), let index = Int(indexString) {
  31. self.ocrLanuguageIndex = index
  32. return
  33. }
  34. }
  35. }
  36. var ocrLanuguageIndex: Int = 2 {
  37. didSet {
  38. self.ocrItemView?.languageIndex = self.ocrLanuguageIndex
  39. }
  40. }
  41. var pageRangeSelectedIndex: Int = 0
  42. var pageRangeDidChange: KMConvertSettingViewPageRangeDidChangeCallback?
  43. var pageRangeDidInputFinishCallback: KMConvertSettingViewPageRangeDidInputFinishCallback?
  44. var lanugageDidSelected: KMValueDidChangeBlock?
  45. private var _ocrItemView: KMConvertOCRSettingItemView?
  46. var ocrItemView: KMConvertOCRSettingItemView? {
  47. get {
  48. return self._ocrItemView
  49. }
  50. set {
  51. if (self._ocrItemView != newValue) {
  52. self._ocrItemView = newValue
  53. newValue?.lanugageDidSelected = { [weak self] value, change in
  54. guard let callback = self?.lanugageDidSelected else {
  55. return
  56. }
  57. callback(value, change)
  58. }
  59. newValue?.needOCRAction = { [weak self] needOCR in
  60. guard let callback = self?.needOCRAction else {
  61. return
  62. }
  63. callback(needOCR)
  64. }
  65. }
  66. }
  67. }
  68. private var _pageRangeItemView: KMConvertPageRangeSettingItemView?
  69. var pageRangeItemView: KMConvertPageRangeSettingItemView? {
  70. get {
  71. return self._pageRangeItemView
  72. }
  73. set {
  74. if (self._pageRangeItemView != newValue) {
  75. self._pageRangeItemView = newValue
  76. newValue?.isTextDidEndCallback = true
  77. newValue?.itemClick = { [weak self] index, _ in
  78. self?.pageRangeSelectedIndex = index
  79. guard let callback = self?.pageRangeDidChange else {
  80. return
  81. }
  82. var value: KMPageRange = .all
  83. if index == 1 {
  84. value = .current
  85. } else if index == 2 {
  86. value = .odd
  87. } else if index == 3 {
  88. value = .even
  89. } else if index == 4 {
  90. value = .custom
  91. }
  92. callback(value)
  93. }
  94. newValue?.textDidEndEditingCallback = { [unowned self] textString in
  95. var value: KMPageRange = .all
  96. let index = self.pageRangeSelectedIndex
  97. if index == 1 {
  98. value = .current
  99. } else if index == 2 {
  100. value = .odd
  101. } else if index == 3 {
  102. value = .even
  103. } else if index == 4 {
  104. value = .custom
  105. }
  106. let pageRange = value
  107. if (pageRange != .custom) {
  108. return
  109. }
  110. guard let callback = self.pageRangeDidInputFinishCallback else {
  111. return
  112. }
  113. callback(textString)
  114. }
  115. }
  116. }
  117. }
  118. var tipView: KMConvertSettingLimitTipView?
  119. override func initSubViews() {
  120. super.initSubViews()
  121. }
  122. override func layout() {
  123. super.layout()
  124. }
  125. var pageCount = 0 {
  126. didSet {
  127. self.pageRangeItemView?.pageCount = self.pageCount
  128. }
  129. }
  130. var ocrEnabled: Bool {
  131. get {
  132. guard let state = self.ocrItemView?.checkView.properties.checkboxType else {
  133. return false
  134. }
  135. return state == .selected
  136. }
  137. }
  138. func getPageRangeString() -> String {
  139. if let view = self.pageRangeItemView {
  140. return view.pageRangeValue
  141. }
  142. return ""
  143. }
  144. override func updateViewColor() {
  145. super.updateViewColor()
  146. KMMainThreadExecute {
  147. self.ocrItemView?.titleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  148. self.pageRangeItemView?.titleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  149. }
  150. }
  151. func reloadData() {
  152. }
  153. }