KMAccountInfoView.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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(12.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. #if VERSION_DMG
  117. if userInfo?.subscriptionInfoList[0].payType == 1 {
  118. self.subscribeButtonContentView.isHidden = true
  119. } else {
  120. self.subscribeButtonContentView.isHidden = false
  121. }
  122. #endif
  123. self.statusLabel.stringValue = NSLocalizedString("On Subscription", comment: "")
  124. if purchaseState == .trial {
  125. self.statusLabel.stringValue = NSLocalizedString("On Trial", comment: "")
  126. }
  127. let dateString = userInfo?.subscriptionInfoList[0].endDate ?? ""
  128. self.statusTimeLabel.stringValue = "\(self.fetchDate(dateString: dateString)) \n\(NSLocalizedString("Subscription expire on", comment: ""))"
  129. } else if purchaseState == .subscriptionExpired ||
  130. purchaseState == .trialExpired {
  131. self.statusContentView.isHidden = false
  132. // self.purchaseInfoContentView.isHidden = false
  133. self.subscribeButtonContentView.isHidden = false
  134. self.statusLabel.stringValue = NSLocalizedString("Expired", comment: "")
  135. self.statusLabel.textColor = NSColor(hex: "#F3465B")
  136. let dateString = userInfo?.subscriptionInfoList[0].endDate ?? ""
  137. self.statusTimeLabel.stringValue = "\(self.fetchDate(dateString: dateString)) \n\(NSLocalizedString("Subscription expire on", comment: ""))"
  138. } else {
  139. self.statusContentView.isHidden = true
  140. // self.purchaseInfoContentView.isHidden = true
  141. self.subscribeButtonContentView.isHidden = true
  142. }
  143. if self.userInfo != nil {
  144. if userInfo?.subscriptionInfoList[0].payType == 1 {
  145. let dateString = userInfo?.subscriptionInfoList[0].endDate ?? ""
  146. self.statusTimeLabel.stringValue = "\(self.fetchDate(dateString: dateString))\n\(NSLocalizedString("Subscription renewal date", comment: ""))"
  147. }
  148. }
  149. self.updateLanguage()
  150. }
  151. override func updateLanguage() {
  152. super.updateLanguage()
  153. self.purchaseInfoButton.title = NSLocalizedString("Order Information", comment: "")
  154. self.tagLabel.stringValue = NSLocalizedString("Auto-renewal", comment: "")
  155. self.statusTitleLabel.stringValue = NSLocalizedString("Status", comment: "")
  156. self.accountDescribeLabel.stringValue = NSLocalizedString("Email Address", comment: "")
  157. self.titleLabel.stringValue = NSLocalizedString("Account Information", comment: "")
  158. // self.logOutButton.stringValue = NSLocalizedString("Log Out", comment: "")
  159. let timeString = self.statusTimeLabel.stringValue
  160. let paragraphStyle = NSMutableParagraphStyle()
  161. paragraphStyle.lineHeightMultiple = 1.32
  162. paragraphStyle.alignment = .right
  163. self.statusTimeLabel.attributedStringValue = NSMutableAttributedString(string: NSLocalizedString(timeString, comment: ""), attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
  164. let paragraphStyle2 = NSMutableParagraphStyle()
  165. paragraphStyle2.lineHeightMultiple = 1.32
  166. paragraphStyle2.alignment = .left
  167. self.statusLabel.attributedStringValue = NSMutableAttributedString(string: NSLocalizedString(self.statusLabel.stringValue, comment: ""), attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle2])
  168. }
  169. func fetchDate(dateString: String) -> String {
  170. let dateFormatter = DateFormatter()
  171. dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  172. let date = dateFormatter.date(from: dateString)
  173. let targetDateFormatter = DateFormatter()
  174. targetDateFormatter.dateFormat = "yyyy-MM-dd"
  175. let formattedDate = targetDateFormatter.string(from: date ?? NSDate() as Date)
  176. KMPrint(formattedDate) // 输出:2024-06-28
  177. return formattedDate
  178. }
  179. }
  180. extension KMAccountInfoView {
  181. func updateMoreButtonState(select: Bool) {
  182. if select {
  183. self.moreButton.backgroundColor(NSColor(hex: "#CED0D4", alpha: 0.6))
  184. self.moreButton.border(NSColor(hex: "#CED0D4"), 1, 4)
  185. } else {
  186. self.moreButton.backgroundColor(NSColor.clear)
  187. self.moreButton.border(NSColor(hex: "#CED0D4"), 0, 4)
  188. }
  189. }
  190. }
  191. protocol KMAccountInfoViewAction {}
  192. extension KMAccountInfoView: KMAccountInfoViewAction {
  193. @IBAction func closeButtonAction(_ sender: Any) {
  194. guard let callBack = closeAction else { return }
  195. callBack(self)
  196. }
  197. @IBAction func moreButtonAction(_ sender: NSButton) {
  198. self.updateMoreButtonState(select: true)
  199. let vc: KMHomePopViewController = KMHomePopViewController().initWithPopViewDataArr([NSLocalizedString("Log out", comment: ""), NSLocalizedString("Cancel", comment: "")])
  200. let createFilePopover: NSPopover = NSPopover.init()
  201. createFilePopover.contentViewController = vc
  202. createFilePopover.delegate = self
  203. createFilePopover.animates = true
  204. createFilePopover.behavior = .semitransient
  205. createFilePopover.setValue(true, forKey: "shouldHideAnchor")
  206. 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)
  207. // vc.customBoxWidthLayoutConstraint.constant = sender.frame.width
  208. vc.downCallback = { [unowned self] (downEntered: Bool, count: String) -> Void in
  209. if downEntered {
  210. if count == NSLocalizedString("Cancel", comment: "") {
  211. createFilePopover.close()
  212. guard let callBack = self.cancellationAction else { return }
  213. callBack(self)
  214. } else if count == "Log out" {
  215. guard let callBack = logOutAction else { return }
  216. callBack(self)
  217. }
  218. }
  219. }
  220. }
  221. @IBAction func purchaseButtonAction(_ sender: Any) {
  222. guard let callBack = purchaseInfoAction else { return }
  223. callBack(self)
  224. }
  225. @IBAction func subcriptionButtonAction(_ sender: Any) {
  226. guard let callBack = subcriptionAction else { return }
  227. callBack(self)
  228. }
  229. }
  230. extension KMAccountInfoView: NSPopoverDelegate {
  231. func popoverWillClose(_ notification: Notification) {
  232. self.updateMoreButtonState(select: false)
  233. }
  234. }