KMMemberCenterWindowController.swift 9.0 KB

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