KMConvertSettingView.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //
  2. // KMConvertSettingView.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2023/5/9.
  6. //
  7. let kKMConvertLanugageSelectedIndex: String = "KMConvertLanugageSelectedIndex"
  8. typealias KMConvertSettingViewPageRangeDidChangeCallback = (_ pageRange: KMPageRange) -> Void
  9. typealias KMConvertSettingViewPageRangeDidInputFinishCallback = (_ pageRangeString: String) -> ()
  10. // 转档 设置视图 基类
  11. class KMConvertSettingView: KMConvertBaseView {
  12. private var _currentLanguage: String?
  13. var currentLanguage: String? {
  14. get {
  15. return self._currentLanguage
  16. }
  17. set {
  18. self._currentLanguage = newValue
  19. if let indexString = UserDefaults.standard.string(forKey: kKMConvertLanugageSelectedIndex), let index = Int(indexString) {
  20. self.ocrLanuguageIndex = index
  21. return
  22. }
  23. var language: COCRLanguage = .english
  24. if let _language = KMPDFConvertManager.defaultManager.ocrLanguage(appleLanguage: self._currentLanguage) {
  25. language = _language
  26. }
  27. if let index = KMPDFConvertManager.defaultManager.languages_asc.firstIndex(of: language) {
  28. self.ocrLanuguageIndex = index
  29. } else {
  30. self.ocrLanuguageIndex = 2
  31. }
  32. }
  33. }
  34. var ocrLanuguageIndex: Int = 3 {
  35. didSet {
  36. self.ocrItemView?.languageIndex = self.ocrLanuguageIndex
  37. }
  38. }
  39. var pageRangeSelectedIndex: Int = 0
  40. var pageRangeDidChange: KMConvertSettingViewPageRangeDidChangeCallback?
  41. var pageRangeDidInputFinishCallback: KMConvertSettingViewPageRangeDidInputFinishCallback?
  42. var lanugageDidSelected: KMValueDidChangeBlock?
  43. private var _ocrItemView: KMConvertOCRSettingItemView?
  44. var ocrItemView: KMConvertOCRSettingItemView? {
  45. get {
  46. return self._ocrItemView
  47. }
  48. set {
  49. if (self._ocrItemView != newValue) {
  50. self._ocrItemView = newValue
  51. newValue?.lanugageDidSelected = { [unowned self] value, change in
  52. guard let callback = self.lanugageDidSelected else {
  53. return
  54. }
  55. callback(value, change)
  56. }
  57. }
  58. }
  59. }
  60. private var _pageRangeItemView: KMConvertPageRangeSettingItemView?
  61. var pageRangeItemView: KMConvertPageRangeSettingItemView? {
  62. get {
  63. return self._pageRangeItemView
  64. }
  65. set {
  66. if (self._pageRangeItemView != newValue) {
  67. self._pageRangeItemView = newValue
  68. newValue?.isTextDidEndCallback = true
  69. newValue?.itemClick = { [unowned self] index, _ in
  70. self.pageRangeSelectedIndex = index
  71. guard let callback = self.pageRangeDidChange else {
  72. return
  73. }
  74. callback(KMPageRange(rawValue: index)!)
  75. }
  76. newValue?.textDidEndEditingCallback = { [unowned self] textString in
  77. let pageRange = KMPageRange(rawValue: self.pageRangeSelectedIndex)
  78. if (pageRange != .custom) {
  79. return
  80. }
  81. guard let callback = self.pageRangeDidInputFinishCallback else {
  82. return
  83. }
  84. callback(textString)
  85. }
  86. }
  87. }
  88. }
  89. var tipView: KMConvertSettingLimitTipView?
  90. override func initSubViews() {
  91. super.initSubViews()
  92. let tipView = KMConvertSettingLimitTipView()
  93. self.addSubview(tipView)
  94. self.tipView = tipView
  95. }
  96. override func layout() {
  97. super.layout()
  98. // let width = NSWidth(self.bounds)
  99. // let height = NSHeight(self.bounds)
  100. // self.tipView?.frame = NSMakeRect(0, 10, width, 16+20+16)
  101. }
  102. var pageCount = 0 {
  103. didSet {
  104. self.pageRangeItemView?.pageCount = self.pageCount
  105. }
  106. }
  107. var ocrEnabled: Bool {
  108. get {
  109. guard let state = self.ocrItemView?.checkVC?.state else {
  110. return false
  111. }
  112. return state == .Checked
  113. }
  114. }
  115. func getPageRangeString() -> String {
  116. if let view = self.pageRangeItemView {
  117. return view.pageRangeValue
  118. }
  119. return ""
  120. }
  121. }