222 lines
6.7 KiB
Dart
Executable File
222 lines
6.7 KiB
Dart
Executable File
/*
|
|
* 持久话数据
|
|
* */
|
|
import 'dart:convert';
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
import '../login/login/entity/LoginData.dart';
|
|
import '../main/lockMian/entity/lockListInfo_entity.dart';
|
|
|
|
const saveBluePublicKey = "BluePublicKey";
|
|
const saveBluePrivateKey = "BluePrivateKey";
|
|
const saveBlueSignKey = "BlueSignKey";
|
|
const saveBlueToken = "BlueGetToken";
|
|
const currentConnectionLockId = "CurrentConnectionLockId";
|
|
const currentConnectionMacAddress = "CurrentConnectionMacAddress";
|
|
const ifIsDemoModeOrNot = "IfIsDemoModeOrNot";
|
|
const isAgreePrivacy = "isAgreePrivacy"; //是否同意隐私协议弹窗
|
|
const isAgreePosition = "isAgreePosition"; //是否同意获取位置弹窗
|
|
const isAgreeCamera = "isAgreeCamera"; //是否同意获取相机/相册弹窗
|
|
|
|
const isShowUpdateVersion = "isShowUpdateVersion"; //是否更新弹窗
|
|
const saveLockAlias = "saveLockAlias"; //锁别名
|
|
const pushDeviceID = 'pushDeviceID'; //推送设备ID
|
|
const saveIsVip = "saveIsVip"; //是否是VIP
|
|
|
|
const saveUserLoginData = "userLoginData";
|
|
const saveLockMainListData = "lockMainListData";
|
|
const isOpenDeBug = "isOpenDeBug"; //是否打开 debug
|
|
|
|
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;
|
|
// AppLog.log(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();
|
|
|
|
// 重新设置需要保留的数据
|
|
if (isAgreePrivacy != null) {
|
|
//退出登录不清除隐私协议同意状态
|
|
await setString(isAgreePrivacy, isAgreePrivacy);
|
|
}
|
|
}
|
|
|
|
static Future<String?> getUid() async {
|
|
LoginData? loginData = await getLoginData();
|
|
String? uid = loginData!.uid.toString();
|
|
// AppLog.log("pubUid:$uid");
|
|
return uid;
|
|
}
|
|
|
|
static Future<String?> getUserid() async {
|
|
LoginData? loginData = await getLoginData();
|
|
String? userid = loginData!.userid.toString();
|
|
// AppLog.log("pubUid:$uid");
|
|
return userid;
|
|
}
|
|
|
|
static Future<String?> getMobile() async {
|
|
LoginData? loginData = await getLoginData();
|
|
String? mobile = loginData!.mobile;
|
|
return mobile ?? '';
|
|
}
|
|
|
|
static Future<String?> getEmail() async {
|
|
LoginData? loginData = await getLoginData();
|
|
String? email = loginData!.email;
|
|
return email;
|
|
}
|
|
|
|
static Future<String?> getNickname() async {
|
|
LoginData? loginData = await getLoginData();
|
|
String? nickname = loginData!.nickname;
|
|
return nickname;
|
|
}
|
|
|
|
static Future<String?> getHeadUrl() async {
|
|
LoginData? loginData = await getLoginData();
|
|
String? headUrl = loginData!.headUrl;
|
|
return headUrl;
|
|
}
|
|
|
|
static Future<LoginData?> getLoginData() async {
|
|
LoginData? loginData;
|
|
final data = await Storage.getString(saveUserLoginData);
|
|
if (data != null && data.isNotEmpty) {
|
|
loginData = LoginData.fromJson(jsonDecode(data));
|
|
}
|
|
// AppLog.log("loginData:$loginData");
|
|
return loginData;
|
|
}
|
|
|
|
static Future<void> saveLoginData(LoginData? data) async {
|
|
await Storage.setString(saveUserLoginData, jsonEncode(data));
|
|
}
|
|
|
|
static Future<void> saveMainListData(LockListInfoGroupEntity? data) async {
|
|
await Storage.setString(saveLockMainListData, jsonEncode(data));
|
|
}
|
|
|
|
static Future<LockListInfoGroupEntity?> getLockMainListData() async {
|
|
LockListInfoGroupEntity? lockListInfoGroupEntity;
|
|
final data = await Storage.getString(saveLockMainListData);
|
|
if (data != null && data.isNotEmpty) {
|
|
lockListInfoGroupEntity =
|
|
LockListInfoGroupEntity.fromJson(jsonDecode(data));
|
|
}
|
|
return lockListInfoGroupEntity;
|
|
}
|
|
}
|