O2UserDefaults.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. subscript(key: DefaultsKey<IMConfig?>) -> IMConfig? {
  32. get { return unarchive(key) }
  33. set { archive(key, newValue) }
  34. }
  35. subscript(key: DefaultsKey<OOMeetingConfigInfo?>) -> OOMeetingConfigInfo? {
  36. get { return unarchive(key) }
  37. set { archive(key, newValue) }
  38. }
  39. }
  40. // MARK:- 扩展定义的键
  41. extension DefaultsKeys {
  42. static let unit = DefaultsKey<O2BindUnitModel?>("O2unit")
  43. static let device = DefaultsKey<O2BindDeviceModel?>("O2device")
  44. static let deviceToken = DefaultsKey<String?>("deviceToken")
  45. static let apnsToken = DefaultsKey<String?>("apnsToken")
  46. static let pushType = DefaultsKey<String?>("pushType")
  47. static let centerServer = DefaultsKey<O2CenterServerModel?>("O2centerServer")
  48. static let myInfo = DefaultsKey<O2LoginAccountModel?>("O2myInfo")
  49. static let customStyle = DefaultsKey<O2CustomStyleModel?>("O2customStyle")
  50. static let customStyleHash = DefaultsKey<String?>("O2customStyleHash")
  51. static let searchHistory = DefaultsKey<[String]?>("O2SearchHistoryKey")
  52. static let imConfig = DefaultsKey<IMConfig?>("imConfig")
  53. static let meetingConfig = DefaultsKey<OOMeetingConfigInfo?>("meetingConfig")
  54. }
  55. class O2UserDefaults {
  56. static let shared: O2UserDefaults = {
  57. return O2UserDefaults()
  58. }()
  59. private init() {}
  60. var myInfo: O2LoginAccountModel? {
  61. get {
  62. guard let me = Defaults[.myInfo] else {
  63. return nil
  64. }
  65. return me
  66. }
  67. set {
  68. Defaults[.myInfo] = newValue
  69. }
  70. }
  71. var unit: O2BindUnitModel? {
  72. get {
  73. guard let unit = Defaults[.unit] else {
  74. return nil
  75. }
  76. return unit
  77. }
  78. set {
  79. Defaults[.unit] = newValue
  80. }
  81. }
  82. var device: O2BindDeviceModel? {
  83. get {
  84. guard let device = Defaults[.device] else {
  85. return nil
  86. }
  87. return device
  88. }
  89. set {
  90. Defaults[.device] = newValue
  91. }
  92. }
  93. var deviceToken: String? {
  94. get {
  95. guard let token = Defaults[.deviceToken] else {
  96. return "deviceToken0x0x000000000xxxxxxxxxxxx"
  97. }
  98. return token
  99. }
  100. set {
  101. Defaults[.deviceToken] = newValue
  102. if let device = Defaults[.device] {
  103. device.name = newValue
  104. Defaults[.device] = device
  105. }
  106. }
  107. }
  108. var pushType: String? {
  109. get {
  110. guard let token = Defaults[.pushType] else {
  111. return ""
  112. }
  113. return token
  114. }
  115. set {
  116. Defaults[.pushType] = newValue
  117. }
  118. }
  119. var apnsToken: String? {
  120. get {
  121. guard let token = Defaults[.apnsToken] else {
  122. return ""
  123. }
  124. return token
  125. }
  126. set {
  127. Defaults[.apnsToken] = newValue
  128. }
  129. }
  130. var centerServer: O2CenterServerModel? {
  131. get {
  132. guard let server = Defaults[.centerServer] else {
  133. return nil
  134. }
  135. return server
  136. }
  137. set {
  138. Defaults[.centerServer] = newValue
  139. }
  140. }
  141. var customStyle: O2CustomStyleModel? {
  142. get {
  143. guard let style = Defaults[.customStyle] else {
  144. return nil
  145. }
  146. return style
  147. }
  148. set {
  149. Defaults[.customStyle] = newValue
  150. }
  151. }
  152. var customStyleHash: String? {
  153. get {
  154. guard let hash = Defaults[.customStyleHash] else {
  155. return ""
  156. }
  157. return hash
  158. }
  159. set {
  160. Defaults[.customStyleHash] = newValue
  161. }
  162. }
  163. var searchHistory: [String] {
  164. get {
  165. guard let history = Defaults[.searchHistory] else {
  166. return []
  167. }
  168. return history
  169. }
  170. set {
  171. Defaults[.searchHistory] = newValue
  172. }
  173. }
  174. var imConfig: IMConfig? {
  175. get {
  176. guard let config = Defaults[.imConfig] else {
  177. return IMConfig()
  178. }
  179. return config
  180. }
  181. set {
  182. Defaults[.imConfig] = newValue
  183. }
  184. }
  185. var meetingConfig: OOMeetingConfigInfo? {
  186. get {
  187. guard let config = Defaults[.meetingConfig] else {
  188. return OOMeetingConfigInfo()
  189. }
  190. return config
  191. }
  192. set {
  193. Defaults[.meetingConfig] = newValue
  194. }
  195. }
  196. }