// // FreeTrialApplyWindowController.swift // PDF Reader Pro // // Created by Niehaoyu on 2024/7/24. // import Cocoa class FreeTrialApplyWindowController: NSWindowController { @IBOutlet var contendBox: NSBox! @IBOutlet var titleLabel: NSTextField! @IBOutlet var headImage: NSImageView! @IBOutlet var emailContendView: NSView! @IBOutlet var emailLabel: NSTextField! @IBOutlet var emailBGView: NSView! @IBOutlet var emailTextField: NSTextField! @IBOutlet var emailErrorLabel: NSTextField! @IBOutlet var emailViewheightConst: NSLayoutConstraint! @IBOutlet var emailErrorViewHeight: NSLayoutConstraint! @IBOutlet var emailErrorTextView: NSTextView! @IBOutlet var emailErrorScrollView: NSScrollView! @IBOutlet var organizeContendView: NSView! @IBOutlet var organizeLabel: NSTextField! @IBOutlet var organizeBGView: NSView! @IBOutlet var organizeTextField: NSTextField! @IBOutlet var userNameContendView: NSView! @IBOutlet var userNameLabel: NSTextField! @IBOutlet var userNameBGView: NSView! @IBOutlet var userNameField: NSTextField! @IBOutlet var freeTrialBox: NSBox! @IBOutlet var freeTrialBtn: KMButton! @IBOutlet var selectedBtn: NSButton! @IBOutlet var privacyPolicyTextView: NSTextView! var trialApplySuccessWC: TrialSuccessWindowController? var isPrivacySelected = false { didSet { if isPrivacySelected { self.selectedBtn.image = NSImage(named: "layoutMCheckSel") } else { self.selectedBtn.image = NSImage(named: "layoutMCheckNull") } } } deinit { DistributedNotificationCenter.default().removeObserver(self) } convenience init() { self.init(windowNibName: "FreeTrialApplyWindowController") } override func windowDidLoad() { super.windowDidLoad() // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file. self.window?.delegate = self self.initViewInfo() self.refreshViewColor() self.isPrivacySelected = false self.refreshTrialBtnState() DistributedNotificationCenter.default().addObserver(self, selector: #selector(themeChange), name: NSNotification.Name(rawValue: "AppleInterfaceThemeChangedNotification"), object: nil) } func initViewInfo() { self.titleLabel.stringValue = NSLocalizedString("Free Trial LynxPDF Editor", comment: "") self.emailLabel.stringValue = NSLocalizedString("Email", comment: "") self.organizeLabel.stringValue = NSLocalizedString("Organization Name", comment: "") self.userNameLabel.stringValue = NSLocalizedString("User Name", comment: "") self.freeTrialBtn.title = NSLocalizedString("Free Trial", comment: "") self.emailTextField.placeholderString = NSLocalizedString("Enter Email", comment: "") self.organizeTextField.placeholderString = NSLocalizedString("Enter organization name...", comment: "") self.userNameField.placeholderString = NSLocalizedString("Enter name...", comment: "") self.emailErrorTextView.string = NSLocalizedString("Email address is not valid", comment: ""); if let data = self.emailErrorTextView.enclosingScrollView, CGRectEqualToRect(data.bounds, .zero) == false { self.emailErrorTextView.frame = data.bounds } self.emailErrorScrollView.isHidden = true self.updateEmailBGViewSize() self.contendBox.borderWidth = 0 self.titleLabel.font = NSFont.UbuntuBoldFontWithSize(24) self.emailLabel.font = NSFont.UbuntuRegularFontWithSize(16) self.organizeLabel.font = NSFont.UbuntuRegularFontWithSize(16) self.userNameLabel.font = NSFont.UbuntuRegularFontWithSize(16) self.freeTrialBtn.font = NSFont.UbuntuMediumFontWithSize(18) self.emailTextField.delegate = self self.organizeTextField.delegate = self self.userNameField.delegate = self self.privacyPolicyTextView.drawsBackground = false self.privacyPolicyTextView.isEditable = false self.emailErrorTextView.drawsBackground = false self.emailErrorTextView.isEditable = false self.emailErrorTextView.textColor = NSColor.red } func refreshViewColor() { if KMAppearance.isDarkMode() { self.contendBox.fillColor = NSColor(red: 14/255, green: 17/255, blue: 20/255, alpha: 1) self.titleLabel.textColor = KMAppearance.Layout.h0Color() self.emailLabel.textColor = KMAppearance.Layout.h0Color() self.organizeLabel.textColor = KMAppearance.Layout.h0Color() self.userNameLabel.textColor = KMAppearance.Layout.h0Color() self.headImage.image = nil let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.lineHeightMultiple = 1 paragraphStyle.alignment = .left let privacyString = NSLocalizedString("By proceeding with this request you agree to our Privacy Policy and Terms of Service. We will use your email address to respond to your request. When you receive that email you will have the ability to choose how you would like us to manage your personal data.", comment: "") let attributedString = NSMutableAttributedString(string: privacyString) let privacyRange = privacyString.range(of: NSLocalizedString("Privacy Policy", comment: "")) let termRange = privacyString.range(of: NSLocalizedString("Terms of Service", comment: "")) attributedString.addAttributes([NSAttributedString.Key.foregroundColor : NSColor(red: 136/255, green: 140/255, blue: 148/255, alpha: 1)], range: NSRange(location: 0, length: privacyString.count)) if privacyRange != nil { attributedString.addAttributes([NSAttributedString.Key.foregroundColor : NSColor(red: 30/255, green: 179/255, blue: 249/255, alpha: 1), NSAttributedString.Key.link : kAPPPrivacyPolicyURL], range: privacyString.nsRange(from: privacyRange!)!) } if termRange != nil { attributedString.addAttributes([NSAttributedString.Key.foregroundColor : NSColor(red: 30/255, green: 179/255, blue: 249/255, alpha: 1), NSAttributedString.Key.link : kAPPTermServiceURL], range: privacyString.nsRange(from: termRange!)!) } attributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: privacyString.count)) self.privacyPolicyTextView.textStorage?.setAttributedString(attributedString) self.privacyPolicyTextView.delegate = self for view in [self.emailBGView, self.organizeBGView, self.userNameBGView] { view?.wantsLayer = true view?.layer?.cornerRadius = 4 view?.layer?.borderWidth = 1 if self.emailErrorScrollView.isHidden { view?.layer?.borderColor = NSColor(red: 86/255, green: 88/255, blue: 90/255, alpha: 1).cgColor } else { if view == self.emailBGView { view?.layer?.borderColor = NSColor(red: 192/255, green: 48/255, blue: 48/255, alpha: 1).cgColor } else { view?.layer?.borderColor = NSColor(red: 86/255, green: 88/255, blue: 90/255, alpha: 1).cgColor } } view?.layer?.backgroundColor = NSColor(red: 57/255, green: 60/255, blue: 62/255, alpha: 1).cgColor } } else { self.contendBox.fillColor = NSColor.white self.titleLabel.textColor = NSColor(red: 0, green: 36/255, blue: 97/255, alpha: 1) self.emailLabel.textColor = NSColor(red: 64/255, green: 70/255, blue: 83/255, alpha: 1) self.organizeLabel.textColor = NSColor(red: 64/255, green: 70/255, blue: 83/255, alpha: 1) self.userNameLabel.textColor = NSColor(red: 64/255, green: 70/255, blue: 83/255, alpha: 1) self.headImage.image = NSImage(named: "trialApplyHead") let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.lineHeightMultiple = 1 paragraphStyle.alignment = .left let privacyString = NSLocalizedString("By proceeding with this request you agree to our Privacy Policy and Terms of Service. We will use your email address to respond to your request. When you receive that email you will have the ability to choose how you would like us to manage your personal data.", comment: "") let attributedString = NSMutableAttributedString(string: privacyString) let privacyRange = privacyString.range(of: NSLocalizedString("Privacy Policy", comment: "")) let termRange = privacyString.range(of: NSLocalizedString("Terms of Service", comment: "")) attributedString.addAttributes([NSAttributedString.Key.foregroundColor : NSColor(red: 136/255, green: 140/255, blue: 146/255, alpha: 1)], range: NSRange(location: 0, length: privacyString.count)) if privacyRange != nil { attributedString.addAttributes([NSAttributedString.Key.foregroundColor : NSColor(red: 30/255, green: 179/255, blue: 249/255, alpha: 1), NSAttributedString.Key.link : kAPPPrivacyPolicyURL], range: privacyString.nsRange(from: privacyRange!)!) } if termRange != nil { attributedString.addAttributes([NSAttributedString.Key.foregroundColor : NSColor(red: 30/255, green: 179/255, blue: 249/255, alpha: 1), NSAttributedString.Key.link : kAPPTermServiceURL], range: privacyString.nsRange(from: termRange!)!) } attributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: privacyString.count)) self.privacyPolicyTextView.textStorage?.setAttributedString(attributedString) if let data = self.privacyPolicyTextView.enclosingScrollView, CGRectEqualToRect(data.bounds, .zero) == false { self.privacyPolicyTextView.frame = data.bounds } self.privacyPolicyTextView.delegate = self for view in [self.emailBGView, self.organizeBGView, self.userNameBGView] { view?.wantsLayer = true view?.layer?.cornerRadius = 4 view?.layer?.borderWidth = 1 if self.emailErrorScrollView.isHidden { view?.layer?.borderColor = NSColor(red: 226/255, green: 227/255, blue: 229/255, alpha: 1).cgColor } else { if view == self.emailBGView { view?.layer?.borderColor = NSColor(red: 192/255, green: 48/255, blue: 48/255, alpha: 1).cgColor } else { view?.layer?.borderColor = NSColor(red: 226/255, green: 227/255, blue: 229/255, alpha: 1).cgColor } } view?.layer?.backgroundColor = NSColor.clear.cgColor } } } func isTrailButtonEnable() -> Bool { var isTrialClickEnable = true if self.emailTextField.stringValue.isEmpty { isTrialClickEnable = false } else { if self.emailTextField.stringValue.isEmail() == false { isTrialClickEnable = false } } if self.organizeTextField.stringValue.isEmpty { isTrialClickEnable = false } if self.userNameField.stringValue.isEmpty { isTrialClickEnable = false } if self.isPrivacySelected == false { isTrialClickEnable = false } return isTrialClickEnable } func refreshTrialBtnState() { let isTrialClickEnable = self.isTrailButtonEnable() if isTrialClickEnable { self.freeTrialBox.fillColor = KMAppearance.Layout.mColor() self.freeTrialBtn.setTitleColor(NSColor.white) } else { self.freeTrialBox.fillColor = KMAppearance.Layout.mColor().withAlphaComponent(0.4) self.freeTrialBtn.setTitleColor(NSColor.white.withAlphaComponent(0.4)) } } func updateEmailBGViewSize() { self.emailErrorLabel.isHidden = true if self.emailErrorScrollView.isHidden { self.emailViewheightConst.constant = 96 } else { self.emailErrorLabel.stringValue = self.emailErrorTextView.string let sizeHeight = self.emailErrorLabel.sizeThatFits(NSSize(width: 512, height: 100)).height self.emailViewheightConst.constant = 96 + sizeHeight - 16 self.emailErrorViewHeight.constant = sizeHeight } self.refreshViewColor() if self.emailErrorTextView.string.contains(NSLocalizedString("Admin Console", comment: "")) { let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.lineHeightMultiple = 1 paragraphStyle.alignment = .left let privacyString = self.emailErrorTextView.string let attributedString = NSMutableAttributedString(string: privacyString) let AdminConsoleRange = privacyString.range(of: NSLocalizedString("Admin Console", comment: "")) attributedString.addAttributes([NSAttributedString.Key.foregroundColor : self.emailErrorLabel.textColor as Any], range: NSRange(location: 0, length: privacyString.count)) attributedString.addAttributes([NSAttributedString.Key.foregroundColor : NSColor(red: 30/255, green: 179/255, blue: 249/255, alpha: 1), NSAttributedString.Key.link : KAdminConsoleLoginURL], range: privacyString.nsRange(from: AdminConsoleRange!)!) attributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: privacyString.count)) self.emailErrorTextView.textStorage?.setAttributedString(attributedString) self.emailErrorTextView.delegate = self } } func trialApplySuccess() { self.addWaingView(self.contendBox) DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 5) { self.removeWaitingView(self.contendBox) self.close() if self.trialApplySuccessWC == nil { self.trialApplySuccessWC = TrialSuccessWindowController.init() } self.trialApplySuccessWC?.window?.center() self.trialApplySuccessWC?.showWindow(nil) } } //MARK: WaitingView func addWaingView(_ view: NSView) -> Void { self.removeWaitingView(view) let wView = WaitingView.init(frame: view.bounds) wView.autoresizingMask = [.width, .height] view.addSubview(wView) wView.startAnimation() } func removeWaitingView(_ view: NSView) -> Void { for view in view.subviews { if view.className == WaitingView.className() { (view as! WaitingView).stopAnimation() view.removeFromSuperview() break } } } //MARK: IBAction @IBAction func applyAction(_ sender: Any) { let isTrialClickEnable = self.isTrailButtonEnable() if isTrialClickEnable == false { return } self.addWaingView((self.window?.contentView)!) var urlString = "https://store.pdfreaderpro.com/api/vpp_members/trialPDFTechEditor" if kTestMode == 1 { urlString = "http://test-store.kdan.cn:3018/api/vpp_members/trialPDFTechEditor" } let params: [String:Any] = ["email": self.emailTextField.stringValue, "username": self.userNameField.stringValue, "company":self.organizeTextField.stringValue] KMAdvertisementRequestServer.requestServer.request(urlString: urlString, method: "GET", params: params) { [weak self] task, responseObject, error in print("正在获取数据") self?.removeWaitingView((self?.window?.contentView)!) if (error == nil && responseObject != nil) { if responseObject != nil { var code: String = responseObject!["code"] as? String ?? "06005" if code == "06005" { if responseObject!["code"] != nil { let intCode = responseObject!["code"] as! Int code = String(intCode) } } let message: String = responseObject!["message"] as? String ?? "unknown error" let error = NSError(domain: message, code: Int(code)!) if Int(code)! == 200 || message == "success"{ self?.trialApplySuccess() } else if Int(code) == 502 || message == "Already tried" { self?.emailErrorScrollView.isHidden = false self?.emailErrorTextView.string = NSLocalizedString("Email has been registered, please log in to the Admin Console for management.", comment: "") self?.updateEmailBGViewSize() } else if Int(code) == 503 || message == "Email is not a super administrator" { self?.emailErrorScrollView.isHidden = false self?.emailErrorTextView.string = NSLocalizedString("Please contact the administrator to distribute the App. Or change the Email to apply for trial/download", comment: "") self?.updateEmailBGViewSize() } else { let alert = NSAlert() alert.alertStyle = .critical alert.messageText = message alert.runModal() } } } else { print("解析数据失败数据不存在") } } } @IBAction func privacySelAction(_ sender: Any) { self.isPrivacySelected = !self.isPrivacySelected self.refreshTrialBtnState() } override func mouseDown(with event: NSEvent) { super.mouseDown(with: event) self.window?.makeFirstResponder(nil) } @objc func themeChange() { DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) { self.refreshViewColor() } } } extension FreeTrialApplyWindowController: NSTextFieldDelegate { func controlTextDidChange(_ obj: Notification) { let textField = obj.object as? NSTextField if (textField == self.emailTextField) { self.emailErrorScrollView.isHidden = true self.updateEmailBGViewSize() } else if (textField == self.organizeTextField) { } else if (textField == self.userNameField) { } self.refreshTrialBtnState() } func controlTextDidEndEditing(_ obj: Notification) { let fied: NSTextField = obj.object as! NSTextField; if fied.isEqual(self.emailTextField) && self.emailTextField.stringValue.isEmpty == false { if self.emailTextField.stringValue.isEmail() == true { self.emailErrorScrollView.isHidden = true } else { self.emailErrorScrollView.isHidden = false; self.emailErrorTextView.string = NSLocalizedString("Email address is not valid", comment: ""); } self.updateEmailBGViewSize() } } } extension FreeTrialApplyWindowController: NSWindowDelegate { func windowShouldClose(_ sender: NSWindow) -> Bool { self.removeWaitingView(self.contendBox) return true } } extension FreeTrialApplyWindowController: NSTextViewDelegate { func textView(_ textView: NSTextView, clickedOnLink link: Any, at charIndex: Int) -> Bool { if link as! String == kAPPPrivacyPolicyURL { NSWorkspace.shared.open(URL(string: kAPPPrivacyPolicyURL)!) } else if link as! String == kAPPTermServiceURL { NSWorkspace.shared.open(URL(string: kAPPTermServiceURL)!) } else if link as! String == KAdminConsoleLoginURL { NSWorkspace.shared.open(URL(string: KAdminConsoleLoginURL)!) } return true } }