KMAccountInfoView.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. //
  2. // KMAccountInfoView.swift
  3. // PDF Master
  4. //
  5. // Created by lizhe on 2023/2/24.
  6. //
  7. import Cocoa
  8. typealias KMAccountInfoViewCloseAction = (_ view: KMAccountInfoView) -> Void
  9. typealias KMAccountInfoViewCancellationAction = (_ view: KMAccountInfoView) -> Void
  10. typealias KMAccountInfoViewLogOutAction = (_ view: KMAccountInfoView) -> Void
  11. typealias KMAccountInfoViewPurchaseInfoAction = (_ view: KMAccountInfoView) -> Void
  12. typealias KMAccountInfoViewSubcriptionAction = (_ view: KMAccountInfoView) -> Void
  13. class KMAccountInfoView: KMBaseXibView {
  14. @IBOutlet weak var titleLabel: NSTextField!
  15. @IBOutlet weak var closeButton: NSButton!
  16. @IBOutlet weak var infoContentView: NSView!
  17. @IBOutlet weak var moreButton: NSButton!
  18. @IBOutlet weak var imageViewButton: NSButton!
  19. @IBOutlet weak var accountDescribeLabel: NSTextField!
  20. @IBOutlet weak var accountLabel: NSTextField!
  21. @IBOutlet weak var closeBox: KMBox!
  22. @IBOutlet weak var statusTitleLabel: NSTextField!
  23. @IBOutlet weak var statusLabel: NSTextField!
  24. @IBOutlet weak var tagLabel: NSTextField!
  25. @IBOutlet weak var tagContentView: NSView!
  26. @IBOutlet weak var statusTimeLabel: NSTextField!
  27. @IBOutlet weak var purchaseInfoButton: NSButton!
  28. @IBOutlet weak var lineView: NSView!
  29. @IBOutlet weak var subscribeButton: NSButton!
  30. @IBOutlet weak var statusContentView: NSView!
  31. @IBOutlet weak var purchaseInfoContentView: NSView!
  32. @IBOutlet weak var subscribeButtonContentView: NSView!
  33. var closeAction: KMAccountInfoViewCloseAction?
  34. var cancellationAction: KMAccountInfoViewCancellationAction?
  35. var logOutAction: KMAccountInfoViewLogOutAction?
  36. var purchaseInfoAction: KMAccountInfoViewPurchaseInfoAction?
  37. var subcriptionAction: KMAccountInfoViewSubcriptionAction?
  38. var userInfo: KMLightMemberUserInfo? {
  39. didSet {
  40. self.reloadData()
  41. }
  42. }
  43. var purchaseState: KMPurchaseManagerState = .unknow {
  44. didSet {
  45. self.reloadData()
  46. }
  47. }
  48. override func draw(_ dirtyRect: NSRect) {
  49. super.draw(dirtyRect)
  50. // Drawing code here.
  51. }
  52. override func setup() {
  53. super.setup()
  54. self.closeBox.moveCallback = { [weak self] (mouseEntered, mouseBox) in
  55. if mouseEntered {
  56. self?.closeButton.image = NSImage(named: "control_btn_icon_close_hov")
  57. } else {
  58. self?.closeButton.image = NSImage(named: "control_btn_icon_close")
  59. }
  60. }
  61. }
  62. override func updateUI() {
  63. super.updateUI()
  64. self.titleLabel.textColor = NSColor(hex: "#252629")
  65. self.titleLabel.font = NSFont.SFProTextSemibold(20.0)
  66. self.accountLabel.textColor = NSColor(hex: "#252629")
  67. self.accountLabel.font = NSFont.SFProTextRegular(14.0)
  68. self.accountDescribeLabel.textColor = NSColor(hex: "#616469")
  69. self.accountDescribeLabel.font = NSFont.SFProTextSemibold(12.0)
  70. self.imageViewButton.backgroundColor(NSColor(hex: "#1770F4"))
  71. self.imageViewButton.font = NSFont.SFProTextSemibold(28.0)
  72. self.imageViewButton.contentTintColor = NSColor(hex: "#FFFFFF")
  73. self.imageViewButton.border(NSColor(hex: "#1770F4"), 1, 24)
  74. self.infoContentView.backgroundColor(NSColor(hex: "#F7F8FA"))
  75. self.infoContentView.border(NSColor(hex: "#F7F8FA"), 0, 6)
  76. self.purchaseInfoButton.font = NSFont.SFProTextRegular(14)
  77. self.purchaseInfoButton.contentTintColor = NSColor(hex: "#1770F4")
  78. self.statusTitleLabel.textColor = NSColor(hex: "#616469")
  79. self.statusTitleLabel.font = NSFont.SFProTextRegular(12.0)
  80. self.statusLabel.textColor = NSColor(hex: "#252629")
  81. self.statusLabel.font = NSFont.SFProTextSemibold(16.0)
  82. self.statusTimeLabel.textColor = NSColor(hex: "#616469")
  83. self.statusTimeLabel.font = NSFont.SFProTextRegular(10.0)
  84. self.subscribeButton.contentTintColor = NSColor(hex: "#FFFFFF")
  85. self.subscribeButton.backgroundColor(NSColor(hex: "#FF852E"))
  86. self.subscribeButton.font = NSFont.SFProTextRegular(16)
  87. self.subscribeButton.border(NSColor.clear, 0, 20)
  88. self.tagLabel.font = NSFont.SFProTextRegular(12)
  89. self.tagLabel.textColor = NSColor(hex: "#252629")
  90. self.tagContentView.border(NSColor(hex: "#DFE1E5"), 1, 4)
  91. self.tagContentView.backgroundColor(NSColor(hex: "#F7F8FA"))
  92. self.lineView.backgroundColor(NSColor(hex: "#ECECEC"))
  93. self.updateMoreButtonState(select: false)
  94. }
  95. override func reloadData() {
  96. super.reloadData()
  97. if self.userInfo != nil {
  98. self.accountLabel.stringValue = userInfo!.email
  99. self.imageViewButton.title = userInfo?.email.substring(to: 1) ?? ""
  100. if userInfo?.subscriptionInfoList[0].payType == 1 {
  101. self.tagContentView.isHidden = false
  102. } else {
  103. self.tagContentView.isHidden = true
  104. }
  105. }
  106. self.statusTimeLabel.stringValue = ""
  107. self.statusLabel.textColor = NSColor(hex: "#252629")
  108. if purchaseState == .unknow {
  109. self.statusContentView.isHidden = true
  110. // self.purchaseInfoContentView.isHidden = true
  111. self.subscribeButtonContentView.isHidden = true
  112. } else if purchaseState == .subscription ||
  113. purchaseState == .trial {
  114. self.statusContentView.isHidden = false
  115. // self.purchaseInfoContentView.isHidden = false
  116. self.subscribeButtonContentView.isHidden = true
  117. self.statusLabel.stringValue = NSLocalizedString("On Subscription", comment: "")
  118. if purchaseState == .trial {
  119. self.statusLabel.stringValue = NSLocalizedString("On Trial", comment: "")
  120. }
  121. let dateString = userInfo?.subscriptionInfoList[0].endDate ?? ""
  122. self.statusTimeLabel.stringValue = "\(self.fetchDate(dateString: dateString)) \n\(NSLocalizedString("Trial expires", comment: ""))"
  123. } else if purchaseState == .subscriptionExpired ||
  124. purchaseState == .trialExpired {
  125. self.statusContentView.isHidden = false
  126. // self.purchaseInfoContentView.isHidden = false
  127. self.subscribeButtonContentView.isHidden = false
  128. self.statusLabel.stringValue = NSLocalizedString("Expired", comment: "")
  129. self.statusLabel.textColor = NSColor(hex: "#F3465B")
  130. let dateString = userInfo?.subscriptionInfoList[0].endDate ?? ""
  131. self.statusTimeLabel.stringValue = "\(self.fetchDate(dateString: dateString)) \n\(NSLocalizedString("Trial expires", comment: ""))"
  132. } else {
  133. self.statusContentView.isHidden = true
  134. // self.purchaseInfoContentView.isHidden = true
  135. self.subscribeButtonContentView.isHidden = true
  136. }
  137. if self.userInfo != nil {
  138. if userInfo?.subscriptionInfoList[0].payType == 1 {
  139. let dateString = userInfo?.subscriptionInfoList[0].endDate ?? ""
  140. self.statusTimeLabel.stringValue = "\(self.fetchDate(dateString: dateString))\n\(NSLocalizedString("Next subscription", comment: ""))"
  141. }
  142. }
  143. self.updateLanguage()
  144. }
  145. override func updateLanguage() {
  146. super.updateLanguage()
  147. self.accountDescribeLabel.stringValue = NSLocalizedString("Email Address", comment: "")
  148. self.titleLabel.stringValue = NSLocalizedString("Account Information", comment: "")
  149. // self.logOutButton.stringValue = NSLocalizedString("Log Out", comment: "")
  150. let timeString = self.statusTimeLabel.stringValue
  151. let paragraphStyle = NSMutableParagraphStyle()
  152. paragraphStyle.lineHeightMultiple = 1.32
  153. paragraphStyle.alignment = .right
  154. self.statusTimeLabel.attributedStringValue = NSMutableAttributedString(string: NSLocalizedString(timeString, comment: ""), attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
  155. let paragraphStyle2 = NSMutableParagraphStyle()
  156. paragraphStyle2.lineHeightMultiple = 1.32
  157. paragraphStyle2.alignment = .left
  158. self.statusLabel.attributedStringValue = NSMutableAttributedString(string: NSLocalizedString(self.statusLabel.stringValue, comment: ""), attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle2])
  159. }
  160. func fetchDate(dateString: String) -> String {
  161. let dateFormatter = DateFormatter()
  162. dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  163. let date = dateFormatter.date(from: dateString)
  164. let targetDateFormatter = DateFormatter()
  165. targetDateFormatter.dateFormat = "yyyy-MM-dd"
  166. let formattedDate = targetDateFormatter.string(from: date ?? NSDate() as Date)
  167. KMPrint(formattedDate) // 输出:2024-06-28
  168. return formattedDate
  169. }
  170. }
  171. extension KMAccountInfoView {
  172. func updateMoreButtonState(select: Bool) {
  173. if select {
  174. self.moreButton.backgroundColor(NSColor(hex: "#CED0D4", alpha: 0.6))
  175. self.moreButton.border(NSColor(hex: "#CED0D4"), 1, 4)
  176. } else {
  177. self.moreButton.backgroundColor(NSColor.clear)
  178. self.moreButton.border(NSColor(hex: "#CED0D4"), 0, 4)
  179. }
  180. }
  181. }
  182. protocol KMAccountInfoViewAction {}
  183. extension KMAccountInfoView: KMAccountInfoViewAction {
  184. @IBAction func closeButtonAction(_ sender: Any) {
  185. guard let callBack = closeAction else { return }
  186. callBack(self)
  187. }
  188. @IBAction func moreButtonAction(_ sender: NSButton) {
  189. self.updateMoreButtonState(select: true)
  190. let vc: KMHomePopViewController = KMHomePopViewController().initWithPopViewDataArr([NSLocalizedString("Log out", comment: ""), NSLocalizedString("Cancellation", comment: "")])
  191. let createFilePopover: NSPopover = NSPopover.init()
  192. createFilePopover.contentViewController = vc
  193. createFilePopover.delegate = self
  194. createFilePopover.animates = true
  195. createFilePopover.behavior = .semitransient
  196. createFilePopover.setValue(true, forKey: "shouldHideAnchor")
  197. createFilePopover.show(relativeTo: CGRect(x: sender.bounds.origin.x, y: 10, width: sender.bounds.size.width, height: sender.bounds.size.height), of: sender, preferredEdge: .maxY)
  198. // vc.customBoxWidthLayoutConstraint.constant = sender.frame.width
  199. vc.downCallback = { [unowned self] (downEntered: Bool, count: String) -> Void in
  200. if downEntered {
  201. if count == "Cancellation" {
  202. createFilePopover.close()
  203. guard let callBack = self.cancellationAction else { return }
  204. callBack(self)
  205. } else if count == "Log out" {
  206. guard let callBack = logOutAction else { return }
  207. callBack(self)
  208. }
  209. }
  210. }
  211. }
  212. @IBAction func purchaseButtonAction(_ sender: Any) {
  213. guard let callBack = purchaseInfoAction else { return }
  214. callBack(self)
  215. }
  216. @IBAction func subcriptionButtonAction(_ sender: Any) {
  217. guard let callBack = subcriptionAction else { return }
  218. callBack(self)
  219. }
  220. }
  221. extension KMAccountInfoView: NSPopoverDelegate {
  222. func popoverWillClose(_ notification: Notification) {
  223. self.updateMoreButtonState(select: false)
  224. }
  225. }