KMAccountInfoView.swift 13 KB

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