AccountRightCellView.swift 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. //
  2. // AccountRightCellView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by User-Tangchao on 2024/10/30.
  6. //
  7. import Cocoa
  8. class AccountRightTitleCellView: NSTableCellView {
  9. private lazy var label_: NSTextField = {
  10. let view = NSTextField(labelWithString: "")
  11. view.font = .systemFont(ofSize: 16)
  12. view.textColor = KMAppearance.titleColor()
  13. return view
  14. }()
  15. private lazy var button_: NSButton = {
  16. let view = NSButton()
  17. view.isBordered = false
  18. view.title = ""
  19. return view
  20. }()
  21. var titleLabel: NSTextField {
  22. get {
  23. return self.label_
  24. }
  25. }
  26. var moreButton: NSButton {
  27. get {
  28. return self.button_
  29. }
  30. }
  31. var itemClick: KMCommonClickBlock?
  32. override func draw(_ dirtyRect: NSRect) {
  33. super.draw(dirtyRect)
  34. // Drawing code here.
  35. }
  36. convenience init() {
  37. self.init(frame: .init(x: 0, y: 0, width: 200, height: 111))
  38. self.initSubviews()
  39. self.initDefaultValue()
  40. }
  41. override func awakeFromNib() {
  42. super.awakeFromNib()
  43. self.initSubviews()
  44. self.initDefaultValue()
  45. }
  46. func initSubviews() {
  47. self.addSubview(label_)
  48. self.addSubview(self.button_)
  49. self.button_.target = self
  50. self.button_.action = #selector(_buttonAction)
  51. self.label_.km_add_leading_constraint(constant: 10)
  52. self.label_.km_add_top_constraint(constant: 16)
  53. self.button_.km_add_trailing_constraint(constant: -20)
  54. self.button_.km_add_top_constraint(constant: 16)
  55. }
  56. func initDefaultValue() {
  57. }
  58. @objc private func _buttonAction() {
  59. self.itemClick?(1)
  60. }
  61. }
  62. class AccountRightCellView: NSTableCellView {
  63. private lazy var contentBox_: NSBox = {
  64. let view = NSBox()
  65. view.boxType = .custom
  66. view.titlePosition = .noTitle
  67. view.contentViewMargins = .zero
  68. view.borderWidth = 0
  69. return view
  70. }()
  71. private lazy var backgroundIv: NSImageView = {
  72. let view = NSImageView()
  73. view.image = NSImage(named: "KMImageNameRightListBg")
  74. view.imageScaling = .scaleNone
  75. return view
  76. }()
  77. private lazy var label_: NSTextField = {
  78. let view = NSTextField(labelWithString: "")
  79. view.font = .systemFont(ofSize: 18)
  80. view.textColor = KMAppearance.titleColor()
  81. return view
  82. }()
  83. private lazy var button_: NSButton = {
  84. let view = NSButton()
  85. view.isBordered = false
  86. view.title = ""
  87. view.wantsLayer = true
  88. view.layer?.cornerRadius = 4
  89. return view
  90. }()
  91. private lazy var hLine: NSView = {
  92. let view = NSView()
  93. view.wantsLayer = true
  94. view.layer?.backgroundColor = NSColor(hex: "#EBEFF4").cgColor
  95. return view
  96. }()
  97. private lazy var devicelabel_: NSTextField = {
  98. let view = NSTextField(labelWithString: "")
  99. return view
  100. }()
  101. private lazy var expireDatelabel_: NSTextField = {
  102. let view = NSTextField(labelWithString: "")
  103. return view
  104. }()
  105. private lazy var statelabel_: NSTextField = {
  106. let view = NSTextField(labelWithString: "")
  107. return view
  108. }()
  109. var model: AccountRightInfoModel? {
  110. didSet {
  111. // self.label_.stringValue = self.model?.name ?? ""
  112. let titleString = (model?.name ?? "")+" (%d Devices)"
  113. let nameString = NSLocalizedString(titleString, comment: "")
  114. self.label_.stringValue = String(format: nameString, model?.total_num ?? 0)
  115. let isSubscribing = (self.model?.automatically_subscribe ?? 0) == 1
  116. // self.button_.isHidden = !isSubscribing
  117. // self.statelabel_.isHidden = !isSubscribing
  118. self.button_.isHidden = true
  119. self.statelabel_.isHidden = true
  120. let status = self.model?.status ?? 0
  121. let expireTime = (self.model?.failure_time_text ?? "")
  122. if status == 1 { // 有效
  123. self.button_.layer?.borderWidth = 1
  124. self.button_.layer?.borderColor = NSColor(hex: "#CCCCCC").cgColor
  125. self.button_.layer?.backgroundColor = .white
  126. self.button_.title = NSLocalizedString("Cancel Subscription", comment: "")
  127. self.button_.setTitleColor(NSColor(hex: "#666666"))
  128. if model?.license_id == 3 { // 终身
  129. self.expireDatelabel_.attributedStringValue = .init(string: NSLocalizedString("Expires:", comment: "") + NSLocalizedString("Unlimited", comment: ""))
  130. } else {
  131. self.expireDatelabel_.attributedStringValue = .init(string: NSLocalizedString("Expires:", comment: "") + expireTime)
  132. }
  133. } else if status == 2 {
  134. self.button_.layer?.borderWidth = 0
  135. self.button_.layer?.backgroundColor = KMAppearance.themeColor().cgColor
  136. self.button_.title = NSLocalizedString("Renew", comment: "")
  137. self.button_.setTitleColor(.white)
  138. var attri = NSMutableAttributedString(string: NSLocalizedString("Expires:", comment: ""))
  139. attri.append(.init(string: expireTime, attributes: [.foregroundColor : NSColor(hex: "#FF0000")]))
  140. self.expireDatelabel_.attributedStringValue = attri
  141. self.statelabel_.stringValue = NSLocalizedString("Expired", comment: "")
  142. self.statelabel_.textColor = NSColor(hex: "#FF0000")
  143. self.button_.isHidden = false
  144. self.statelabel_.isHidden = false
  145. }
  146. let num = self.model?.surplus_num ?? 0
  147. let totalNum = self.model?.total_num ?? 0
  148. if isSubscribing {
  149. var attri = NSMutableAttributedString(string: NSLocalizedString("Device", comment: "")+NSLocalizedString("Available:", comment: ""))
  150. if num <= 0 {
  151. attri.append(.init(string: "\(num)/\(totalNum)", attributes: [.foregroundColor : NSColor(hex: "#FF0000")]))
  152. } else {
  153. attri.append(.init(string: "\(num)/\(totalNum)"))
  154. }
  155. self.devicelabel_.attributedStringValue = attri
  156. self.statelabel_.stringValue = NSLocalizedString("In Subscription", comment: "")
  157. self.statelabel_.textColor = NSColor(hex: "#333333")
  158. self.button_.isHidden = false
  159. self.statelabel_.isHidden = false
  160. } else {
  161. self.devicelabel_.attributedStringValue = .init(string: NSLocalizedString("Device", comment: "") + NSLocalizedString("Available:", comment: "") + "\(num)/\(totalNum)", attributes: [.foregroundColor : NSColor(hex: "#333333").withAlphaComponent(0.3)])
  162. }
  163. }
  164. }
  165. var itemClick: KMCommonClickBlock?
  166. override func draw(_ dirtyRect: NSRect) {
  167. super.draw(dirtyRect)
  168. // Drawing code here.
  169. }
  170. convenience init() {
  171. self.init(frame: .init(x: 0, y: 0, width: 200, height: 111))
  172. self.initSubviews()
  173. self.initDefaultValue()
  174. }
  175. override func awakeFromNib() {
  176. super.awakeFromNib()
  177. self.initSubviews()
  178. self.initDefaultValue()
  179. }
  180. func initSubviews() {
  181. self.addSubview(self.backgroundIv)
  182. self.backgroundIv.km_add_inset_constraint(inset: .init(top: 8, left: 12, bottom: 14, right: 12))
  183. self.addSubview(self.contentBox_)
  184. self.contentBox_.km_add_inset_constraint(inset: .init(top: 8, left: 8, bottom: 14, right: 8))
  185. self.contentBox_.contentView?.addSubview(label_)
  186. self.contentBox_.contentView?.addSubview(self.button_)
  187. self.button_.target = self
  188. self.button_.action = #selector(_buttonAction)
  189. self.label_.km_add_leading_constraint(constant: 20)
  190. self.label_.km_add_top_constraint(constant: 12)
  191. self.button_.km_add_trailing_constraint(constant: -20)
  192. self.button_.km_add_top_constraint(constant: 9)
  193. self.button_.km_add_size_constraint(size: .init(width: 168, height: 32))
  194. self.contentBox_.contentView?.addSubview(self.hLine)
  195. self.hLine.km_add_leading_constraint(constant: 4)
  196. self.hLine.km_add_trailing_constraint(constant: -4)
  197. self.hLine.km_add_height_constraint(constant: 1)
  198. self.hLine.km_add_top_constraint(equalTo: self.label_, attribute: .bottom, constant: 14)
  199. self.contentBox_.contentView?.addSubview(self.devicelabel_)
  200. self.contentBox_.contentView?.addSubview(self.expireDatelabel_)
  201. self.contentBox_.contentView?.addSubview(self.statelabel_)
  202. self.devicelabel_.km_add_leading_constraint(constant: 20)
  203. self.devicelabel_.km_add_top_constraint(equalTo: self.hLine, attribute: .bottom, constant: 20)
  204. self.expireDatelabel_.km_add_leading_constraint(equalTo: self.devicelabel_, attribute: .trailing, constant: 30)
  205. self.expireDatelabel_.km_add_top_constraint(equalTo: self.hLine, attribute: .bottom, constant: 20)
  206. self.statelabel_.km_add_leading_constraint(equalTo: self.expireDatelabel_, attribute: .trailing, constant: 30)
  207. self.statelabel_.km_add_top_constraint(equalTo: self.hLine, attribute: .bottom, constant: 20)
  208. }
  209. func initDefaultValue() {
  210. }
  211. @objc private func _buttonAction() {
  212. self.itemClick?(1)
  213. }
  214. }