AIUserInfoController.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. //
  2. // AIUserInfoController.swift
  3. // PDF Reader Pro Edition
  4. //
  5. // Created by Niehaoyu on 2024/1/17.
  6. //
  7. import Cocoa
  8. @objcMembers class AIUserInfoController: NSViewController {
  9. @IBOutlet weak var contendBox: NSBox!
  10. @IBOutlet weak var titleLabel: NSTextField!
  11. @IBOutlet weak var whatNewlabel: NSTextField!
  12. @IBOutlet weak var emptyContendBox: NSBox!
  13. @IBOutlet weak var emptyTitleLabel: NSTextField!
  14. @IBOutlet weak var emptyBuyNowBox: KMBox!
  15. @IBOutlet weak var emptyBuyNowLabel: NSTextField!
  16. @IBOutlet weak var emptyRestoreBtn: HyperLinkButton!
  17. @IBOutlet weak var emptyBuyNowBtn: KMButton!
  18. @IBOutlet weak var emptyTipButton: KMButton!
  19. @IBOutlet weak var creditsContendBox: NSBox!
  20. @IBOutlet weak var creditsInfoBox: KMBox!
  21. @IBOutlet weak var creditsTitleLabel: NSTextField!
  22. @IBOutlet weak var validCreditLabel: NSTextField!
  23. @IBOutlet weak var totalCreditLabel: NSTextField!
  24. @IBOutlet weak var alreadCreditBox: NSBox!
  25. @IBOutlet weak var alreadyCreditLabel: NSTextField!
  26. @IBOutlet weak var alreadySeplineView: NSView!
  27. @IBOutlet weak var timeDescriptionInfoLabel: NSTextField!
  28. @IBOutlet weak var dateInfoLabel: NSTextField!
  29. @IBOutlet weak var creditsBuyNowBox: KMBox!
  30. @IBOutlet weak var creditsBuyNowLabel: NSTextField!
  31. @IBOutlet weak var creditsRestoreBtn: HyperLinkButton!
  32. @IBOutlet weak var creditBuyNowBtn: KMButton!
  33. @IBOutlet weak var creditTipBtn: KMButton!
  34. var popOver: NSPopover!
  35. var guideHandle: ((_ vc: AIUserInfoController) -> Void)?
  36. var purchaseHandle: ((_ vc: AIUserInfoController) -> Void)?
  37. var enterLicenseHandle: ((_ vc: AIUserInfoController) -> Void)?
  38. deinit {
  39. NotificationCenter.default.removeObserver(self)
  40. }
  41. override func viewDidLoad() {
  42. super.viewDidLoad()
  43. // Do view setup here.
  44. self.view.wantsLayer = true
  45. if KMAppearance.isDarkMode() {
  46. self.view.layer?.backgroundColor = NSColor(red: 43/255, green: 43/255, blue: 43/255, alpha: 1).cgColor
  47. } else {
  48. self.view.layer?.backgroundColor = NSColor.white.cgColor
  49. }
  50. self.titleLabel.font = NSFont.SFProTextSemiboldFont(18)
  51. self.whatNewlabel.font = NSFont.SFProTextSemiboldFont(12)
  52. self.titleLabel.textColor = KMAppearance.KMColor_Layout_H0()
  53. self.whatNewlabel.textColor = KMAppearance.KMColor_Interactive_A0()
  54. self.titleLabel.stringValue = NSLocalizedString("AI Tools", comment: "")
  55. self.whatNewlabel.stringValue = NSLocalizedString("What‘s New", comment: "") + " →"
  56. self.creditsTitleLabel.stringValue = NSLocalizedString("Available credits this month", comment: "")
  57. self.alreadyCreditLabel.stringValue = NSLocalizedString("Credits to be unlocked", comment: "")
  58. self.creditsBuyNowLabel.stringValue = NSLocalizedString("Buy Now", comment: "")
  59. self.emptyContendBox.isHidden = true
  60. self.creditsContendBox.isHidden = true
  61. if AIInfoManager.default().aiInfoValid == true {
  62. self.creditsContendBox.isHidden = false
  63. } else {
  64. self.emptyContendBox.isHidden = false
  65. }
  66. self.creditsRestoreBtn.isHidden = true
  67. #if VERSION_DMG
  68. self.emptyRestoreBtn.isHidden = false
  69. #else
  70. self.emptyRestoreBtn.isHidden = true
  71. #endif
  72. self.updateEmptyCreditViewUI()
  73. self.updateCreditsViewUI()
  74. NotificationCenter.default.addObserver(self, selector: #selector(aiStatusInfoChangeNoti), name: NSNotification.Name(rawValue: kDeviceAIStatusChangeNotification), object: nil)
  75. self.updateCreditsViewInfo()
  76. self.creditTipBtn.mouseMoveCallback = {[unowned self] mouseEntered in
  77. if mouseEntered {
  78. let tipString = NSLocalizedString("Each use of the AI tools costs 1 credit.", comment: "")
  79. let popViewController = KMToolbarItemPopViewController.init()
  80. if self.popOver == nil {
  81. self.popOver = NSPopover.init()
  82. }
  83. self.popOver.contentViewController = popViewController
  84. self.popOver.animates = false
  85. self.popOver.behavior = .semitransient
  86. self.popOver.contentSize = (popViewController.view.frame.size)
  87. popViewController.updateWithHelpTip(helpTip: tipString)
  88. self.popOver.show(relativeTo: self.creditTipBtn.frame, of: self.creditTipBtn.superview!, preferredEdge: .maxY)
  89. } else {
  90. self.popOver.close()
  91. }
  92. }
  93. self.emptyTipButton.mouseMoveCallback = {[unowned self] mouseEntered in
  94. if mouseEntered {
  95. let tipString = NSLocalizedString("Each use of the AI tools costs 1 credit.", comment: "")
  96. let popViewController = KMToolbarItemPopViewController.init()
  97. if self.popOver == nil {
  98. self.popOver = NSPopover.init()
  99. }
  100. self.popOver.contentViewController = popViewController
  101. self.popOver.animates = false
  102. self.popOver.behavior = .semitransient
  103. self.popOver.contentSize = (popViewController.view.frame.size)
  104. popViewController.updateWithHelpTip(helpTip: tipString)
  105. self.popOver.show(relativeTo: self.emptyTipButton.frame, of: self.emptyTipButton.superview!, preferredEdge: .maxY)
  106. } else {
  107. self.popOver.close()
  108. }
  109. }
  110. }
  111. func updateEmptyCreditViewUI() -> Void {
  112. self.emptyContendBox.cornerRadius = 8
  113. if KMAppearance.isDarkMode() {
  114. self.emptyContendBox.fillColor = NSColor(red: 0, green: 0, blue: 0, alpha: 0.4)
  115. } else {
  116. self.emptyContendBox.fillColor = NSColor(red: 249/255, green: 247/255, blue: 250/255, alpha: 1)
  117. }
  118. self.emptyTitleLabel.textColor = KMAppearance.KMColor_Layout_H0()
  119. self.emptyBuyNowLabel.textColor = NSColor.white
  120. self.emptyBuyNowBox.fillColor = NSColor.clear
  121. self.emptyBuyNowBox.wantsLayer = true
  122. self.emptyBuyNowBox.layer?.cornerRadius = 20
  123. self.emptyBuyNowBox.layer?.masksToBounds = true
  124. self.emptyBuyNowBtn.wantsLayer = true
  125. self.emptyBuyNowBtn.layer?.backgroundColor = NSColor.clear.cgColor
  126. self.emptyBuyNowBtn.mouseMoveCallback = {[unowned self] mouseEntered in
  127. if mouseEntered {
  128. self.emptyBuyNowBtn.layer?.backgroundColor = NSColor.white.withAlphaComponent(0.2).cgColor
  129. } else {
  130. self.emptyBuyNowBtn.layer?.backgroundColor = NSColor.clear.cgColor
  131. }
  132. }
  133. self.emptyTitleLabel.font = NSFont.SFProTextSemiboldFont(14)
  134. self.emptyBuyNowLabel.font = NSFont.UbuntuMediumFontWithSize(16)
  135. self.emptyRestoreBtn.font = NSFont.SFProTextRegularFont(14)
  136. self.emptyTitleLabel.stringValue = NSLocalizedString("You have no credit", comment: "")
  137. self.emptyBuyNowLabel.stringValue = NSLocalizedString("Buy Now", comment: "")
  138. self.emptyRestoreBtn.title = NSLocalizedString("Enter AI License", comment: "")
  139. self.emptyRestoreBtn.setTitleColor(KMAppearance.KMColor_Interactive_M0())
  140. self.emptyRestoreBtn.mouseMoveCallback = {[unowned self] mouseEntered in
  141. if mouseEntered {
  142. self.emptyRestoreBtn.setTitleColor(KMAppearance.KMColor_Interactive_M1())
  143. } else {
  144. self.emptyRestoreBtn.setTitleColor(KMAppearance.KMColor_Interactive_M0())
  145. }
  146. }
  147. }
  148. func updateCreditsViewUI() -> Void {
  149. self.creditsInfoBox.cornerRadius = 8
  150. self.creditsContendBox.fillColor = NSColor.clear
  151. self.alreadySeplineView.wantsLayer = true
  152. if KMAppearance.isDarkMode() {
  153. self.creditsInfoBox.fillColor = NSColor(red: 0, green: 0, blue: 0, alpha: 0.4)
  154. self.creditsTitleLabel.textColor = NSColor.white
  155. self.alreadCreditBox.fillColor = NSColor(red: 155/255, green: 83/255, blue: 1, alpha: 0.3)
  156. self.alreadySeplineView.layer?.backgroundColor = NSColor.white.withAlphaComponent(0.1).cgColor
  157. } else {
  158. self.creditsInfoBox.fillColor = NSColor(red: 249/255, green: 247/255, blue: 250/255, alpha: 1)
  159. self.creditsTitleLabel.textColor = KMAppearance.KMColor_Layout_H0()
  160. self.alreadCreditBox.fillColor = NSColor(red: 155/255, green: 83/255, blue: 1, alpha: 0.1)
  161. self.alreadySeplineView.layer?.backgroundColor = NSColor.black.withAlphaComponent(0.05).cgColor
  162. }
  163. self.creditBuyNowBtn.wantsLayer = true
  164. self.creditBuyNowBtn.layer?.backgroundColor = NSColor.clear.cgColor
  165. self.creditBuyNowBtn.mouseMoveCallback = {[unowned self] mouseEntered in
  166. if mouseEntered {
  167. self.creditBuyNowBtn.layer?.backgroundColor = NSColor.white.withAlphaComponent(0.2).cgColor
  168. } else {
  169. self.creditBuyNowBtn.layer?.backgroundColor = NSColor.clear.cgColor
  170. }
  171. }
  172. self.creditsTitleLabel.font = NSFont.SFProTextSemiboldFont(14)
  173. self.validCreditLabel.font = NSFont.UbuntuBoldFontWithSize(32)
  174. self.validCreditLabel.textColor = NSColor(red: 166/255, green: 102/255, blue: 1, alpha: 1)
  175. self.totalCreditLabel.font = NSFont.UbuntuMediumFontWithSize(20)
  176. self.totalCreditLabel.textColor = KMAppearance.KMColor_Layout_H2()
  177. self.alreadCreditBox.cornerRadius = 12
  178. self.alreadCreditBox.borderWidth = 0
  179. self.alreadyCreditLabel.font = NSFont.SFProTextRegularFont(13)
  180. self.alreadyCreditLabel.textColor = KMAppearance.KMColor_Layout_H1()
  181. self.timeDescriptionInfoLabel.textColor = KMAppearance.KMColor_Status_Err()
  182. self.timeDescriptionInfoLabel.font = NSFont.SFProTextRegularFont(13)
  183. self.dateInfoLabel.textColor = KMAppearance.KMColor_Layout_H2()
  184. self.dateInfoLabel.font = NSFont.SFProTextRegularFont(12)
  185. self.creditsRestoreBtn.font = NSFont.SFProTextRegularFont(14)
  186. self.creditsBuyNowBox.fillColor = NSColor.clear
  187. self.creditsBuyNowBox.wantsLayer = true
  188. self.creditsBuyNowBox.layer?.cornerRadius = 18
  189. self.creditsBuyNowBox.layer?.masksToBounds = true
  190. self.creditsBuyNowLabel.font = NSFont.UbuntuMediumFontWithSize(14)
  191. self.creditsBuyNowLabel.textColor = NSColor.white
  192. self.creditsRestoreBtn.setTitleColor(KMAppearance.KMColor_Interactive_M0())
  193. #if VERSION_DMG
  194. #else
  195. if AIInfoManager.default().aiInfoValid == true {
  196. self.creditsBuyNowBox.isHidden = true
  197. } else {
  198. self.creditsBuyNowBox.isHidden = false
  199. }
  200. #endif
  201. }
  202. func updateCreditsViewInfo() -> Void {
  203. let aiInfo: AIInfo = AIInfoManager.default().aiInfo
  204. if aiInfo.infoDict.keys.count == 0 {
  205. return
  206. }
  207. self.validCreditLabel.stringValue = String(format: "%d", aiInfo.totalToken-aiInfo.usedTimes)
  208. self.totalCreditLabel.stringValue = "/" + String(format: "%d", aiInfo.totalToken)
  209. self.alreadyCreditLabel.stringValue = NSLocalizedString("Credits to be unlocked", comment: "") + ": 0"
  210. let leftDays = String(format: "%ld", aiInfo.remainingDays)
  211. self.timeDescriptionInfoLabel.stringValue = String(format: NSLocalizedString("%@ days left on subscription", comment: ""), leftDays)
  212. let dateFormatter = DateFormatter.init()
  213. dateFormatter.dateFormat = "yyyy.MM.dd"
  214. let startDateStr = dateFormatter.string(from: aiInfo.startDate)
  215. let endDateStr = dateFormatter.string(from: aiInfo.endDate)
  216. self.dateInfoLabel.stringValue = startDateStr + " - " + endDateStr
  217. self.emptyContendBox.isHidden = true
  218. self.creditsContendBox.isHidden = true
  219. if AIInfoManager.default().aiInfoValid == true {
  220. self.creditsContendBox.isHidden = false
  221. } else {
  222. self.emptyContendBox.isHidden = false
  223. }
  224. }
  225. //MARK: - IBAction
  226. @IBAction func newGuideAction(_ sender: Any) {
  227. guard let callBack = self.guideHandle else {
  228. return
  229. }
  230. callBack(self)
  231. }
  232. @IBAction func purchaseAction(_ sender: KMButton) {
  233. guard let callBack = self.purchaseHandle else {
  234. return
  235. }
  236. let newStatus: Bool = KMCloudServer.isConnectionAvailable()
  237. if !newStatus {
  238. let alert = NSAlert()
  239. alert.alertStyle = .critical
  240. alert.messageText = NSLocalizedString("Please make sure your internet connection is available.", comment: "")
  241. alert.runModal()
  242. return
  243. }
  244. callBack(self)
  245. }
  246. @IBAction func enterLicenseAction(_ sender: KMButton) {
  247. guard let callBack = self.enterLicenseHandle else {
  248. return
  249. }
  250. callBack(self)
  251. }
  252. @objc func aiStatusInfoChangeNoti() {
  253. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
  254. self.updateCreditsViewInfo()
  255. }
  256. }
  257. }