AIUserInfoController.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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.updateBtnShowState()
  76. self.updateEmptyCreditViewUI()
  77. self.updateCreditsViewUI()
  78. NotificationCenter.default.addObserver(self, selector: #selector(aiStatusInfoChangeNoti), name: NSNotification.Name(rawValue: kDeviceAIStatusChangeNotification), object: nil)
  79. self.updateCreditsViewInfo()
  80. self.creditTipBtn.mouseMoveCallback = {[unowned self] mouseEntered in
  81. if mouseEntered {
  82. let tipString = NSLocalizedString("Each use of the AI tools costs 1 or more credits.", comment: "")
  83. let popViewController = KMToolbarItemPopViewController.init()
  84. if self.popOver == nil {
  85. self.popOver = NSPopover.init()
  86. }
  87. self.popOver.contentViewController = popViewController
  88. self.popOver.animates = false
  89. self.popOver.behavior = .semitransient
  90. self.popOver.contentSize = (popViewController.view.frame.size)
  91. popViewController.updateWithHelpTip(helpTip: tipString)
  92. self.popOver.show(relativeTo: self.creditTipBtn.frame, of: self.creditTipBtn.superview!, preferredEdge: .maxY)
  93. } else {
  94. self.popOver.close()
  95. }
  96. }
  97. self.emptyTipButton.mouseMoveCallback = {[unowned self] mouseEntered in
  98. if mouseEntered {
  99. let tipString = NSLocalizedString("Each use of the AI tools costs 1 or more credits.", comment: "")
  100. let popViewController = KMToolbarItemPopViewController.init()
  101. if self.popOver == nil {
  102. self.popOver = NSPopover.init()
  103. }
  104. self.popOver.contentViewController = popViewController
  105. self.popOver.animates = false
  106. self.popOver.behavior = .semitransient
  107. self.popOver.contentSize = (popViewController.view.frame.size)
  108. popViewController.updateWithHelpTip(helpTip: tipString)
  109. self.popOver.show(relativeTo: self.emptyTipButton.frame, of: self.emptyTipButton.superview!, preferredEdge: .maxY)
  110. } else {
  111. self.popOver.close()
  112. }
  113. }
  114. }
  115. func updateEmptyCreditViewUI() -> Void {
  116. self.emptyContendBox.cornerRadius = 8
  117. if KMAppearance.isDarkMode() {
  118. self.emptyContendBox.fillColor = NSColor(red: 0, green: 0, blue: 0, alpha: 0.4)
  119. } else {
  120. self.emptyContendBox.fillColor = NSColor(red: 249/255, green: 247/255, blue: 250/255, alpha: 1)
  121. }
  122. self.emptyTitleLabel.textColor = KMAppearance.KMColor_Layout_H0()
  123. self.emptyBuyNowLabel.textColor = NSColor.white
  124. self.emptyBuyNowBox.fillColor = NSColor.clear
  125. self.emptyBuyNowBox.wantsLayer = true
  126. self.emptyBuyNowBox.layer?.cornerRadius = 20
  127. self.emptyBuyNowBox.layer?.masksToBounds = true
  128. self.emptyBuyNowBtn.wantsLayer = true
  129. self.emptyBuyNowBtn.layer?.backgroundColor = NSColor.clear.cgColor
  130. self.emptyBuyNowBtn.mouseMoveCallback = {[weak self] mouseEntered in
  131. if mouseEntered {
  132. self?.emptyBuyNowBtn.layer?.backgroundColor = NSColor.white.withAlphaComponent(0.2).cgColor
  133. } else {
  134. self?.emptyBuyNowBtn.layer?.backgroundColor = NSColor.clear.cgColor
  135. }
  136. }
  137. self.emptyTitleLabel.font = NSFont.SFProTextSemiboldFont(14)
  138. self.emptyBuyNowLabel.font = NSFont.UbuntuMediumFontWithSize(16)
  139. self.emptyRestoreBtn.font = NSFont.SFProTextRegularFont(14)
  140. self.emptyTitleLabel.stringValue = NSLocalizedString("You have no credit", comment: "")
  141. self.emptyBuyNowLabel.stringValue = NSLocalizedString("Buy Now", comment: "")
  142. self.emptyRestoreBtn.title = NSLocalizedString("Enter AI License", comment: "")
  143. self.emptyRestoreBtn.setTitleColor(KMAppearance.KMColor_Interactive_M0())
  144. self.emptyRestoreBtn.mouseMoveCallback = {[weak self] mouseEntered in
  145. if mouseEntered {
  146. self?.emptyRestoreBtn.setTitleColor(KMAppearance.KMColor_Interactive_M1())
  147. } else {
  148. self?.emptyRestoreBtn.setTitleColor(KMAppearance.KMColor_Interactive_M0())
  149. }
  150. }
  151. self.emptyShowAIBtn.title = NSLocalizedString("Show AI robot icon", comment: "")
  152. self.emptyShowAIBtn.setTitleColor(KMAppearance.KMColor_Layout_H0())
  153. self.emptyShowAIBtn.state = AIInfoManager.default().showAIIcon ? .on : .off
  154. self.emptyShowAIBtn.isHidden = true
  155. }
  156. func updateCreditsViewUI() -> Void {
  157. self.creditsInfoBox.wantsLayer = true
  158. self.creditsInfoBox.layer?.cornerRadius = 8
  159. self.creditsInfoBox.layer?.masksToBounds = true
  160. self.creditsContendBox.fillColor = NSColor.clear
  161. self.showAIRobotBtn.title = NSLocalizedString("Show AI robot icon", comment: "")
  162. self.showAIRobotBtn.setTitleColor(KMAppearance.KMColor_Layout_H0())
  163. self.showAIRobotBtn.state = AIInfoManager.default().showAIIcon ? .on : .off
  164. self.showAIRobotBtn.isHidden = true
  165. if KMAppearance.isDarkMode() {
  166. self.creditsInfoBox.fillColor = NSColor(red: 0, green: 0, blue: 0, alpha: 0.4)
  167. self.creditsTitleLabel.textColor = NSColor.white
  168. self.alreadCreditBox.fillColor = NSColor(red: 155/255, green: 83/255, blue: 1, alpha: 0.3)
  169. } else {
  170. self.creditsInfoBox.fillColor = NSColor(red: 249/255, green: 247/255, blue: 250/255, alpha: 1)
  171. self.creditsTitleLabel.textColor = KMAppearance.KMColor_Layout_H0()
  172. self.alreadCreditBox.fillColor = NSColor(red: 155/255, green: 83/255, blue: 1, alpha: 0.1)
  173. }
  174. self.creditsRestoreBtn.title = NSLocalizedString("Enter AI License", comment: "")
  175. self.creditsRestoreBtn.setTitleColor(KMAppearance.KMColor_Interactive_M0())
  176. self.creditBuyNowBtn.wantsLayer = true
  177. self.creditBuyNowBtn.layer?.backgroundColor = NSColor.clear.cgColor
  178. self.creditBuyNowBtn.mouseMoveCallback = {[weak self] mouseEntered in
  179. if mouseEntered {
  180. self?.creditBuyNowBtn.layer?.backgroundColor = NSColor.white.withAlphaComponent(0.2).cgColor
  181. } else {
  182. self?.creditBuyNowBtn.layer?.backgroundColor = NSColor.clear.cgColor
  183. }
  184. }
  185. self.creditsTitleLabel.font = NSFont.SFProTextSemiboldFont(14)
  186. self.validCreditLabel.font = NSFont.UbuntuBoldFontWithSize(32)
  187. self.validCreditLabel.textColor = NSColor(red: 166/255, green: 102/255, blue: 1, alpha: 1)
  188. self.totalCreditLabel.font = NSFont.UbuntuMediumFontWithSize(20)
  189. self.totalCreditLabel.textColor = KMAppearance.KMColor_Layout_H2()
  190. self.alreadCreditBox.borderWidth = 0
  191. self.alreadyCreditLabel.font = NSFont.SFProTextRegularFont(13)
  192. self.alreadyCreditLabel.textColor = KMAppearance.KMColor_Layout_H1()
  193. self.timeDescriptionInfoLabel.textColor = KMAppearance.KMColor_Status_Err()
  194. self.timeDescriptionInfoLabel.font = NSFont.SFProTextRegularFont(13)
  195. self.dateInfoLabel.textColor = KMAppearance.KMColor_Layout_H2()
  196. self.dateInfoLabel.font = NSFont.SFProTextRegularFont(12)
  197. self.creditsRestoreBtn.font = NSFont.SFProTextRegularFont(14)
  198. self.creditsBuyNowBox.fillColor = NSColor.clear
  199. self.creditsBuyNowBox.wantsLayer = true
  200. self.creditsBuyNowBox.layer?.cornerRadius = 18
  201. self.creditsBuyNowBox.layer?.masksToBounds = true
  202. self.creditsBuyNowLabel.font = NSFont.UbuntuMediumFontWithSize(14)
  203. self.creditsBuyNowLabel.textColor = NSColor.white
  204. self.creditsRestoreBtn.setTitleColor(KMAppearance.KMColor_Interactive_M0())
  205. self.updateBtnShowState()
  206. }
  207. func updateBtnShowState() {
  208. #if VERSION_DMG
  209. self.emptyRestoreBtn.isHidden = false
  210. #else
  211. self.emptyRestoreBtn.isHidden = true
  212. self.creditsRestoreBtn.isHidden = true
  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.emptyShowAIBtn.isHidden = true
  237. self.creditsContendBox.isHidden = true
  238. if AIInfoManager.default().aiInfoValid == true {
  239. self.creditsContendBox.isHidden = false
  240. } else {
  241. self.emptyContendBox.isHidden = false
  242. self.emptyShowAIBtn.isHidden = false
  243. }
  244. if AIInfoManager.default().aiInfoValid == true {
  245. var viewRect = self.view.frame
  246. viewRect.size.height = 330
  247. self.view.frame = viewRect
  248. } else {
  249. var viewRect = self.view.frame
  250. viewRect.size.height = 386
  251. self.view.frame = viewRect
  252. }
  253. }
  254. //MARK: - IBAction
  255. @IBAction func newGuideAction(_ sender: Any) {
  256. guard let callBack = self.guideHandle else {
  257. return
  258. }
  259. callBack(self)
  260. }
  261. @IBAction func purchaseAction(_ sender: KMButton) {
  262. guard let callBack = self.purchaseHandle else {
  263. return
  264. }
  265. let newStatus: Bool = KMCloudServer.isConnectionAvailable()
  266. if !newStatus {
  267. let alert = NSAlert()
  268. alert.alertStyle = .critical
  269. alert.messageText = NSLocalizedString("Please make sure your internet connection is available.", comment: "")
  270. alert.runModal()
  271. return
  272. }
  273. callBack(self)
  274. }
  275. @IBAction func enterLicenseAction(_ sender: KMButton) {
  276. guard let callBack = self.enterLicenseHandle else {
  277. return
  278. }
  279. callBack(self)
  280. }
  281. @IBAction func showAIRobotAction(_ sender: NSButton) {
  282. if self.showAIRobotBtn.state == .on {
  283. AIInfoManager.default().showAIIcon = true
  284. } else {
  285. AIInfoManager.default().showAIIcon = false
  286. }
  287. NotificationCenter.default.post(name: NSNotification.Name(rawValue: kAIIconShowStateChangeNotification), object: nil)
  288. }
  289. @IBAction func emptyShowAIRototAction(_ sender: NSButton) {
  290. if self.emptyShowAIBtn.state == .on {
  291. AIInfoManager.default().showAIIcon = true
  292. } else {
  293. AIInfoManager.default().showAIIcon = false
  294. }
  295. NotificationCenter.default.post(name: NSNotification.Name(rawValue: kAIIconShowStateChangeNotification), object: nil)
  296. }
  297. @IBAction func creditRestoreAction(_ sender: NSButton) {
  298. guard let callBack = self.enterLicenseHandle else {
  299. return
  300. }
  301. callBack(self)
  302. }
  303. @objc func aiStatusInfoChangeNoti() {
  304. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
  305. self.updateCreditsViewInfo()
  306. }
  307. }
  308. }