AIUserInfoController.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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.remainLabel.textColor = KMAppearance.KMColor_Layout_H0()
  164. self.creditsRestoreBtn.font = NSFont.SFProTextRegularFont(14)
  165. self.creditsBuyNowBox.fillColor = NSColor.clear
  166. self.creditsBuyNowBox.wantsLayer = true
  167. self.creditsBuyNowBox.layer?.cornerRadius = 18
  168. self.creditsBuyNowBox.layer?.masksToBounds = true
  169. self.creditsBuyNowLabel.font = NSFont.UbuntuMediumFontWithSize(14)
  170. self.creditsBuyNowLabel.textColor = NSColor.white
  171. self.creditsRestoreBtn.setTitleColor(KMAppearance.KMColor_Interactive_M0())
  172. self.updateBtnShowState()
  173. }
  174. func updateBtnShowState() {
  175. #if VERSION_DMG
  176. self.emptyRestoreBtn.isHidden = false
  177. #else
  178. self.emptyRestoreBtn.isHidden = true
  179. self.creditsRestoreBtn.isHidden = true
  180. if KMMemberInfo.shared.isPermitAI == true {
  181. self.creditsBuyNowBox.isHidden = true
  182. } else {
  183. self.creditsBuyNowBox.isHidden = false
  184. }
  185. #endif
  186. }
  187. func updateCreditsViewInfo() -> Void {
  188. let aiInfo: AIInfo = AIInfoManager.default().aiInfo
  189. self.validCreditLabel.stringValue = String(format: "%d", aiInfo.totalToken-aiInfo.usedTimes + Int32(KMMemberInfo.shared.aiPoint))
  190. self.emptyContendBox.isHidden = true
  191. self.creditsContendBox.isHidden = true
  192. if KMMemberInfo.shared.isPermitAI == true {
  193. self.creditsContendBox.isHidden = false
  194. } else {
  195. self.emptyContendBox.isHidden = false
  196. }
  197. if KMMemberInfo.shared.isPermitAI == true {
  198. var viewRect = self.view.frame
  199. viewRect.size.height = 330
  200. self.view.frame = viewRect
  201. } else {
  202. var viewRect = self.view.frame
  203. viewRect.size.height = 386
  204. self.view.frame = viewRect
  205. }
  206. }
  207. //MARK: - IBAction
  208. @IBAction func newGuideAction(_ sender: Any) {
  209. guard let callBack = self.guideHandle else {
  210. return
  211. }
  212. callBack(self)
  213. }
  214. @IBAction func purchaseAction(_ sender: KMButton) {
  215. guard let callBack = self.purchaseHandle else {
  216. return
  217. }
  218. if KMMemberInfo.shared.isLogin == false {
  219. KMLoginWindowsController.shared.openWindow() { success in
  220. if KMMemberInfo.shared.aiPoint > 0 {
  221. } else {
  222. if success {
  223. callBack(self)
  224. }
  225. }
  226. }
  227. return
  228. }
  229. let newStatus: Bool = KMCloudServer.isConnectionAvailable()
  230. if !newStatus {
  231. let alert = NSAlert()
  232. alert.alertStyle = .critical
  233. alert.messageText = NSLocalizedString("Please make sure your internet connection is available.", comment: "")
  234. alert.runModal()
  235. return
  236. }
  237. callBack(self)
  238. }
  239. @IBAction func enterLicenseAction(_ sender: KMButton) {
  240. guard let callBack = self.enterLicenseHandle else {
  241. return
  242. }
  243. callBack(self)
  244. }
  245. @IBAction func showAIRobotAction(_ sender: NSButton) {
  246. if self.showAIRobotBtn.state == .on {
  247. AIInfoManager.default().showAIIcon = true
  248. } else {
  249. AIInfoManager.default().showAIIcon = false
  250. }
  251. NotificationCenter.default.post(name: NSNotification.Name(rawValue: kAIIconShowStateChangeNotification), object: nil)
  252. }
  253. @IBAction func emptyShowAIRototAction(_ sender: NSButton) {
  254. if self.emptyShowAIBtn.state == .on {
  255. AIInfoManager.default().showAIIcon = true
  256. } else {
  257. AIInfoManager.default().showAIIcon = false
  258. }
  259. NotificationCenter.default.post(name: NSNotification.Name(rawValue: kAIIconShowStateChangeNotification), object: nil)
  260. }
  261. @IBAction func creditRestoreAction(_ sender: NSButton) {
  262. guard let callBack = self.enterLicenseHandle else {
  263. return
  264. }
  265. callBack(self)
  266. }
  267. @objc func aiStatusInfoChangeNoti() {
  268. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
  269. self.updateCreditsViewInfo()
  270. }
  271. }
  272. }