// // AIUserInfoController.swift // Cisdem PDFMaster // // 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 creditsContendBox: NSBox! @IBOutlet weak var creditsInfoBox: KMBox! @IBOutlet weak var creditsTitleLabel: NSTextField! @IBOutlet weak var validCreditLabel: NSTextField! @IBOutlet weak var totalCreditLabel: NSTextField! @IBOutlet weak var alreadCreditBox: NSBox! @IBOutlet weak var alreadyCreditLabel: NSTextField! @IBOutlet weak var timeDescriptionInfoLabel: NSTextField! @IBOutlet weak var dateInfoLabel: NSTextField! @IBOutlet weak var creditsBuyNowBox: KMBox! @IBOutlet weak var creditsBuyNowLabel: NSTextField! @IBOutlet weak var creditsRestoreBtn: HyperLinkButton! @IBOutlet weak var creditBuyNowBtn: KMButton! @IBOutlet weak var creditTipBtn: KMButton! @IBOutlet weak var showAIRobotBtn: NSButton! 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. 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", comment: "") self.whatNewlabel.stringValue = NSLocalizedString("What‘s New", comment: "") + " →" self.creditsTitleLabel.stringValue = NSLocalizedString("Available credits this month", comment: "") self.alreadyCreditLabel.stringValue = NSLocalizedString("Credits to be unlocked: ", comment: "") self.creditsBuyNowLabel.stringValue = NSLocalizedString("Buy Now", comment: "") self.emptyContendBox.isHidden = true self.creditsContendBox.isHidden = true self.emptyShowAIBtn.isHidden = true if AIInfoManager.default().aiInfoValid == true { self.creditsContendBox.isHidden = false var viewRect = self.view.frame viewRect.size.height = 330 self.view.frame = viewRect } else { self.emptyContendBox.isHidden = false self.emptyShowAIBtn.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.creditTipBtn.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.creditTipBtn.frame, of: self.creditTipBtn.superview!, preferredEdge: .maxY) } else { self.popOver.close() } } 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() } } } 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 self.emptyBuyNowBtn.wantsLayer = true self.emptyBuyNowBtn.layer?.backgroundColor = NSColor.clear.cgColor self.emptyBuyNowBtn.mouseMoveCallback = {[unowned 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 = {[unowned 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) self.creditsTitleLabel.textColor = NSColor.white self.alreadCreditBox.fillColor = NSColor(red: 155/255, green: 83/255, blue: 1, alpha: 0.3) } else { self.creditsInfoBox.fillColor = NSColor(red: 249/255, green: 247/255, blue: 250/255, alpha: 1) self.creditsTitleLabel.textColor = KMAppearance.KMColor_Layout_H0() self.alreadCreditBox.fillColor = NSColor(red: 155/255, green: 83/255, blue: 1, alpha: 0.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 = {[unowned self] mouseEntered in if mouseEntered { self.creditBuyNowBtn.layer?.backgroundColor = NSColor.white.withAlphaComponent(0.2).cgColor } else { self.creditBuyNowBtn.layer?.backgroundColor = NSColor.clear.cgColor } } self.creditsTitleLabel.font = NSFont.SFProTextSemiboldFont(14) self.validCreditLabel.font = NSFont.UbuntuBoldFontWithSize(32) self.validCreditLabel.textColor = NSColor(red: 166/255, green: 102/255, blue: 1, alpha: 1) self.totalCreditLabel.font = NSFont.UbuntuMediumFontWithSize(20) self.totalCreditLabel.textColor = KMAppearance.KMColor_Layout_H2() self.alreadCreditBox.borderWidth = 0 self.alreadyCreditLabel.font = NSFont.SFProTextRegularFont(13) self.alreadyCreditLabel.textColor = KMAppearance.KMColor_Layout_H1() self.timeDescriptionInfoLabel.textColor = KMAppearance.KMColor_Status_Err() self.timeDescriptionInfoLabel.font = NSFont.SFProTextRegularFont(13) self.dateInfoLabel.textColor = KMAppearance.KMColor_Layout_H2() self.dateInfoLabel.font = NSFont.SFProTextRegularFont(12) 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 #else self.emptyRestoreBtn.isHidden = true self.creditsRestoreBtn.isHidden = true if AIInfoManager.default().aiInfoValid == true { self.creditsBuyNowBox.isHidden = true } else { self.creditsBuyNowBox.isHidden = false } #endif } func updateCreditsViewInfo() -> Void { let aiInfo: AIInfo = AIInfoManager.default().aiInfo if aiInfo.infoDict.keys.count == 0 { return } self.validCreditLabel.stringValue = String(format: "%d", aiInfo.totalToken-aiInfo.usedTimes) self.totalCreditLabel.stringValue = "/" + String(format: "%d", aiInfo.totalToken) self.alreadyCreditLabel.stringValue = NSLocalizedString("Credits to be unlocked: ", comment: "") + String(format: "%d", aiInfo.pre_creditToken) let leftDays = String(format: "%ld", aiInfo.remainingDays) self.timeDescriptionInfoLabel.stringValue = String(format: NSLocalizedString("%@ days left on subscription", comment: ""), leftDays) let dateFormatter = DateFormatter.init() dateFormatter.dateFormat = "yyyy.MM.dd" let startDateStr = dateFormatter.string(from: aiInfo.startDate) let endDateStr = dateFormatter.string(from: aiInfo.endDate) self.dateInfoLabel.stringValue = startDateStr + " - " + endDateStr self.emptyContendBox.isHidden = true self.emptyShowAIBtn.isHidden = true self.creditsContendBox.isHidden = true if AIInfoManager.default().aiInfoValid == true { self.creditsContendBox.isHidden = false } else { self.emptyContendBox.isHidden = false self.emptyShowAIBtn.isHidden = false } if AIInfoManager.default().aiInfoValid == 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 } 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() } } }