123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- //
- // KMCompressWindowController.swift
- // PDF Reader Pro
- //
- // Created by lizhe on 2023/11/8.
- //
- import Cocoa
- typealias KMCompressWindowControllerBatchAction = (_ view: KMCompressWindowController, _ filePaths: [URL]) -> Void
- typealias KMCompressWindowControllerItemClick = () -> Void
- typealias KMCompressWindowControllerResultCallback = (_ result: Bool,_ openDocuemt: Bool, _ fileURL: URL, _ error: String)->()
- class KMCompressWindowController: KMNBaseWindowController {
- @IBOutlet weak var compressView: KMCompressView!
-
- var resultCallback: KMCompressWindowControllerResultCallback!
- var itemClick: KMCompressWindowControllerItemClick?
- var batchAction: KMCompressWindowControllerBatchAction?
-
- var documentURL: URL! {
- didSet{
- if self.password.count != 0 {
- self.compressView?.documentURL = self.documentURL
- } else {
- NSWindowController.checkPassword(url: documentURL, type: .owner) { [unowned self] success, paasswordString in
- if success {
- self.password = paasswordString
- self.compressView?.documentURL = self.documentURL
- } else {
- guard let callBack = self.itemClick else { return }
-
- callBack()
- }
- }
- }
- }
- }
-
- var password: String = "" {
- didSet {
- self.compressView?.password = password
- }
- }
- private var datas: [String] = []
-
- var limit = true
- var oriDocumentUrl: URL?
-
- override func windowDidLoad() {
- super.windowDidLoad()
- // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
- reloadData()
-
- self.compressView.cancelAction = { [weak self] view in
- // self.closeWindow()
-
- guard let callBack = self?.itemClick else { return }
-
- callBack()
- }
-
- self.compressView.batchAction = { [unowned self] view in
- print("Batch 按钮点击")
- if !IAPProductsManager.default().isAvailableAllFunction(){
- KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
- return
- }
- guard let callBack = batchAction else { return }
-
- callBack(self, [documentURL])
- }
-
- self.compressView.compressAction = { [weak self] view, model, showView in
-
- guard let view = self?.window?.contentView else { return }
-
- self?.compressButtonAction(model: model, showView: (showView ?? view))
- }
- }
-
- func reloadData() {
- self.compressView?.password = password
- self.compressView?.documentURL = self.documentURL
- }
-
- private func compressButtonAction(model: KMCompressSettingModel, limit: Bool = false, showView: NSView) {
- KMCompressManager.shared.compress(documentURL: self.documentURL,
- password: self.password,
- limit: limit,
- model: model,
- view: showView) { [unowned self] isFinish, resultURL in
- if isFinish {
- if showView != self.window?.contentView {
- self.compressView.compressContentView.closeSettingWindow()
- }
- self.closeWindow()
- } else {
- print("压缩失败")
- }
- }
- }
-
- private func _clearData() {
- if let _ = self.oriDocumentUrl {
- if let data = self.documentURL?.path, FileManager.default.fileExists(atPath: data) {
- try?FileManager.default.removeItem(atPath: data)
- }
- }
- }
-
- func closeWindow() {
- self.close()
- }
- }
- extension KMCompressWindowController {
- func beginLoading() {
- self.window?.contentView?.beginLoading()
- }
-
- func endLoading() {
- self.window?.contentView?.endLoading()
- }
- }
|