KMMemberCenterWindowController.swift 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. if i == 0 {
  122. device1Label.stringValue = device.deviceName ?? "PDF Reader Pro User"
  123. } else if i == 1 {
  124. device2Label.stringValue = device.deviceName ?? "PDF Reader Pro User"
  125. } else if i == 2 {
  126. device3Label.stringValue = device.deviceName ?? "PDF Reader Pro User"
  127. } else if i == 3 {
  128. device4Label.stringValue = device.deviceName ?? "PDF Reader Pro User"
  129. }
  130. }
  131. let maxDeviceNum = memberCenterdeviceResult.maxDeviceNum ?? 1
  132. if (maxDeviceNum <= 1) {
  133. tipLabel.stringValue = String(format: NSLocalizedString("Can keep %d device online at the same time", tableName: "MemberCenterLocalizable", comment: ""), maxDeviceNum)
  134. } else {
  135. tipLabel.stringValue = String(format: NSLocalizedString("Can keep %d devices online at the same time", tableName: "MemberCenterLocalizable", comment: ""), maxDeviceNum)
  136. }
  137. if deviceList_Result.count == maxDeviceNum {
  138. loginButton.isEnabled = false
  139. } else {
  140. loginButton.isEnabled = true
  141. }
  142. }
  143. // MARK: Button Actions
  144. @IBAction func buttonItemClicked_UntieDevice(_ sender: NSButton) {
  145. let tag = sender.tag
  146. var deviceList_Result:[KMMemberDeviceList] = memberCenterdeviceResult.deviceList_Result ?? []
  147. if(deviceList_Result.count > tag) {
  148. let device = deviceList_Result[tag]
  149. if(device.id?.isEmpty == true) {
  150. return
  151. }
  152. if KMMemberCenterManager.manager.isConnectionAvailable() == false {
  153. let alert = NSAlert()
  154. alert.alertStyle = .critical
  155. alert.messageText = NSLocalizedString("Error Information", comment: "")
  156. alert.informativeText = NSLocalizedString("Please make sure your internet connection is available.", comment: "")
  157. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  158. alert.runModal()
  159. return
  160. }
  161. showWaitingView(to: window?.contentView ?? NSView())
  162. KMMemberCenterManager.manager.logoutDevice(deviceId: device.id!, complete: {[weak self] success, result in
  163. self?.hideWaitingView(from: self?.window?.contentView ?? NSView())
  164. guard let result = result else { return }
  165. let resultDict = result as KMMemberCenterResult
  166. let msg = resultDict.msg
  167. if success {
  168. deviceList_Result.remove(at: Int(tag))
  169. self?.memberCenterdeviceResult.deviceList_Result = deviceList_Result
  170. self?.refreshData()
  171. } else {
  172. print(msg as Any)
  173. }
  174. })
  175. }
  176. }
  177. @IBAction func buttonItemClicked_Cancel(_ sender: NSButton) {
  178. window?.close()
  179. }
  180. @IBAction func buttonItemClicked_Login(_ sender: NSButton) {
  181. window?.showWaitingView()
  182. signUpViewModel?.signUpAction { [weak self] result, _ in
  183. self?.window?.hideWaitingView()
  184. self?.window?.close()
  185. }
  186. }
  187. }