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