KMAccountInfoView.swift 12 KB

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