AIUserInfoController.swift 15 KB

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