O2UserDefaults.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // O2UserDefaults.swift
  3. // O2OA_SDK_Framwork
  4. //
  5. // Created by FancyLou on 2018/11/7.
  6. //
  7. import Foundation
  8. import SwiftyUserDefaults
  9. // MARK: - 扩展自定义对象存储
  10. extension UserDefaults {
  11. subscript(key: DefaultsKey<O2BindUnitModel?>) -> O2BindUnitModel? {
  12. get { return unarchive(key) }
  13. set { archive(key, newValue) }
  14. }
  15. subscript(key: DefaultsKey<O2CenterServerModel?>) -> O2CenterServerModel? {
  16. get { return unarchive(key) }
  17. set { archive(key, newValue) }
  18. }
  19. subscript(key: DefaultsKey<O2LoginAccountModel?>) -> O2LoginAccountModel? {
  20. get { return unarchive(key) }
  21. set { archive(key, newValue) }
  22. }
  23. subscript(key: DefaultsKey<O2CustomStyleModel?>) -> O2CustomStyleModel? {
  24. get { return unarchive(key) }
  25. set { archive(key, newValue) }
  26. }
  27. subscript(key: DefaultsKey<O2BindDeviceModel?>) -> O2BindDeviceModel? {
  28. get { return unarchive(key) }
  29. set { archive(key, newValue) }
  30. }
  31. }
  32. // MARK:- 扩展定义的键
  33. extension DefaultsKeys {
  34. static let unit = DefaultsKey<O2BindUnitModel?>("O2unit")
  35. static let device = DefaultsKey<O2BindDeviceModel?>("O2device")
  36. static let deviceToken = DefaultsKey<String?>("deviceToken")
  37. static let centerServer = DefaultsKey<O2CenterServerModel?>("O2centerServer")
  38. static let myInfo = DefaultsKey<O2LoginAccountModel?>("O2myInfo")
  39. static let customStyle = DefaultsKey<O2CustomStyleModel?>("O2customStyle")
  40. static let customStyleHash = DefaultsKey<String?>("O2customStyleHash")
  41. }
  42. class O2UserDefaults {
  43. static let shared: O2UserDefaults = {
  44. return O2UserDefaults()
  45. }()
  46. private init() {}
  47. var myInfo: O2LoginAccountModel? {
  48. get {
  49. guard let me = Defaults[.myInfo] else {
  50. return nil
  51. }
  52. return me
  53. }
  54. set {
  55. Defaults[.myInfo] = newValue
  56. }
  57. }
  58. var unit: O2BindUnitModel? {
  59. get {
  60. guard let unit = Defaults[.unit] else {
  61. return nil
  62. }
  63. return unit
  64. }
  65. set {
  66. Defaults[.unit] = newValue
  67. }
  68. }
  69. var device: O2BindDeviceModel? {
  70. get {
  71. guard let device = Defaults[.device] else {
  72. return nil
  73. }
  74. return device
  75. }
  76. set {
  77. Defaults[.device] = newValue
  78. }
  79. }
  80. var deviceToken: String? {
  81. get {
  82. guard let token = Defaults[.deviceToken] else {
  83. return "deviceToken0x0x000000000xxxxxxxxxxxx"
  84. }
  85. return token
  86. }
  87. set {
  88. Defaults[.deviceToken] = newValue
  89. if let device = Defaults[.device] {
  90. device.name = newValue
  91. Defaults[.device] = device
  92. }
  93. }
  94. }
  95. var centerServer: O2CenterServerModel? {
  96. get {
  97. guard let server = Defaults[.centerServer] else {
  98. return nil
  99. }
  100. return server
  101. }
  102. set {
  103. Defaults[.centerServer] = newValue
  104. }
  105. }
  106. var customStyle: O2CustomStyleModel? {
  107. get {
  108. guard let style = Defaults[.customStyle] else {
  109. return nil
  110. }
  111. return style
  112. }
  113. set {
  114. Defaults[.customStyle] = newValue
  115. }
  116. }
  117. var customStyleHash: String? {
  118. get {
  119. guard let hash = Defaults[.customStyleHash] else {
  120. return ""
  121. }
  122. return hash
  123. }
  124. set {
  125. Defaults[.customStyleHash] = newValue
  126. }
  127. }
  128. }