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
3.9 KiB

//
// 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
}
}