KMMemberCenterWindowController.swift 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. productView.wantsLayer = true
  51. device1View.wantsLayer = true
  52. device2View.wantsLayer = true
  53. device3View.wantsLayer = true
  54. device4View.wantsLayer = true
  55. initializeUI()
  56. NotificationCenter.default.addObserver(self, selector: #selector(changeEffectiveAppearance), name: NSNotification.Name(rawValue: "kEffectiveAppearance"), object: nil)
  57. }
  58. @objc func changeEffectiveAppearance() {
  59. self.initializeUI()
  60. }
  61. private func initializeUI() -> Void {
  62. self.window?.contentView?.wantsLayer = true
  63. let isDarkModel = KMAdvertisementConfig.isDarkModel()
  64. var proColor = NSColor(hex: "323232")
  65. var prosubColor = NSColor(hex: "272727")
  66. if isDarkModel {
  67. self.window?.contentView?.layer?.backgroundColor = NSColor(hex: "282828").cgColor
  68. proColor = NSColor(hex: "323232")
  69. prosubColor = NSColor(hex: "272727")
  70. } else {
  71. self.window?.contentView?.layer?.backgroundColor = NSColor(hex: "f6f6f6").cgColor;
  72. proColor = NSColor(hex: "E4E4E4")
  73. prosubColor = NSColor(hex: "F0F0F0")
  74. }
  75. productView.layer?.backgroundColor = proColor.cgColor
  76. device1View.layer?.backgroundColor = prosubColor.cgColor
  77. device2View.layer?.backgroundColor = prosubColor.cgColor
  78. device3View.layer?.backgroundColor = prosubColor.cgColor
  79. device4View.layer?.backgroundColor = prosubColor.cgColor
  80. productBox.borderColor = NSColor(named: "DADBDE") ?? .gray
  81. productLabel.textColor = NSColor(named: "42464D")
  82. typeLabel.textColor = NSColor(named: "42464D")
  83. device1Label.textColor = NSColor(named: "42464D")
  84. device2Label.textColor = NSColor(named: "42464D")
  85. device3Label.textColor = NSColor(named: "42464D")
  86. device4Label.textColor = NSColor(named: "42464D")
  87. tipLabel.textColor = NSColor(named: "4982E6")
  88. cancelButton.setTitleColor(color: NSColor(named: "000000") ?? NSColor.white, font: NSFont.SFProTextRegularFont(13))
  89. device1Button.setTitleColor(color: NSColor(named: "4982E6") ?? NSColor.blue, font: NSFont.SFProTextRegularFont(13))
  90. device2Button.setTitleColor(color: NSColor(named: "4982E6") ?? NSColor.blue, font: NSFont.SFProTextRegularFont(13))
  91. device3Button.setTitleColor(color: NSColor(named: "4982E6") ?? NSColor.blue, font: NSFont.SFProTextRegularFont(13))
  92. device4Button.setTitleColor(color: NSColor(named: "4982E6") ?? NSColor.blue, font: NSFont.SFProTextRegularFont(13))
  93. }
  94. private func refreshData() {
  95. let deviceList_Result:[KMMemberDeviceList] = memberCenterdeviceResult.deviceList_Result ?? []
  96. bottomoffset.constant = CGFloat(Int(deviceList_Result.count) * 32)
  97. if(deviceList_Result.count == 4) {
  98. } else if (deviceList_Result.count == 3) {
  99. device4View.isHidden = true
  100. device3View.isHidden = false
  101. device2View.isHidden = false
  102. device1View.isHidden = false
  103. } else if (deviceList_Result.count == 2) {
  104. device4View.isHidden = true
  105. device3View.isHidden = true
  106. device2View.isHidden = false
  107. device1View.isHidden = false
  108. } else if (deviceList_Result.count == 1) {
  109. device4View.isHidden = true
  110. device3View.isHidden = true
  111. device2View.isHidden = true
  112. device1View.isHidden = false
  113. } else if (deviceList_Result.count == 0) {
  114. device4View.isHidden = true
  115. device3View.isHidden = true
  116. device2View.isHidden = true
  117. device1View.isHidden = true
  118. }
  119. for i in 0 ..< deviceList_Result.count {
  120. let device = deviceList_Result[i]
  121. let name = String(format: "%@(%@)",device.deviceName ?? "PDF Reader Pro User for ",device.device_model ?? " Device Name")
  122. if i == 0 {
  123. device1Label.stringValue = name
  124. } else if i == 1 {
  125. device2Label.stringValue = name
  126. } else if i == 2 {
  127. device3Label.stringValue = name
  128. } else if i == 3 {
  129. device4Label.stringValue = name
  130. }
  131. }
  132. let maxDeviceNum = memberCenterdeviceResult.maxDeviceNum ?? 1
  133. if (maxDeviceNum <= 1) {
  134. tipLabel.stringValue = String(format: NSLocalizedString("Can keep %d device online at the same time", tableName: "MemberCenterLocalizable", comment: ""), maxDeviceNum)
  135. } else {
  136. tipLabel.stringValue = String(format: NSLocalizedString("Can keep %d devices online at the same time", tableName: "MemberCenterLocalizable", comment: ""), maxDeviceNum)
  137. }
  138. if deviceList_Result.count == maxDeviceNum {
  139. loginButton.isEnabled = false
  140. } else {
  141. loginButton.isEnabled = true
  142. }
  143. }
  144. // MARK: Button Actions
  145. @IBAction func buttonItemClicked_UntieDevice(_ sender: NSButton) {
  146. let tag = sender.tag
  147. var deviceList_Result:[KMMemberDeviceList] = memberCenterdeviceResult.deviceList_Result ?? []
  148. if(deviceList_Result.count > tag) {
  149. let device = deviceList_Result[tag]
  150. if(device.id?.isEmpty == true) {
  151. return
  152. }
  153. if KMMemberCenterManager.manager.isConnectionAvailable() == false {
  154. let alert = NSAlert()
  155. alert.alertStyle = .critical
  156. alert.messageText = NSLocalizedString("Error Information", comment: "")
  157. alert.informativeText = NSLocalizedString("Please make sure your internet connection is available.", comment: "")
  158. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  159. alert.runModal()
  160. return
  161. }
  162. showWaitingView(to: window?.contentView ?? NSView())
  163. KMMemberCenterManager.manager.logoutDevice(deviceId: device.id!, complete: {[weak self] success, result in
  164. self?.hideWaitingView(from: self?.window?.contentView ?? NSView())
  165. guard let result = result else { return }
  166. let resultDict = result as KMMemberCenterResult
  167. let msg = resultDict.msg
  168. if success {
  169. deviceList_Result.remove(at: Int(tag))
  170. self?.memberCenterdeviceResult.deviceList_Result = deviceList_Result
  171. self?.refreshData()
  172. } else {
  173. KMMemberCenterManager.showAlert(code: .init(rawValue: result.code), message: result.msg, callback: nil)
  174. }
  175. })
  176. }
  177. }
  178. @IBAction func buttonItemClicked_Cancel(_ sender: NSButton) {
  179. window?.close()
  180. }
  181. @IBAction func buttonItemClicked_Login(_ sender: NSButton) {
  182. window?.showWaitingView()
  183. signUpViewModel?.signUpAction { [weak self] result, _ in
  184. self?.window?.hideWaitingView()
  185. self?.window?.close()
  186. }
  187. }
  188. }