KMPlanViewController.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // KMPlanViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by liujiajie on 2023/12/6.
  6. //
  7. import Cocoa
  8. @objc(KMPlanTableCellView)
  9. protocol KMPlanTableCellViewDelegate: AnyObject {
  10. func tableCellViewCheckButtonDidChange(_ cell: KMPlanTableCellView)
  11. }
  12. class KMPlanTableCellView: NSTableCellView{
  13. var delegate: KMPlanTableCellViewDelegate?
  14. var language: String = ""
  15. @IBOutlet var planTextField: NSTextField!
  16. @IBOutlet var planButton: NSButton!
  17. @IBAction func planButtonAction(_ sender: NSButton) {
  18. self.delegate?.tableCellViewCheckButtonDidChange(self)
  19. }
  20. }
  21. class KMPlanViewController: NSViewController,KMPlanTableCellViewDelegate,NSOutlineViewDelegate,NSOutlineViewDataSource{
  22. @IBOutlet var outlineView: NSOutlineView!
  23. lazy var palns: Array<String> = {
  24. let palns = [KMLocalizedString("Plan 1 (Online)",nil), KMLocalizedString("Plan 2 (Offline)",nil)]
  25. return palns
  26. }()
  27. override init(nibName nibNameOrNil: NSNib.Name?, bundle nibBundleOrNil: Bundle?) {
  28. super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
  29. }
  30. required init?(coder: NSCoder) {
  31. super.init(coder: coder)
  32. }
  33. override func awakeFromNib() {
  34. super.awakeFromNib()
  35. self.outlineView.reloadData()
  36. }
  37. //MARK: NSOutlineViewDataSource
  38. func outlineView(_ outlineView: NSOutlineView, numberOfChildrenOfItem item: Any?) -> Int {
  39. return self.palns.count
  40. }
  41. func outlineView(_ outlineView: NSOutlineView, child index: Int, ofItem item: Any?) -> Any {
  42. return self.palns[index]
  43. }
  44. func outlineView(_ outlineView: NSOutlineView, isItemExpandable item: Any) -> Bool {
  45. return false
  46. }
  47. // NSOutlineViewDelegate
  48. func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? {
  49. let result = outlineView.makeView(withIdentifier: NSUserInterfaceItemIdentifier("PlanDataCell"), owner: self) as! KMPlanTableCellView
  50. let plan = UserDefaults.standard.integer(forKey: "KMOCRCurrentPlanKey")
  51. if plan == transformCellNum(cellContent: item as! String) {
  52. result.planButton.state = .on
  53. } else {
  54. result.planButton.state = .off
  55. }
  56. result.planTextField.stringValue = item as! String
  57. result.language = item as! String
  58. result.delegate = self
  59. return result
  60. }
  61. func outlineView(_ outlineView: NSOutlineView, shouldSelectItem item: Any) -> Bool {
  62. return false
  63. }
  64. //MARK: KMPlanTableCellViewDelegate
  65. func tableCellViewCheckButtonDidChange(_ cell: KMPlanTableCellView) {
  66. let plan = transformCellNum(cellContent: cell.planTextField.stringValue)
  67. if cell.planButton.state == .on{
  68. UserDefaults.standard.set(plan, forKey: "KMOCRCurrentPlanKey")
  69. } else {
  70. if plan == 0 {
  71. UserDefaults.standard.set(1, forKey: "KMOCRCurrentPlanKey")
  72. } else {
  73. UserDefaults.standard.set(0, forKey: "KMOCRCurrentPlanKey")
  74. }
  75. UserDefaults.standard.synchronize()
  76. }
  77. self.outlineView.reloadData()
  78. NotificationCenter.default.post(name: NSNotification.Name("KMOCRSelectedPlanChangeNotification"), object: nil)
  79. }
  80. func transformCellNum(cellContent:String) -> Int {
  81. var index = 0
  82. for i in 0..<(self.palns.count) {
  83. let str: String = self.palns[i]
  84. if cellContent == str {
  85. index = i
  86. break
  87. }
  88. }
  89. return index
  90. }
  91. }