123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369 |
- //
- // AIUserInfoController.swift
- // PDF Reader Pro Edition
- //
- // Created by Niehaoyu on 2024/1/17.
- //
- import Cocoa
- @objcMembers class AIUserInfoController: NSViewController {
- @IBOutlet weak var contendBox: NSBox!
- @IBOutlet weak var titleLabel: NSTextField!
- @IBOutlet weak var whatNewlabel: NSTextField!
-
- @IBOutlet weak var emptyContendBox: NSBox!
- @IBOutlet weak var emptyTitleLabel: NSTextField!
- @IBOutlet weak var emptyBuyNowBox: KMBox!
- @IBOutlet weak var emptyBuyNowLabel: NSTextField!
- @IBOutlet weak var emptyRestoreBtn: HyperLinkButton!
- @IBOutlet weak var emptyBuyNowBtn: KMButton!
- @IBOutlet weak var emptyTipButton: KMButton!
- @IBOutlet weak var emptyShowAIBtn: NSButton!
-
- @IBOutlet weak var newFuncBtn: NSButton!
- @IBOutlet weak var creditsContendBox: NSBox!
- @IBOutlet weak var creditsInfoBox: KMBox!
- @IBOutlet weak var validCreditLabel: NSTextField!
- @IBOutlet weak var creditsBuyNowBox: KMBox!
- @IBOutlet weak var creditsBuyNowLabel: NSTextField!
- @IBOutlet weak var creditsRestoreBtn: HyperLinkButton!
- @IBOutlet weak var creditBuyNowBtn: KMButton!
- @IBOutlet weak var showAIRobotBtn: NSButton!
- @IBOutlet weak var remainLabel: NSTextField!
-
- var popOver: NSPopover!
-
- var guideHandle: ((_ vc: AIUserInfoController) -> Void)?
- var purchaseHandle: ((_ vc: AIUserInfoController) -> Void)?
- var enterLicenseHandle: ((_ vc: AIUserInfoController) -> Void)?
-
- deinit {
- NotificationCenter.default.removeObserver(self)
- }
-
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do view setup here.
-
- newFuncBtn.isHidden = true
- whatNewlabel.isHidden = true
- self.view.wantsLayer = true
- if KMAppearance.isDarkMode() {
- self.view.layer?.backgroundColor = NSColor(red: 43/255, green: 43/255, blue: 43/255, alpha: 1).cgColor
- } else {
- self.view.layer?.backgroundColor = NSColor.white.cgColor
- }
-
- self.titleLabel.font = NSFont.SFProTextSemiboldFont(18)
- self.whatNewlabel.font = NSFont.SFProTextSemiboldFont(12)
- self.titleLabel.textColor = KMAppearance.KMColor_Layout_H0()
- self.whatNewlabel.textColor = KMAppearance.KMColor_Interactive_A0()
-
- self.titleLabel.stringValue = NSLocalizedString("AI Tools Title", comment: "")
- self.whatNewlabel.stringValue = NSLocalizedString("What‘s New", comment: "") + " →"
-
- self.creditsBuyNowLabel.stringValue = NSLocalizedString("Buy Now", comment: "")
-
- self.emptyContendBox.isHidden = true
- self.creditsContendBox.isHidden = true
- if KMMemberInfo.shared.isPermitAI == true {
- self.creditsContendBox.isHidden = false
-
- var viewRect = self.view.frame
- viewRect.size.height = 330
- self.view.frame = viewRect
-
- } else {
- self.emptyContendBox.isHidden = false
-
- var viewRect = self.view.frame
- viewRect.size.height = 386
- self.view.frame = viewRect
- }
-
- self.updateBtnShowState()
-
- self.updateEmptyCreditViewUI()
- self.updateCreditsViewUI()
-
- NotificationCenter.default.addObserver(self, selector: #selector(aiStatusInfoChangeNoti), name: NSNotification.Name(rawValue: kDeviceAIStatusChangeNotification), object: nil)
-
- self.updateCreditsViewInfo()
-
- self.emptyTipButton.mouseMoveCallback = {[unowned self] mouseEntered in
- if mouseEntered {
- let tipString = NSLocalizedString("Each use of the AI tools costs 1 or more credits.", comment: "")
-
- let popViewController = KMToolbarItemPopViewController.init()
- if self.popOver == nil {
- self.popOver = NSPopover.init()
- }
- self.popOver.contentViewController = popViewController
- self.popOver.animates = false
- self.popOver.behavior = .semitransient
- self.popOver.contentSize = (popViewController.view.frame.size)
- popViewController.updateWithHelpTip(helpTip: tipString)
- self.popOver.show(relativeTo: self.emptyTipButton.frame, of: self.emptyTipButton.superview!, preferredEdge: .maxY)
- } else {
- self.popOver.close()
- }
- }
-
- if(KMMemberInfo.shared.isLogin == true) {
- addWaitingView(to: self.view)
- KMUserInfoViewController.refreshTokenUserInfo {[weak self] success, resDic in
- DispatchQueue.main.async {
- self?.removeWaitingView(from: self?.view ?? NSView())
- if(success == true) {
- }
- }
- }
- }
- }
-
- func addWaitingView(to view: NSView) {
- removeWaitingView(from: view)
- let waitingView = WaitingView(frame: view.bounds)
- waitingView.autoresizingMask = [.width, .height]
- view.addSubview(waitingView)
- waitingView.startAnimation()
- }
- func removeWaitingView(from view: NSView) {
- for subview in view.subviews {
- if subview is WaitingView {
- subview.removeFromSuperview()
- break
- }
- }
- }
-
-
- func updateEmptyCreditViewUI() -> Void {
- self.emptyContendBox.cornerRadius = 8
- if KMAppearance.isDarkMode() {
- self.emptyContendBox.fillColor = NSColor(red: 0, green: 0, blue: 0, alpha: 0.4)
- } else {
- self.emptyContendBox.fillColor = NSColor(red: 249/255, green: 247/255, blue: 250/255, alpha: 1)
- }
- self.emptyTitleLabel.textColor = KMAppearance.KMColor_Layout_H0()
- self.emptyBuyNowLabel.textColor = NSColor.white
- self.emptyBuyNowBox.fillColor = NSColor.clear
-
- self.emptyBuyNowBox.wantsLayer = true
- self.emptyBuyNowBox.layer?.cornerRadius = 20
- self.emptyBuyNowBox.layer?.masksToBounds = true
- if KMMemberInfo.shared.ai_status == 1 {
- self.emptyBuyNowBox.isHidden = true
- } else {
- self.emptyBuyNowBox.isHidden = false
- }
-
- self.emptyBuyNowBtn.wantsLayer = true
- self.emptyBuyNowBtn.layer?.backgroundColor = NSColor.clear.cgColor
- self.emptyBuyNowBtn.mouseMoveCallback = {[weak self] mouseEntered in
- if mouseEntered {
- self?.emptyBuyNowBtn.layer?.backgroundColor = NSColor.white.withAlphaComponent(0.2).cgColor
- } else {
- self?.emptyBuyNowBtn.layer?.backgroundColor = NSColor.clear.cgColor
- }
- }
-
- self.emptyTitleLabel.font = NSFont.SFProTextSemiboldFont(14)
- self.emptyBuyNowLabel.font = NSFont.UbuntuMediumFontWithSize(16)
- self.emptyRestoreBtn.font = NSFont.SFProTextRegularFont(14)
-
- self.emptyTitleLabel.stringValue = NSLocalizedString("You have no credit", comment: "")
- self.emptyBuyNowLabel.stringValue = NSLocalizedString("Buy Now", comment: "")
- self.emptyRestoreBtn.title = NSLocalizedString("Enter AI License", comment: "")
-
- self.emptyRestoreBtn.setTitleColor(KMAppearance.KMColor_Interactive_M0())
- self.emptyRestoreBtn.mouseMoveCallback = {[weak self] mouseEntered in
- if mouseEntered {
- self?.emptyRestoreBtn.setTitleColor(KMAppearance.KMColor_Interactive_M1())
- } else {
- self?.emptyRestoreBtn.setTitleColor(KMAppearance.KMColor_Interactive_M0())
- }
- }
-
- self.emptyShowAIBtn.title = NSLocalizedString("Show AI robot icon", comment: "")
- self.emptyShowAIBtn.setTitleColor(KMAppearance.KMColor_Layout_H0())
- self.emptyShowAIBtn.state = AIInfoManager.default().showAIIcon ? .on : .off
- self.emptyShowAIBtn.isHidden = true
- }
-
- func updateCreditsViewUI() -> Void {
-
- self.creditsInfoBox.wantsLayer = true
- self.creditsInfoBox.layer?.cornerRadius = 8
- self.creditsInfoBox.layer?.masksToBounds = true
- self.creditsContendBox.fillColor = NSColor.clear
-
- self.showAIRobotBtn.title = NSLocalizedString("Show AI robot icon", comment: "")
- self.showAIRobotBtn.setTitleColor(KMAppearance.KMColor_Layout_H0())
- self.showAIRobotBtn.state = AIInfoManager.default().showAIIcon ? .on : .off
- self.showAIRobotBtn.isHidden = true
-
- if KMAppearance.isDarkMode() {
- self.creditsInfoBox.fillColor = NSColor(red: 0, green: 0, blue: 0, alpha: 0.4)
- } else {
- self.creditsInfoBox.fillColor = NSColor(red: 249/255, green: 247/255, blue: 250/255, alpha: 1)
- }
-
- self.creditsRestoreBtn.title = NSLocalizedString("Enter AI License", comment: "")
- self.creditsRestoreBtn.setTitleColor(KMAppearance.KMColor_Interactive_M0())
-
- self.creditBuyNowBtn.wantsLayer = true
- self.creditBuyNowBtn.layer?.backgroundColor = NSColor.clear.cgColor
- self.creditBuyNowBtn.mouseMoveCallback = {[weak self] mouseEntered in
- if mouseEntered {
- self?.creditBuyNowBtn.layer?.backgroundColor = NSColor.white.withAlphaComponent(0.2).cgColor
- } else {
- self?.creditBuyNowBtn.layer?.backgroundColor = NSColor.clear.cgColor
- }
- }
- self.validCreditLabel.font = NSFont.UbuntuBoldFontWithSize(32)
- self.validCreditLabel.textColor = .blue
- self.remainLabel.font = NSFont.UbuntuBoldFontWithSize(20)
- // self.remainLabel.textColor = .black
- self.remainLabel.textColor = KMAppearance.KMColor_Layout_H0()
- self.creditsRestoreBtn.font = NSFont.SFProTextRegularFont(14)
- self.creditsBuyNowBox.fillColor = NSColor.clear
- self.creditsBuyNowBox.wantsLayer = true
- self.creditsBuyNowBox.layer?.cornerRadius = 18
- self.creditsBuyNowBox.layer?.masksToBounds = true
- self.creditsBuyNowLabel.font = NSFont.UbuntuMediumFontWithSize(14)
- self.creditsBuyNowLabel.textColor = NSColor.white
- self.creditsRestoreBtn.setTitleColor(KMAppearance.KMColor_Interactive_M0())
-
- self.updateBtnShowState()
-
- }
-
- func updateBtnShowState() {
- #if VERSION_DMG
- self.emptyRestoreBtn.isHidden = false
- if KMMemberInfo.shared.ai_status == 1 {
- self.creditsBuyNowBox.isHidden = true
- } else {
- self.creditsBuyNowBox.isHidden = false
- }
- #else
- self.emptyRestoreBtn.isHidden = true
- self.creditsRestoreBtn.isHidden = true
- if KMMemberInfo.shared.isPermitAI == true {
- self.creditsBuyNowBox.isHidden = true
- } else {
- self.creditsBuyNowBox.isHidden = false
- }
- #endif
- }
-
- func updateCreditsViewInfo() -> Void {
- let aiInfo: AIInfo = AIInfoManager.default().aiInfo
- self.validCreditLabel.stringValue = String(format: "%d", aiInfo.totalToken-aiInfo.usedTimes + Int32(KMMemberInfo.shared.aiPoint))
-
- self.emptyContendBox.isHidden = true
- self.creditsContendBox.isHidden = true
- if KMMemberInfo.shared.isPermitAI == true {
- self.creditsContendBox.isHidden = false
- } else {
- self.emptyContendBox.isHidden = false
- }
-
- if KMMemberInfo.shared.isPermitAI == true {
- var viewRect = self.view.frame
- viewRect.size.height = 330
- self.view.frame = viewRect
- } else {
- var viewRect = self.view.frame
- viewRect.size.height = 386
- self.view.frame = viewRect
- }
- }
-
- //MARK: - IBAction
-
- @IBAction func newGuideAction(_ sender: Any) {
- guard let callBack = self.guideHandle else {
- return
- }
- callBack(self)
- }
-
- @IBAction func purchaseAction(_ sender: KMButton) {
- guard let callBack = self.purchaseHandle else {
- return
- }
- if KMMemberInfo.shared.isLogin == false {
- KMLoginWindowsController.shared.openWindow() { success in
- if KMMemberInfo.shared.aiPoint > 0 {
-
- } else {
- if success {
- callBack(self)
- }
- }
- }
- return
- }
-
- let newStatus: Bool = KMCloudServer.isConnectionAvailable()
- if !newStatus {
- let alert = NSAlert()
- alert.alertStyle = .critical
- alert.messageText = NSLocalizedString("Please make sure your internet connection is available.", comment: "")
- alert.runModal()
- return
- }
- callBack(self)
- }
-
- @IBAction func enterLicenseAction(_ sender: KMButton) {
- guard let callBack = self.enterLicenseHandle else {
- return
- }
- callBack(self)
- }
-
- @IBAction func showAIRobotAction(_ sender: NSButton) {
- if self.showAIRobotBtn.state == .on {
- AIInfoManager.default().showAIIcon = true
- } else {
- AIInfoManager.default().showAIIcon = false
- }
- NotificationCenter.default.post(name: NSNotification.Name(rawValue: kAIIconShowStateChangeNotification), object: nil)
- }
-
- @IBAction func emptyShowAIRototAction(_ sender: NSButton) {
- if self.emptyShowAIBtn.state == .on {
- AIInfoManager.default().showAIIcon = true
- } else {
- AIInfoManager.default().showAIIcon = false
- }
- NotificationCenter.default.post(name: NSNotification.Name(rawValue: kAIIconShowStateChangeNotification), object: nil)
- }
-
-
- @IBAction func creditRestoreAction(_ sender: NSButton) {
- guard let callBack = self.enterLicenseHandle else {
- return
- }
- callBack(self)
- }
-
-
-
- @objc func aiStatusInfoChangeNoti() {
- DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
- self.updateCreditsViewInfo()
- }
- }
- }
|