KMCompressWindowController.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // KMCompressWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2023/11/8.
  6. //
  7. import Cocoa
  8. typealias KMCompressWindowControllerBatchAction = (_ view: KMCompressWindowController, _ filePaths: [URL]) -> Void
  9. typealias KMCompressWindowControllerItemClick = () -> Void
  10. typealias KMCompressWindowControllerResultCallback = (_ result: Bool,_ openDocuemt: Bool, _ fileURL: URL, _ error: String)->()
  11. class KMCompressWindowController: KMNBaseWindowController {
  12. @IBOutlet weak var compressView: KMCompressView!
  13. var resultCallback: KMCompressWindowControllerResultCallback!
  14. var itemClick: KMCompressWindowControllerItemClick?
  15. var batchAction: KMCompressWindowControllerBatchAction?
  16. var documentURL: URL! {
  17. didSet{
  18. if self.password.count != 0 {
  19. self.compressView?.documentURL = self.documentURL
  20. } else {
  21. NSWindowController.checkPassword(url: documentURL, type: .owner) { [unowned self] success, paasswordString in
  22. if success {
  23. self.password = paasswordString
  24. self.compressView?.documentURL = self.documentURL
  25. } else {
  26. guard let callBack = self.itemClick else { return }
  27. callBack()
  28. }
  29. }
  30. }
  31. }
  32. }
  33. var password: String = "" {
  34. didSet {
  35. self.compressView?.password = password
  36. }
  37. }
  38. private var datas: [String] = []
  39. var limit = true
  40. var oriDocumentUrl: URL?
  41. override func windowDidLoad() {
  42. super.windowDidLoad()
  43. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  44. reloadData()
  45. self.compressView.cancelAction = { [weak self] view in
  46. // self.closeWindow()
  47. guard let callBack = self?.itemClick else { return }
  48. callBack()
  49. }
  50. self.compressView.batchAction = { [unowned self] view in
  51. print("Batch 按钮点击")
  52. if !IAPProductsManager.default().isAvailableAllFunction(){
  53. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  54. return
  55. }
  56. guard let callBack = batchAction else { return }
  57. callBack(self, [documentURL])
  58. }
  59. self.compressView.compressAction = { [weak self] view, model, showView in
  60. guard let view = self?.window?.contentView else { return }
  61. self?.compressButtonAction(model: model, showView: (showView ?? view))
  62. }
  63. }
  64. func reloadData() {
  65. self.compressView?.password = password
  66. self.compressView?.documentURL = self.documentURL
  67. }
  68. private func compressButtonAction(model: KMCompressSettingModel, limit: Bool = false, showView: NSView) {
  69. KMCompressManager.shared.compress(documentURL: self.documentURL,
  70. password: self.password,
  71. limit: limit,
  72. model: model,
  73. view: showView) { [unowned self] isFinish, resultURL in
  74. if isFinish {
  75. if showView != self.window?.contentView {
  76. self.compressView.compressContentView.closeSettingWindow()
  77. }
  78. self.closeWindow()
  79. } else {
  80. print("压缩失败")
  81. }
  82. }
  83. }
  84. private func _clearData() {
  85. if let _ = self.oriDocumentUrl {
  86. if let data = self.documentURL?.path, FileManager.default.fileExists(atPath: data) {
  87. try?FileManager.default.removeItem(atPath: data)
  88. }
  89. }
  90. }
  91. func closeWindow() {
  92. self.close()
  93. }
  94. }
  95. extension KMCompressWindowController {
  96. func beginLoading() {
  97. self.window?.contentView?.beginLoading()
  98. }
  99. func endLoading() {
  100. self.window?.contentView?.endLoading()
  101. }
  102. }