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

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