KMMemberCenterWindowController.swift 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. //
  2. // KMMemberCenterWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by 王帅 on 2024/11/12.
  6. //
  7. import Cocoa
  8. class KMMemberCenterWindowController: NSWindowController {
  9. @IBOutlet weak var productBox: NSBox!
  10. @IBOutlet weak var productView: NSView!
  11. @IBOutlet weak var productLabel: NSTextField!
  12. @IBOutlet weak var typeLabel: NSTextField!
  13. @IBOutlet weak var device1View: NSView!
  14. @IBOutlet weak var device1Label: NSTextField!
  15. @IBOutlet weak var device1Button: NSButton!
  16. @IBOutlet weak var device2View: NSView!
  17. @IBOutlet weak var device2Label: NSTextField!
  18. @IBOutlet weak var device2Button: NSButton!
  19. @IBOutlet weak var device3View: NSView!
  20. @IBOutlet weak var device3Label: NSTextField!
  21. @IBOutlet weak var device3Button: NSButton!
  22. @IBOutlet weak var device4View: NSView!
  23. @IBOutlet weak var device4Label: NSTextField!
  24. @IBOutlet weak var device4Button: NSButton!
  25. @IBOutlet var bottomoffset:NSLayoutConstraint!
  26. @IBOutlet weak var tipLabel: NSTextField!
  27. @IBOutlet weak var cancelButton: NSButton!
  28. @IBOutlet weak var loginButton: NSButton!
  29. var signUpViewModel:KMSignUpViewModel?
  30. static let shared: KMMemberCenterWindowController = {
  31. let windowC = KMMemberCenterWindowController(windowNibName: "KMMemberCenterWindowController")
  32. return windowC
  33. }()
  34. public var memberCenterdeviceResult: KMMemberCenterResult = KMMemberCenterResult(loginResult: KMMemberLoginResult(refreshToken: "", accessToken: "", tokenType: "", expiresIn: "")) {
  35. didSet {
  36. refreshData()
  37. }
  38. }
  39. override func windowDidLoad() {
  40. super.windowDidLoad()
  41. self.window?.title = NSLocalizedString("Device Management", tableName: "MemberCenterLocalizable", comment: "")
  42. productLabel.stringValue = NSLocalizedString("Device name", tableName: "MemberCenterLocalizable", comment: "")
  43. typeLabel.stringValue = NSLocalizedString("Action", tableName: "MemberCenterLocalizable", comment: "")
  44. device1Button.title = NSLocalizedString("Unbind", tableName: "MemberCenterLocalizable", comment: "")
  45. device2Button.title = NSLocalizedString("Unbind", tableName: "MemberCenterLocalizable", comment: "")
  46. device3Button.title = NSLocalizedString("Unbind", tableName: "MemberCenterLocalizable", comment: "")
  47. device4Button.title = NSLocalizedString("Unbind", tableName: "MemberCenterLocalizable", comment: "")
  48. cancelButton.title = NSLocalizedString("Do not log in to the current device", tableName: "MemberCenterLocalizable", comment: "")
  49. loginButton.title = NSLocalizedString("Log in to the current device", tableName: "MemberCenterLocalizable", comment: "")
  50. initializeUI()
  51. DistributedNotificationCenter.default.addObserver(self, selector: #selector(themeChanged(_:)), name: NSNotification.Name("AppleInterfaceThemeChangedNotification"), object: nil)
  52. }
  53. @objc func themeChanged(_ notification: Notification) {
  54. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  55. self.initializeUI()
  56. }
  57. }
  58. private func initializeUI() -> Void {
  59. self.window?.contentView?.wantsLayer = true
  60. let isDarkModel = KMAdvertisementConfig.isDarkModel()
  61. if isDarkModel {
  62. self.window?.contentView?.layer?.backgroundColor = NSColor(hex: "282828").cgColor;
  63. } else {
  64. self.window?.contentView?.layer?.backgroundColor = NSColor(hex: "f6f6f6").cgColor;
  65. }
  66. productBox.borderColor = NSColor(named: "DADBDE") ?? .gray
  67. productLabel.textColor = NSColor(named: "42464D")
  68. typeLabel.textColor = NSColor(named: "42464D")
  69. device1Label.textColor = NSColor(named: "42464D")
  70. device2Label.textColor = NSColor(named: "42464D")
  71. device3Label.textColor = NSColor(named: "42464D")
  72. device4Label.textColor = NSColor(named: "42464D")
  73. tipLabel.textColor = NSColor(named: "4982E6")
  74. cancelButton.setTitleColor(color: NSColor(named: "000000") ?? NSColor.white, font: NSFont.SFProTextRegularFont(13))
  75. productView.wantsLayer = true
  76. productView.layer?.backgroundColor = NSColor(named: "000000_0.05")?.cgColor
  77. device1View.wantsLayer = true
  78. device1View.layer?.backgroundColor = NSColor(named: "000000_0.76")?.cgColor
  79. device2View.wantsLayer = true
  80. device2View.layer?.backgroundColor = NSColor(named: "000000_0.76")?.cgColor
  81. device3View.wantsLayer = true
  82. device3View.layer?.backgroundColor = NSColor(named: "000000_0.76")?.cgColor
  83. device4View.wantsLayer = true
  84. device4View.layer?.backgroundColor = NSColor(named: "000000_0.76")?.cgColor
  85. device1Button.setTitleColor(color: NSColor(named: "4982E6") ?? NSColor.blue, font: NSFont.SFProTextRegularFont(13))
  86. device2Button.setTitleColor(color: NSColor(named: "4982E6") ?? NSColor.blue, font: NSFont.SFProTextRegularFont(13))
  87. device3Button.setTitleColor(color: NSColor(named: "4982E6") ?? NSColor.blue, font: NSFont.SFProTextRegularFont(13))
  88. device4Button.setTitleColor(color: NSColor(named: "4982E6") ?? NSColor.blue, font: NSFont.SFProTextRegularFont(13))
  89. }
  90. private func refreshData() {
  91. let deviceList_Result:[KMMemberDeviceList] = memberCenterdeviceResult.deviceList_Result ?? []
  92. bottomoffset.constant = CGFloat(Int(deviceList_Result.count) * 32)
  93. if(deviceList_Result.count == 4) {
  94. } else if (deviceList_Result.count == 3) {
  95. device4View.isHidden = true
  96. device3View.isHidden = false
  97. device2View.isHidden = false
  98. device1View.isHidden = false
  99. } else if (deviceList_Result.count == 2) {
  100. device4View.isHidden = true
  101. device3View.isHidden = true
  102. device2View.isHidden = false
  103. device1View.isHidden = false
  104. } else if (deviceList_Result.count == 1) {
  105. device4View.isHidden = true
  106. device3View.isHidden = true
  107. device2View.isHidden = true
  108. device1View.isHidden = false
  109. } else if (deviceList_Result.count == 0) {
  110. device4View.isHidden = true
  111. device3View.isHidden = true
  112. device2View.isHidden = true
  113. device1View.isHidden = true
  114. }
  115. for i in 0 ..< deviceList_Result.count {
  116. let device = deviceList_Result[i]
  117. if i == 0 {
  118. device1Label.stringValue = device.deviceName ?? "PDF Reader Pro User"
  119. } else if i == 1 {
  120. device2Label.stringValue = device.deviceName ?? "PDF Reader Pro User"
  121. } else if i == 2 {
  122. device3Label.stringValue = device.deviceName ?? "PDF Reader Pro User"
  123. } else if i == 3 {
  124. device4Label.stringValue = device.deviceName ?? "PDF Reader Pro User"
  125. }
  126. }
  127. tipLabel.stringValue = String(format: NSLocalizedString("Can keep %d devices online at the same time", tableName: "MemberCenterLocalizable", comment: ""), memberCenterdeviceResult.maxDeviceNum ?? 1)
  128. if deviceList_Result.count == memberCenterdeviceResult.maxDeviceNum {
  129. loginButton.isEnabled = false
  130. } else {
  131. loginButton.isEnabled = true
  132. }
  133. }
  134. // MARK: Button Actions
  135. @IBAction func buttonItemClicked_UntieDevice(_ sender: NSButton) {
  136. let tag = sender.tag
  137. var deviceList_Result:[KMMemberDeviceList] = memberCenterdeviceResult.deviceList_Result ?? []
  138. if(deviceList_Result.count > tag) {
  139. let device = deviceList_Result[tag]
  140. if(device.id?.isEmpty == true) {
  141. return
  142. }
  143. if KMMemberCenterManager.manager.isConnectionAvailable() == false {
  144. let alert = NSAlert()
  145. alert.alertStyle = .critical
  146. alert.messageText = NSLocalizedString("Error Information", comment: "")
  147. alert.informativeText = NSLocalizedString("Please make sure your internet connection is available.", comment: "")
  148. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  149. alert.runModal()
  150. return
  151. }
  152. showWaitingView(to: window?.contentView ?? NSView())
  153. KMMemberCenterManager.manager.logoutDevice(deviceId: device.id!, complete: {[weak self] success, result in
  154. self?.hideWaitingView(from: self?.window?.contentView ?? NSView())
  155. guard let result = result else { return }
  156. let resultDict = result as KMMemberCenterResult
  157. let msg = resultDict.msg
  158. if success {
  159. deviceList_Result.remove(at: Int(tag))
  160. self?.memberCenterdeviceResult.deviceList_Result = deviceList_Result
  161. self?.refreshData()
  162. } else {
  163. print(msg as Any)
  164. }
  165. })
  166. }
  167. }
  168. @IBAction func buttonItemClicked_Cancel(_ sender: NSButton) {
  169. window?.close()
  170. }
  171. @IBAction func buttonItemClicked_Login(_ sender: NSButton) {
  172. window?.showWaitingView()
  173. signUpViewModel?.signUpAction { [weak self] result, _ in
  174. self?.window?.hideWaitingView()
  175. self?.window?.close()
  176. }
  177. }
  178. }