// // KMPlanViewController.swift // PDF Reader Pro // // Created by liujiajie on 2023/12/6. // import Cocoa @objc(KMPlanTableCellView) protocol KMPlanTableCellViewDelegate: AnyObject { func tableCellViewCheckButtonDidChange(_ cell: KMPlanTableCellView) } class KMPlanTableCellView: NSTableCellView{ var delegate: KMPlanTableCellViewDelegate? var language: String = "" @IBOutlet var planTextField: NSTextField! @IBOutlet var planButton: NSButton! @IBAction func planButtonAction(_ sender: NSButton) { self.delegate?.tableCellViewCheckButtonDidChange(self) } } class KMPlanViewController: NSViewController,KMPlanTableCellViewDelegate,NSOutlineViewDelegate,NSOutlineViewDataSource{ @IBOutlet var outlineView: NSOutlineView! lazy var palns: Array = { let palns = [KMLocalizedString("Plan 1 (Online)"), KMLocalizedString("Plan 2 (Offline)")] return palns }() override init(nibName nibNameOrNil: NSNib.Name?, bundle nibBundleOrNil: Bundle?) { super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) } required init?(coder: NSCoder) { super.init(coder: coder) } override func awakeFromNib() { super.awakeFromNib() self.outlineView.reloadData() } //MARK: NSOutlineViewDataSource func outlineView(_ outlineView: NSOutlineView, numberOfChildrenOfItem item: Any?) -> Int { return self.palns.count } func outlineView(_ outlineView: NSOutlineView, child index: Int, ofItem item: Any?) -> Any { return self.palns[index] } func outlineView(_ outlineView: NSOutlineView, isItemExpandable item: Any) -> Bool { return false } // NSOutlineViewDelegate func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? { let result = outlineView.makeView(withIdentifier: NSUserInterfaceItemIdentifier("PlanDataCell"), owner: self) as! KMPlanTableCellView let plan = UserDefaults.standard.integer(forKey: "KMOCRCurrentPlanKey") if plan == transformCellNum(cellContent: item as! String) { result.planButton.state = .on } else { result.planButton.state = .off } result.planTextField.stringValue = item as! String result.language = item as! String result.delegate = self return result } func outlineView(_ outlineView: NSOutlineView, shouldSelectItem item: Any) -> Bool { return false } //MARK: KMPlanTableCellViewDelegate func tableCellViewCheckButtonDidChange(_ cell: KMPlanTableCellView) { let plan = transformCellNum(cellContent: cell.planTextField.stringValue) if cell.planButton.state == .on{ UserDefaults.standard.set(plan, forKey: "KMOCRCurrentPlanKey") } else { if plan == 0 { UserDefaults.standard.set(1, forKey: "KMOCRCurrentPlanKey") } else { UserDefaults.standard.set(0, forKey: "KMOCRCurrentPlanKey") } UserDefaults.standard.synchronize() } self.outlineView.reloadData() NotificationCenter.default.post(name: NSNotification.Name("KMOCRSelectedPlanChangeNotification"), object: nil) } func transformCellNum(cellContent:String) -> Int { var index = 0 for i in 0..<(self.palns.count) { let str: String = self.palns[i] if cellContent == str { index = i break } } return index } }