KMProfileInfo.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 = "KMProfileUnarchiveLyncPDFKeyTemp1"
  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 let KMProfileSaveInfoHeadNameKey = "KMProfileSaveInfoHeadNameKey"
  27. private let KMProfileSaveInfoOrganizeNameKey = "KMProfileSaveInfoOrganizeNameKey"
  28. private let KMProfileSaveInfoOrganizePartKey = "KMProfileSaveInfoOrganizePartKey"
  29. private var _fullName: String = ""
  30. var fullName: String {
  31. get {
  32. return self._fullName
  33. }
  34. set {
  35. if (self._fullName != newValue) {
  36. self._fullName = newValue
  37. self._saveInfo()
  38. }
  39. }
  40. }
  41. private var _firstName: String = ""
  42. var firstName: String {
  43. get {
  44. return self._firstName
  45. }
  46. set {
  47. if (self._firstName != newValue) {
  48. self._firstName = newValue
  49. self._saveInfo()
  50. }
  51. }
  52. }
  53. private var _middleName: String = ""
  54. var middleName: String {
  55. get {
  56. return self._middleName
  57. }
  58. set {
  59. if (self._middleName != newValue) {
  60. self._middleName = newValue
  61. self._saveInfo()
  62. }
  63. }
  64. }
  65. private var _lastName: String = ""
  66. var lastName: String {
  67. get {
  68. return self._lastName
  69. }
  70. set {
  71. if (self._lastName != newValue) {
  72. self._lastName = newValue
  73. self._saveInfo()
  74. }
  75. }
  76. }
  77. private var _street1: String = ""
  78. var street1: String {
  79. get {
  80. return self._street1
  81. }
  82. set {
  83. if (self._street1 != newValue) {
  84. self._street1 = newValue
  85. self._saveInfo()
  86. }
  87. }
  88. }
  89. private var _street2: String = ""
  90. var street2: String {
  91. get {
  92. return self._street2
  93. }
  94. set {
  95. if (self._street2 != newValue) {
  96. self._street2 = newValue
  97. self._saveInfo()
  98. }
  99. }
  100. }
  101. private var _city: String = ""
  102. var city: String {
  103. get {
  104. return self._city
  105. }
  106. set {
  107. if (self._city != newValue) {
  108. self._city = newValue
  109. self._saveInfo()
  110. }
  111. }
  112. }
  113. private var _state: String = ""
  114. var state: String {
  115. get {
  116. return self._state
  117. }
  118. set {
  119. if (self._state != newValue) {
  120. self._state = newValue
  121. self._saveInfo()
  122. }
  123. }
  124. }
  125. private var _zip: String = ""
  126. var zip: String {
  127. get {
  128. return self._zip
  129. }
  130. set {
  131. if (self._zip != newValue) {
  132. self._zip = newValue
  133. self._saveInfo()
  134. }
  135. }
  136. }
  137. private var _country: String = ""
  138. var country: String {
  139. get {
  140. return self._country
  141. }
  142. set {
  143. if (self._country != newValue) {
  144. self._country = newValue
  145. self._saveInfo()
  146. }
  147. }
  148. }
  149. private var _email: String = ""
  150. var email: String {
  151. get {
  152. return self._email
  153. }
  154. set {
  155. if (self._email != newValue) {
  156. self._email = newValue
  157. self._saveInfo()
  158. }
  159. }
  160. }
  161. private var _tel: String = ""
  162. var tel: String {
  163. get {
  164. return self._tel
  165. }
  166. set {
  167. if (self._tel != newValue) {
  168. self._tel = newValue
  169. self._saveInfo()
  170. }
  171. }
  172. }
  173. private var _birthDate: String = ""
  174. var birthDate: String {
  175. get {
  176. return self._birthDate
  177. }
  178. set {
  179. if (self._birthDate != newValue) {
  180. self._birthDate = newValue
  181. self._saveInfo()
  182. }
  183. }
  184. }
  185. private var _customInfoArray: [Any] = []
  186. var customInfoArray: [Any] {
  187. get {
  188. return self._customInfoArray
  189. }
  190. set {
  191. if NSMutableArray(array: self.customInfoArray).isEqual(newValue) == false {
  192. self._customInfoArray = newValue
  193. self._saveInfo()
  194. }
  195. }
  196. }
  197. private var _headName: String = ""
  198. var headName: String {
  199. get {
  200. return self._headName
  201. }
  202. set {
  203. if (self._headName != newValue) {
  204. self._headName = newValue
  205. self._saveInfo()
  206. }
  207. }
  208. }
  209. private var _OrganizeName: String = ""
  210. var OrganizeName: String {
  211. get {
  212. return self._OrganizeName
  213. }
  214. set {
  215. if (self._OrganizeName != newValue) {
  216. self._OrganizeName = newValue
  217. self._saveInfo()
  218. }
  219. }
  220. }
  221. private var _OrganizePart: String = ""
  222. var OrganizePart: String {
  223. get {
  224. return self._OrganizePart
  225. }
  226. set {
  227. if (self._OrganizePart != newValue) {
  228. self._OrganizePart = newValue
  229. self._saveInfo()
  230. }
  231. }
  232. }
  233. @objc (sharedInstance)
  234. static func shared() -> KMProfileInfo {
  235. if let instanch = _shared {
  236. return instanch
  237. }
  238. let defaults = UserDefaults.standard
  239. if let encodedObject = defaults.object(forKey: KMProfileUnarchiveNewKey) as? Data {
  240. let instance = NSKeyedUnarchiver.unarchiveObject(with: encodedObject)
  241. _shared = (instance as! KMProfileInfo)
  242. } else if let encodedObject = defaults.object(forKey: KMProfileUnarchiveKey) as? Data {
  243. NSKeyedUnarchiver.setClass(Self.self, forClassName: "KMProfileInfo")
  244. let instance = NSKeyedUnarchiver.unarchiveObject(with: encodedObject)
  245. _shared = (instance as! KMProfileInfo)
  246. } else {
  247. let instance = KMProfileInfo()
  248. instance._setBaseInfo()
  249. instance._saveInfo()
  250. _shared = instance
  251. }
  252. if let didSetFullName = defaults.object(forKey: "didSetFullNameKey") {
  253. } else {
  254. if _shared?._fullName.isEmpty == true {
  255. _shared?.fullName = NSFullUserName()
  256. defaults.set("didSet", forKey: "didSetFullNameKey")
  257. }
  258. }
  259. return _shared!
  260. }
  261. override init() {
  262. super.init()
  263. }
  264. required init?(coder: NSCoder) {
  265. super.init()
  266. self._fullName = coder.decodeObject(forKey: KMProfileSaveInfoFullNameKey) as? String ?? ""
  267. self._firstName = coder.decodeObject(forKey: KMProfileSaveInfoFirstNameKey) as? String ?? ""
  268. self._middleName = coder.decodeObject(forKey: KMProfileSaveInfoMiddleNameKey) as? String ?? ""
  269. self._lastName = coder.decodeObject(forKey: KMProfileSaveInfoLastNameKey) as? String ?? ""
  270. self._street1 = coder.decodeObject(forKey: KMProfileSaveInfoStreet1Key) as? String ?? ""
  271. self._street2 = coder.decodeObject(forKey: KMProfileSaveInfoStreet2Key) as? String ?? ""
  272. self._city = coder.decodeObject(forKey: KMProfileSaveInfoCityKey) as? String ?? ""
  273. self._state = coder.decodeObject(forKey: KMProfileSaveInfoStateKey) as? String ?? ""
  274. self._zip = coder.decodeObject(forKey: KMProfileSaveInfoZipKey) as? String ?? ""
  275. self._country = coder.decodeObject(forKey: KMProfileSaveInfoCountryKey) as? String ?? ""
  276. self._email = coder.decodeObject(forKey: KMProfileSaveInfoEmailKey) as? String ?? ""
  277. self._tel = coder.decodeObject(forKey: KMProfileSaveInfoTelKey) as? String ?? ""
  278. self._birthDate = coder.decodeObject(forKey: KMProfileSaveInfoBirthDateKey) as? String ?? ""
  279. self._headName = coder.decodeObject(forKey: KMProfileSaveInfoHeadNameKey) as? String ?? ""
  280. self._OrganizeName = coder.decodeObject(forKey: KMProfileSaveInfoOrganizeNameKey) as? String ?? ""
  281. self._OrganizePart = coder.decodeObject(forKey: KMProfileSaveInfoOrganizePartKey) as? String ?? ""
  282. self._customInfoArray = coder.decodeObject(forKey: KMProfileSaveInfoCustomInfoKey) as? [String] ?? []
  283. }
  284. func encode(with coder: NSCoder) {
  285. coder.encode(self.fullName, forKey: KMProfileSaveInfoFullNameKey)
  286. coder.encode(self.firstName, forKey: KMProfileSaveInfoFirstNameKey)
  287. coder.encode(self.middleName, forKey: KMProfileSaveInfoMiddleNameKey)
  288. coder.encode(self.lastName, forKey: KMProfileSaveInfoLastNameKey)
  289. coder.encode(self.street1, forKey: KMProfileSaveInfoStreet1Key)
  290. coder.encode(self.street2, forKey: KMProfileSaveInfoStreet2Key)
  291. coder.encode(self.city, forKey: KMProfileSaveInfoCityKey)
  292. coder.encode(self.state, forKey: KMProfileSaveInfoStateKey)
  293. coder.encode(self.zip, forKey: KMProfileSaveInfoZipKey)
  294. coder.encode(self.country, forKey: KMProfileSaveInfoCountryKey)
  295. coder.encode(self.email, forKey: KMProfileSaveInfoEmailKey)
  296. coder.encode(self.tel, forKey: KMProfileSaveInfoTelKey)
  297. coder.encode(self.birthDate, forKey: KMProfileSaveInfoBirthDateKey)
  298. coder.encode(self.customInfoArray, forKey: KMProfileSaveInfoCustomInfoKey)
  299. coder.encode(self.headName, forKey: KMProfileSaveInfoHeadNameKey)
  300. coder.encode(self.OrganizeName, forKey: KMProfileSaveInfoOrganizeNameKey)
  301. coder.encode(self.OrganizePart, forKey: KMProfileSaveInfoOrganizePartKey)
  302. }
  303. func replaceObjectInCustomInfoArray(at index: Int, with object: Any) {
  304. self._customInfoArray[index] = object
  305. self._saveInfo()
  306. }
  307. func insertObject(_ object: Any, inCustomInfoArrayAt index: Int) {
  308. self._customInfoArray.insert(object, at: index)
  309. self._saveInfo()
  310. }
  311. func removeObjectFromCustomInfoArray(at index: Int) {
  312. self.customInfoArray.remove(at: index)
  313. self._saveInfo()
  314. }
  315. /*
  316. - (void):(NSUInteger)index {
  317. }
  318. #pragma mark - NSCoding
  319. - (NSString *)description {
  320. 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];
  321. }
  322. */
  323. }
  324. // MARK: - Private Methods
  325. extension KMProfileInfo {
  326. private func _saveInfo() {
  327. let encodedObject = NSKeyedArchiver.archivedData(withRootObject: self)
  328. let defaults = UserDefaults.standard
  329. defaults.set(encodedObject, forKey: KMProfileInfo.KMProfileUnarchiveNewKey)
  330. defaults.synchronize()
  331. }
  332. private func _setBaseInfo() {
  333. self._fullName = ""
  334. self._firstName = ""
  335. self._middleName = ""
  336. self._lastName = ""
  337. self._street1 = ""
  338. self._street2 = ""
  339. self._city = ""
  340. self._state = ""
  341. self._zip = ""
  342. self._country = ""
  343. self._email = ""
  344. self._tel = ""
  345. self._headName = ""
  346. self._OrganizeName = ""
  347. self._OrganizePart = ""
  348. self._customInfoArray = []
  349. }
  350. }