AccountBenefitsController.swift 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //
  2. // AccountBenefitsController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by User-Tangchao on 2024/10/29.
  6. //
  7. import Cocoa
  8. class AccountBenefitsController: NSViewController {
  9. @IBOutlet weak var leftBox: NSBox!
  10. @IBOutlet weak var rightBox: NSBox!
  11. @IBOutlet weak var leftContentBox: NSBox!
  12. @IBOutlet weak var leftBackgroundIv: NSImageView!
  13. @IBOutlet weak var leftTitleLabel: NSTextField!
  14. @IBOutlet weak var leftRightListBox: NSBox!
  15. @IBOutlet weak var tabBox: NSBox!
  16. @IBOutlet weak var tabSegmentLabel: NSTextField!
  17. @IBOutlet weak var tabSegmentLabel2: NSTextField!
  18. @IBOutlet weak var tabSegmentLabel3: NSTextField!
  19. @IBOutlet weak var tabBottomLine: NSView!
  20. @IBOutlet weak var tabSliderView: NSView!
  21. @IBOutlet weak var scrollView: NSScrollView!
  22. @IBOutlet weak var tableView: NSTableView!
  23. @IBOutlet weak var tabSegmentButton: NSButton!
  24. @IBOutlet weak var tabSegmentButton2: NSButton!
  25. @IBOutlet weak var tabSegmentButton3: NSButton!
  26. @IBOutlet weak var tabSliderLeadConst: NSLayoutConstraint!
  27. var rightDatas: [String] = [] {
  28. didSet {
  29. self.rightListView?.rightDatas = self.rightDatas
  30. }
  31. }
  32. var rightListView: AccountRightListView?
  33. var datas: [ProductListModel] = []
  34. private var tabIndex_: Int = 0
  35. private var currentModel_: ProductListModel?
  36. convenience init() {
  37. self.init(nibName: "AccountBenefitsController", bundle: MainBundle)
  38. }
  39. override func viewDidLoad() {
  40. super.viewDidLoad()
  41. // Do view setup here.
  42. self.leftBox.borderWidth = 0
  43. self.leftContentBox.borderWidth = 0
  44. self.leftTitleLabel.stringValue = NSLocalizedString("Purchase and unlock all features", comment: "")
  45. self.leftTitleLabel.font = .systemFont(ofSize: 18)
  46. self.leftTitleLabel.textColor = KMAppearance.themeColor()
  47. self.leftBackgroundIv.image = NSImage(named: "KMImageNameAccountBenefitBg")
  48. self.leftRightListBox.borderWidth = 0
  49. self.leftRightListBox.fillColor = .clear
  50. let rightListView = AccountRightListView.createFromNib()
  51. self.leftRightListBox.contentView = rightListView
  52. self.rightListView = rightListView
  53. rightListView?.rightDatas = self.rightDatas
  54. self.rightBox.borderWidth = 0
  55. self.tabBox.borderWidth = 0
  56. // self.tabSegmentLabel.stringValue = NSLocalizedString("Win+Mac", comment: "")
  57. self.tabSegmentLabel.font = .systemFont(ofSize: 16)
  58. // self.tabSegmentLabel2.stringValue = NSLocalizedString("Win", comment: "")
  59. self.tabSegmentLabel2.font = .systemFont(ofSize: 16)
  60. // self.tabSegmentLabel3.stringValue = NSLocalizedString("Mac", comment: "")
  61. self.tabSegmentLabel3.font = .systemFont(ofSize: 16)
  62. if let model = self.datas.safe_element(for: 0) as? ProductListModel {
  63. let msg = model.name ?? ""
  64. self.tabSegmentLabel.stringValue = NSLocalizedString(msg, comment: "")
  65. }
  66. if let model = self.datas.safe_element(for: 1) as? ProductListModel {
  67. let msg = model.name ?? ""
  68. self.tabSegmentLabel2.stringValue = NSLocalizedString(msg, comment: "")
  69. }
  70. if let model = self.datas.safe_element(for: 2) as? ProductListModel {
  71. let msg = model.name ?? ""
  72. self.tabSegmentLabel3.stringValue = NSLocalizedString(msg, comment: "")
  73. }
  74. self.tabSegmentButton.target = self
  75. self.tabSegmentButton.action = #selector(_tabSegmentAction)
  76. // self.tabSegmentButton.wantsLayer = true
  77. // self.tabSegmentButton.layer?.backgroundColor = NSColor.red.cgColor
  78. self.tabSegmentButton2.target = self
  79. self.tabSegmentButton2.action = #selector(_tabSegmentAction)
  80. self.tabSegmentButton3.target = self
  81. self.tabSegmentButton3.action = #selector(_tabSegmentAction)
  82. self.tabBottomLine.wantsLayer = true
  83. self.tabBottomLine.layer?.backgroundColor = NSColor(hex: "#F4F4F4").cgColor
  84. self.tabSliderView.wantsLayer = true
  85. self.tabSliderView.layer?.backgroundColor = KMAppearance.themeColor().cgColor
  86. self.tableView.delegate = self
  87. self.tableView.dataSource = self
  88. self.tableView.selectionHighlightStyle = .none
  89. self._selectTab(index: 0)
  90. }
  91. private func _selectTab(index: Int) {
  92. self.tabIndex_ = index
  93. if index == 0 {
  94. self.tabSegmentLabel.textColor = KMAppearance.themeColor()
  95. self.tabSegmentLabel2.textColor = KMAppearance.titleColor()
  96. self.tabSegmentLabel3.textColor = KMAppearance.titleColor()
  97. let x = self.tabSegmentLabel.frame.origin.x
  98. self.tabSliderLeadConst.constant = x + 19
  99. } else if index == 1 {
  100. self.tabSegmentLabel.textColor = KMAppearance.titleColor()
  101. self.tabSegmentLabel2.textColor = KMAppearance.themeColor()
  102. self.tabSegmentLabel3.textColor = KMAppearance.titleColor()
  103. let x = self.tabSegmentLabel2.frame.origin.x
  104. self.tabSliderLeadConst.constant = x + 4
  105. } else if index == 2 {
  106. self.tabSegmentLabel.textColor = KMAppearance.titleColor()
  107. self.tabSegmentLabel2.textColor = KMAppearance.titleColor()
  108. self.tabSegmentLabel3.textColor = KMAppearance.themeColor()
  109. let x = self.tabSegmentLabel3.frame.origin.x
  110. self.tabSliderLeadConst.constant = x + 4
  111. }
  112. guard let model = self.datas.safe_element(for: index) as? ProductListModel else {
  113. return
  114. }
  115. self.currentModel_ = model
  116. DispatchQueue.main.async {
  117. self.tableView.reloadData()
  118. }
  119. }
  120. @objc private func _tabSegmentAction(_ sender: NSButton) {
  121. if self.tabSegmentButton.isEqual(to: sender) {
  122. self._selectTab(index: 0)
  123. } else if self.tabSegmentButton2.isEqual(to: sender) {
  124. self._selectTab(index: 1)
  125. } else if self.tabSegmentButton3.isEqual(to: sender) {
  126. self._selectTab(index: 2)
  127. }
  128. }
  129. }
  130. extension AccountBenefitsController: NSTableViewDelegate, NSTableViewDataSource {
  131. func numberOfRows(in tableView: NSTableView) -> Int {
  132. // return 3
  133. return self.currentModel_?.list?.count ?? 0
  134. }
  135. func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
  136. var view = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "cell"), owner: self) as? AccountBenefitCellView
  137. if view == nil {
  138. view = AccountBenefitCellView()
  139. }
  140. let model = self.currentModel_?.list?[row]
  141. // "1-Month Plan (%d Devices)"
  142. let titleString = (model?.name ?? "")+" (%d Devices)"
  143. let nameString = NSLocalizedString(titleString, comment: "")
  144. view?.titleLabel.stringValue = String(format: nameString, model?.devices_num ?? 0)
  145. view?.subTitleLabel.stringValue = "$89.99  $139.99"
  146. let attri = NSMutableAttributedString(string: "$"+String(format: "%.2f", model?.actual_price ?? 0)+" ", attributes: [.font : NSFont.systemFont(ofSize: 24), .foregroundColor : KMAppearance.themeColor()])
  147. attri.append(.init(string: "$"+String(format: "%.2f", model?.virtual_price ?? 0), attributes: [.font : NSFont.systemFont(ofSize: 16), .strikethroughStyle : 1]))
  148. view?.subTitleLabel.attributedStringValue = attri
  149. view?.hotIv.isHidden = (model?.is_hot ?? 0) == 1
  150. view?.itemClick = { idx, _ in
  151. if idx == 1 { // 购买
  152. KMTools.openURL(urlString: model?.buy_url)
  153. }
  154. }
  155. return view
  156. }
  157. func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
  158. return 126;
  159. }
  160. }