You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
124 lines
4.0 KiB
124 lines
4.0 KiB
// |
|
// UserDefaultsManagerFrieBoltt.swift |
|
// FireBoltt |
|
// |
|
// Created by lemo. on 2020/3/15. |
|
// Copyright © 2020 Sheldon. All rights reserved. |
|
// |
|
|
|
import Foundation |
|
|
|
struct UserDefaultsKey { |
|
/// 用户信息 |
|
static let kfUserInfo = "fUserinfo" |
|
/// 设备信息 |
|
static let kfDeviceInfo = "fDeviceInfo" |
|
/// 设备设置信息 |
|
static let kfDeviceSet = "fDeviceSet" |
|
/// 实时计步数据 |
|
static let kfRealTimeStep = "fRealTimeStep" |
|
/// 实时心率数据 |
|
static let kfRealTimeHeartRate = "fRealTimeHeartRate" |
|
/// 实时体温数据 |
|
static let kfRealTimeTemperature = "kfRealTimeTemperature" |
|
/// 实时血压数据 |
|
static let kfRealTimeBloodPressure = "kfRealTimeBloodPressure" |
|
/// 实时血氧数据 |
|
static let kfRealTimeBloodOxygen = "kfRealTimeBloodOxygen" |
|
} |
|
|
|
@objcMembers class UserDefaultsManagerFrieBoltt: 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 UserDefaultsManagerFrieBoltt { |
|
|
|
static func getUserInfo() -> UserInfo? { |
|
if let temp = objectFromSandBox(UserDefaultsKey.kfUserInfo), 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.kfUserInfo: json]) |
|
} |
|
|
|
static func deleteUserInfo() { |
|
clearSandBoxForKey(UserDefaultsKey.kfUserInfo) |
|
} |
|
|
|
/// 清空所有实时缓存数据 |
|
static func deleteRealTimeData() { |
|
clearSandBoxForKey(UserDefaultsKey.kfRealTimeStep) |
|
clearSandBoxForKey(UserDefaultsKey.kfRealTimeHeartRate) |
|
clearSandBoxForKey(UserDefaultsKey.kfRealTimeTemperature) |
|
clearSandBoxForKey(UserDefaultsKey.kfRealTimeBloodPressure) |
|
clearSandBoxForKey(UserDefaultsKey.kfRealTimeBloodOxygen) |
|
} |
|
} |
|
|
|
extension UserDefaultsManagerFrieBoltt { |
|
|
|
static func getDeviceInfo() -> DeviceInfo? { |
|
if let temp = objectFromSandBox(UserDefaultsKey.kfDeviceInfo), 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.kfDeviceInfo), 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.kfDeviceInfo: json]) |
|
} |
|
|
|
static func deleteDeviceInfo() { |
|
clearSandBoxForKey(UserDefaultsKey.kfDeviceInfo) |
|
// clearSandBoxForKey(UserDefaultsKey.kDeviceSet) |
|
} |
|
|
|
/// 获取本地设备Mac地址 |
|
/// - Returns: mac地址 |
|
@objc static func getLocalDeviceMacID() -> String? { |
|
return getDeviceInfo()?.MACString |
|
} |
|
|
|
}
|
|
|