AIUserInfoController.swift 13 KB

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