2023-10-17 15:49:09 +08:00

150 lines
4.3 KiB
Dart

/*
* 持久话数据
* */
import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';
import '../login/login/entity/LoginData.dart';
import '../login/login/entity/LoginEntity.dart';
const saveBluePublicKey = "BluePublicKey";
const saveBluePrivateKey = "BluePrivateKey";
const saveBlueSignKey = "BlueSignKey";
const saveBlueToken = "BlueGetToken";
const currentConnectionLockId = "CurrentConnectionLockId";
const currentConnectionMacAddress = "CurrentConnectionMacAddress";
const ifIsDemoModeOrNot = "IfIsDemoModeOrNot";
class Storage {
Storage._internal();
factory Storage() => _instance;
static late final Storage _instance = Storage._internal();
static late SharedPreferences _preferences;
static Future<Storage> getInstance() async {
_preferences = await SharedPreferences.getInstance();
return _instance;
}
// ///存数据
// static Future<void> setData(key, value) async {
// SharedPreferences sp = await SharedPreferences.getInstance();
// if (value is int) {
// await sp.setInt(key, value);
// } else if (value is bool) {
// await sp.setBool(key, value);
// } else if (value is double) {
// await sp.setDouble(key, value);
// } else if (value is String) {
// await sp.setString(key, value);
// } else if (value is List<String>) {
// await sp.setStringList(key, value);
// }
// }
//
// ///取数据
// ///
// static Future<T?> getData<T>(key) async {
// SharedPreferences sp = await SharedPreferences.getInstance();
// switch(T){
// case int: return (sp.getInt(key) ?? 0) as T;
// case bool: return (sp.getBool(key) ?? false) as T;
// case double: return (sp.getDouble(key) ?? 0.0) as T;
// case String: return (sp.getString(key) ?? '') as T;
// case List: return (sp.getStringList(key) ?? []) as T;
// default: return null;
// }
// }
// int
static Future<void> setInt(key, value) async {
SharedPreferences sp = await SharedPreferences.getInstance();
sp.setInt(key, value);
}
static Future<int?> getInt(key) async {
SharedPreferences sp = await SharedPreferences.getInstance();
return sp.getInt(key);
}
// bool
static Future<void> setBool(key, value) async {
SharedPreferences sp = await SharedPreferences.getInstance();
sp.setBool(key, value);
}
static Future<bool?> getBool(key) async {
SharedPreferences sp = await SharedPreferences.getInstance();
return sp.getBool(key);
}
// double
static Future<void> setDouble(key, value) async {
SharedPreferences sp = await SharedPreferences.getInstance();
sp.setDouble(key, value);
}
static Future<double?> getDouble(key) async {
SharedPreferences sp = await SharedPreferences.getInstance();
return sp.getDouble(key);
}
// string
static Future<void> setString(key, value) async {
SharedPreferences sp = await SharedPreferences.getInstance();
sp.setString(key, value);
}
static Future<String?> getString(key) async {
SharedPreferences sp = await SharedPreferences.getInstance();
return sp.getString(key);
}
// 字符串数组
static Future<void> setStringList(key, value) async {
SharedPreferences sp = await SharedPreferences.getInstance();
sp.setStringList(key, value);
}
static Future<List<String>?> getStringList(key) async {
SharedPreferences sp = await SharedPreferences.getInstance();
return sp.getStringList(key);
}
// 判断本地是否包含某个key
static Future<bool> ifHaveKey(key) async {
SharedPreferences sp = await SharedPreferences.getInstance();
bool isContainKey = sp.containsKey(key) ?? false;
print(isContainKey);
return isContainKey;
}
// 移除数据
static Future<void> removeData(key) async {
SharedPreferences sp = await SharedPreferences.getInstance();
sp.remove(key);
}
// 移除所有的键值对
static Future<void> clearAll() async {
SharedPreferences sp = await SharedPreferences.getInstance();
sp.clear();
}
static Future<String?> getUid() async {
String? userId = '';
final data = await Storage.getString('userLoginData');
if (data != null && data.isNotEmpty) {
userId = LoginData.fromJson(jsonDecode(data)).userid.toString();
}
print("pubUserId:$userId");
return userId;
}
}