123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- //
- // KMProfileInfo.swift
- // PDF Reader Pro
- //
- // Created by tangchao on 2023/11/9.
- //
- import Cocoa
- private var _shared: KMProfileInfo?
- @objcMembers class KMProfileInfo: NSObject, NSCoding {
- private static let KMProfileUnarchiveKey = "KMProfileUnarchiveKeyTemp1"
-
- private static let KMProfileUnarchiveNewKey = "KMProfileUnarchiveNewKeyTemp1"
-
- private let KMProfileSaveInfoFullNameKey = "KMProfileSaveInfoFullNameKey"
- private let KMProfileSaveInfoFirstNameKey = "KMProfileSaveInfoFirstNameKey"
- private let KMProfileSaveInfoMiddleNameKey = "KMProfileSaveInfoMiddleNameKey"
- private let KMProfileSaveInfoLastNameKey = "KMProfileSaveInfoLastNameKey"
- private let KMProfileSaveInfoStreet1Key = "KMProfileSaveInfoStreet1Key"
- private let KMProfileSaveInfoStreet2Key = "KMProfileSaveInfoStreet2Key"
- private let KMProfileSaveInfoCityKey = "KMProfileSaveInfoCityKey"
- private let KMProfileSaveInfoStateKey = "KMProfileSaveInfoStateKey"
- private let KMProfileSaveInfoZipKey = "KMProfileSaveInfoZipKey"
- private let KMProfileSaveInfoCountryKey = "KMProfileSaveInfoCountryKey"
- private let KMProfileSaveInfoEmailKey = "KMProfileSaveInfoEmailKey"
- private let KMProfileSaveInfoTelKey = "KMProfileSaveInfoTelKey"
- private let KMProfileSaveInfoBirthDateKey = "KMProfileSaveInfoBirthDateKey"
- private let KMProfileSaveInfoCustomInfoKey = "KMProfileSaveInfoCustomInfoKey"
-
- private var _fullName: String = ""
- var fullName: String {
- get {
- return self._fullName
- }
- set {
- if (self._fullName != newValue) {
- self._fullName = newValue
-
- self._saveInfo()
- }
- }
- }
-
- private var _firstName: String = ""
- var firstName: String {
- get {
- return self._firstName
- }
- set {
- if (self._firstName != newValue) {
- self._firstName = newValue
-
- self._saveInfo()
- }
- }
- }
-
- private var _middleName: String = ""
- var middleName: String {
- get {
- return self._middleName
- }
- set {
- if (self._middleName != newValue) {
- self._middleName = newValue
-
- self._saveInfo()
- }
- }
- }
- private var _lastName: String = ""
- var lastName: String {
- get {
- return self._lastName
- }
- set {
- if (self._lastName != newValue) {
- self._lastName = newValue
-
- self._saveInfo()
- }
- }
- }
-
- private var _street1: String = ""
- var street1: String {
- get {
- return self._street1
- }
- set {
- if (self._street1 != newValue) {
- self._street1 = newValue
-
- self._saveInfo()
- }
- }
- }
-
- private var _street2: String = ""
- var street2: String {
- get {
- return self._street2
- }
- set {
- if (self._street2 != newValue) {
- self._street2 = newValue
-
- self._saveInfo()
- }
- }
- }
-
- private var _city: String = ""
- var city: String {
- get {
- return self._city
- }
- set {
- if (self._city != newValue) {
- self._city = newValue
-
- self._saveInfo()
- }
- }
- }
-
- private var _state: String = ""
- var state: String {
- get {
- return self._state
- }
- set {
- if (self._state != newValue) {
- self._state = newValue
-
- self._saveInfo()
- }
- }
- }
-
- private var _zip: String = ""
- var zip: String {
- get {
- return self._zip
- }
- set {
- if (self._zip != newValue) {
- self._zip = newValue
-
- self._saveInfo()
- }
- }
- }
-
- private var _country: String = ""
- var country: String {
- get {
- return self._country
- }
- set {
- if (self._country != newValue) {
- self._country = newValue
-
- self._saveInfo()
- }
- }
- }
-
- private var _email: String = ""
- var email: String {
- get {
- return self._email
- }
- set {
- if (self._email != newValue) {
- self._email = newValue
-
- self._saveInfo()
- }
- }
- }
- private var _tel: String = ""
- var tel: String {
- get {
- return self._tel
- }
- set {
- if (self._tel != newValue) {
- self._tel = newValue
-
- self._saveInfo()
- }
- }
- }
-
- private var _birthDate: String = ""
- var birthDate: String {
- get {
- return self._birthDate
- }
- set {
- if (self._birthDate != newValue) {
- self._birthDate = newValue
-
- self._saveInfo()
- }
- }
- }
-
- private var _customInfoArray: [Any] = []
- var customInfoArray: [Any] {
- get {
- return self._customInfoArray
- }
- set {
- if NSMutableArray(array: self.customInfoArray).isEqual(newValue) == false {
- self._customInfoArray = newValue
-
- self._saveInfo()
- }
- }
- }
-
- @objc (sharedInstance)
- static func shared() -> KMProfileInfo {
- if let instanch = _shared {
- return instanch
- }
-
- let defaults = UserDefaults.standard
- if let encodedObject = defaults.object(forKey: KMProfileUnarchiveNewKey) as? Data {
- let instance = NSKeyedUnarchiver.unarchiveObject(with: encodedObject)
- _shared = (instance as! KMProfileInfo)
- } else if let encodedObject = defaults.object(forKey: KMProfileUnarchiveKey) as? Data {
- NSKeyedUnarchiver.setClass(Self.self, forClassName: "KMProfileInfo")
- let instance = NSKeyedUnarchiver.unarchiveObject(with: encodedObject)
- _shared = (instance as! KMProfileInfo)
- } else {
- let instance = KMProfileInfo()
- instance._setBaseInfo()
- instance._saveInfo()
- _shared = instance
- }
-
- return _shared!
- }
-
- override init() {
- super.init()
- }
-
- required init?(coder: NSCoder) {
- super.init()
-
- self._fullName = coder.decodeObject(forKey: KMProfileSaveInfoFullNameKey) as? String ?? ""
- self._firstName = coder.decodeObject(forKey: KMProfileSaveInfoFirstNameKey) as? String ?? ""
- self._middleName = coder.decodeObject(forKey: KMProfileSaveInfoMiddleNameKey) as? String ?? ""
- self._lastName = coder.decodeObject(forKey: KMProfileSaveInfoLastNameKey) as? String ?? ""
-
- self._street1 = coder.decodeObject(forKey: KMProfileSaveInfoStreet1Key) as? String ?? ""
- self._street2 = coder.decodeObject(forKey: KMProfileSaveInfoStreet2Key) as? String ?? ""
- self._city = coder.decodeObject(forKey: KMProfileSaveInfoCityKey) as? String ?? ""
- self._state = coder.decodeObject(forKey: KMProfileSaveInfoStateKey) as? String ?? ""
- self._zip = coder.decodeObject(forKey: KMProfileSaveInfoZipKey) as? String ?? ""
- self._country = coder.decodeObject(forKey: KMProfileSaveInfoCountryKey) as? String ?? ""
-
- self._email = coder.decodeObject(forKey: KMProfileSaveInfoEmailKey) as? String ?? ""
-
- self._tel = coder.decodeObject(forKey: KMProfileSaveInfoTelKey) as? String ?? ""
-
- self._birthDate = coder.decodeObject(forKey: KMProfileSaveInfoBirthDateKey) as? String ?? ""
-
- self._customInfoArray = coder.decodeObject(forKey: KMProfileSaveInfoCustomInfoKey) as? [String] ?? []
- }
-
- func encode(with coder: NSCoder) {
- coder.encode(self.fullName, forKey: KMProfileSaveInfoFullNameKey)
- coder.encode(self.firstName, forKey: KMProfileSaveInfoFirstNameKey)
- coder.encode(self.middleName, forKey: KMProfileSaveInfoMiddleNameKey)
- coder.encode(self.lastName, forKey: KMProfileSaveInfoLastNameKey)
-
- coder.encode(self.street1, forKey: KMProfileSaveInfoStreet1Key)
- coder.encode(self.street2, forKey: KMProfileSaveInfoStreet2Key)
- coder.encode(self.city, forKey: KMProfileSaveInfoCityKey)
- coder.encode(self.state, forKey: KMProfileSaveInfoStateKey)
- coder.encode(self.zip, forKey: KMProfileSaveInfoZipKey)
- coder.encode(self.country, forKey: KMProfileSaveInfoCountryKey)
-
- coder.encode(self.email, forKey: KMProfileSaveInfoEmailKey)
- coder.encode(self.tel, forKey: KMProfileSaveInfoTelKey)
-
- coder.encode(self.birthDate, forKey: KMProfileSaveInfoBirthDateKey)
- coder.encode(self.customInfoArray, forKey: KMProfileSaveInfoCustomInfoKey)
- }
-
- func replaceObjectInCustomInfoArray(at index: Int, with object: Any) {
- self._customInfoArray[index] = object
- self._saveInfo()
- }
-
-
- func insertObject(_ object: Any, inCustomInfoArrayAt index: Int) {
- self._customInfoArray.insert(object, at: index)
- self._saveInfo()
- }
-
- func removeObjectFromCustomInfoArray(at index: Int) {
- self.customInfoArray.remove(at: index)
- self._saveInfo()
- }
- /*
- - (void):(NSUInteger)index {
- }
- #pragma mark - NSCoding
-
- - (NSString *)description {
- 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];
- }
- */
- }
- // MARK: - Private Methods
- extension KMProfileInfo {
- private func _saveInfo() {
- let encodedObject = NSKeyedArchiver.archivedData(withRootObject: self)
- let defaults = UserDefaults.standard
- defaults.set(encodedObject, forKey: KMProfileInfo.KMProfileUnarchiveNewKey)
- defaults.synchronize()
- }
-
- private func _setBaseInfo() {
- self._fullName = ""
- self._firstName = ""
- self._middleName = ""
- self._lastName = ""
-
- self._street1 = ""
- self._street2 = ""
- self._city = ""
- self._state = ""
- self._zip = ""
- self._country = ""
-
- self._email = ""
- self._tel = ""
-
- self._customInfoArray = []
- }
- }
|