123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470 |
- //
- // 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
- }
- }
|