FreeTrialApplyWindowController.swift 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. //
  2. // FreeTrialApplyWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/7/24.
  6. //
  7. import Cocoa
  8. class FreeTrialApplyWindowController: NSWindowController {
  9. @IBOutlet var contendBox: NSBox!
  10. @IBOutlet var titleLabel: NSTextField!
  11. @IBOutlet var headImage: NSImageView!
  12. @IBOutlet var emailContendView: NSView!
  13. @IBOutlet var emailLabel: NSTextField!
  14. @IBOutlet var emailBGView: NSView!
  15. @IBOutlet var emailTextField: NSTextField!
  16. @IBOutlet var emailErrorLabel: NSTextField!
  17. @IBOutlet var emailViewheightConst: NSLayoutConstraint!
  18. @IBOutlet var emailErrorViewHeight: NSLayoutConstraint!
  19. @IBOutlet var emailErrorTextView: NSTextView!
  20. @IBOutlet var emailErrorScrollView: NSScrollView!
  21. @IBOutlet var organizeContendView: NSView!
  22. @IBOutlet var organizeLabel: NSTextField!
  23. @IBOutlet var organizeBGView: NSView!
  24. @IBOutlet var organizeTextField: NSTextField!
  25. @IBOutlet var userNameContendView: NSView!
  26. @IBOutlet var userNameLabel: NSTextField!
  27. @IBOutlet var userNameBGView: NSView!
  28. @IBOutlet var userNameField: NSTextField!
  29. @IBOutlet var freeTrialBox: NSBox!
  30. @IBOutlet var freeTrialBtn: KMButton!
  31. @IBOutlet var selectedBtn: NSButton!
  32. @IBOutlet var privacyPolicyTextView: NSTextView!
  33. var trialApplySuccessWC: TrialSuccessWindowController?
  34. var isPrivacySelected = false {
  35. didSet {
  36. if isPrivacySelected {
  37. self.selectedBtn.image = NSImage(named: "layoutMCheckSel")
  38. } else {
  39. self.selectedBtn.image = NSImage(named: "layoutMCheckNull")
  40. }
  41. }
  42. }
  43. deinit {
  44. DistributedNotificationCenter.default().removeObserver(self)
  45. }
  46. convenience init() {
  47. self.init(windowNibName: "FreeTrialApplyWindowController")
  48. }
  49. override func windowDidLoad() {
  50. super.windowDidLoad()
  51. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  52. self.window?.delegate = self
  53. self.initViewInfo()
  54. self.refreshViewColor()
  55. self.isPrivacySelected = false
  56. self.refreshTrialBtnState()
  57. DistributedNotificationCenter.default().addObserver(self, selector: #selector(themeChange), name: NSNotification.Name(rawValue: "AppleInterfaceThemeChangedNotification"), object: nil)
  58. }
  59. func initViewInfo() {
  60. self.titleLabel.stringValue = NSLocalizedString("Free Trial LynxPDF Editor", comment: "")
  61. self.emailLabel.stringValue = NSLocalizedString("Email", comment: "")
  62. self.organizeLabel.stringValue = NSLocalizedString("Organization Name", comment: "")
  63. self.userNameLabel.stringValue = NSLocalizedString("User Name", comment: "")
  64. self.freeTrialBtn.title = NSLocalizedString("Free Trial", comment: "")
  65. self.emailTextField.placeholderString = NSLocalizedString("Enter Email", comment: "")
  66. self.organizeTextField.placeholderString = NSLocalizedString("Enter organization name...", comment: "")
  67. self.userNameField.placeholderString = NSLocalizedString("Enter name...", comment: "")
  68. self.emailErrorTextView.string = NSLocalizedString("Email address is not valid", comment: "");
  69. if let data = self.emailErrorTextView.enclosingScrollView, CGRectEqualToRect(data.bounds, .zero) == false {
  70. self.emailErrorTextView.frame = data.bounds
  71. }
  72. self.emailErrorScrollView.isHidden = true
  73. self.updateEmailBGViewSize()
  74. self.contendBox.borderWidth = 0
  75. self.titleLabel.font = NSFont.UbuntuBoldFontWithSize(24)
  76. self.emailLabel.font = NSFont.UbuntuRegularFontWithSize(16)
  77. self.organizeLabel.font = NSFont.UbuntuRegularFontWithSize(16)
  78. self.userNameLabel.font = NSFont.UbuntuRegularFontWithSize(16)
  79. self.freeTrialBtn.font = NSFont.UbuntuMediumFontWithSize(18)
  80. self.emailTextField.delegate = self
  81. self.organizeTextField.delegate = self
  82. self.userNameField.delegate = self
  83. self.privacyPolicyTextView.drawsBackground = false
  84. self.privacyPolicyTextView.isEditable = false
  85. self.emailErrorTextView.drawsBackground = false
  86. self.emailErrorTextView.isEditable = false
  87. self.emailErrorTextView.textColor = NSColor.red
  88. }
  89. func refreshViewColor() {
  90. if KMAppearance.isDarkMode() {
  91. self.contendBox.fillColor = NSColor(red: 14/255, green: 17/255, blue: 20/255, alpha: 1)
  92. self.titleLabel.textColor = KMAppearance.Layout.h0Color()
  93. self.emailLabel.textColor = KMAppearance.Layout.h0Color()
  94. self.organizeLabel.textColor = KMAppearance.Layout.h0Color()
  95. self.userNameLabel.textColor = KMAppearance.Layout.h0Color()
  96. self.headImage.image = nil
  97. let paragraphStyle = NSMutableParagraphStyle()
  98. paragraphStyle.lineHeightMultiple = 1
  99. paragraphStyle.alignment = .left
  100. 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: "")
  101. let attributedString = NSMutableAttributedString(string: privacyString)
  102. let privacyRange = privacyString.range(of: NSLocalizedString("Privacy Policy", comment: ""))
  103. let termRange = privacyString.range(of: NSLocalizedString("Terms of Service", comment: ""))
  104. attributedString.addAttributes([NSAttributedString.Key.foregroundColor : NSColor(red: 136/255, green: 140/255, blue: 148/255, alpha: 1)],
  105. range: NSRange(location: 0, length: privacyString.count))
  106. if privacyRange != nil {
  107. attributedString.addAttributes([NSAttributedString.Key.foregroundColor : NSColor(red: 30/255, green: 179/255, blue: 249/255, alpha: 1),
  108. NSAttributedString.Key.link : kAPPPrivacyPolicyURL],
  109. range: privacyString.nsRange(from: privacyRange!)!)
  110. }
  111. if termRange != nil {
  112. attributedString.addAttributes([NSAttributedString.Key.foregroundColor : NSColor(red: 30/255, green: 179/255, blue: 249/255, alpha: 1),
  113. NSAttributedString.Key.link : kAPPTermServiceURL],
  114. range: privacyString.nsRange(from: termRange!)!)
  115. }
  116. attributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: privacyString.count))
  117. self.privacyPolicyTextView.textStorage?.setAttributedString(attributedString)
  118. self.privacyPolicyTextView.delegate = self
  119. for view in [self.emailBGView, self.organizeBGView, self.userNameBGView] {
  120. view?.wantsLayer = true
  121. view?.layer?.cornerRadius = 4
  122. view?.layer?.borderWidth = 1
  123. if self.emailErrorScrollView.isHidden {
  124. view?.layer?.borderColor = NSColor(red: 86/255, green: 88/255, blue: 90/255, alpha: 1).cgColor
  125. } else {
  126. if view == self.emailBGView {
  127. view?.layer?.borderColor = NSColor(red: 192/255, green: 48/255, blue: 48/255, alpha: 1).cgColor
  128. } else {
  129. view?.layer?.borderColor = NSColor(red: 86/255, green: 88/255, blue: 90/255, alpha: 1).cgColor
  130. }
  131. }
  132. view?.layer?.backgroundColor = NSColor(red: 57/255, green: 60/255, blue: 62/255, alpha: 1).cgColor
  133. }
  134. } else {
  135. self.contendBox.fillColor = NSColor.white
  136. self.titleLabel.textColor = NSColor(red: 0, green: 36/255, blue: 97/255, alpha: 1)
  137. self.emailLabel.textColor = NSColor(red: 64/255, green: 70/255, blue: 83/255, alpha: 1)
  138. self.organizeLabel.textColor = NSColor(red: 64/255, green: 70/255, blue: 83/255, alpha: 1)
  139. self.userNameLabel.textColor = NSColor(red: 64/255, green: 70/255, blue: 83/255, alpha: 1)
  140. self.headImage.image = NSImage(named: "trialApplyHead")
  141. let paragraphStyle = NSMutableParagraphStyle()
  142. paragraphStyle.lineHeightMultiple = 1
  143. paragraphStyle.alignment = .left
  144. 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: "")
  145. let attributedString = NSMutableAttributedString(string: privacyString)
  146. let privacyRange = privacyString.range(of: NSLocalizedString("Privacy Policy", comment: ""))
  147. let termRange = privacyString.range(of: NSLocalizedString("Terms of Service", comment: ""))
  148. attributedString.addAttributes([NSAttributedString.Key.foregroundColor : NSColor(red: 136/255, green: 140/255, blue: 146/255, alpha: 1)],
  149. range: NSRange(location: 0, length: privacyString.count))
  150. if privacyRange != nil {
  151. attributedString.addAttributes([NSAttributedString.Key.foregroundColor : NSColor(red: 30/255, green: 179/255, blue: 249/255, alpha: 1),
  152. NSAttributedString.Key.link : kAPPPrivacyPolicyURL],
  153. range: privacyString.nsRange(from: privacyRange!)!)
  154. }
  155. if termRange != nil {
  156. attributedString.addAttributes([NSAttributedString.Key.foregroundColor : NSColor(red: 30/255, green: 179/255, blue: 249/255, alpha: 1),
  157. NSAttributedString.Key.link : kAPPTermServiceURL],
  158. range: privacyString.nsRange(from: termRange!)!)
  159. }
  160. attributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: privacyString.count))
  161. self.privacyPolicyTextView.textStorage?.setAttributedString(attributedString)
  162. if let data = self.privacyPolicyTextView.enclosingScrollView, CGRectEqualToRect(data.bounds, .zero) == false {
  163. self.privacyPolicyTextView.frame = data.bounds
  164. }
  165. self.privacyPolicyTextView.delegate = self
  166. for view in [self.emailBGView, self.organizeBGView, self.userNameBGView] {
  167. view?.wantsLayer = true
  168. view?.layer?.cornerRadius = 4
  169. view?.layer?.borderWidth = 1
  170. if self.emailErrorScrollView.isHidden {
  171. view?.layer?.borderColor = NSColor(red: 226/255, green: 227/255, blue: 229/255, alpha: 1).cgColor
  172. } else {
  173. if view == self.emailBGView {
  174. view?.layer?.borderColor = NSColor(red: 192/255, green: 48/255, blue: 48/255, alpha: 1).cgColor
  175. } else {
  176. view?.layer?.borderColor = NSColor(red: 226/255, green: 227/255, blue: 229/255, alpha: 1).cgColor
  177. }
  178. }
  179. view?.layer?.backgroundColor = NSColor.clear.cgColor
  180. }
  181. }
  182. }
  183. func isTrailButtonEnable() -> Bool {
  184. var isTrialClickEnable = true
  185. if self.emailTextField.stringValue.isEmpty {
  186. isTrialClickEnable = false
  187. } else {
  188. if self.emailTextField.stringValue.isEmail() == false {
  189. isTrialClickEnable = false
  190. }
  191. }
  192. if self.organizeTextField.stringValue.isEmpty {
  193. isTrialClickEnable = false
  194. }
  195. if self.userNameField.stringValue.isEmpty {
  196. isTrialClickEnable = false
  197. }
  198. if self.isPrivacySelected == false {
  199. isTrialClickEnable = false
  200. }
  201. return isTrialClickEnable
  202. }
  203. func refreshTrialBtnState() {
  204. let isTrialClickEnable = self.isTrailButtonEnable()
  205. if isTrialClickEnable {
  206. self.freeTrialBox.fillColor = KMAppearance.Layout.mColor()
  207. self.freeTrialBtn.setTitleColor(NSColor.white)
  208. } else {
  209. self.freeTrialBox.fillColor = KMAppearance.Layout.mColor().withAlphaComponent(0.4)
  210. self.freeTrialBtn.setTitleColor(NSColor.white.withAlphaComponent(0.4))
  211. }
  212. }
  213. func updateEmailBGViewSize() {
  214. self.emailErrorLabel.isHidden = true
  215. if self.emailErrorScrollView.isHidden {
  216. self.emailViewheightConst.constant = 96
  217. } else {
  218. self.emailErrorLabel.stringValue = self.emailErrorTextView.string
  219. let sizeHeight = self.emailErrorLabel.sizeThatFits(NSSize(width: 512, height: 100)).height
  220. self.emailViewheightConst.constant = 96 + sizeHeight - 16
  221. self.emailErrorViewHeight.constant = sizeHeight
  222. }
  223. self.refreshViewColor()
  224. if self.emailErrorTextView.string.contains(NSLocalizedString("Admin Console", comment: "")) {
  225. let paragraphStyle = NSMutableParagraphStyle()
  226. paragraphStyle.lineHeightMultiple = 1
  227. paragraphStyle.alignment = .left
  228. let privacyString = self.emailErrorTextView.string
  229. let attributedString = NSMutableAttributedString(string: privacyString)
  230. let AdminConsoleRange = privacyString.range(of: NSLocalizedString("Admin Console", comment: ""))
  231. attributedString.addAttributes([NSAttributedString.Key.foregroundColor : self.emailErrorLabel.textColor as Any],
  232. range: NSRange(location: 0, length: privacyString.count))
  233. attributedString.addAttributes([NSAttributedString.Key.foregroundColor : NSColor(red: 30/255, green: 179/255, blue: 249/255, alpha: 1),
  234. NSAttributedString.Key.link : KAdminConsoleLoginURL],
  235. range: privacyString.nsRange(from: AdminConsoleRange!)!)
  236. attributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: privacyString.count))
  237. self.emailErrorTextView.textStorage?.setAttributedString(attributedString)
  238. self.emailErrorTextView.delegate = self
  239. }
  240. }
  241. func trialApplySuccess() {
  242. self.addWaingView(self.contendBox)
  243. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 5) {
  244. self.removeWaitingView(self.contendBox)
  245. self.close()
  246. if self.trialApplySuccessWC == nil {
  247. self.trialApplySuccessWC = TrialSuccessWindowController.init()
  248. }
  249. self.trialApplySuccessWC?.window?.center()
  250. self.trialApplySuccessWC?.showWindow(nil)
  251. }
  252. }
  253. //MARK: WaitingView
  254. func addWaingView(_ view: NSView) -> Void {
  255. self.removeWaitingView(view)
  256. let wView = WaitingView.init(frame: view.bounds)
  257. wView.autoresizingMask = [.width, .height]
  258. view.addSubview(wView)
  259. wView.startAnimation()
  260. }
  261. func removeWaitingView(_ view: NSView) -> Void {
  262. for view in view.subviews {
  263. if view.className == WaitingView.className() {
  264. (view as! WaitingView).stopAnimation()
  265. view.removeFromSuperview()
  266. break
  267. }
  268. }
  269. }
  270. //MARK: IBAction
  271. @IBAction func applyAction(_ sender: Any) {
  272. let isTrialClickEnable = self.isTrailButtonEnable()
  273. if isTrialClickEnable == false {
  274. return
  275. }
  276. self.addWaingView((self.window?.contentView)!)
  277. var urlString = "https://store.pdfreaderpro.com/api/vpp_members/trialPDFTechEditor"
  278. if kTestMode == 1 {
  279. urlString = "http://test-store.kdan.cn:3018/api/vpp_members/trialPDFTechEditor"
  280. }
  281. let params: [String:Any] = ["email": self.emailTextField.stringValue,
  282. "username": self.userNameField.stringValue,
  283. "company":self.organizeTextField.stringValue]
  284. KMAdvertisementRequestServer.requestServer.request(urlString: urlString, method: "GET", params: params) { [weak self] task, responseObject, error in
  285. print("正在获取数据")
  286. self?.removeWaitingView((self?.window?.contentView)!)
  287. if (error == nil && responseObject != nil) {
  288. if responseObject != nil {
  289. var code: String = responseObject!["code"] as? String ?? "06005"
  290. if code == "06005" {
  291. if responseObject!["code"] != nil {
  292. let intCode = responseObject!["code"] as! Int
  293. code = String(intCode)
  294. }
  295. }
  296. let message: String = responseObject!["message"] as? String ?? "unknown error"
  297. let error = NSError(domain: message, code: Int(code)!)
  298. if Int(code)! == 200 || message == "success"{
  299. self?.trialApplySuccess()
  300. } else if Int(code) == 502 || message == "Already tried" {
  301. self?.emailErrorScrollView.isHidden = false
  302. self?.emailErrorTextView.string = NSLocalizedString("Email has been registered, please log in to the Admin Console for management.", comment: "")
  303. self?.updateEmailBGViewSize()
  304. } else if Int(code) == 503 || message == "Email is not a super administrator" {
  305. self?.emailErrorScrollView.isHidden = false
  306. self?.emailErrorTextView.string = NSLocalizedString("Please contact the administrator to distribute the App. Or change the Email to apply for trial/download", comment: "")
  307. self?.updateEmailBGViewSize()
  308. } else {
  309. let alert = NSAlert()
  310. alert.alertStyle = .critical
  311. alert.messageText = message
  312. alert.runModal()
  313. }
  314. }
  315. } else {
  316. print("解析数据失败数据不存在")
  317. }
  318. }
  319. }
  320. @IBAction func privacySelAction(_ sender: Any) {
  321. self.isPrivacySelected = !self.isPrivacySelected
  322. self.refreshTrialBtnState()
  323. }
  324. override func mouseDown(with event: NSEvent) {
  325. super.mouseDown(with: event)
  326. self.window?.makeFirstResponder(nil)
  327. }
  328. @objc func themeChange() {
  329. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
  330. self.refreshViewColor()
  331. }
  332. }
  333. }
  334. extension FreeTrialApplyWindowController: NSTextFieldDelegate {
  335. func controlTextDidChange(_ obj: Notification) {
  336. let textField = obj.object as? NSTextField
  337. if (textField == self.emailTextField) {
  338. self.emailErrorScrollView.isHidden = true
  339. self.updateEmailBGViewSize()
  340. } else if (textField == self.organizeTextField) {
  341. } else if (textField == self.userNameField) {
  342. }
  343. self.refreshTrialBtnState()
  344. }
  345. func controlTextDidEndEditing(_ obj: Notification) {
  346. let fied: NSTextField = obj.object as! NSTextField;
  347. if fied.isEqual(self.emailTextField) && self.emailTextField.stringValue.isEmpty == false {
  348. if self.emailTextField.stringValue.isEmail() == true {
  349. self.emailErrorScrollView.isHidden = true
  350. } else {
  351. self.emailErrorScrollView.isHidden = false;
  352. self.emailErrorTextView.string = NSLocalizedString("Email address is not valid", comment: "");
  353. }
  354. self.updateEmailBGViewSize()
  355. }
  356. }
  357. }
  358. extension FreeTrialApplyWindowController: NSWindowDelegate {
  359. func windowShouldClose(_ sender: NSWindow) -> Bool {
  360. self.removeWaitingView(self.contendBox)
  361. return true
  362. }
  363. }
  364. extension FreeTrialApplyWindowController: NSTextViewDelegate {
  365. func textView(_ textView: NSTextView, clickedOnLink link: Any, at charIndex: Int) -> Bool {
  366. if link as! String == kAPPPrivacyPolicyURL {
  367. NSWorkspace.shared.open(URL(string: kAPPPrivacyPolicyURL)!)
  368. } else if link as! String == kAPPTermServiceURL {
  369. NSWorkspace.shared.open(URL(string: kAPPTermServiceURL)!)
  370. } else if link as! String == KAdminConsoleLoginURL {
  371. NSWorkspace.shared.open(URL(string: KAdminConsoleLoginURL)!)
  372. }
  373. return true
  374. }
  375. }