KMAccountInfoView.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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.km_init(hex: "#252629")
  68. self.titleLabel.font = NSFont.SFProTextSemiboldFont(20.0)
  69. self.accountLabel.textColor = NSColor.km_init(hex: "#252629")
  70. self.accountLabel.font = NSFont.SFProTextRegularFont(14.0)
  71. self.accountDescribeLabel.textColor = NSColor.km_init(hex: "#616469")
  72. self.accountDescribeLabel.font = NSFont.SFProTextSemiboldFont(12.0)
  73. self.imageViewButton.backgroundColor(NSColor.km_init(hex: "#1770F4"))
  74. self.imageViewButton.font = NSFont.SFProTextSemiboldFont(28.0)
  75. self.imageViewButton.contentTintColor = NSColor.km_init(hex: "#FFFFFF")
  76. self.imageViewButton.border(NSColor.km_init(hex: "#1770F4"), 1, 24)
  77. self.infoContentView.backgroundColor(NSColor.km_init(hex: "#F7F8FA"))
  78. self.infoContentView.border(NSColor.km_init(hex: "#F7F8FA"), 0, 6)
  79. self.purchaseInfoButton.font = NSFont.SFProTextRegularFont(14)
  80. self.purchaseInfoButton.contentTintColor = NSColor.km_init(hex: "#1770F4")
  81. self.statusTitleLabel.textColor = NSColor.km_init(hex: "#616469")
  82. self.statusTitleLabel.font = NSFont.SFProTextRegularFont(12.0)
  83. self.statusLabel.textColor = NSColor.km_init(hex: "#252629")
  84. self.statusLabel.font = NSFont.SFProTextSemiboldFont(16.0)
  85. self.statusTimeLabel.textColor = NSColor.km_init(hex: "#616469")
  86. self.statusTimeLabel.font = NSFont.SFProTextRegularFont(12.0)
  87. self.subscribeButton.contentTintColor = NSColor.km_init(hex: "#FFFFFF")
  88. self.subscribeButton.backgroundColor(NSColor.km_init(hex: "#FF852E"))
  89. self.subscribeButton.font = NSFont.SFProTextRegularFont(16)
  90. self.subscribeButton.border(NSColor.clear, 0, 20)
  91. self.tagLabel.font = NSFont.SFProTextRegularFont(12)
  92. self.tagLabel.textColor = NSColor.km_init(hex: "#252629")
  93. self.tagContentView.border(NSColor.km_init(hex: "#DFE1E5"), 1, 4)
  94. self.tagContentView.backgroundColor(NSColor.km_init(hex: "#F7F8FA"))
  95. self.lineView.backgroundColor(NSColor.km_init(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 KMLightMemberManager.manager.info.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.km_init(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 KMLightMemberManager.manager.info.subscriptionInfoList[0].payType == 1 {
  132. self.subscribeButtonContentView.isHidden = true
  133. } else {
  134. self.subscribeButtonContentView.isHidden = false
  135. }
  136. #else
  137. self.purchaseInfoContentView.isHidden = true
  138. #endif
  139. self.statusLabel.stringValue = NSLocalizedString("On Subscription", comment: "")
  140. if purchaseState == .trial {
  141. self.statusLabel.stringValue = NSLocalizedString("On Trial", comment: "")
  142. }
  143. let dateString = userInfo?.subscriptionInfoList[0].endDate ?? ""
  144. self.statusTimeLabel.stringValue = "\(self.fetchDate(dateString: dateString)) \n\(NSLocalizedString("Subscription expire on", comment: ""))"
  145. } else if purchaseState == .subscriptionExpired ||
  146. purchaseState == .trialExpired {
  147. self.statusContentView.isHidden = false
  148. self.subscribeButtonContentView.isHidden = false
  149. #if VERSION_DMG
  150. self.purchaseInfoContentView.isHidden = false
  151. #else
  152. self.purchaseInfoContentView.isHidden = true
  153. #endif
  154. self.statusLabel.stringValue = NSLocalizedString("Expired", comment: "")
  155. self.statusLabel.textColor = NSColor.km_init(hex: "#F3465B")
  156. let dateString = userInfo?.subscriptionInfoList[0].endDate ?? ""
  157. self.statusTimeLabel.stringValue = "\(self.fetchDate(dateString: dateString)) \n\(NSLocalizedString("Subscription expire on", comment: ""))"
  158. }
  159. if self.userInfo != nil {
  160. if KMLightMemberManager.manager.info.subscriptionInfoList[0].payType == 1 {
  161. let dateString = userInfo?.subscriptionInfoList[0].endDate ?? ""
  162. self.statusTimeLabel.stringValue = "\(self.fetchDate(dateString: dateString))\n\(NSLocalizedString("Subscription renewal date", comment: ""))"
  163. }
  164. }
  165. self.updateLanguage()
  166. }
  167. override func updateLanguage() {
  168. super.updateLanguage()
  169. self.purchaseInfoButton.title = NSLocalizedString("Order Information", comment: "")
  170. self.tagLabel.stringValue = NSLocalizedString("Auto-renewal", comment: "")
  171. self.statusTitleLabel.stringValue = NSLocalizedString("Status", comment: "")
  172. self.accountDescribeLabel.stringValue = NSLocalizedString("Email Address", comment: "")
  173. self.titleLabel.stringValue = NSLocalizedString("Account Information", comment: "")
  174. // self.logOutButton.stringValue = NSLocalizedString("Log Out", comment: "")
  175. let timeString = self.statusTimeLabel.stringValue
  176. let paragraphStyle = NSMutableParagraphStyle()
  177. paragraphStyle.lineHeightMultiple = 1.32
  178. paragraphStyle.alignment = .right
  179. self.statusTimeLabel.attributedStringValue = NSMutableAttributedString(string: NSLocalizedString(timeString, comment: ""), attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
  180. let paragraphStyle2 = NSMutableParagraphStyle()
  181. paragraphStyle2.lineHeightMultiple = 1.32
  182. paragraphStyle2.alignment = .left
  183. self.statusLabel.attributedStringValue = NSMutableAttributedString(string: NSLocalizedString(self.statusLabel.stringValue, comment: ""), attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle2])
  184. }
  185. func fetchDate(dateString: String) -> String {
  186. let dateFormatter = DateFormatter()
  187. dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  188. let date = dateFormatter.date(from: dateString)
  189. let targetDateFormatter = DateFormatter()
  190. targetDateFormatter.dateFormat = "yyyy-MM-dd"
  191. let formattedDate = targetDateFormatter.string(from: date ?? NSDate() as Date)
  192. KMPrint(formattedDate) // 输出:2024-06-28
  193. return formattedDate
  194. }
  195. }
  196. extension KMAccountInfoView {
  197. func updateMoreButtonState(select: Bool) {
  198. if select {
  199. self.moreButton.backgroundColor(NSColor.km_init(hex: "#CED0D4", alpha: 0.6))
  200. self.moreButton.border(NSColor.km_init(hex: "#CED0D4"), 1, 4)
  201. } else {
  202. self.moreButton.backgroundColor(NSColor.clear)
  203. self.moreButton.border(NSColor.km_init(hex: "#CED0D4"), 0, 4)
  204. }
  205. }
  206. }
  207. protocol KMAccountInfoViewAction {}
  208. extension KMAccountInfoView: KMAccountInfoViewAction {
  209. @IBAction func closeButtonAction(_ sender: Any) {
  210. guard let callBack = closeAction else { return }
  211. callBack(self)
  212. }
  213. @IBAction func moreButtonAction(_ sender: NSButton) {
  214. self.updateMoreButtonState(select: true)
  215. let vc: KMHomePopViewController = KMHomePopViewController().initWithPopViewDataArr([NSLocalizedString("Log out", comment: ""), NSLocalizedString("Cancel", comment: "")])
  216. let createFilePopover: NSPopover = NSPopover.init()
  217. createFilePopover.contentViewController = vc
  218. createFilePopover.delegate = self
  219. createFilePopover.animates = true
  220. createFilePopover.behavior = .semitransient
  221. createFilePopover.setValue(true, forKey: "shouldHideAnchor")
  222. 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)
  223. // vc.customBoxWidthLayoutConstraint.constant = sender.frame.width
  224. vc.downCallback = { [unowned self] (downEntered: Bool, count: String) -> Void in
  225. if downEntered {
  226. if count == NSLocalizedString("Cancel", comment: "") {
  227. createFilePopover.close()
  228. guard let callBack = self.cancellationAction else { return }
  229. callBack(self)
  230. } else if count == "Log out" {
  231. guard let callBack = logOutAction else { return }
  232. callBack(self)
  233. }
  234. }
  235. }
  236. }
  237. @IBAction func purchaseButtonAction(_ sender: Any) {
  238. guard let callBack = purchaseInfoAction else { return }
  239. callBack(self)
  240. }
  241. @IBAction func subcriptionButtonAction(_ sender: Any) {
  242. guard let callBack = subcriptionAction else { return }
  243. callBack(self)
  244. }
  245. }
  246. extension KMAccountInfoView: NSPopoverDelegate {
  247. func popoverWillClose(_ notification: Notification) {
  248. self.updateMoreButtonState(select: false)
  249. }
  250. }