// // 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 = [] } }