123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- //
- // 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<String> = {
- let palns = [KMLocalizedString("Plan 1 (Online)",nil), KMLocalizedString("Plan 2 (Offline)",nil)]
- 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
- }
-
- }
|