/* * 持久话数据 * */ 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 getInstance() async { _preferences = await SharedPreferences.getInstance(); return _instance; } // ///存数据 // static Future 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) { // await sp.setStringList(key, value); // } // } // // ///取数据 // /// // static Future getData(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 setInt(key, value) async { SharedPreferences sp = await SharedPreferences.getInstance(); sp.setInt(key, value); } static Future getInt(key) async { SharedPreferences sp = await SharedPreferences.getInstance(); return sp.getInt(key); } // bool static Future setBool(key, value) async { SharedPreferences sp = await SharedPreferences.getInstance(); sp.setBool(key, value); } static Future getBool(key) async { SharedPreferences sp = await SharedPreferences.getInstance(); return sp.getBool(key); } // double static Future setDouble(key, value) async { SharedPreferences sp = await SharedPreferences.getInstance(); sp.setDouble(key, value); } static Future getDouble(key) async { SharedPreferences sp = await SharedPreferences.getInstance(); return sp.getDouble(key); } // string static Future setString(key, value) async { SharedPreferences sp = await SharedPreferences.getInstance(); sp.setString(key, value); } static Future getString(key) async { SharedPreferences sp = await SharedPreferences.getInstance(); return sp.getString(key); } // 字符串数组 static Future setStringList(key, value) async { SharedPreferences sp = await SharedPreferences.getInstance(); sp.setStringList(key, value); } static Future?> getStringList(key) async { SharedPreferences sp = await SharedPreferences.getInstance(); return sp.getStringList(key); } // 判断本地是否包含某个key static Future ifHaveKey(key) async { SharedPreferences sp = await SharedPreferences.getInstance(); bool isContainKey = sp.containsKey(key) ?? false; // AppLog.log(isContainKey); return isContainKey; } // 移除数据 static Future removeData(key) async { SharedPreferences sp = await SharedPreferences.getInstance(); sp.remove(key); } // 移除所有的键值对 static Future clearAll() async { SharedPreferences sp = await SharedPreferences.getInstance(); sp.clear(); } static Future getUid() async { LoginData? loginData = await getLoginData(); String? uid = loginData!.uid.toString(); // AppLog.log("pubUid:$uid"); return uid; } static Future getUserid() async { LoginData? loginData = await getLoginData(); String? userid = loginData!.userid.toString(); // AppLog.log("pubUid:$uid"); return userid; } static Future getMobile() async { LoginData? loginData = await getLoginData(); String? mobile = loginData!.mobile; return mobile ?? ''; } static Future getEmail() async { LoginData? loginData = await getLoginData(); String? email = loginData!.email; return email; } static Future getNickname() async { LoginData? loginData = await getLoginData(); String? nickname = loginData!.nickname; return nickname; } static Future getHeadUrl() async { LoginData? loginData = await getLoginData(); String? headUrl = loginData!.headUrl; return headUrl; } static Future 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 saveLoginData(LoginData? data) async { await Storage.setString(saveUserLoginData, jsonEncode(data)); } static Future saveMainListData(LockListInfoGroupEntity? data) async { await Storage.setString(saveLockMainListData, jsonEncode(data)); } static Future getLockMainListData() async { LockListInfoGroupEntity? lockListInfoGroupEntity; final data = await Storage.getString(saveLockMainListData); if (data != null && data.isNotEmpty) { lockListInfoGroupEntity = LockListInfoGroupEntity.fromJson(jsonDecode(data)); } return lockListInfoGroupEntity; } }