KMProfileInfo.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. //
  2. // KMProfileInfo.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/11/9.
  6. //
  7. import Cocoa
  8. private var _shared: KMProfileInfo?
  9. @objcMembers class KMProfileInfo: NSObject, NSCoding {
  10. private static let KMProfileUnarchiveKey = "KMProfileUnarchiveKeyTemp1"
  11. private static let KMProfileUnarchiveNewKey = "KMProfileUnarchiveNewKeyTemp1"
  12. private let KMProfileSaveInfoFullNameKey = "KMProfileSaveInfoFullNameKey"
  13. private let KMProfileSaveInfoFirstNameKey = "KMProfileSaveInfoFirstNameKey"
  14. private let KMProfileSaveInfoMiddleNameKey = "KMProfileSaveInfoMiddleNameKey"
  15. private let KMProfileSaveInfoLastNameKey = "KMProfileSaveInfoLastNameKey"
  16. private let KMProfileSaveInfoStreet1Key = "KMProfileSaveInfoStreet1Key"
  17. private let KMProfileSaveInfoStreet2Key = "KMProfileSaveInfoStreet2Key"
  18. private let KMProfileSaveInfoCityKey = "KMProfileSaveInfoCityKey"
  19. private let KMProfileSaveInfoStateKey = "KMProfileSaveInfoStateKey"
  20. private let KMProfileSaveInfoZipKey = "KMProfileSaveInfoZipKey"
  21. private let KMProfileSaveInfoCountryKey = "KMProfileSaveInfoCountryKey"
  22. private let KMProfileSaveInfoEmailKey = "KMProfileSaveInfoEmailKey"
  23. private let KMProfileSaveInfoTelKey = "KMProfileSaveInfoTelKey"
  24. private let KMProfileSaveInfoBirthDateKey = "KMProfileSaveInfoBirthDateKey"
  25. private let KMProfileSaveInfoCustomInfoKey = "KMProfileSaveInfoCustomInfoKey"
  26. private var _fullName: String = ""
  27. var fullName: String {
  28. get {
  29. return self._fullName
  30. }
  31. set {
  32. if (self._fullName != newValue) {
  33. self._fullName = newValue
  34. self._saveInfo()
  35. }
  36. }
  37. }
  38. private var _firstName: String = ""
  39. var firstName: String {
  40. get {
  41. return self._firstName
  42. }
  43. set {
  44. if (self._firstName != newValue) {
  45. self._firstName = newValue
  46. self._saveInfo()
  47. }
  48. }
  49. }
  50. private var _middleName: String = ""
  51. var middleName: String {
  52. get {
  53. return self._middleName
  54. }
  55. set {
  56. if (self._middleName != newValue) {
  57. self._middleName = newValue
  58. self._saveInfo()
  59. }
  60. }
  61. }
  62. private var _lastName: String = ""
  63. var lastName: String {
  64. get {
  65. return self._lastName
  66. }
  67. set {
  68. if (self._lastName != newValue) {
  69. self._lastName = newValue
  70. self._saveInfo()
  71. }
  72. }
  73. }
  74. private var _street1: String = ""
  75. var street1: String {
  76. get {
  77. return self._street1
  78. }
  79. set {
  80. if (self._street1 != newValue) {
  81. self._street1 = newValue
  82. self._saveInfo()
  83. }
  84. }
  85. }
  86. private var _street2: String = ""
  87. var street2: String {
  88. get {
  89. return self._street2
  90. }
  91. set {
  92. if (self._street2 != newValue) {
  93. self._street2 = newValue
  94. self._saveInfo()
  95. }
  96. }
  97. }
  98. private var _city: String = ""
  99. var city: String {
  100. get {
  101. return self._city
  102. }
  103. set {
  104. if (self._city != newValue) {
  105. self._city = newValue
  106. self._saveInfo()
  107. }
  108. }
  109. }
  110. private var _state: String = ""
  111. var state: String {
  112. get {
  113. return self._state
  114. }
  115. set {
  116. if (self._state != newValue) {
  117. self._state = newValue
  118. self._saveInfo()
  119. }
  120. }
  121. }
  122. private var _zip: String = ""
  123. var zip: String {
  124. get {
  125. return self._zip
  126. }
  127. set {
  128. if (self._zip != newValue) {
  129. self._zip = newValue
  130. self._saveInfo()
  131. }
  132. }
  133. }
  134. private var _country: String = ""
  135. var country: String {
  136. get {
  137. return self._country
  138. }
  139. set {
  140. if (self._country != newValue) {
  141. self._country = newValue
  142. self._saveInfo()
  143. }
  144. }
  145. }
  146. private var _email: String = ""
  147. var email: String {
  148. get {
  149. return self._email
  150. }
  151. set {
  152. if (self._email != newValue) {
  153. self._email = newValue
  154. self._saveInfo()
  155. }
  156. }
  157. }
  158. private var _tel: String = ""
  159. var tel: String {
  160. get {
  161. return self._tel
  162. }
  163. set {
  164. if (self._tel != newValue) {
  165. self._tel = newValue
  166. self._saveInfo()
  167. }
  168. }
  169. }
  170. private var _birthDate: String = ""
  171. var birthDate: String {
  172. get {
  173. return self._birthDate
  174. }
  175. set {
  176. if (self._birthDate != newValue) {
  177. self._birthDate = newValue
  178. self._saveInfo()
  179. }
  180. }
  181. }
  182. private var _customInfoArray: [Any] = []
  183. var customInfoArray: [Any] {
  184. get {
  185. return self._customInfoArray
  186. }
  187. set {
  188. if NSMutableArray(array: self.customInfoArray).isEqual(newValue) == false {
  189. self._customInfoArray = newValue
  190. self._saveInfo()
  191. }
  192. }
  193. }
  194. @objc (sharedInstance)
  195. static func shared() -> KMProfileInfo {
  196. if let instanch = _shared {
  197. return instanch
  198. }
  199. let defaults = UserDefaults.standard
  200. if let encodedObject = defaults.object(forKey: KMProfileUnarchiveNewKey) as? Data {
  201. let instance = NSKeyedUnarchiver.unarchiveObject(with: encodedObject)
  202. _shared = (instance as! KMProfileInfo)
  203. } else if let encodedObject = defaults.object(forKey: KMProfileUnarchiveKey) as? Data {
  204. NSKeyedUnarchiver.setClass(Self.self, forClassName: "KMProfileInfo")
  205. let instance = NSKeyedUnarchiver.unarchiveObject(with: encodedObject)
  206. _shared = (instance as! KMProfileInfo)
  207. } else {
  208. let instance = KMProfileInfo()
  209. instance._setBaseInfo()
  210. instance._saveInfo()
  211. _shared = instance
  212. }
  213. return _shared!
  214. }
  215. override init() {
  216. super.init()
  217. }
  218. required init?(coder: NSCoder) {
  219. super.init()
  220. self._fullName = coder.decodeObject(forKey: KMProfileSaveInfoFullNameKey) as? String ?? ""
  221. self._firstName = coder.decodeObject(forKey: KMProfileSaveInfoFirstNameKey) as? String ?? ""
  222. self._middleName = coder.decodeObject(forKey: KMProfileSaveInfoMiddleNameKey) as? String ?? ""
  223. self._lastName = coder.decodeObject(forKey: KMProfileSaveInfoLastNameKey) as? String ?? ""
  224. self._street1 = coder.decodeObject(forKey: KMProfileSaveInfoStreet1Key) as? String ?? ""
  225. self._street2 = coder.decodeObject(forKey: KMProfileSaveInfoStreet2Key) as? String ?? ""
  226. self._city = coder.decodeObject(forKey: KMProfileSaveInfoCityKey) as? String ?? ""
  227. self._state = coder.decodeObject(forKey: KMProfileSaveInfoStateKey) as? String ?? ""
  228. self._zip = coder.decodeObject(forKey: KMProfileSaveInfoZipKey) as? String ?? ""
  229. self._country = coder.decodeObject(forKey: KMProfileSaveInfoCountryKey) as? String ?? ""
  230. self._email = coder.decodeObject(forKey: KMProfileSaveInfoEmailKey) as? String ?? ""
  231. self._tel = coder.decodeObject(forKey: KMProfileSaveInfoTelKey) as? String ?? ""
  232. self._birthDate = coder.decodeObject(forKey: KMProfileSaveInfoBirthDateKey) as? String ?? ""
  233. self._customInfoArray = coder.decodeObject(forKey: KMProfileSaveInfoCustomInfoKey) as? [String] ?? []
  234. }
  235. func encode(with coder: NSCoder) {
  236. coder.encode(self.fullName, forKey: KMProfileSaveInfoFullNameKey)
  237. coder.encode(self.firstName, forKey: KMProfileSaveInfoFirstNameKey)
  238. coder.encode(self.middleName, forKey: KMProfileSaveInfoMiddleNameKey)
  239. coder.encode(self.lastName, forKey: KMProfileSaveInfoLastNameKey)
  240. coder.encode(self.street1, forKey: KMProfileSaveInfoStreet1Key)
  241. coder.encode(self.street2, forKey: KMProfileSaveInfoStreet2Key)
  242. coder.encode(self.city, forKey: KMProfileSaveInfoCityKey)
  243. coder.encode(self.state, forKey: KMProfileSaveInfoStateKey)
  244. coder.encode(self.zip, forKey: KMProfileSaveInfoZipKey)
  245. coder.encode(self.country, forKey: KMProfileSaveInfoCountryKey)
  246. coder.encode(self.email, forKey: KMProfileSaveInfoEmailKey)
  247. coder.encode(self.tel, forKey: KMProfileSaveInfoTelKey)
  248. coder.encode(self.birthDate, forKey: KMProfileSaveInfoBirthDateKey)
  249. coder.encode(self.customInfoArray, forKey: KMProfileSaveInfoCustomInfoKey)
  250. }
  251. func replaceObjectInCustomInfoArray(at index: Int, with object: Any) {
  252. self._customInfoArray[index] = object
  253. self._saveInfo()
  254. }
  255. func insertObject(_ object: Any, inCustomInfoArrayAt index: Int) {
  256. self._customInfoArray.insert(object, at: index)
  257. self._saveInfo()
  258. }
  259. func removeObjectFromCustomInfoArray(at index: Int) {
  260. self.customInfoArray.remove(at: index)
  261. self._saveInfo()
  262. }
  263. /*
  264. - (void):(NSUInteger)index {
  265. }
  266. #pragma mark - NSCoding
  267. - (NSString *)description {
  268. return [NSString stringWithFormat:@"fullName:%@,firstName:%@,middleName:%@,lastName:%@,street1:%@,street2:%@,city:%@,zip:%@,country:%@,email:%@,tel:%@,birthdate:%@,customInfo:%@",self.fullName,self.firstName,self.middleName,self.lastName,self.street1,self.street2,self.city,self.zip,self.country,self.email,self.tel,self.birthDate,self.customInfoArray];
  269. }
  270. */
  271. }
  272. // MARK: - Private Methods
  273. extension KMProfileInfo {
  274. private func _saveInfo() {
  275. let encodedObject = NSKeyedArchiver.archivedData(withRootObject: self)
  276. let defaults = UserDefaults.standard
  277. defaults.set(encodedObject, forKey: KMProfileInfo.KMProfileUnarchiveNewKey)
  278. defaults.synchronize()
  279. }
  280. private func _setBaseInfo() {
  281. self._fullName = ""
  282. self._firstName = ""
  283. self._middleName = ""
  284. self._lastName = ""
  285. self._street1 = ""
  286. self._street2 = ""
  287. self._city = ""
  288. self._state = ""
  289. self._zip = ""
  290. self._country = ""
  291. self._email = ""
  292. self._tel = ""
  293. self._customInfoArray = []
  294. }
  295. }