KMAccountInfoView.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. typealias KMAccountInfoViewChangeEmailAction = (_ view: KMAccountInfoView) -> Void
  14. class KMAccountInfoView: KMBaseXibView {
  15. @IBOutlet weak var titleLabel: NSTextField!
  16. @IBOutlet weak var closeButton: NSButton!
  17. @IBOutlet weak var infoContentView: NSView!
  18. @IBOutlet weak var moreButton: NSButton!
  19. @IBOutlet weak var imageViewButton: NSButton!
  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 changeEemailButton: NSButton!
  34. @IBOutlet weak var changeEmailHelpButton: NSButton!
  35. @IBOutlet weak var subscriptionInfoContentViewHeightConstraint: NSLayoutConstraint!
  36. @IBOutlet weak var subscriptionInfoContentViewTopConstraint: NSLayoutConstraint!
  37. @IBOutlet weak var lineViewTopConttraint: NSLayoutConstraint!
  38. var popover: NSPopover?
  39. var closeAction: KMAccountInfoViewCloseAction?
  40. var cancellationAction: KMAccountInfoViewCancellationAction?
  41. var logOutAction: KMAccountInfoViewLogOutAction?
  42. var purchaseInfoAction: KMAccountInfoViewPurchaseInfoAction?
  43. var subcriptionAction: KMAccountInfoViewSubcriptionAction?
  44. var changeEmailAction: KMAccountInfoViewChangeEmailAction?
  45. var userInfo: KMLightMemberUserInfo? {
  46. didSet {
  47. self.reloadData()
  48. }
  49. }
  50. var purchaseState: KMPurchaseManagerState = .unknow {
  51. didSet {
  52. self.reloadData()
  53. }
  54. }
  55. override func draw(_ dirtyRect: NSRect) {
  56. super.draw(dirtyRect)
  57. // Drawing code here.
  58. }
  59. override func setup() {
  60. super.setup()
  61. self.closeBox.moveCallback = { [weak self] (mouseEntered, mouseBox) in
  62. if mouseEntered {
  63. self?.closeButton.image = NSImage(named: "control_btn_icon_close_hov")
  64. } else {
  65. self?.closeButton.image = NSImage(named: "control_btn_icon_close")
  66. }
  67. }
  68. }
  69. override func updateUI() {
  70. super.updateUI()
  71. self.titleLabel.textColor = NSColor.km_init(hex: "#252629")
  72. self.titleLabel.font = NSFont.SFProTextSemiboldFont(20.0)
  73. self.accountLabel.textColor = NSColor.km_init(hex: "#252629")
  74. self.accountLabel.font = NSFont.SFProTextRegularFont(14.0)
  75. self.changeEemailButton.font = NSFont.SFProTextRegularFont(12.0)
  76. self.changeEemailButton.contentTintColor = NSColor.km_init(hex: "#1770F4")
  77. self.imageViewButton.backgroundColor(NSColor.km_init(hex: "#1770F4"))
  78. self.imageViewButton.font = NSFont.SFProTextSemiboldFont(28.0)
  79. self.imageViewButton.contentTintColor = NSColor.km_init(hex: "#FFFFFF")
  80. self.imageViewButton.border(NSColor.km_init(hex: "#1770F4"), 1, 24)
  81. self.infoContentView.backgroundColor(NSColor.km_init(hex: "#F7F8FA"))
  82. self.infoContentView.border(NSColor.km_init(hex: "#F7F8FA"), 0, 6)
  83. self.purchaseInfoButton.font = NSFont.SFProTextRegularFont(14)
  84. self.purchaseInfoButton.contentTintColor = NSColor.km_init(hex: "#1770F4")
  85. self.statusTitleLabel.textColor = NSColor.km_init(hex: "#616469")
  86. self.statusTitleLabel.font = NSFont.SFProTextRegularFont(12.0)
  87. self.statusLabel.textColor = NSColor.km_init(hex: "#252629")
  88. self.statusLabel.font = NSFont.SFProTextSemiboldFont(16.0)
  89. self.statusTimeLabel.textColor = NSColor.km_init(hex: "#616469")
  90. self.statusTimeLabel.font = NSFont.SFProTextRegularFont(12.0)
  91. self.subscribeButton.contentTintColor = NSColor.km_init(hex: "#FFFFFF")
  92. self.subscribeButton.backgroundColor(NSColor.km_init(hex: "#FF852E"))
  93. self.subscribeButton.font = NSFont.SFProTextRegularFont(16)
  94. self.subscribeButton.border(NSColor.clear, 0, 20)
  95. self.tagLabel.font = NSFont.SFProTextRegularFont(12)
  96. self.tagLabel.textColor = NSColor.km_init(hex: "#252629")
  97. self.tagContentView.border(NSColor.km_init(hex: "#DFE1E5"), 1, 4)
  98. self.tagContentView.backgroundColor(NSColor.km_init(hex: "#F7F8FA"))
  99. self.lineView.backgroundColor(NSColor.km_init(hex: "#ECECEC"))
  100. self.updateMoreButtonState(select: false)
  101. }
  102. override func reloadData() {
  103. super.reloadData()
  104. if self.userInfo != nil {
  105. self.accountLabel.stringValue = userInfo!.email
  106. self.imageViewButton.title = userInfo?.email.substring(to: 1) ?? ""
  107. if KMLightMemberManager.manager.info.subscriptionInfoList[0].payType == 1 {
  108. self.tagContentView.isHidden = false
  109. } else {
  110. self.tagContentView.isHidden = true
  111. }
  112. }
  113. self.statusTimeLabel.stringValue = ""
  114. self.statusLabel.textColor = NSColor.km_init(hex: "#252629")
  115. //初始化
  116. self.purchaseInfoContentView.isHidden = KMLightMemberManager.manager.existOrderInfo() ? false:true
  117. self.subscribeButtonContentView.isHidden = true
  118. self.statusContentView.isHidden = true
  119. self.subscriptionInfoContentViewHeightConstraint.constant = 74
  120. self.subscriptionInfoContentViewTopConstraint.constant = 20
  121. self.lineViewTopConttraint.constant = 16
  122. self.lineView.isHidden = false
  123. if purchaseState == .unknow {
  124. #if VERSION_DMG
  125. self.subscriptionInfoContentViewHeightConstraint.constant = 0
  126. self.subscriptionInfoContentViewTopConstraint.constant = 0
  127. self.lineViewTopConttraint.constant = 12
  128. self.lineView.isHidden = true
  129. #endif
  130. } else if purchaseState == .subscription ||
  131. purchaseState == .trial {
  132. self.statusContentView.isHidden = false
  133. #if VERSION_DMG
  134. self.purchaseInfoContentView.isHidden = false
  135. if KMLightMemberManager.manager.info.subscriptionInfoList[0].payType == 1 {
  136. self.subscribeButtonContentView.isHidden = true
  137. } else {
  138. self.subscribeButtonContentView.isHidden = false
  139. }
  140. #else
  141. self.purchaseInfoContentView.isHidden = true
  142. #endif
  143. self.statusLabel.stringValue = NSLocalizedString("On Subscription", comment: "")
  144. if purchaseState == .trial {
  145. self.statusLabel.stringValue = NSLocalizedString("On Trial", comment: "")
  146. }
  147. let dateString = userInfo?.subscriptionInfoList[0].endDate ?? ""
  148. self.statusTimeLabel.stringValue = "\(self.fetchDate(dateString: dateString)) \n\(NSLocalizedString("Subscription expire on", comment: ""))"
  149. } else if purchaseState == .subscriptionExpired ||
  150. purchaseState == .trialExpired {
  151. self.statusContentView.isHidden = false
  152. self.subscribeButtonContentView.isHidden = false
  153. #if VERSION_DMG
  154. self.purchaseInfoContentView.isHidden = false
  155. #else
  156. self.purchaseInfoContentView.isHidden = true
  157. #endif
  158. self.statusLabel.stringValue = NSLocalizedString("Expired", comment: "")
  159. self.statusLabel.textColor = NSColor.km_init(hex: "#F3465B")
  160. let dateString = userInfo?.subscriptionInfoList[0].endDate ?? ""
  161. self.statusTimeLabel.stringValue = "\(self.fetchDate(dateString: dateString)) \n\(NSLocalizedString("Subscription expire on", comment: ""))"
  162. }
  163. if self.userInfo != nil {
  164. if KMLightMemberManager.manager.info.subscriptionInfoList[0].payType == 1 {
  165. let dateString = userInfo?.subscriptionInfoList[0].endDate ?? ""
  166. self.statusTimeLabel.stringValue = "\(self.fetchDate(dateString: dateString))\n\(NSLocalizedString("Subscription renewal date", comment: ""))"
  167. }
  168. }
  169. self.updateLanguage()
  170. }
  171. override func updateLanguage() {
  172. super.updateLanguage()
  173. self.purchaseInfoButton.title = NSLocalizedString("Order Information", comment: "")
  174. self.tagLabel.stringValue = NSLocalizedString("Auto-renewal", comment: "")
  175. self.statusTitleLabel.stringValue = NSLocalizedString("Status", comment: "")
  176. self.titleLabel.stringValue = NSLocalizedString("Account Information", comment: "")
  177. // self.logOutButton.stringValue = NSLocalizedString("Log Out", comment: "")
  178. let timeString = self.statusTimeLabel.stringValue
  179. let paragraphStyle = NSMutableParagraphStyle()
  180. paragraphStyle.lineHeightMultiple = 1.32
  181. paragraphStyle.alignment = .right
  182. self.statusTimeLabel.attributedStringValue = NSMutableAttributedString(string: NSLocalizedString(timeString, comment: ""), attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
  183. let paragraphStyle2 = NSMutableParagraphStyle()
  184. paragraphStyle2.lineHeightMultiple = 1.32
  185. paragraphStyle2.alignment = .left
  186. self.statusLabel.attributedStringValue = NSMutableAttributedString(string: NSLocalizedString(self.statusLabel.stringValue, comment: ""), attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle2])
  187. }
  188. func fetchDate(dateString: String) -> String {
  189. let dateFormatter = DateFormatter()
  190. dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  191. let date = dateFormatter.date(from: dateString)
  192. let targetDateFormatter = DateFormatter()
  193. targetDateFormatter.dateFormat = "yyyy-MM-dd"
  194. let formattedDate = targetDateFormatter.string(from: date ?? NSDate() as Date)
  195. KMPrint(formattedDate) // 输出:2024-06-28
  196. return formattedDate
  197. }
  198. }
  199. extension KMAccountInfoView {
  200. func updateMoreButtonState(select: Bool) {
  201. if select {
  202. self.moreButton.backgroundColor(NSColor.km_init(hex: "#CED0D4", alpha: 0.6))
  203. self.moreButton.border(NSColor.km_init(hex: "#CED0D4"), 1, 4)
  204. } else {
  205. self.moreButton.backgroundColor(NSColor.clear)
  206. self.moreButton.border(NSColor.km_init(hex: "#CED0D4"), 0, 4)
  207. }
  208. }
  209. }
  210. extension KMAccountInfoView {
  211. override func mouseDown(with event: NSEvent) {
  212. self.dismissPopview()
  213. }
  214. }
  215. protocol KMAccountInfoViewAction {}
  216. extension KMAccountInfoView: KMAccountInfoViewAction {
  217. @IBAction func closeButtonAction(_ sender: Any) {
  218. guard let callBack = closeAction else { return }
  219. callBack(self)
  220. }
  221. @IBAction func moreButtonAction(_ sender: NSButton) {
  222. self.updateMoreButtonState(select: true)
  223. let vc: KMHomePopViewController = KMHomePopViewController().initWithPopViewDataArr([NSLocalizedString("Log out", comment: ""), NSLocalizedString("Cancel", comment: "")])
  224. let createFilePopover: NSPopover = NSPopover.init()
  225. createFilePopover.contentViewController = vc
  226. createFilePopover.delegate = self
  227. createFilePopover.animates = true
  228. createFilePopover.behavior = .semitransient
  229. createFilePopover.setValue(true, forKey: "shouldHideAnchor")
  230. 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)
  231. // vc.customBoxWidthLayoutConstraint.constant = sender.frame.width
  232. vc.downCallback = { [unowned self] (downEntered: Bool, count: String) -> Void in
  233. if downEntered {
  234. if count == NSLocalizedString("Cancel", comment: "") {
  235. createFilePopover.close()
  236. guard let callBack = self.cancellationAction else { return }
  237. callBack(self)
  238. } else if count == "Log out" {
  239. guard let callBack = logOutAction else { return }
  240. callBack(self)
  241. }
  242. }
  243. }
  244. }
  245. @IBAction func purchaseButtonAction(_ sender: Any) {
  246. guard let callBack = purchaseInfoAction else { return }
  247. callBack(self)
  248. }
  249. @IBAction func subcriptionButtonAction(_ sender: Any) {
  250. guard let callBack = subcriptionAction else { return }
  251. callBack(self)
  252. }
  253. @IBAction func changeEmailButtonAction(_ sender: Any) {
  254. guard let callBack = changeEmailAction else { return }
  255. callBack(self)
  256. }
  257. @IBAction func changeEmailHelpButtonAction(_ sender: NSButton) {
  258. if self.popover == nil {
  259. let pop = NSPopover()
  260. self.popover = pop
  261. let controller = KMAccountInfoChangeEmailTipController()
  262. pop.contentViewController = controller
  263. controller.stringValue = """
  264. * Bind the current device's subscription benefits to the logged in member account.
  265. * Once a member is attached, you can use advanced features on other devices by logging in to your member account.
  266. """
  267. pop.setValue(true, forKey: "shouldHideAnchor")
  268. pop.show(relativeTo: NSMakeRect(0, -8, NSWidth(sender.bounds), NSHeight(sender.bounds)), of: sender, preferredEdge: .maxY)
  269. } else {
  270. self.dismissPopview()
  271. }
  272. }
  273. func dismissPopview() {
  274. if self.popover != nil {
  275. self.popover?.close()
  276. self.popover = nil
  277. }
  278. }
  279. }
  280. //extension KMAccountInfoView {
  281. // func dismissHelpTip() {
  282. //
  283. // }
  284. // - (void)showHelpTip:(NSButton *)sender {
  285. // if (self.popover) {
  286. // [self dismissHelpTip];
  287. // return;
  288. // }
  289. //
  290. // NSPopover *pop = [[NSPopover alloc] init];
  291. // self.popover = pop;
  292. // KMSignatureHelpViewController *controller = [[KMSignatureHelpViewController alloc] init];
  293. // pop.contentViewController = controller;
  294. // controller.tipString = NSLocalizedString(@"Remove white background from images", nil);
  295. // [pop setValue:@YES forKey:@"shouldHideAnchor"];
  296. //
  297. // [pop showRelativeToRect:NSMakeRect(0, -8, NSWidth(sender.bounds), NSHeight(sender.bounds)) ofView:sender preferredEdge:NSRectEdgeMaxY];
  298. // }
  299. //
  300. // - (void)dismissHelpTip {
  301. // [self.popover close];
  302. // self.popover = nil;
  303. // }
  304. //}
  305. extension KMAccountInfoView: NSPopoverDelegate {
  306. func popoverWillClose(_ notification: Notification) {
  307. self.updateMoreButtonState(select: false)
  308. }
  309. }