// // KMOCRPresenter.swift // PDF Master // // Created by lizhe on 2022/12/8. // import Cocoa class KMOCRPresenter: NSObject { lazy var model: KMOCRModel! = KMOCRModel() var view: NSView? fileprivate weak var delegate: KMOCRPresenterDelegate? /** 初始化presenter */ func initPresenter(delegate: KMOCRPresenterDelegate, data: KMOCRModel) { self.delegate = delegate self.model = model self.delegate?.showData(presenter: self, data: self.model) } func cleanAll() { self.delegate?.showData(presenter: self, data: self.model) } } extension KMOCRPresenter: KMOCRPresenterProtocol { func selectLanguage(sender: NSButton) { KMGOCRManager.default().ocrType = .apple let languageArray:[NSDictionary] = (KMGOCRManager.languages() as? Array)! let menu = NSMenu.init() for dic in languageArray { let item = menu.addItem(withTitle: NSLocalizedString(dic[KMGOCRLanguageStringKey] as! String, comment: ""), action: #selector(language), keyEquivalent: "") item.target = self item.representedObject = dic } menu.popUp(positioning: menu.item(at: 0), at: CGPoint(x: 0, y: (sender as NSView).fittingSize.height), in: sender as NSView) } func saveTXT(sender: NSButton) { if sender.state == .on { self.model.exportOptions.insert(.txt) } else { self.model.exportOptions.remove(.txt) } if self.delegate != nil { self.delegate!.showData(presenter: self, data: self.model) } } func savePDF(sender: NSButton) { if sender.state == .on { self.model.exportOptions.insert(.pdf) } else { self.model.exportOptions.remove(.pdf) } if self.delegate != nil { self.delegate!.showData(presenter: self, data: self.model) } } func export(filePath: String) { self.model.exportFilePath = filePath if self.delegate != nil { self.delegate!.export(presenter: self, data: self.model) } } @objc func language(sender: NSMenuItem) { KMPrint(sender.representedObject!) let dic: NSDictionary = sender.representedObject as! NSDictionary self.model.languageDic = dic self.model.languageType = KMOCRLanguageType.init(rawValue: dic[KMGOCRLanguageCodeKey] as! String) if (self.delegate != nil) { self.delegate?.showData(presenter: self, data: self.model) } } func free() { view = nil delegate = nil } } protocol KMOCRPresenterDelegate: AnyObject { /** @abstract 回调,返回处理后的数据 @param presenter @param data 返回数据 */ func showData(presenter: KMOCRPresenter, data: KMOCRModel) /** @abstract 导出成功回调 */ func export(presenter: KMOCRPresenter, data: KMOCRModel) } protocol KMOCRPresenterProtocol { /** @abstract 选择语言 */ func selectLanguage(sender: NSButton) /** @abstract 保存TXT */ func saveTXT(sender: NSButton) /** @abstract 保存PDF */ func savePDF(sender: NSButton) /** @abstract 导出 */ func export(filePath: String) /** @abstract 解除绑定 */ func free() }