// // UserDefaultsManager.swift // Lookfit // // Created by lemo. on 2020/3/15. // Copyright © 2020 Sheldon. All rights reserved. // import Foundation struct UserDefaultsKey { /// 用户信息 static let kUserInfo = "Userinfo" /// 设备信息 static let kDeviceInfo = "DeviceInfo" /// 设备设置信息 static let kDeviceSet = "DeviceSet" /// 实时计步数据 static let kRealTimeStep = "RealTimeStep" /// 实时心率数据 static let kRealTimeHeartRate = "RealTimeHeartRate" /// 实时体温数据 static let kRealTimeTemperature = "kRealTimeTemperature" /// 实时血压数据 static let kRealTimeBloodPressure = "kRealTimeBloodPressure" /// 实时血氧数据 static let kRealTimeBloodOxygen = "kRealTimeBloodOxygen" } @objcMembers class UserDefaultsManager: NSObject { static let kUserDefualt = UserDefaults.standard static func saveObjectsToSandBox(_ dict: [String: Any]) { let userDefault = UserDefaults.standard for (key, value) in dict { userDefault.set(value, forKey: key) } userDefault.synchronize() } static func saveObjectsToSandBox(key: String, value: Any) { let userDefault = UserDefaults.standard userDefault.set(value, forKey: key) userDefault.synchronize() } static func objectFromSandBox(_ key: String) -> Any? { let userDefault = UserDefaults.standard return userDefault.object(forKey: key) } static func clearSandBoxForKey(_ key: String) { let userDefault = UserDefaults.standard userDefault.removeObject(forKey: key) } } extension UserDefaultsManager { static func getUserInfo() -> UserInfo? { if let temp = objectFromSandBox(UserDefaultsKey.kUserInfo), let json = temp as? [String: Any] { let userInfo = UserInfo(JSON: json) return userInfo } return nil } /// 保存用户信息 /// - Parameter userInfo: 用户信息 static func saveUserInfo(userInfo: UserInfo) { let json = userInfo.toJSON() saveObjectsToSandBox([UserDefaultsKey.kUserInfo: json]) } static func deleteUserInfo() { clearSandBoxForKey(UserDefaultsKey.kUserInfo) } /// 清空所有实时缓存数据 static func deleteRealTimeData() { clearSandBoxForKey(UserDefaultsKey.kRealTimeStep) clearSandBoxForKey(UserDefaultsKey.kRealTimeHeartRate) clearSandBoxForKey(UserDefaultsKey.kRealTimeTemperature) clearSandBoxForKey(UserDefaultsKey.kRealTimeBloodPressure) clearSandBoxForKey(UserDefaultsKey.kRealTimeBloodOxygen) } } extension UserDefaultsManager { static func getDeviceInfo() -> DeviceInfo? { if let temp = objectFromSandBox(UserDefaultsKey.kDeviceInfo), let json = temp as? [String: Any] { let deviceInfo = DeviceInfo(JSON: json) return deviceInfo } return nil } static func saveDeviceInfo(deviceInfo: DeviceInfo) { var deviceInfo = deviceInfo // 保存判断适配是否需要更新 if let temp = objectFromSandBox(UserDefaultsKey.kDeviceInfo), let json = temp as? [String: Any], let oldDeviceInfo = DeviceInfo(JSON: json) { if oldDeviceInfo.adapterNumber == deviceInfo.adapterNumber, deviceInfo.adapterInfo == nil, oldDeviceInfo.adapterInfo != nil { deviceInfo.adapterInfo = oldDeviceInfo.adapterInfo } } let json = deviceInfo.toJSON() saveObjectsToSandBox([UserDefaultsKey.kDeviceInfo: json]) } static func deleteDeviceInfo() { clearSandBoxForKey(UserDefaultsKey.kDeviceInfo) // clearSandBoxForKey(UserDefaultsKey.kDeviceSet) } /// 获取本地设备Mac地址 /// - Returns: mac地址 @objc static func getLocalDeviceMacID() -> String? { return getDeviceInfo()?.MACString } }